Clear Inventory problem?

Discussion in 'Plugin Development' started by Peter25715, Nov 22, 2014.

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

    Peter25715

    Hi there.. Today I tried to do something which is, When a player drops something while he's in creative mode and he doesn't have permissions for it, He will be in survival mode and his items will be cleared but I have an error which is.. Everything will be cleared except the item in hand. I tried to clear the item in hand but didn't work.
    ClickMe to see a picture.

    Code:java
    1. @EventHandler
    2. public void dropEvent(PlayerDropItemEvent e) {
    3. Player p = e.getPlayer();
    4.  
    5. if (!p.hasPermission("creative.drop")) {
    6.  
    7. if (p.getGameMode() == GameMode.CREATIVE) {
    8.  
    9.  
    10. p.getInventory().clear();
    11. p.getInventory().setHelmet(null);
    12. p.getInventory().setChestplate(null);
    13. p.getInventory().setLeggings(null);
    14. p.getInventory().setBoots(null);
    15.  
    16.  
    17. e.setCancelled(true);
    18.  
    19. p.setGameMode(GameMode.SURVIVAL);
    20. p.sendMessage("§6[§aNoCreativeDP§6] You are not allowed to drop items using creative mode!, " + p.getName());
    21. }
    22. }
    23.  
    24. }


    Thanks, I hope you're here with a fix :p
     
  2. Offline

    Fhbgsdhkfbl

    Just put p.getItemInHand().setType(Material.AIR);
     
  3. Offline

    mrCookieSlime

    Peter25715
    Make sure you cancel the Event before you clear the Inventory.

    Also, you can use p.getInventory().setArmorContents(null);
    instead of manually clearing every armorpiece.
     
  4. Offline

    teej107

    That doesn't matter. Code will still run in your event regardless of it being cancelled or not, if it was the first or last thing you do.
     
  5. Offline

    mrCookieSlime

    teej107
    Thats not what I meant.

    If he clears the Inventory all Items which are currently in the Inventory will be removed.
    But if he hasnt cancelled the Event yet, the Inventory still thinks the Diamond Block has been placed and is no longer in his Inventory.

    But if he cancels the Event first, the Inventory is aware that the Block of Diamond is still in the Inventory since it hasnt been placed.
    It will clear it then as well.
     
  6. Offline

    teej107

    mrCookieSlime I see what you mean now. In that case, he will need to run a delayed task to clear the inventory.
     
  7. Offline

    mythbusterma

    mrCookieSlime

    Canceling the event doesn't actually do anything except set a flag until the completion of all the handlers listening for it. Regardless of where he sets this flag, it will still behave the same.
     
    teej107 likes this.
  8. Offline

    Dragonphase

    Peter25715

    Have you tried updating the Player's inventory?

    Code:java
    1. Player#updateInventory();
     
  9. Offline

    Peter25715

    I tried everything in here. But my problem still the same. When there's something stacked or unstacked (only one)..
    if it's in a stack; Everything will be removed except it will keep one in my hand.
    if it's not in stack; Same.
     
Thread Status:
Not open for further replies.

Share This Page