Solved Block the move of an item

Discussion in 'Spigot Plugin Development' started by Stellrow, Aug 12, 2018 at 4:16 PM.

  1. So im almost done with my plugin now i want to make it so the item will only be in the players inventory,no drop,no move it to chests/or something what events do i have to work with to cancel every attempt to throw/get rid of the item?

    But also if its possible to let it change the item to hotbar from the inventory
     
  2. Most likely, PlayerItemDropEvent, or InventoryClickEvent, check if where they click is null (outside of inventory) and check what they are holding in their cursor, cancel if they holding an item.
     
  3. Use PlayerItemDropEvent and InventoryClickEvent.
     
  4. Code (Java):
    @EventHandler
    public void onDrop (PlayerDropItemEvent e) {
    // If
    e.setCancelled(true);
    }

    @EventHandler
    public void onClick (InventoryClickEvent e) {
    // If
    e.setCancelled(true);
    }
     
  5. Please don't spoonfeed, gets on my nerves.
     
  6. That is hardly spoonfeeding. All he really did was provide the event names that can be used.
     
    • Agree Agree x 1
  7. Hmm but will this also block the movement of the item inside theyr own inventory? if i make a check to see where they move it too would it work to only block moving the item to chest...? and the clickevent would still let them move it?(like with numbers from inventory to hotbat?)
     
  8. if thats spoon feeding i doubt your coding experience:)
     
  9. Well, you also need to check which inventory has been moved to in the InventoryClickEvent. There is #getClickedInventory, so you can check in which inventory the item has been moved. Plus, it offers values also for the type of Click (In case is shift click that moves the item to the other inventory but #getClickedInventory returns the player inventory) and action so you can check if is a hotbar swap or whatever.
    Even more, you should also use InventoryDragEvent since dragging is not included in the click event.
     
  10. Did those events are they good or should i add something else?
    Code (Text):
    @EventHandler
        public void onDrop(PlayerDropItemEvent e) {
            Player p = e.getPlayer();
            if(e.getItemDrop().getItemStack().getItemMeta().isUnbreakable()) {
                e.setCancelled(true);
            }
        }
        @EventHandler
        public void onClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            Inventory i = e.getClickedInventory();
            if(e.getCurrentItem().getItemMeta().isUnbreakable()) {
                if(i.getTitle() != p.getInventory().getTitle()) {
                    e.setCancelled(true);
                }
            }
        }
        @EventHandler
        public void onDrag(InventoryDragEvent e) {
            Player p = (Player) e.getWhoClicked();
            if(e.getCursor().getItemMeta().isUnbreakable()) {
                e.setCancelled(true);
            }
        }
    So i tested i cant drop it,i can put it in the chest but i wont allow me to get it back and also shift click moves it too
     
  11. In the click event, you should check for the inventory type not titles. Even more, #getClickedInventory can return null if the player clicked outside the inventory.
     
  12. Code (Text):
    @EventHandler
        public void onClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            Inventory i = e.getClickedInventory();
            ItemStack item = e.getCurrentItem();
            if (item == null) {
                   return;
            }
            if(e.getCurrentItem().getItemMeta().isUnbreakable()) {
                if(e.getInventory().getType() != p.getInventory().getType()) {
                    e.setCancelled(true);
                }
            }
        }
    Tried this but still seems is not allowing me to move the item freely in the player inventory
     
  13. To check if it’s a chest :
    Code (Java):
    if(e.getInventory().getType == InventoryType.CHEST);
     
  14. Does still also include other things like furnances,enderchest
    or is there also InventoryType.PLAYER... i only want the item to stay inside the inventory no moving in something else at all
     

Share This Page