Regen

Discussion in 'Plugin Development' started by LazerAspect, Aug 20, 2016.

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

    LazerAspect

    Trying to give 10 seconds of regen 2 when a player eats a named golden apple, but it isn't working.

    Code:
    @EventHandler
        public void onHeadEat(PlayerItemConsumeEvent e) {
            ItemStack item = e.getItem();
            if (item.getType().equals((Material.GOLDEN_APPLE))) {
                Player p = e.getPlayer();
                if (item.getItemMeta().getDisplayName() != null){
                    String itemname = item.getItemMeta().getDisplayName();
                    if (itemname.equalsIgnoreCase(ChatColor.GOLD + "Golden Head")) {
                         PotionEffect regen = new PotionEffect(PotionEffectType.REGENERATION, 200, 1);
                         p.removePotionEffect(PotionEffectType.REGENERATION);
                         p.addPotionEffect(regen);
                         p.sendMessage("test");
                   
                    }
                }
               
            }
        }
    the debug "test" message runs, so I'm not sure why it isn't working
     
  2. Offline

    InstanceofDeath

    Your @EventHandler is in the wrong position. Its an Bukkit annotation so you must put it direct above the "public void" ... just a small mistake ;) so the method will get executed without knowing its annotation parameter
     
  3. Offline

    LazerAspect

    It is directly above it. I don't know why but it messed up when I pasted it here
     
  4. Offline

    InstanceofDeath

    okey ..... Then maybe you forgot to implement the interface Listener?
     
  5. Offline

    LazerAspect

    Nope. Like I said in my original post, the debug message after the potion effect runs.
     
  6. Offline

    InstanceofDeath

    maybe the status effect does not work, because it has only a 1 sec. duration??? The I have no more ideas ..
     
  7. Offline

    LazerAspect

    ? I put it to 200 ticks which is 10 seconds
     
  8. Offline

    InstanceofDeath

    Isnt it Effect, Power of Effect, Duration?!
     
  9. Offline

    LazerAspect

  10. Offline

    Zombie_Striker

    Use == to compare enums.

    You should check if the item has itemmeta before you access it. If the item has no item meta, then this will throw an NPE.

    What if you do not remove the potion effect? If you cancel the event, is the potion effect added?
     
    Last edited: Aug 20, 2016
  11. Offline

    LazerAspect


    Alright, thank you for responding.

    I made those changes and it still only gives the default effects of a golden apple.

    EDIT: To answer your question, it still does not work if I remove the part where it gets rid of the potion effects.
     
    Last edited: Aug 20, 2016
  12. Offline

    LazerAspect

    Bump. This is my code now, still doesn't work. I tried doing a different potion effect and it does that, but won't work with regen.

    Code:
    @EventHandler
        public void onHeadEat(PlayerItemConsumeEvent e) {
            ItemStack item = e.getItem();
            if (item.getType() == Material.GOLDEN_APPLE) {
                Player p = e.getPlayer();
                if (item.hasItemMeta()) {
                    if (item.getItemMeta().getDisplayName() != null) {
                        String itemname = item.getItemMeta().getDisplayName();
    
                        if (itemname.equalsIgnoreCase(ChatColor.GOLD + "Golden Head")) {
                            PotionEffect effect = new PotionEffect(PotionEffectType.REGENERATION, 200, 2);
                            p.addPotionEffect(effect, true);
                           
                           
                        }
                                           
                                   
                    }
                }
    
            }
     
  13. @LazerAspect The best think you can do at this point is just to debug.
     
  14. Offline

    TehCoderHD

    Code:
    @EventHandler
    public void onHeadEat(PlayerItemConsumeEvent e) {
    ItemStack item = e.getItem();
    if (item.getType() == Material.GOLDEN_APPLE) {
    Player p = e.getPlayer();
    if (item.hasItemMeta()) {
    if (item.getItemMeta().getDisplayName() != null) {
    String itemname = item.getItemMeta().getDisplayName();
    
    if (itemname.equalsIgnoreCase(ChatColor.GOLD + "Golden Head")) {
    Bukkit.getScheduler().scheduleSyncDelayedTask(this.getPlugin(), new Runnable(){
    public void run(){
    PotionEffect effect = new PotionEffect(PotionEffectType.REGENERATION, 200, 2);
    p.addPotionEffect(effect, true);
    }
    }, 2L);
    
    
    }
    
    
    }
    }
    
    }
    Basically, I'm pretty sure you went wrong because the golden apple was giving effects after you triggered your event, try this delayed task to give the effect after 2 ticks/0.02s and if it doesn't work then send the error and I'm sure someone else will help

    - Traineeee
     
  15. Offline

    EndureBlackout

    Try stripping the chat color from the display name before checking it.
    like this:
    Code:
     if (itemname.equalsIgnoreCase(ChatColor.stripColor( "Golden Head"))) {
                            PotionEffect effect = new PotionEffect(PotionEffectType.REGENERATION, 200, 2);
                            p.addPotionEffect(effect, true);
                         
                         
                        }
     
  16. Offline

    bwfcwalshy Retired Staff

    @EndureBlackout Your stripping a normal string so it will return the same.
     
  17. Offline

    Orange Tabby

    @LazerAspect
    I think it's because the default golden apple re-gen effect is overriding your custom one, so this maybe:
    Code:
    PotionEffect effect = new PotionEffect(PotionEffectType.REGENERATION, 200, 2);
    p.removePotionEffect(effect.getType());
    p.addPotionEffect(effect, true);
    
    or this may work
    Code:
    PotionEffect effect = new PotionEffect(PotionEffectType.REGENERATION, 200, 2);
    p.addPotionEffect(effect, true);
    e.setCancelled(true)
    p.setItemInHand(air/null)
    
    I'm not sure if this will work, but It's worth a shot
     
Thread Status:
Not open for further replies.

Share This Page