diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandfeed.java b/Essentials/src/com/earth2me/essentials/commands/Commandfeed.java index 4644e6c3a..3dda416eb 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandfeed.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandfeed.java @@ -6,6 +6,7 @@ import java.util.List; import org.bukkit.Server; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.event.entity.FoodLevelChangeEvent; public class Commandfeed extends EssentialsCommand @@ -24,13 +25,22 @@ public class Commandfeed extends EssentialsCommand } else { - user.setFoodLevel(20); - user.setSaturation(10); - user.sendMessage(_("feed")); + feedPlayer(user, user); } } - private void feedOtherPlayers(final Server server, final CommandSender sender, final String name) throws NotEnoughArgumentsException + @Override + protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception + { + if (args.length < 1) + { + throw new NotEnoughArgumentsException(); + } + + feedOtherPlayers(server, sender, args[1]); + } + + private void feedOtherPlayers(final Server server, final CommandSender sender, final String name) throws NotEnoughArgumentsException, QuietAbortException { boolean skipHidden = sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.vanish.interact"); boolean foundUser = false; @@ -43,13 +53,28 @@ public class Commandfeed extends EssentialsCommand continue; } foundUser = true; - matchPlayer.setFoodLevel(20); - matchPlayer.setSaturation(10); - sender.sendMessage(_("feedOther", matchPlayer.getDisplayName())); + feedPlayer(sender, matchPlayer); + } if (!foundUser) { throw new NotEnoughArgumentsException(_("playerNotFound")); } } + + private void feedPlayer(CommandSender sender, Player player) throws QuietAbortException + { + final int amount = 20; + + final FoodLevelChangeEvent flce = new FoodLevelChangeEvent(player, amount); + ess.getServer().getPluginManager().callEvent(flce); + if (flce.isCancelled()) + { + throw new QuietAbortException(); + } + + player.setFoodLevel(flce.getFoodLevel()); + player.setSaturation(10); + sender.sendMessage(sender.equals(player) ? _("feed") : _("feedOther", player.getDisplayName())); + } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandheal.java b/Essentials/src/com/earth2me/essentials/commands/Commandheal.java index e1495bdc6..c95ad6a81 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandheal.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandheal.java @@ -6,6 +6,8 @@ import java.util.List; import org.bukkit.Server; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.event.entity.EntityRegainHealthEvent; +import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason; import org.bukkit.potion.PotionEffect; @@ -61,8 +63,15 @@ public class Commandheal extends EssentialsCommand continue; } foundUser = true; - healPlayer(matchPlayer); - sender.sendMessage(_("healOther", matchPlayer.getDisplayName())); + try + { + healPlayer(matchPlayer); + sender.sendMessage(_("healOther", matchPlayer.getDisplayName())); + } + catch (QuietAbortException e) + { + //Handle Quietly + } } if (!foundUser) { @@ -76,7 +85,22 @@ public class Commandheal extends EssentialsCommand { throw new Exception(_("healDead")); } - player.setHealth(player.getMaxHealth()); + + final int amount = player.getMaxHealth() - player.getHealth(); + final EntityRegainHealthEvent erhe = new EntityRegainHealthEvent(player, amount, RegainReason.CUSTOM); + ess.getServer().getPluginManager().callEvent(erhe); + if (erhe.isCancelled()) + { + throw new QuietAbortException(); + } + + int newAmount = player.getHealth() + erhe.getAmount(); + if (newAmount > player.getMaxHealth()) + { + newAmount = player.getMaxHealth(); + } + + player.setHealth(newAmount); player.setFoodLevel(20); player.setFireTicks(0); player.sendMessage(_("heal")); diff --git a/Essentials/src/com/earth2me/essentials/commands/QuietAbortException.java b/Essentials/src/com/earth2me/essentials/commands/QuietAbortException.java new file mode 100644 index 000000000..2667ef0d3 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/commands/QuietAbortException.java @@ -0,0 +1,15 @@ +package com.earth2me.essentials.commands; + + +public class QuietAbortException extends Exception +{ + public QuietAbortException() + { + super(); + } + + public QuietAbortException(String message) + { + super(message); + } +}