How to check name of helmet?

Discussion in 'Plugin Development' started by Mr_maderator_UY, Oct 30, 2022.

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

    Mr_maderator_UY

    Hi, I want to check name of helmet like this, but idk where trouble:
    Code:
    if(p.getInventory().getHelmet() != null && p.getInventory().getHelmet().getItemMeta().getDisplayName().equals("Helmet of power")){
    How can i check name of helmet when it dressed on player
     
    Last edited: Oct 30, 2022
  2. Offline

    pixelrider2000

    What exactly isn't working? Besides, you also have to check if the item has an itemmeta and after that if the item has a display name. You current code would throw an exception when a player wears a helmet without a name.
     
  3. Offline

    Mr_maderator_UY

    @pixelrider2000 , i need to add some effects during dressing this helmet:
    Code:
    @EventHandler
        public void Helmet(InventoryCloseEvent event) {
            Player p = (Player) event.getPlayer();
    
                if (p.getInventory().getHelmet() != null && p.getInventory().getHelmet().containsEnchantment(Enchantment.SILK_TOUCH)) {
                    PotionEffect health = new PotionEffect(PotionEffectType.REGENERATION, 10000000, 2);
                    PotionEffect dolphins = new PotionEffect(PotionEffectType.DOLPHINS_GRACE, 10000000, 1);
                    p.addPotionEffect(health);
                    p.addPotionEffect(dolphins);
                } else {
    
                        p.removePotionEffect(PotionEffectType.REGENERATION);
                        p.removePotionEffect(PotionEffectType.DOLPHINS_GRACE);
                    }
                }
    I don't know why effects didn`t added, but name of helmet was added
    EDIT:

    here is it:
    Code:
    if(p.getInventory().getHelmet() != null && p.getInventory().getHelmet().getItemMeta().getDisplayName().equals("Helmet of power")){
     
    Last edited: Oct 31, 2022
  4. Offline

    Kars

    @Mr_maderator_UY you didn't answer either of the two questions that were asked.
     
  5. Offline

    Strahan

    There are a couple problems with your approach. First, one doesn't necessarily need to trigger InventoryCloseEvent to put a helmet on. If I see a helmet on the ground, I walk over it with an empty hotbar, it will pop into slot 0. I can then shift right click slot 0 to equip it, having never opened my inv. Second, you're screwing players who use regen potions. If I craft a regen potion and drink it, then open my inv to get something, when I close it your code will strip away my regeneration even though it was not granted due to the helmet.

    To avoid both issues, you should use ArmorEquipEvent. It's not built in, but can be obtained via https://github.com/Arnuh/ArmorEquipEvent Works very well, and exposes ArmorEquipEvent#getOldArmorPiece() and ArmorEquipEvent#getNewArmorPiece() making it easy to handle the application/removal of potion effects w/o stripping legit ones.

    Lastly you posted the same code you had originally, so you did not read or understand pixel's post. Don't hang methods off things that are nullable. You understood that for getHelmet(), but didn't realize the same applies to getDisplayName(). getItemMeta() is nullable too, but as you already verified an item is present on the helmet slot no need to null check that as it will have meta.
     
  6. Offline

    Mr_maderator_UY

    Which one?
     
Thread Status:
Not open for further replies.

Share This Page