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
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
maybe the status effect does not work, because it has only a 1 sec. duration??? The I have no more ideas ..
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?
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.
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); } } } }
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
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); }
@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