[Fix] Allow Feed to be used via console

Throw events when modifying heath or food levels.
This commit is contained in:
KHobbits 2013-05-04 16:01:12 +01:00
parent aac9e4884d
commit 6e20275cc7
3 changed files with 74 additions and 10 deletions

View File

@ -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()));
}
}

View File

@ -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"));

View File

@ -0,0 +1,15 @@
package com.earth2me.essentials.commands;
public class QuietAbortException extends Exception
{
public QuietAbortException()
{
super();
}
public QuietAbortException(String message)
{
super(message);
}
}