Solved Players able to move items

Discussion in 'Plugin Development' started by XSilverSlayerX, Dec 6, 2016.

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

    XSilverSlayerX

    So I have an EventHandler that cancels all moving of certain items, It works perfectly except, When a player clicks a number 1-10 in their hot bar and has their cursor hovering over a blank spot in their inventory it allows movement of the item. This is because for some reason the item cached is either NULL or AIR. If you click on the item or try to drop it out of your inventory it blocks that but for some reason moving the item to the cursors location is allowed. I have tried adding a delay and it seems to correctly cache the item but then the event doesn't cancel and everything goes in reverse - meaning when players try to click and drag the item or drop it, it shows up as NULL or AIR. So I have came to the conclusion adding a delay doesn't work since it breaks everything else.. This has been such a pain for me.. Any help would be amazing!


    Code I use;
    Code:
        @EventHandler
          public void onInventoryModify(InventoryClickEvent event)
          {
          String modifier = ".itemflags";
          String mod = "inventory-modify";
            Player player = (Player) event.getWhoClicked();
            String world = WorldHandler.getWorld(player.getWorld().getName());
           ItemStack item = event.getCurrentItem(); // Shows up as NULL or AIR //
            Registers.Console.sendMessage(ChatColor.RED + item.toString()); // Debugging //
            if (!CheckItem.isAllowedItem(player, world, item, modifier, mod)) // Checks if Item is equal to an item in my list //
            {
               event.setCancelled(true);
            PlayerHandlers.updateInventory(player);
            Registers.Console.sendMessage(ChatColor.RED + "finished."); // Debugging //
           }
         }
    
     
  2. Offline

    Zombie_Striker

    tyler672 likes this.
  3. Offline

    XSilverSlayerX

    Again this isnt the problem. The problem is
    ItemStack item = event.getCurrentItem(); returns NULL or AIR. I need it to return the item that was moved so I can cancel the event

    If I do what you are saying it will cancel ALL movement of ANY items. I am trying to compare the item with a list of items to see if they are similar but the item keeps caching null or air instead of the actual item that was moved. (The CurrentItem)
     
  4. Offline

    DuaneTheDev_

    @XSilverSlayerX

    Code:
           Action a = event.getAction();
           ItemStack is = event.getItem();
    
        if (a == Action.PHYSICAL || is == null || is.getType() == Material.AIR)
            return;
    
        //do stuff if not null or air
          if(itemList.Contains(is))  {
    }
    
     
  5. Offline

    XSilverSlayerX

    Doesn't work. Not sure how this is suppose to fix my issue with the Item still not being correctly detected. Also the event.getAction() doesn't work because this isnt a playerinteractevent.
     
  6. Offline

    DuaneTheDev_

    Code:
    if(!ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("invName"))
                   return;
           Player player = (Player) event.getWhoClicked();
           event.setCancelled(true);
        
           if(event.getCurrentItem()==null || event.getCurrentItem().getType()==Material.AIR||!event.getCurrentItem().hasItemMeta())  {
               player.closeInventory();
               return;
           }

    Can I see this "item" list.
     
    Last edited: Dec 6, 2016
  7. Offline

    XSilverSlayerX

    No because its user definable nothing is wrong with the item list.
    What I am trying to do is get the point across is that, ItemStack item = event.getCurrentItem(); returns null or air instead of the item the player moved in their inventory using keys 1-10 on their keyboard to their mouse location.
     
  8. Offline

    XSilverSlayerX

    Still having problems with this, anyone have any ideas?
     
  9. Offline

    tastybento

  10. Offline

    XSilverSlayerX

    The problem is this wont work because, I am trying to prevent certain items from being moved, so i cant do all by default, and its the players inventory not and open inventory. I also cant use RawSlot because its an int not an itemstack.

    I would love to know why my method is not working. Its like its a damn bug with bukkit/spigot because it should logically work.
     
  11. Offline

    ipodtouch0218

    @XSilverSlayerX
    I was testing and I found out that you can use #getSlot() and #getClickedInventory() to be able to #get(int slot) the itemstack, and check the material from that.

    One problem is that it won't work for changing items that aren't hovered over the item itself. What you can do then is #getSlot() the same way, but schedule the event one tick later.

    If none of these work, you could always get all the items from an array and compare which one is changed/missing in the newest one.
     
  12. Offline

    MaxFireIce

    If you just want them to not be able to move anything, why dont you try making them not able to open the inventory in the first place. And, if you only want it for specific worlds, iterate through a config list of worlds and check if the players world is one of the saved worlds.
    @XSilverSlayerX
     
  13. Offline

    XSilverSlayerX

    If you read the whole post you would see I only dont want them to move CERTAIN items. I want them to be able to move any other items in their inventory freely but the items I set to not move should not move.
     
  14. Offline

    mythbusterma

    @XSilverSlayerX

    I'm not sure there is a particularly clean way to do this, as this has to do with the way that particular event is handled.

    One thought is you may want to schedule something to run one tick later that checks if one of the bad items was the one that was moved, and undo it if was.

    It's kinda hacky, but should work fine.
     
    ipodtouch0218 likes this.
  15. Offline

    RockinChaos

    Sorry for the late reply I have been having issues logging into my bukkit account on my main computer.
    I tried this method but it conflicts with other movement of items, since I want some of the items to be movable when I want them to be, as well as if I have it checking constantly it causes some lag since I have a bunch of loops checking each item.

    Finally figured out a fix for it.
    Just use this in your code;

    Code:
                            ItemStack item;
                                if (event.getAction().name().contains("HOTBAR")) {
                                    item = event.getView().getBottomInventory().getItem(event.getHotbarButton());
                                } else {
                                    item = event.getCurrentItem();
                                }
    
    This checks if the item is in your hotbar, and if you are clicking a hotbar button on your keyboard trying to move it. If your not doing that then it just reverts back to the default getCurrentItem().
     
    Last edited: Jan 27, 2017
Thread Status:
Not open for further replies.

Share This Page