Properly cancelling the PlayerDropItemEvent?

Discussion in 'Plugin Development' started by OHQCraft, Jul 27, 2014.

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

    OHQCraft

    I want to cancel the PlayerDropItemEvent, but instead of reverting the inventory back to the same after canceling, the ItemStack gets put to the first available slot. Anyway of getting the ItemStack back in the exact slot? I have achieved this before for a lobby plugin, but I knew where the items were, any way of stopping this without knowing where the ItemStack should be like this?:
    Code:java
    1. @EventHandler
    2. public void onItemDrop(final PlayerDropItemEvent event) {
    3. if (event.getItemDrop().getItemStack().equals(PrimeFactions.primeStar)) {
    4. final ItemStack drop = event.getItemDrop().getItemStack();
    5. event.setCancelled(true);
    6. Bukkit.getScheduler().scheduleSyncDelayedTask(PrimeFactions.getInstance(), new Runnable() {
    7. @Override
    8. public void run() {
    9. event.getPlayer().getInventory().remove(drop);
    10. event.getPlayer().getInventory().setItem(8, drop);
    11. }
    12. }, 1L);
    13. return;
    14. }
    15. if (event.getItemDrop().getItemStack().equals(PrimeFactions.comingSoon)) {
    16. final ItemStack drop = event.getItemDrop().getItemStack();
    17. event.setCancelled(true);
    18. Bukkit.getScheduler().scheduleSyncDelayedTask(PrimeFactions.getInstance(), new Runnable() {
    19. @Override
    20. public void run() {
    21. event.getPlayer().getInventory().remove(drop);
    22. event.getPlayer().getInventory().setItem(0, drop);
    23. }
    24. }, 1L);
    25. }
    26. }
     
  2. Offline

    LukesComputers

    Could you please write a more detailed explanation of the problem?
     
  3. Offline

    GeorgeeeHD

    OHQCraft p.setItemInHand(event.getItemDrop().getItemStack());
     
    OHQCraft likes this.
  4. Offline

    OHQCraft

    Seems like the solution. Trying it out now.
     
  5. Offline

    mine-care

    If u want items on specific slot find the slot is and sues inf.setitem method.
    And as LukesComputers said, please be more detailed. Thanks.
     
  6. Offline

    OHQCraft



    It really can't get more detailed than that.

    This is the solution:
    Code:java
    1. Bukkit.getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
    2. @Override
    3. public void run() {
    4. event.getPlayer().getInventory().remove(event.getItemDrop().getItemStack());
    5.  
    6. if(event.getPlayer().getItemInHand().getData().equals(event.getItemDrop().getItemStack())){
    7. event.getPlayer().getItemInHand().setAmount(event.getPlayer().getItemInHand().getAmount() + event.getItemDrop().getItemStack().getAmount());
    8. }else{
    9. event.getPlayer().getInventory().setItemInHand(event.getItemDrop().getItemStack());
    10. }
    11. }
    12. }, 1L);
    13. event.setCancelled(true);


    Ok, just noticed exploits and glitches in this, not solved.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page