How to get the item in the eventhandler

Discussion in 'Plugin Development' started by Maxpnl, May 14, 2017.

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

    Maxpnl

    Hello, I can't figure out how to make this work, I have a public boolean (the onCommand one) and a public void(the eventHandler), I need to get an ItemStack object from the onCommand to the EventHandler but if I declare a public ItemStack inside the class it's just void... What's the problem?

    Here's the code

    Code:
     public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (!(sender instanceof Player)) {
                return false;
            }
            Player p = (Player) sender;
            ItemStack item = new ItemStack(Material.CHEST);
            ItemMeta im = item.getItemMeta();
            im.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "LotteryChest");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.DARK_AQUA + "Click to open!");
            im.setLore(lore);
            item.setItemMeta(im);
            p.getInventory().addItem(new ItemStack(item));
    
            return true;
        }
    
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent event) {
            Player p = (Player) event.getPlayer();
            ItemStack inhand = event.getItemInHand();
            logger.info(String.valueOf(inhand) + "1");
            logger.info(String.valueOf(item));
            if (inhand == item) {
                new OpenChest(plugin).openInventory(p);
                event.setCancelled(true);
            }
        }
    }
     
  2. Offline

    Zombie_Striker

    @Maxpnl
    Since the chest is the same for all players, only create the chest once and store it as a field. Then, reference that field in both the onCommand and the event.

    BTW: You don't need to create a new itemstack of "Item" when adding it to the inventory. 'item' is already an itemstack, so just reference that object..

    [Edit] Don't use == when comparing objects. == compares memory location, which is only useful if you are looking for exact instances or numbers. For this, you need to do the checks for the displayname and item types instead of the whole instance.

    And, are you sure you want to use BlockPlaceEvent? That is only called when a they click a block close to them. If they aim at the sky, or to a block far away, this event will not be called. You should try using PlayerInteractEvent instead.
     
    Maxpnl likes this.
  3. Offline

    Maxpnl

    I am using both (should I use only PlayerInteract?)




    So, how the public ItemStack should look like?

    That's why I am having problem with this right now, what should I use instead of == ? EDIT: found that the equal() method fixes the problem

    Thanks for the help by the way :D
     
    Last edited: May 14, 2017
  4. Offline

    Zombie_Striker

    @Maxpnl
    1. PlayerInteractEvent is called whenever a player click anything anywhere. BlockPlaceEvent is called only when the player is click a valid spot where the block can be placed. Not only that, but grief protection plugins or build protection plugins cancel the event, so this will only work in places where the player can actually build. As you can see, using BPE limits the amount of ways you can open this inventory.
    2. public Itemstack chest; and then, in the onEnable or the class's constructor, create the itemstack and set chest equal to that itemstack.
    3. .equals will only work if the item is exactly the same, so you cannot change the amount (i.e can't pick up multiple chests).
    If your problem has been solved, mark this thread as solved.
     
  5. Offline

    Maxpnl

    Just one last question: What should I use instead of .equals? Thanks!
     
  6. Offline

    ipodtouch0218

    Use ItemStack#isSimilar. It's the exact same as .equals, but doesn't check amounts.
     
  7. Offline

    Zombie_Striker

    @ipodtouch0218
    No. Not only does it not check the amount, it also does not check the item meta.

    @Maxpnl
    You have to check if the displaynames are the same.
     
  8. Offline

    ipodtouch0218

Thread Status:
Not open for further replies.

Share This Page