Java Code Examples for org.spongepowered.api.event.item.inventory.DropItemEvent#Dispense

The following examples show how to use org.spongepowered.api.event.item.inventory.DropItemEvent#Dispense . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HuskyUI.java    From HuskyUI-Plugin with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Handle item drops
 * @param event dispense event
 */
@Listener
public void onItemDrop(DropItemEvent.Dispense event){
    for(Entity e :event.getEntities()){
        if(e instanceof Item){
            ItemStack affectedStack = ((Item) e).getItemData().item().get().createStack();
            Optional<Integer> potentialID = registry.getElementIDFromItemStack(affectedStack);
            if(potentialID.isPresent()){
                if(registry.elementExists(potentialID.get())){
                    event.setCancelled(true); //NOTHING should drop a registered item. >:(
                    //TODO: handle https://github.com/SpongePowered/SpongeCommon/issues/1678 properly w/ workaround
                }
            }
        }
    }
}
 
Example 2
Source File: BlacklistListener.java    From EssentialCmds with MIT License 6 votes vote down vote up
@Listener
public void onDropItem(DropItemEvent.Dispense event, @Root Player player)
{
	if (!player.hasPermission("essentialcmds.blacklist.bypass"))
	{
		event.filterEntities(e -> {
			if (e.get(RepresentedItemData.class).isPresent())
			{
				RepresentedItemData itemData = e.get(RepresentedItemData.class).get();
				boolean blacklisted = Utils.getBlacklistItems().contains(itemData.item().get().getType().getId());

				if (blacklisted)
				{
					if (Utils.areBlacklistMsgsEnabled())
						player.sendMessage(Text.of(TextColors.RED, "The item ", TextColors.GRAY, itemData.item().get().getType().getId(), TextColors.RED, " has been confiscated as it is blacklisted."));
				}

				return !blacklisted;
			}
			return false;
		});
	}
}
 
Example 3
Source File: ItemListener.java    From UltimateCore with MIT License 6 votes vote down vote up
@Listener
public void onDrop(DropItemEvent.Dispense event) {

    ModuleConfig config = Modules.BLACKLIST.get().getConfig().get();
    CommentedConfigurationNode hnode = config.get();
    for (Entity en : event.getEntities()) {
        if (!(en instanceof Item)) continue;
        Item item = (Item) en;
        CommentedConfigurationNode node = hnode.getNode("items", item.getItemType().getId());
        if (!node.isVirtual()) {
            if (node.getNode("deny-drop").getBoolean()) {
                event.setCancelled(true);
            }
        }
    }
}
 
Example 4
Source File: PlayerEventHandler.java    From GriefDefender with MIT License 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerDispenseItem(DropItemEvent.Dispense event, @Root Entity spawncause) {
    if (!GDFlags.ITEM_DROP || !(spawncause instanceof User)) {
        return;
    }

    final User user = (User) spawncause;
    final World world = spawncause.getWorld();
    if (!GriefDefenderPlugin.getInstance().claimsEnabledForWorld(world.getUniqueId())) {
        return;
    }

    GDTimings.PLAYER_DISPENSE_ITEM_EVENT.startTimingIfSync();
    Player player = user instanceof Player ? (Player) user : null;
    GDPlayerData playerData = this.dataStore.getOrCreatePlayerData(world, user.getUniqueId());

    for (Entity entityItem : event.getEntities()) {
        if (GriefDefenderPlugin.isTargetIdBlacklisted(Flags.ITEM_DROP.toString(), entityItem, world.getProperties())) {
            continue;
        }

        Location<World> location = entityItem.getLocation();
        GDClaim claim = this.dataStore.getClaimAtPlayer(playerData, location);
        if (claim != null) {
            if (GDPermissionManager.getInstance().getFinalPermission(event, location, claim, Flags.ITEM_DROP, user, entityItem, user, TrustTypes.ACCESSOR, true) == Tristate.FALSE) {
                event.setCancelled(true);
                if (spawncause instanceof Player) {
                    final Component message = GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.PERMISSION_ITEM_DROP,
                            ImmutableMap.of(
                            "player", claim.getOwnerName(),
                            "item", entityItem.getType().getId()));
                    GriefDefenderPlugin.sendClaimDenyMessage(claim, player, message);
                }
                GDTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                return;
            }
        }
    }
    GDTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
}
 
Example 5
Source File: GlobalListener.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void PlayerDropItem(DropItemEvent.Dispense e, @Root Player p) {
    for (Entity ent : e.getEntities()) {
        Location<World> l = ent.getLocation();
        Region r = RedProtect.get().rm.getTopRegion(l, this.getClass().getName());

        if (r == null && !RedProtect.get().config.globalFlagsRoot().worlds.get(p.getWorld().getName()).player_candrop && !p.hasPermission("redprotect.world.bypass")) {
            e.setCancelled(true);
        }
    }
}
 
Example 6
Source File: PlayerListener.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void PlayerDropItem(DropItemEvent.Dispense e, @Root Player p) {
    RedProtect.get().logger.debug(LogLevel.PLAYER, "Is DropItemEvent.Dispense event.");

    for (Entity ent : e.getEntities()) {
        Location<World> l = ent.getLocation();
        Region r = RedProtect.get().rm.getTopRegion(l, this.getClass().getName());

        if (r != null && ((!r.canExit(p) && !r.canDrop(p)) || !r.canDrop(p))) {
            e.setCancelled(true);
            RedProtect.get().lang.sendMessage(p, "playerlistener.region.cantdrop");
        }
    }
}
 
Example 7
Source File: InventoryListener.java    From Prism with MIT License 5 votes vote down vote up
/**
 * Saves event records when a player drops an item on to the ground.
 *
 * @param event  DropItemEvent.Dispense
 * @param player Player
 */
@Listener(order = Order.POST)
public void onDropItemDispense(DropItemEvent.Dispense event, @Root Player player) {
    if (event.getEntities().isEmpty() || !Prism.getInstance().getConfig().getEventCategory().isItemDrop()) {
        return;
    }

    for (Entity entity : event.getEntities()) {
        if (!(entity instanceof Item)) {
            continue;
        }

        Item item = (Item) entity;
        if (!item.item().exists()) {
            continue;
        }

        ItemStackSnapshot itemStack = item.item().get();
        Prism.getInstance().getLogger().debug("Inventory dropped - {} x{}", itemStack.getType().getId(), itemStack.getQuantity());

        PrismRecord.create()
                .source(event.getCause())
                .event(PrismEvents.ITEM_DROP)
                .itemStack(itemStack)
                .location(player.getLocation())
                .buildAndSave();
    }
}
 
Example 8
Source File: PlayerEventHandler.java    From GriefPrevention with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerDispenseItem(DropItemEvent.Dispense event, @Root Entity spawncause) {
    if (!GPFlags.ITEM_DROP || !(spawncause instanceof User)) {
        return;
    }

    final User user = (User) spawncause;
    final World world = spawncause.getWorld();
    if (!GriefPreventionPlugin.instance.claimsEnabledForWorld(world.getProperties())) {
        return;
    }

    // in creative worlds, dropping items is blocked
    if (GriefPreventionPlugin.instance.claimModeIsActive(world.getProperties(), ClaimsMode.Creative)) {
        event.setCancelled(true);
        return;
    }

    GPTimings.PLAYER_DISPENSE_ITEM_EVENT.startTimingIfSync();
    Player player = user instanceof Player ? (Player) user : null;
    GPPlayerData playerData = this.dataStore.getOrCreatePlayerData(world, user.getUniqueId());

    // FEATURE: players in PvP combat, can't throw items on the ground to hide
    // them or give them away to other players before they are defeated

    // if in combat, don't let him drop it
    if (player != null && !GriefPreventionPlugin.getActiveConfig(world.getProperties()).getConfig().pvp.allowCombatItemDrops && playerData.inPvpCombat(player.getWorld())) {
        GriefPreventionPlugin.sendMessage(player, GriefPreventionPlugin.instance.messageData.pvpNoItemDrop.toText());
        event.setCancelled(true);
        GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
        return;
    }

    for (Entity entityItem : event.getEntities()) {
        if (GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.ITEM_DROP.toString(), entityItem, world.getProperties())) {
            continue;
        }

        Location<World> location = entityItem.getLocation();
        GPClaim claim = this.dataStore.getClaimAtPlayer(playerData, location);
        if (claim != null) {
            final Tristate override = GPPermissionHandler.getFlagOverride(event, location, claim, GPPermissions.ITEM_DROP,  user, entityItem, user, playerData, true);
            if (override != Tristate.UNDEFINED) {
                if (override == Tristate.TRUE) {
                    GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                    return;
                }

                event.setCancelled(true);
                GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                return;
            }

            // allow trusted users
            if (claim.isUserTrusted(user, TrustType.ACCESSOR)) {
                GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                return;
            }

            Tristate perm = GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.ITEM_DROP, user, entityItem, user);
            if (perm == Tristate.TRUE) {
                GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                return;
            } else if (perm == Tristate.FALSE) {
                event.setCancelled(true);
                if (spawncause instanceof Player) {
                    final Text message = GriefPreventionPlugin.instance.messageData.permissionItemDrop
                            .apply(ImmutableMap.of(
                            "owner", claim.getOwnerName(),
                            "item", entityItem.getType().getId())).build();
                    GriefPreventionPlugin.sendClaimDenyMessage(claim, player, message);
                }
                GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
                return;
            }
        }
    }
    GPTimings.PLAYER_DISPENSE_ITEM_EVENT.stopTimingIfSync();
}