Enderchest right click

Discussion in 'Spigot Plugin Development' started by UnlimitedNinjas, Jul 30, 2018.

  1. How do i make it so on right click of enderchest in inventory, the enderchest opens.
    this is what i have
    Code (Text):
        @SuppressWarnings({ "deprecation", "unlikely-arg-type" })
        public void onUse(PlayerInteractEvent e) {
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (e.getPlayer().getItemInHand().equals(Material.ENDER_CHEST)) {
                        e.getPlayer().sendMessage(ChatColor.AQUA + "Opening Enderchest!");
    • Funny Funny x 1
  2. Try to change "if (e.getPlayer().getItemInHand().equals(Material.ENDER_CHEST)) {" to "if (e.getClickedBlock().getType() == Material.ENDER_CHEST) {".
    • Like Like x 1
  3. You're comparing an ItemStack to a Material. You'd need to use ItemStack#getType and compare that to ENDER_CHEST, and you should use the '==' operator rather than #equals for the enum comparison - make sure you check if the item in their hand is null beforehand to avoid an NPE. You'd probably also want to check whether the action is RIGHT_CLICK_AIR as well as the block action.
    #3 Shockzeh, Jul 30, 2018
    Last edited: Jul 30, 2018
    • Like Like x 1
  4. I think you misread his question, it looks as if he wants it to open when they click an Ender Chest in their inventory rather than the block itself as clicking that would open it anyways.
  5. It doesnt let me to check for ItemStack.getType
  6. The item in their hand is an ItemStack, so invoke getType() from that.
  7. Make it "if (e.getPlayer().getItemInHand().getType().equals(Material.ENDER_CHEST)) {
  8. Use the '==' operator to compare enums.
  9. You'd probably want to use Action.RIGHT_CLICK_AIR, rather than Action.RIGHT_CLICK_BLOCK.
  10. The fact that you are suppressing warnings indicates that you are ignoring a potentially useful message.
    When you see this, you should immediately be thinking of a comparison of a generic object such as what might be passed to the equals method (where your problem lies)
    • Agree Agree x 1
  11. I figured it out but it uses a depreciatied method (idk really how to spell that word). I dont know any other way for getItemInHand()
  12. The reason that it's deprecated is that there are two hands - there's an event method for this, and that's event.getItemInHand(). As for why you're getting an unlikely-arg-type warning as well, it's because you were comparing an ItemStack to a Material type.