Solved Checking if item has certain name when clicked upon?

Discussion in 'Plugin Development' started by beanonaboard, Aug 14, 2014.

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

    beanonaboard

    Hi I have given a player a custom item but I cannot work out what I am doing wrong when I am attempting to check to see if that item has that name, the code for it is below.
    Code:java
    1. @EventHandler
    2. public void onPlayerUse(PlayerInteractEvent e) {
    3.  
    4. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    5. Player p = e.getPlayer();
    6. Arena a = this.playerInArena(p);
    7. if(a == null) return;
    8.  
    9. if(p.getInventory().getItemInHand().getType().equals(Material.PAPER) && (p.getInventory().getItemInHand().getItemMeta().getDisplayName().contains("Click To Vote"))) {
    10. p.openInventory(myInventory);
    11. return;
    12. }
    13. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  2. Offline

    Luntic

    You can use this

    Code:java
    1. ItemStack item = event.getCurrentItem();
    2. if ((item != null) && (item.getType() != Material.AIR)) {
    3. String dispname = item.getItemMeta().getDisplayName();
    4. if (dispname.equals("§cClick to vote"))
    5. {
    6. Player player = (Player)event.getWhoClicked();
    7. player.playEffect(player.getLocation(), Effect.CLICK1, 1);
    8. player.openInventory(myInventory);
    9. player.closeInventory();
    10. }
     
Thread Status:
Not open for further replies.

Share This Page