Nerdfuryz I'll make this Nerdfuryz Here you go. The only way to get the absorption to run for 20 seconds was to increase it to level II, so I hope that's alright with you. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
au2001 This is the line in question: Code:java PotionEffect absorb = new PotionEffect(PotionEffectType.ABSORPTION, 400, 1); It gives Absorption II for 20 seconds. When I change the amplifier to 0 it gives Absorption I, however it lasts for 2 minutes even though the duration remains at 400 ticks.
timtower That's what I thought; 0 does give the Absorption I effect, however it lasts for 2 minutes even though I've specified 20 seconds. In the line that I posted above the player gets Absorption II and it lasts for 20 seconds.
dsouzamatt Did you use the same code besides the amplifier? Did you also made sure that you removed the old effect?
Yes. Here, I'll past the whole class: Code:java package me.dsouzamatt.NerfApples; import org.bukkit.Material;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.player.PlayerItemConsumeEvent;import org.bukkit.plugin.java.JavaPlugin;import org.bukkit.potion.PotionEffect;import org.bukkit.potion.PotionEffectType; public class Main extends JavaPlugin implements Listener{ public void onEnable() { getServer().getPluginManager().registerEvents(this, this); } @EventHandler public void onEat(PlayerItemConsumeEvent event) { if(event.getItem().getType() == Material.GOLDEN_APPLE) { Player player = event.getPlayer(); player.removePotionEffect(PotionEffectType.ABSORPTION); player.removePotionEffect(PotionEffectType.REGENERATION); PotionEffect absorb = new PotionEffect(PotionEffectType.ABSORPTION, 400, 0); PotionEffect regen = new PotionEffect(PotionEffectType.REGENERATION, 400, 1); player.addPotionEffect(absorb); player.addPotionEffect(regen); } }} The player is left with Absorption I for 2 minutes. When the amplifier is changed to 1 they get Absorption II for 20 seconds.
I did test it, and did remove player.addPotionEffect(absorb); And the plugin did not remove the old potion effect.
Just as a test if you tried: Code:java PotionEffect absorb = new PotionEffect(PotionEffectType.ABSORPTION, 400 / 6, 0); It might, or might not work. Worth a try though?
It does not remove the old apple potion effect. The event is called when the player eats and not when he finished the apple. This code will work But you only have to remove the apple and i am not sure how to do that in this event. Code:java package me.dsouzamatt.NerfApples; import org.bukkit.Material;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.player.PlayerItemConsumeEvent;import org.bukkit.plugin.java.JavaPlugin;import org.bukkit.potion.PotionEffect;import org.bukkit.potion.PotionEffectType; public class Main extends JavaPlugin implements Listener{ public void onEnable() { getServer().getPluginManager().registerEvents(this, this); } @EventHandler public void onEat(PlayerItemConsumeEvent e) { if(e.getItem().getType() == Material.GOLDEN_APPLE){ Player p = e.getPlayer(); e.setCancelled(true); //Get players inventory and remove 1 goldenapple PotionEffect a = new PotionEffect(PotionEffectType.ABSORPTION, 400, 0); PotionEffect r = new PotionEffect(PotionEffectType.REGENERATION, 400, 1); p.addPotionEffect(a); p.addPotionEffect(r); }else{ return; } }}
TheLegendOffPabu Why not let the event run, make a runnable that runs a tick later ( so the player will have the effect already and the apple is gone ) and then remove the existing effect and set a new one.
Quick question is this for golden apples or god/notch/enchanted apples? Because you would need to discriminate between them.
timtower Yes that would work, if somebody will make it okey. but not 4 me. I hate runnable, i always ahve trouble whit it. @dsouzamatt @Nerdfuryz This code will remove the old and add the new, it will take 1 apple It works for enchanted golden apples and normal golden apples. And it will give you the right apple back. I did put the code to split the 2 kinds of apples if you want to do that. Code:java package me.dsouzamatt.NerfApples; import org.bukkit.Material;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.player.PlayerItemConsumeEvent;import org.bukkit.plugin.java.JavaPlugin;import org.bukkit.potion.PotionEffect;import org.bukkit.potion.PotionEffectType; public class Main extends JavaPlugin implements Listener{public void onEnable(){getServer().getPluginManager().registerEvents(this, this);} @SuppressWarnings("deprecation")@EventHandlerpublic void onEat(PlayerItemConsumeEvent e){ if(e.getItem().getType() == Material.GOLDEN_APPLE)/*&& e.getItem().getDurability() == 1 (Enchanted Golden apples || && e.getItem().getDurability() == 0 (Normal golden apple */{Player p = e.getPlayer();e.setCancelled(true);p.updateInventory();int as = e.getItem().getAmount() - 1;ItemStack apple = new ItemStack(Material.GOLDEN_APPLE, as);int dura = e.getItem().getDurability(); /* To see if it is an Enchanted golden apple or not */apple.setDurability((short) dura);p.getInventory().setItemInHand(apple);p.updateInventory();PotionEffect a = new PotionEffect(PotionEffectType.ABSORPTION, 400, 0);PotionEffect r = new PotionEffect(PotionEffectType.REGENERATION, 400, 1); p.addPotionEffect(a);p.addPotionEffect(r);}else{return;}}} EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Yeah thanks guys. Ok one problem, you made the god apple exactly the same as the normal golden apple. If you could make it slightly better that would be cool. I think the regen 2 thing is what made it the same. EDIT by Moderator: merged posts, please use the edit button instead of double posting.