org.spongepowered.api.event.item.inventory.DropItemEvent Java Examples

The following examples show how to use org.spongepowered.api.event.item.inventory.DropItemEvent. 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: GlobalListener.java    From RedProtect with GNU General Public License v3.0 6 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onFramePlace(SpawnEntityEvent event, @Root Player p) {
    for (Entity e : event.getEntities()) {
        if (e == null || RedProtect.get().rm.getTopRegion(e.getLocation(), this.getClass().getName()) != null) {
            continue;
        }

        if (event instanceof DropItemEvent) {
            continue;
        }

        if (e instanceof Hanging) {
            if (!RedProtect.get().config.globalFlagsRoot().worlds.get(e.getWorld().getName()).build && !p.hasPermission("redprotect.world.bypass")) {
                event.setCancelled(true);
                return;
            }
        }
    }
}
 
Example #4
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 #5
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 #6
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 #7
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 PlayerDropItemGui(DropItemEvent.Pre e, @Root Player p) {
    e.getDroppedItems().forEach(item -> {
        if (RedProtect.get().getUtil().isGuiItem(item.createStack())) {
            RedProtect.get().getVersionHelper().removeGuiItem(p);
            e.setCancelled(true);
        }
    });
}
 
Example #8
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 #9
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 #10
Source File: EntityEventHandler.java    From GriefDefender with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onEntityDropItemDeath(DropItemEvent.Destruct event) {
    if (!GDFlags.ITEM_DROP || event.getEntities().isEmpty()) {
        return;
    }

    final World world = event.getEntities().get(0).getWorld();
    if (!GriefDefenderPlugin.getInstance().claimsEnabledForWorld(world.getUniqueId())) {
        return;
    }

    Object source = event.getSource();
    // If root cause is damage source, look for target as that should be passed instead
    // Ex. Entity dies and drops an item would be after EntityDamageSource
    if (source instanceof DamageSource) {
        final Object target = event.getCause().after(DamageSource.class).orElse(null);
        if (target != null) {
            source = target;
        }
    }
    if (!(source instanceof Entity)) {
        return;
    }

    final Entity entity = (Entity) source;
    if (GriefDefenderPlugin.isSourceIdBlacklisted(Flags.ITEM_DROP.getName(), entity, world.getProperties())) {
        return;
    }

    GDTimings.ENTITY_DROP_ITEM_DEATH_EVENT.startTimingIfSync();

    final User user = CauseContextHelper.getEventUser(event);
    event.filterEntities(new Predicate<Entity>() {
        GDClaim targetClaim = null;

        @Override
        public boolean test(Entity item) {
            if (GriefDefenderPlugin.isTargetIdBlacklisted(Flags.ITEM_DROP.getName(), item, world.getProperties())) {
                return true;
            }

            targetClaim = GriefDefenderPlugin.getInstance().dataStore.getClaimAt(item.getLocation(), targetClaim);
            if (targetClaim == null) {
                return true;
            }

            if (user == null) {
                return true;
            }
            if (GriefDefenderPlugin.isTargetIdBlacklisted(Flags.ITEM_DROP.getName(), item, world.getProperties())) {
                return true;
            }

            if (GDPermissionManager.getInstance().getFinalPermission(event, item.getLocation(), targetClaim, Flags.ITEM_DROP, entity, item, user, TrustTypes.ACCESSOR, true) == Tristate.FALSE) {
                return false;
            }
            return true;
        }
    });

    GDTimings.ENTITY_DROP_ITEM_DEATH_EVENT.stopTimingIfSync();
}
 
Example #11
Source File: EntityEventHandler.java    From GriefPrevention with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onEntityDropItemDeath(DropItemEvent.Destruct event) {
    if (!GPFlags.ITEM_DROP || event.getEntities().isEmpty()) {
        return;
    }

    final World world = event.getEntities().get(0).getWorld();
    if (!GriefPreventionPlugin.instance.claimsEnabledForWorld(world.getProperties())) {
        return;
    }

    Object source = event.getSource();
    // If root cause is damage source, look for target as that should be passed instead
    // Ex. Entity dies and drops an item would be after EntityDamageSource
    if (source instanceof DamageSource) {
        final Object target = event.getCause().after(DamageSource.class).orElse(null);
        if (target != null) {
            source = target;
        }
    }
    if (!(source instanceof Entity)) {
        return;
    }

    final Entity entity = (Entity) source;
    if (GriefPreventionPlugin.isSourceIdBlacklisted(ClaimFlag.ITEM_DROP.toString(), entity, world.getProperties())) {
        return;
    }

    GPTimings.ENTITY_DROP_ITEM_DEATH_EVENT.startTimingIfSync();
    // special rule for creative worlds: killed entities don't drop items or experience orbs
    if (GriefPreventionPlugin.instance.claimModeIsActive(entity.getLocation().getExtent().getProperties(), ClaimsMode.Creative)) {
        event.setCancelled(true);
        GPTimings.ENTITY_DROP_ITEM_DEATH_EVENT.stopTimingIfSync();
        return;
    }

    final User user = CauseContextHelper.getEventUser(event);
    event.filterEntities(new Predicate<Entity>() {
        GPClaim targetClaim = null;

        @Override
        public boolean test(Entity item) {
            if (GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.ITEM_DROP.toString(), item, world.getProperties())) {
                return true;
            }

            targetClaim = GriefPreventionPlugin.instance.dataStore.getClaimAt(item.getLocation(), targetClaim);
            if (targetClaim == null) {
                return true;
            }

            if (user == null) {
                return true;
            }
            if (GriefPreventionPlugin.isTargetIdBlacklisted(ClaimFlag.ITEM_DROP.toString(), item, world.getProperties())) {
                return true;
            }

            if (GPPermissionHandler.getClaimPermission(event, item.getLocation(), targetClaim, GPPermissions.ITEM_DROP, entity, item, user, TrustType.ACCESSOR, true) == Tristate.FALSE) {
                return false;
            }
            return true;
        }
    });

    GPTimings.ENTITY_DROP_ITEM_DEATH_EVENT.stopTimingIfSync();
}
 
Example #12
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();
}
 
Example #13
Source File: PreventListener.java    From FlexibleLogin with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerItemDrop(DropItemEvent dropItemEvent, @First Player player) {
    checkLoginStatus(dropItemEvent, player);
}