Solved Prevent player to put a specific item in Chest

Discussion in 'Spigot Plugin Development' started by Poinball, Jun 12, 2017.

  1. Hey guys,

    I found some information on the forum, but at the end I have a major problem.

    the methods an item can be inserted into a chest :
    1. Shift click an item from your inventory into the chest
    2. Click the item, and then click it into the slot in the chest
    3. Click the item, and drag it inside the chest
    The problem is at the moment that a player Click the item, and then click it into the slot in the chest.
    It let the player actualy do it.

    Here is my code :
    Code (Text):
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onInventoryClick(InventoryClickEvent event) {
            Player p = (Player) event.getWhoClicked();

               

                    Inventory clicked = event.getClickedInventory();
                    if (event.getClick().isShiftClick()) {
                        if (clicked == event.getWhoClicked().getInventory()) {
                            // The item is being shift clicked from the bottom to the top
                            ItemStack clickedOn = event.getCurrentItem();

                            if (clickedOn != null && event.getCurrentItem().getItemMeta().getDisplayName() != null && event.getCurrentItem().getItemMeta().getDisplayName().equals( "Red King's Crown" )) {
                                event.setCancelled( true );
                            }
                        }
                    }

                    if (clicked != event.getWhoClicked().getInventory()) { // Note: !=
                        // The cursor item is going into the top inventory
                        ItemStack onCursor = event.getCursor();

                        if (onCursor != null && event.getCurrentItem().getItemMeta().getDisplayName() != null && event.getCurrentItem().getItemMeta().getDisplayName().equals( "Red King's Crown" )) {
                            event.setCancelled( true );
                        }
                    }
        }

        @EventHandler(priority = EventPriority.HIGHEST)
        public void onInventoryDrag(InventoryDragEvent event) {
            ItemStack dragged = event.getOldCursor(); // This is the item that is being dragged

            if (dragged.getItemMeta().getDisplayName() != null && dragged.getItemMeta().getDisplayName().equals( "Red King's Crown" )) {
                int inventorySize = event.getInventory().getSize(); // The size of the inventory, for reference

                // Now we go through all of the slots and check if the slot is inside our inventory (using the inventory size as reference)
                for (int i : event.getRawSlots()) {
                    if (i < inventorySize) {
                        event.setCancelled(true);
                        break;
                    }
                }
            }
        }
    This is the error I got :
    [​IMG]

    Thanks for your time !
     
  2. I have a feeling that in this part of code :
    Code (Text):
                    if (clicked != event.getWhoClicked().getInventory()) { // Note: !=
                        // The cursor item is going into the top inventory
                        ItemStack onCursor = event.getCursor();

                        if (onCursor != null && event.getCurrentItem().getItemMeta().getDisplayName() != null && event.getCurrentItem().getItemMeta().getDisplayName().equals( "Red King's Crown" )) {
                            event.setCancelled( true );
                        }
                    }
    When an item is onCursor, the name can't be read
     
  3. Copy and paste line 228 and all code surrounding it.
     
  4. This is 228,229 and 230
    Code (Text):
             if (onCursor != null && event.getCurrentItem().getItemMeta().getDisplayName() != null && event.getCurrentItem().getItemMeta().getDisplayName().equals( "Red King's Crown" )) {
                            event.setCancelled( true );
                        }
     
  5. If onCursor is not event.getCurrentItem(), make sure to check if event.getCurrentItem() is not null.

    Change
    Code (Text):
    event.getCurrentItem().getItemMeta().getDisplayName() != null
    to
    Code (Text):
    event.getCurrentItem().getItemMeta() != null
    or even better, change it to
    Code (Text):
    event.getCurrentItem().hasItemMeta()
     
  6. Better yet
    Code (Text):
    event.getCurrentItem().hasItemMeta()
     
  7. I no longer have the error, but the problem is still here. the player can drop the item in a chest

    Code (Text):
     
    if (clicked != event.getWhoClicked().getInventory()) { // Note: !=
        // The cursor item is going into the top inventory
        ItemStack onCursor = event.getCursor();

        if (onCursor != null && event.getCurrentItem().hasItemMeta() && event.getCurrentItem().getItemMeta().getDisplayName().equals( "Red King's Crown" )) {
            event.setCancelled( true );
        }
    }
     
     
  8. Change
    Code (Text):
    event.getCursor();
    to
    Code (Text):
    event.getCurrentItem();
     
  9. It do the same thing .

    Code (Text):
                    if (clicked != event.getWhoClicked().getInventory()) { // Note: !=
                        // The cursor item is going into the top inventory
                        ItemStack currentItem = event.getCurrentItem();

                        if (currentItem != null && currentItem.hasItemMeta() && currentItem.getItemMeta().getDisplayName().equals( "Red King's Crown" )) {
                            event.setCancelled( true );
                        }

                    }
     
    #10 Poinball, Jun 12, 2017
    Last edited: Jun 12, 2017
  10. I still have the same problem :/

    Someone have an idea ??
     
  11. Have you done some debugging? like print out texts over the codes, then its easier to fix the problem.

    EDIT; Does the display name have some color in it?
     
  12. item has any format or color on name? try replacing
    event.getCurrentItem().getItemMeta().getDisplayName().equals( "Red King's Crown" ) with
    event.getCurrentItem().getItemMeta().getDisplayName().contains( "Red King's Crown" )
     
  13. He can also do stripColor method or something. This may be the case if he has any color formats in his name.
     
  14. Nope , The name is standard name without color and its exacly the name I got with the same capital letter
     
  15. then start putting some debugcode in the listener

    add Bukkit.broadcastMessage(yourfeedback) anywhere you need like:

    Code (Text):
    @EventHandler(priority = EventPriority.HIGHEST)
        public void onInventoryClick(InventoryClickEvent event) {
            Player p = (Player) event.getWhoClicked();

           

                    Inventory clicked = event.getClickedInventory();
                    if (event.getClick().isShiftClick()) {
    Bukkit.broadcastMessage("is shiftclick? yes");
                        if (clicked == event.getWhoClicked().getInventory()) {
                            // The item is being shift clicked from the bottom to the top
                            ItemStack clickedOn = event.getCurrentItem();

                            if (clickedOn != null && event.getCurrentItem().getItemMeta().getDisplayName() != null && event.getCurrentItem().getItemMeta().getDisplayName().equals( "Red King's Crown" )) {
    Bukkit.broadcastMessage("is red crown? true");
                                event.setCancelled( true );
                            }else Bukkit.broadcastMessage("is red crown? false");
                        }
                    }

                    if (clicked != event.getWhoClicked().getInventory()) { // Note: !=
    Bukkit.broadcastMessage("i clicked on chest? true");
                        // The cursor item is going into the top inventory
                        ItemStack onCursor = event.getCursor();

                        if (onCursor != null && event.getCurrentItem().getItemMeta().getDisplayName() != null && event.getCurrentItem().getItemMeta().getDisplayName().equals( "Red King's Crown" )) {
                            event.setCancelled( true );
    Bukkit.broadcastMessage("is red crown? true");
                        }else
    Bukkit.broadcastMessage("is red crown? false");
                    }
    Bukkit.broadcastMessage("event is cancelled? "+event.isCancelled());
        }
     
    #16 emanon, Jun 12, 2017
    Last edited: Jun 12, 2017
  16. Thanks for the advise, but In my version of the code I already did it -_-
    Its the Basic ...
     
  17. then you should be able to see which check fails
     
  18. I thought that inventoryclickevent checks for all clicks and shifts and stuff, so all you would have to do is check its your item and cancel the event
     
    • Agree Agree x 1
  19. I Got It !!

    Thanks to Everyone !

    Code (Text):
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onInventoryClick(InventoryClickEvent event) {

            Player p = (Player) event.getWhoClicked();
            Inventory clicked = event.getClickedInventory();
            if ( event.getInventory().getHolder() instanceof Chest || event.getInventory().getHolder() instanceof DoubleChest ) {

                if (clicked == event.getWhoClicked().getInventory()) {

                    ItemStack clickedOn = event.getCurrentItem();
                    if (clickedOn != null && event.getCurrentItem().hasItemMeta() && event.getCurrentItem().getItemMeta().getDisplayName().equals( "Red King's Crown" )) {
                        event.setCancelled( true );
                    }
                }
            }
        }