Stopping Players From Dropping Items

Discussion in 'Plugin Development' started by toxiccoke, Jan 5, 2015.

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

    toxiccoke

    Hi

    I was working on a plugin and i was trying to make it so players cannot drop items

    Code:
        }
        @EventHandler
        public void onItemDrop(PlayerDropItemEvent e){
        if(e.getPlayer().getInventory().getItem(8).getType().equals(e.getItemDrop().getItemStack().getType())){
        e.setCancelled(true);
        }
            
        }
    }
    i have that but it don't seem to work

    Could anyone help me out
    Thanks toxiccoke
     
  2. Offline

    nj2miami

    I found it easiest to allow the drop initially and respond to it. Mind you this is to return the item rather then just "eat" it. You could just e.getItemDrop().remove(); to allow them to lose an item by dropping it.

    The first line is validation code to allow OPs to drop whatever.
    Code:
            
    
    @EventHandler(priority = EventPriority.HIGHEST)
      public void onDrop (PlayerDropItemEvent e) {
    
    final ItemStack dropItem = new ItemStack(e.getItemDrop().getItemStack().clone()); 
    
    // remove the dropped item and put it back in their inventory
                if(!e.getPlayer().isOp()) {
                    main.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
                    @Override
                    public void run() {
                        p.getPlayer().getInventory().addItem(dropItem);
                    }
                }, 10L);
    
              e.getPlayer().sendMessage("You dropped something important, I do not think you meant to do that!");
              e.getItemDrop().remove(); 
     
    Last edited: Jan 5, 2015
  3. Offline

    toxiccoke

    That Does Not Work For Me
     
  4. Offline

    nverdier

    @toxiccoke Don't try to copy and paste. Please. You have to understand what you are doing, so you don't come back here every time.
     
  5. Offline

    SuperOriginal

  6. Offline

    toxiccoke

    @SuperOriginal Basically i am making a plugin for my hub and i want to make a so that player cannot use q to drop items i tryed using the code above but that doesn't seem to be working
     
  7. Offline

    SuperOriginal

     
  8. Offline

    nj2miami

    @toxiccoke - The code works as it comes from a working plugin I wrote, so you need to be more specific, show a stack trace, explain yourself better or show some of YOUR code to be helped.

    It should go without saying, but you did register the listener right?

    Also, see my above example, I forgot to show you -> final ItemStack dropItem = new ItemStack(e.getItemDrop().getItemStack().clone());
     
    Last edited: Jan 5, 2015
  9. Offline

    Takedown

    How would you get it to do that only when the player is in creative mode? so they can drop items in survival but not in creative?
     
  10. Offline

    nverdier

    @Takedown Check for the gamemode before canceling.
     
  11. Offline

    nj2miami

    @Takedown See above, but from a functional standpoint, are you expecting this to prevent players from dropping in items from Creative in a Survival world?

    I say this because this will NOT prevent them from placing the items in chest, furnaces, dispensers, etc.
     
  12. Offline

    Takedown

    I already made a plugin that disallows the players to use those items while in creative. and yes, on my server, it is a survival but with a certain rank players gain access to creative and I dont want them to be able to give survival players items from creative.
     
  13. Offline

    nj2miami

    It is not really going to disallow giving of items since they can easily place them in chest, etc, even if you do not allow them to drop. To completely stop it you would need a lot more coding looking at many more listeners and even then I am not sure you could completely lock it down.

    The method above will only stop someone from dropping the item to the ground. So be aware of that.

    Allowing Creative players on a Survival server is pretty much a recipe for disaster.
     
  14. Offline

    nverdier

    @nj2miami @Takedown You can still always add on to this and stop players in create from putting items in chests/hoppers etc. OR you could even not let them have anything in their inventories..
     
  15. Offline

    nj2miami

    Of course you can add on to it... he is still working on JUST THIS though and he needs to know a lot more is required to try and lock down a creative user from giving items to his survival players. I am not even sure it is practical because what point is even being in Creative mode if you are going to restrict the user from taking anything from it.

    Also, if they can take stuff from Creative, once they are back in Survival mode they can hand it all off so I really do not see the practical use of the plugin at all honestly.

    The ONLY way it is really practical is if you lock out all dropping of items, usage of all inventories like chest, furnace , etc... and then empty their inventory when they switch from Creative to Survival.

    This then adds the complexity of storing and returning inventories to a player in between modes. All of which can be done but I think outside the scope of this conversation and the aptitude of the OP.
     
  16. Offline

    nverdier

    @nj2miami Well I think the best thing to do would be to not let creative players to have anything in their inventories, but I'm not sure what exactly the OP wants. You could also add all of the stuff to block chests etc in creative, and then have per-gamemode inventories.
     
  17. Offline

    nj2miami

    He wants to not allow Creative players the ability to drop items, which in itself is not really useful. Your extreme of not having anything in their inventory makes Creative useless and therefore moot.

    Locking it all down and having switching inventories is the only real viable solution to answer his question, but again, far outside the scope of this thread and even farther from what he understands quite frankly.
     
    nverdier likes this.
  18. Offline

    nverdier

    GrandmaJam likes this.
Thread Status:
Not open for further replies.

Share This Page