How to make interact event with exact ItemMeta?

Discussion in 'Plugin Development' started by uyscutix, Sep 21, 2017.

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

    uyscutix

    What's the code or if statement thing to make tools in my player interact event ONLY work if the tool has the display name "(insert display name here)" and optionally lores with each line named "(insert lore lines here)"?

    I've been working on this explosive thor hammer for a while now, and any diamond axe will trigger the event. I want to know the code/if statement to make it so only diamond axes named something like the above I mentioned (display name and/or lore lines) work. For example, the event will only trigger if the diamond axe is named "Thor's Hammer". (Also includes chat colours)

    I need to know one that is compatible with the way I coded the thor's hammer here, below:
    Code:
        @EventHandler
        public void onInteract(PlayerInteractEvent e)
        {
            final Player player = e.getPlayer();
           
            switch (e.getAction())
            {
                case RIGHT_CLICK_AIR:
                case RIGHT_CLICK_BLOCK:
                {
                    switch (e.getMaterial())
                    {
                        case DIAMOND_AXE:
                        {
                            if (Command_exec.THOR_HAMMER.contains(player.getName()))
                            {
                                Location target = player.getTargetBlock((Set) null, 78).getLocation();
    
                                int i = 0;
    
                                do
                                {
                                    player.getWorld().strikeLightning(target);
                                    i++;
                                } 
                                while (i < Command_exec.THOR_HAMMER_POWER);
    
                                player.getWorld().createExplosion(target, Command_exec.THOR_HAMMER_EXP_RADIUS);
                                player.getWorld().playSound(target, Sound.ENTITY_ENDERDRAGON_GROWL, 10, 1);
                               
                                player.getWorld().spawnParticle(Particle.CLOUD, target, 750, 0, 0, 0, 1.5F);
                                player.getWorld().spawnParticle(Particle.SMOKE_LARGE, target, 750, 0, 0, 0, 1.5F);
                                player.getWorld().spawnParticle(Particle.FLAME, target, 750, 0, 0, 0, 1.5F);
                            }
                            else
                            {
                                e.setCancelled(true);
                            }
                        }
                    }
                }
            }
        }
    
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    Minesuchtiiii

    What I use and works for me:
    Code:
    if(player.getItemInHand().hasItemMeta() && player.getItemInHand().getItemMeta().getDisplayName().equals("Your display name here")) {
    
    //do stuff
    
    }
     
  4. Offline

    uyscutix

    I've tried that one before, when I used it, the entire event didn't work. Maybe it does work, but I equipped the item with the exact same display name as I wrote on the if statement and it didn't trigger the event.

    I named the display name: ChatColor.RED + "Thor's Hammer!",
    I then used Essentials' command and did: /i DAxe 1 name:&cThor's_Hammer!

    After spawning the diamond axe with exact matching name, it still didn't work.
     
  5. Offline

    Minesuchtiiii

    Try using "§cThor's Hammer!", it's less "code" than "ChatColor.RED + .... then check if the displayName equals ("§cThor's Hammer!");
     
  6. Offline

    Zombie_Striker

    @Minesuchtiiii
    Do not use raw color codes. Not only do the change the encoding of the files, making them larger on disk, but can break in future updates. Use ChatColors or the translteColorCode methods if you need to include chatcolors.
     
  7. Offline

    uyscutix

    assuming even the colour code is required too, will it also accept '&' instead of §?
     
  8. Offline

    Horsey

    @uyscutix I'd just use the ChatColor enum; it will definitely be the same over all versions, and it will not break in the (unlikely) event that Minecraft changes it's formation codes.
     
  9. Offline

    Zombie_Striker

    @Horsey
    They're making a lot of changes to minecraft (changing all textures, moving to C, changing the usages or parameters for commands, changing the way items are stored). I would not be surprised if they changed the way color codes work.
     
  10. Offline

    Minesuchtiiii

    If you are getting it from the config you first have to replace the strings "&[colorcode]" with "§[colorcode]"
     
  11. Online

    timtower Administrator Administrator Moderator

    No you don't... You can just use ChatColor.translateAlternateColorCode('&', string )
     
  12. Offline

    Minesuchtiiii

    Yeah, that's like almost the same
     
  13. Online

    timtower Administrator Administrator Moderator

    But protected against the changed that @Zombie_Striker pointed out.
     
Thread Status:
Not open for further replies.

Share This Page