Solved ItemMeta with PlayerInteractEvent

Discussion in 'Spigot Plugin Development' started by PCPSells, May 23, 2016.

  1. Code -
    Code (Text):
    package listeners;
    import ActionBarAPI.ActionBarAPI;
    import me.PCPSells.MaineT;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import stats.Stats;
    public class RightClickToken
      implements Listener {
      MaineT plugin;
      public RightClickToken(MaineT pl) {
        this.plugin = pl;
      public void onPlayerInteract(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        ItemStack air = new ItemStack(Material.AIR);
        if (e.getItem().getType() == Material.getMaterial(plugin.getConfig().getString("tokens.TokenID")) && e.getItem().getItemMeta().equals(plugin.getConfig().getString("tokens.TokenName").replace("&", "§"))) {
          ActionBarAPI bar = new ActionBarAPI(p, plugin.getConfig().getString("ActionBarMsgs.Profit").replace("&", "§").replace("%prefix%", plugin.getConfig().getString("messages.Prefix").replace("&", "§")).replace("%amount%", "" + e.getPlayer().getItemInHand().getAmount()));
          p.playSound(p.getLocation(), Sound.valueOf(plugin.getConfig().getString("sounds.Success")), 1.0F, 1.0F);
          Stats.addPoints(p, e.getPlayer().getItemInHand().getAmount());
    This would be a simple Item click event, but for
    Code (Text):
    if (e.getItem().getType() == Material.getMaterial(plugin.getConfig().getString("tokens.TokenID")) && e.getItem().getItemMeta().equals(plugin.getConfig().getString("tokens.TokenName").replace("&", "§"))) {
    It doesn't work when I add the ItemMeta... any ideas why?
  2. You're trying to compare ItemMeta object with a string. Perhaps you want to compare display name?
  3. Ohh yeah, that makes sense. Completely forgot you have to get the display name. Appreciate it. DERP <3
    • Friendly Friendly x 1
  4. e.getItem().getItemMeta() returns ItemMeta, you're comparing it to a string. If you're looking for the name do e.getItem().getItemMeta().getName().
    NINJA'D BY @NinjaStix

    But I also have another comment to make, you replaced & with the weird character thingy, but you don't need to do that (I don't think, but still easier). Just do:

    Code (Text):
    • Like Like x 1
  5. ItemMeta isn't a string, so why are you comparing it with a string? I think ItemMeta.getDisplayName() is what you're looking for.

    Whoops little late reponse :p Question had been answered already, my bad.
  6. ItemMeta.getName doesn't exist as far as I know :p
  7. Funny thing is, I did that, I had it get the Material and added
    Code (Text):
     && e.getItem().getItemMeta().getDisplayName() == plugin.getConfig().getString("tokens.TokenName").replace("&", "")
    and it doesn't work.. I remove that and it works again.. any idea why?
  8. Use the #equals method here and not ==