Solved PlayerItemHeldEvent

Discussion in 'Plugin Development' started by ArcheRion720, Mar 17, 2016.

Thread Status:
Not open for further replies.
  1. I have a problem with PlayerItemHeldEvent. I can't add potion effect to player who holds the selected item.
    Event works when i change slot from selected item.

    My code:

    Code:
    @EventHandler(priority=EventPriority.LOWEST)
            public void onItemHeld(PlayerItemHeldEvent e){
                Player p = (Player) e.getPlayer();
                if(p.getItemInHand()==null||!p.getItemInHand().getType().name().endsWith("SWORD")||!p.getItemInHand().hasItemMeta())return;
                if(!p.getItemInHand().getItemMeta().hasLore())return;
                if(!hasCustomEnchant(p.getItemInHand().getItemMeta().getLore()))return;
                for(String s : p.getItemInHand().getItemMeta().getLore()){
                    if(!isCustomEnchant(s))continue;
                    if(s.startsWith(ChatColor.RESET + "Scout ")){
                        int level = parseRomanNumber(s.split("\\s+")[1]);
                        p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 5*20, level, false));
                        p.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 5*20, level, false));
                        }
                    }
                }
     
    Last edited: Mar 17, 2016
  2. Online

    timtower Administrator Administrator Moderator

  3. done
     
  4. Online

    timtower Administrator Administrator Moderator

    @ArcheRion720 Did you register the event? Are you sure that the event is running? Are you sure that the code that is adding the potion effects is actually being called?
     
  5. I have more EventHandlers and all works correctly. That works, but not correctly. This event runs when i change from correct item to another item or empty slot. All of events is registered and all should work. :|
     
  6. Online

    timtower Administrator Administrator Moderator

    @ArcheRion720 Is the code responsible for the potion effects running?
    And I am not talking about the entire method, just about those 3 lines.
     
  7. Yes
     
  8. Online

    timtower Administrator Administrator Moderator

    And what is level when the code gets called?
     
  9. That plugin is about enchants, "level" is a level of enchant and amplifier of potions.
     
  10. Online

    timtower Administrator Administrator Moderator

    Yeah, but what is the level when the code is running? What value does it contain?
     
  11. in this plugin there is event about enchanting and there is a chance to add enchant (properly lore) and level is based on Enchant Exp lvl cost. There is two methods about levels. GetRomanNumber (1 = I) and ParseRomanNumber(I = 1)
     
  12. Online

    timtower Administrator Administrator Moderator

    @ArcheRion720 Put a debug message in your code, print the values, find out if it is running.
     
  13. 0 errors, all should be allright :(
     
  14. Online

    timtower Administrator Administrator Moderator

    I am not talking about errors.
    Print messages, show your console log.
     
  15. Offline

    MajorSkillage

    You don't need to use ItemStack#getType().name() just use ItemStack#getName() and as timtower said, you should be debugging before even coming here.
     
Thread Status:
Not open for further replies.

Share This Page