Checking if you are holding a trapdoor not working?

Discussion in 'Plugin Development' started by Joey402MC, Mar 6, 2016.

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

    Joey402MC

    I'm trying to make a plugin where if you hold a trapdoor it gives you regeneration, kind of like a shield. I'm trying to make it check whether or not you are holding a trapdoor using the PlayerItemHeldEvent and if you are, adding the potion effect.

    Here's the code: http://pastebin.com/pd2RA2mA


    However when I hold a trapdoor I don't get any potion effects.
     
  2. Offline

    webbhead

    Answer is simple, the event is PlayerItemHeldEvent, you have to get the item they switched to: ItemHeldEvent gets the item the held last.
     
  3. Offline

    Joey402MC

    How would you get the item they switched to? Would it have to do with getting the new item slot and then finding the item in that slot? I was able to do e.getNewSlot but don't know how to get the item in that slot. @webbhead
     
    Last edited: Mar 6, 2016
  4. Offline

    cluter123

    Code:
    @EventHandler
        public void onHold(PlayerItemHeldEvent e) {
            if (e.getPlayer().getInventory().getItem(e.getNewSlot()).getType().equals(Material.IRON_TRAPDOOR)) {
                e.getPlayer().addPotionEffect(PotionEffectType.REGENERATION.createEffect(5000, 10));
            }
            else {
                e.getPlayer().removePotionEffect(PotionEffectType.REGENERATION);
            }
        }
    
     
  5. Offline

    Lordloss

    @cluter123
    Good job, cpt. spoonfeed!
    Btw, dont know if it matters for his plugin, but this way every player who switches an item will loose the regeneration effect, wether it was gained by the trapdoor or not. Could be problematic if there are regen pots too etc.
     
    Zombie_Striker likes this.
  6. Offline

    cluter123

    Yeah. Add the player to an arraylist and and check if the player is on the array list when the player switches out and remove the player if he and remove the regen effect. Or you could just check if the player isn't holding a trapdoor and just exit out of the code.
     
Thread Status:
Not open for further replies.

Share This Page