No item dropping

Discussion in 'Plugin Development' started by Nebuler, Aug 30, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Nebuler

    Code:
    package me.nebuler;
    
    import java.util.List;
    
    import org.bukkit.World;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class NoDrop extends JavaPlugin implements Listener {
       
        @EventHandler
        public void ondrop(PlayerDropItemEvent event) {
            event.getItemDrop().remove();
        }
        
        @EventHandler
        public void onBlockDrop(BlockBreakEvent event) {
           
            Player player = event.getPlayer();
            player.setCanPickupItems(false);
            World world = this.getServer().getWorld(player.getWorld().getName());
            List<Entity> entList = world.getEntities();
            for (Entity current : entList) {
                if (!(current instanceof Item)) continue;
                current.remove();
            }
           
        }
       
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents((Listener)this, (Plugin)this);
        }
    }
    I'd like to make it so no items can be dropped onto the ground and are removed. I have this code but the items can still be dropped. Thanks in advance - Nebuler
     
  2. Offline

    mine-care

    You cast a Listener to Listener and a Plugin to a Plugin?
    Have you debuged?
     
  3. Offline

    Nebuler

    @mine-care Haven't debugged yet, but was guessing it was an easy fix

    Is the registerEvents line pointless?
     
  4. Offline

    mine-care

    Well you dont need to loop through all the entities, you can just get the drops by the event...
    other than this it seems fine, take the time to debug
     
  5. Offline

    Nebuler

    @mine-care
    how do I do that?
     
  6. @Nebuler @mine-care
    You can't get the drops from a BlockBreakEvent, and the dropped items are not in the world's entity list. You need to listen for an ItemSpawnEvent and cancel that
     
  7. Offline

    Nebuler

    @megamichiel

    Code:
    package me.nebuler;
    
    import java.util.List;
    
    import org.bukkit.World;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.entity.ItemSpawnEvent;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class NoDrop extends JavaPlugin implements Listener {
      
        @EventHandler
        public void ondrop(PlayerDropItemEvent event) {
            event.getItemDrop().remove();
        }
       
        @EventHandler
        public void onBlockDrop(BlockBreakEvent event) {
          
            Player player = event.getPlayer();
            player.setCanPickupItems(false);
            World world = this.getServer().getWorld(player.getWorld().getName());
            List<Entity> entList = world.getEntities();
            for (Entity current : entList) {
                if (!(current instanceof Item)) continue;
                current.remove();
            }
          
        }
      
        @EventHandler
        public void onBlockDropSpawn(ItemSpawnEvent event) {
          
            event.setCancelled(true);
          
        }
      
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents((Listener)this, (Plugin)this);
        }
    }
    
    I added the item spawn event so now when you break a block in survival it isn't dropped, but what should I do about the block break event? Delete it? Also, when you drop an item with q it is not deleted which im pretty sure should happen because I have this:
    Code:
    @EventHandler
        public void ondrop(PlayerDropItemEvent event) {
            event.getItemDrop().remove();
        }
    But I may be wrong...
     
  8. Offline

    Zombie_Striker

    This only get triggered when a player drops an item.


    player.setCanPickupItems will make sure the players can no longer pick up any item. If there won't be any items on the ground, this line will be useless.


    To stop items from being dropped, listen to the Entitiy Spawn Event. If the entitiy is an instance of an Item, remove it.
     
  9. Offline

    Nebuler

  10. Offline

    Zombie_Striker

    @Nebuler
    Spoonfeeding is not* allowed on the forums.

    Also, what I said you should do can be done in Three-Lines
     
    Last edited: Aug 30, 2015
  11. Offline

    Nebuler

    @Zombie_Striker
    Not an event
     
  12. Offline

    Zombie_Striker

    @Nebuler
    Sorry, It's CreatureSpawnEvent.
     
  13. Offline

    Nebuler

    @Zombie_Striker sorry haven't used the creaturespawnevent yet, what's the line to check if the 'creature' is an item drop?
     
  14. Offline

    Kyorax

    Instead of removing the item once it's dropped,
    just put "event.setCancelled(true) into your "PlayerDropItemEvent".
    They will not be able to drop it.

    @Zombie_Striker Did you mean to say "Spoonfeeding is not allowed on the forums." or "now allowed"?
     
  15. Offline

    Nebuler

    @Kyorax i want the player's to be able to drop the item.
     
  16. Offline

    Kyorax

    @Nebuler "I'd like to make it so no items can be dropped onto the ground and are removed"
    My interpretation was that they can't drop them.

    And to help you with your code:
    As Zombie_Striker pointed out, remove the "playerCanPickUp"-line, it's useless when there are no items to be picked up.
    If you want to listen to all drops, don't use the PlayerDropItemEvent. You also need to check the BlockBreakEvent.
     
  17. Offline

    Nebuler

    @Kyorax what events should I listen to then?
     
  18. Offline

    Kyorax

    Get the dropped item in the BlockBreakEvent and remove it. That should do it.
     
  19. Offline

    Nebuler

    @Kyorax how do you get the item drop from that event?
     
  20. Offline

    Zombie_Striker

    @Nebuler @Kyorax
    You can get the item by using .getDrops(), but then you are using two events to make sure players can't drop items, and that blocks can't drop items. If you don't want any items to drop. Just do the following:
    Code:
    public void spawnEvent(CreatureSpawnEvent e){
    if(entitiy instance of Item)
    entitiy.remove();
    }
     
  21. Offline

    Nebuler

    @Zombie_Striker I have this:
    Code:
    @EventHandler
        public void onBlockDrop(CreatureSpawnEvent event) {
          
            Entity entity = event.getEntity();
          
            if(entity instanceof Item) {
          
                entity.remove();
              
            }
    And on the line entity.remove(); I have a syntax error on the semicolon: 'Syntax error, insert "}" to complete Statement' No idea why this would be

    EDIT: facepalm, forgot a brace to close the spawnevent :'(

    @Zombie_Striker
    Code:
    package me.nebuler;
    
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Item;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class NoDrop
    extends JavaPlugin
    implements Listener {
        @EventHandler
        public void ondrop(PlayerDropItemEvent event) {
            event.getItemDrop().remove();
        }
      
        @EventHandler
        public void onBlockDrop(CreatureSpawnEvent event) {
          
            Entity entity = event.getEntity();
          
            if(entity instanceof Item) {
          
                entity.remove();
              
            }
        }
    
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents((Listener)this, (Plugin)this);
        }
    }
    
    Items are removed when the block is broken in survival or dropped with q but not when shot out of a dispenser
    Also, I need it so that when items are in a chest/ dropper/ hopper or whatever and the 'container' is broken, I don't want the items that were in it to be dropped and just be removed.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  22. Offline

    Nebuler

  23. Offline

    Nebuler

    Bump

    bumpety bumpy

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  24. Offline

    Oxyorum

  25. Offline

    Nebuler

    @Oxyorum that doesn't help, I can't find a method to do what I want, that's why i'm asking on here.
     
  26. Offline

    Zombie_Striker

    @Nebuler
    let me just fix your code. Maybe ill see the problem afterwards
    Code:
    public class NoDrop extends JavaPlugin implements Listener {
    
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents((Listener)this, (Plugin)this);
    //   You don't need to cast. This class does extend JavaPlugin and does implment listener. So reemove casting
        }
    
        @EventHandler
        public void ondrop(PlayerDropItemEvent event) {
            event.getItemDrop().remove();
    //      This whole method should not be necessary. 
        }
        @EventHandler
        public void onBlockDrop(CreatureSpawnEvent event) {
        
            Entity entity = event.getEntity();
        
            if(entity instanceof Item || entitiy.getType() == EntitiyType.ITEM) {
                entity.remove();
            }
        }
    }
    
     
  27. Offline

    Nebuler

    @Zombie_Striker sure that's not the same as
    Code:
    @EventHandler
        public void onBlockDrop(CreatureSpawnEvent event) {
           
            Entity entity = event.getEntity();
           
            if(entity instanceof Item) {
           
                entity.remove();
               
            }
        }
     
  28. Offline

    Oxyorum

  29. Offline

    Zombie_Striker

    @Nebuler
    Use this because I have on idea why it's not removing all items.\

    EDIT] Oxyorum is right. nvm
     
  30. Offline

    Nebuler

    @Zombie_Striker
    Code:
    @EventHandler
        public void onBlockDrop(ItemSpawnEvent event) {
           
            Entity entity = event.getEntity();
           
            entity.remove();
        }
    Will work?
     
Thread Status:
Not open for further replies.

Share This Page