Extract user match looping

This commit is contained in:
KHobbits 2013-09-28 17:21:16 +01:00
parent e6b76cadbc
commit 03368e45df
14 changed files with 381 additions and 386 deletions

View File

@ -26,6 +26,7 @@ import com.earth2me.essentials.commands.EssentialsCommand;
import com.earth2me.essentials.commands.IEssentialsCommand;
import com.earth2me.essentials.commands.NoChargeException;
import com.earth2me.essentials.commands.NotEnoughArgumentsException;
import com.earth2me.essentials.commands.QuietAbortException;
import com.earth2me.essentials.metrics.Metrics;
import com.earth2me.essentials.metrics.MetricsListener;
import com.earth2me.essentials.metrics.MetricsStarter;
@ -510,6 +511,10 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials
{
return true;
}
catch (QuietAbortException ex)
{
return true;
}
catch (NotEnoughArgumentsException ex)
{
sender.sendMessage(command.getDescription());

View File

@ -152,7 +152,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, net.es
}
@Override
public void payUser(final User reciever, final BigDecimal value) throws Exception
public void payUser(final User reciever, final BigDecimal value) throws ChargeException
{
if (value.signum() == 0)
{
@ -167,7 +167,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, net.es
}
else
{
throw new Exception(_("notEnoughMoney"));
throw new ChargeException(_("notEnoughMoney"));
}
}

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.ChargeException;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.NumberUtil;
@ -7,11 +8,13 @@ import java.math.BigDecimal;
import java.util.Locale;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commandeco extends EssentialsCommand
public class Commandeco extends EssentialsLoopCommand
{
Commandeco.EcoCommands cmd;
BigDecimal amount;
public Commandeco()
{
super("eco");
@ -25,11 +28,8 @@ public class Commandeco extends EssentialsCommand
throw new NotEnoughArgumentsException();
}
Commandeco.EcoCommands cmd;
BigDecimal startingBalance = ess.getSettings().getStartingBalance();
BigDecimal amount;
BigDecimal broadcast = null;
BigDecimal broadcastAll = null;
try
{
cmd = Commandeco.EcoCommands.valueOf(args[0].toUpperCase(Locale.ENGLISH));
@ -40,83 +40,42 @@ public class Commandeco extends EssentialsCommand
throw new NotEnoughArgumentsException(ex);
}
if (args[1].contentEquals("**"))
loopOfflinePlayers(server, sender, false, args[1], args);
if (cmd == Commandeco.EcoCommands.RESET || cmd == Commandeco.EcoCommands.SET)
{
for (String sUser : ess.getUserMap().getAllUniqueUsers())
if (args[1].contentEquals("**"))
{
final User player = ess.getUser(sUser);
switch (cmd)
{
case GIVE:
player.giveMoney(amount);
break;
case TAKE:
take(amount, player, null);
break;
case RESET:
case SET:
set(amount, player, null);
broadcastAll = amount;
break;
}
server.broadcastMessage(_("resetBalAll", NumberUtil.displayCurrency(amount, ess)));
}
}
else if (args[1].contentEquals("*"))
{
for (Player onlinePlayer : server.getOnlinePlayers())
else if (args[1].contentEquals("*"))
{
final User player = ess.getUser(onlinePlayer);
switch (cmd)
{
case GIVE:
player.giveMoney(amount);
break;
case TAKE:
take(amount, player, null);
break;
case RESET:
case SET:
set(amount, player, null);
broadcast = amount;
break;
}
server.broadcastMessage(_("resetBal", NumberUtil.displayCurrency(amount, ess)));
}
}
else
{
final User player = getPlayer(server, args, 1, true, true);
switch (cmd)
{
case GIVE:
player.giveMoney(amount, sender);
break;
case TAKE:
take(amount, player, sender);
break;
case RESET:
case SET:
set(amount, player, sender);
break;
}
}
if (broadcast != null)
{
server.broadcastMessage(_("resetBal", NumberUtil.displayCurrency(broadcast, ess)));
}
if (broadcastAll != null)
{
server.broadcastMessage(_("resetBalAll", NumberUtil.displayCurrency(broadcastAll, ess)));
}
}
private void take(BigDecimal amount, final User player, final CommandSender sender) throws Exception
@Override
protected void updatePlayer(final Server server, final CommandSender sender, final User player, final String[] args) throws NotEnoughArgumentsException, ChargeException
{
switch (cmd)
{
case GIVE:
player.giveMoney(amount, sender);
break;
case TAKE:
take(amount, player, sender);
break;
case RESET:
case SET:
set(amount, player, sender);
break;
}
}
private void take(BigDecimal amount, final User player, final CommandSender sender) throws ChargeException
{
BigDecimal money = player.getMoney();
BigDecimal minBalance = ess.getSettings().getMinMoney();
@ -131,7 +90,7 @@ public class Commandeco extends EssentialsCommand
}
else
{
throw new Exception(_("insufficientFunds"));
throw new ChargeException(_("insufficientFunds"));
}
}

View File

@ -2,13 +2,12 @@ package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User;
import java.util.List;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commandext extends EssentialsCommand
public class Commandext extends EssentialsLoopCommand
{
public Commandext()
{
@ -23,7 +22,7 @@ public class Commandext extends EssentialsCommand
throw new NotEnoughArgumentsException();
}
extinguishPlayers(server, sender, args[0]);
loopOnlinePlayers(server, sender, true, args[0], null);
}
@Override
@ -31,7 +30,7 @@ public class Commandext extends EssentialsCommand
{
if (args.length < 1)
{
user.setFireTicks(0);
extPlayer(user.getBase());
user.sendMessage(_("extinguish"));
return;
}
@ -41,28 +40,18 @@ public class Commandext extends EssentialsCommand
throw new PlayerNotFoundException();
}
extinguishPlayers(server, user.getBase(), args[0]);
loopOnlinePlayers(server, user.getBase(), true, args[0], null);
}
private void extinguishPlayers(final Server server, final CommandSender sender, final String name) throws Exception
@Override
protected void updatePlayer(final Server server, final CommandSender sender, final User player, final String[] args)
{
boolean skipHidden = sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.vanish.interact");
boolean foundUser = false;
final List<Player> matchedPlayers = server.matchPlayer(name);
for (Player matchPlayer : matchedPlayers)
{
final User player = ess.getUser(matchPlayer);
if (skipHidden && player.isHidden())
{
continue;
}
foundUser = true;
matchPlayer.setFireTicks(0);
sender.sendMessage(_("extinguishOthers", matchPlayer.getDisplayName()));
}
if (!foundUser)
{
throw new PlayerNotFoundException();
}
extPlayer(player.getBase());
sender.sendMessage(_("extinguishOthers", player.getDisplayName()));
}
private void extPlayer(final Player player)
{
player.setFireTicks(0);
}
}

View File

@ -2,14 +2,13 @@ package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User;
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
public class Commandfeed extends EssentialsLoopCommand
{
public Commandfeed()
{
@ -19,33 +18,18 @@ public class Commandfeed extends EssentialsCommand
@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{
if (args.length > 0 && user.isAuthorized("essentials.feed.others"))
{
if (args[0].trim().length() < 2)
{
throw new PlayerNotFoundException();
}
if (!user.isAuthorized("essentials.feed.cooldown.bypass"))
{
user.healCooldown();
}
feedOtherPlayers(server, user.getBase(), args[0]);
return;
}
if (!user.isAuthorized("essentials.feed.cooldown.bypass"))
{
user.healCooldown();
}
try
if (args.length > 0 && user.isAuthorized("essentials.feed.others"))
{
feedPlayer(user.getBase(), user.getBase());
}
catch (QuietAbortException e)
{
//User does not need feeding.
loopOnlinePlayers(server, user.getBase(), true, args[0], null);
return;
}
feedPlayer(user.getBase());
user.sendMessage(_("feed"));
}
@ -57,38 +41,24 @@ public class Commandfeed extends EssentialsCommand
throw new NotEnoughArgumentsException();
}
feedOtherPlayers(server, sender, args[0]);
loopOnlinePlayers(server, sender, true, args[0], null);
}
private void feedOtherPlayers(final Server server, final CommandSender sender, final String name) throws PlayerNotFoundException
@Override
protected void updatePlayer(final Server server, final CommandSender sender, final User player, final String[] args) throws PlayerExemptException
{
boolean skipHidden = sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.vanish.interact");
boolean foundUser = false;
final List<Player> matchedPlayers = server.matchPlayer(name);
for (Player matchPlayer : matchedPlayers)
try
{
final User player = ess.getUser(matchPlayer);
if (skipHidden && player.isHidden())
{
continue;
}
foundUser = true;
try
{
feedPlayer(sender, matchPlayer);
}
catch (QuietAbortException e)
{
//User does not need feeding.
}
feedPlayer(player.getBase());
sender.sendMessage(_("feedOther", player.getDisplayName()));
}
if (!foundUser)
catch (QuietAbortException e)
{
throw new PlayerNotFoundException();
//Handle Quietly
}
}
private void feedPlayer(CommandSender sender, Player player) throws QuietAbortException
private void feedPlayer(final Player player) throws QuietAbortException
{
final int amount = 30;
@ -101,10 +71,5 @@ public class Commandfeed extends EssentialsCommand
player.setFoodLevel(flce.getFoodLevel() > 20 ? 20 : flce.getFoodLevel());
player.setSaturation(10);
if (!sender.equals(player))
{
sender.sendMessage(_("feedOther", player.getDisplayName()));
}
}
}

View File

@ -2,7 +2,6 @@ package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User;
import java.util.List;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -11,7 +10,7 @@ import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason;
import org.bukkit.potion.PotionEffect;
public class Commandheal extends EssentialsCommand
public class Commandheal extends EssentialsLoopCommand
{
public Commandheal()
{
@ -21,26 +20,18 @@ public class Commandheal extends EssentialsCommand
@Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{
if (args.length > 0 && user.isAuthorized("essentials.heal.others"))
{
if (args[0].trim().length() < 2)
{
throw new PlayerNotFoundException();
}
if (!user.isAuthorized("essentials.heal.cooldown.bypass"))
{
user.healCooldown();
}
healOtherPlayers(server, user.getBase(), args[0]);
return;
}
if (!user.isAuthorized("essentials.heal.cooldown.bypass"))
{
user.healCooldown();
}
healPlayer(user.getBase());
if (args.length > 0 && user.isAuthorized("essentials.heal.others"))
{
loopOnlinePlayers(server, user.getBase(), true, args[0], null);
return;
}
healPlayer(user);
}
@Override
@ -51,43 +42,30 @@ public class Commandheal extends EssentialsCommand
throw new NotEnoughArgumentsException();
}
healOtherPlayers(server, sender, args[0]);
loopOnlinePlayers(server, sender, true, args[0], null);
}
private void healOtherPlayers(final Server server, final CommandSender sender, final String name) throws Exception
@Override
protected void updatePlayer(final Server server, final CommandSender sender, final User player, final String[] args) throws PlayerExemptException
{
boolean skipHidden = sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.vanish.interact");
boolean foundUser = false;
final List<Player> matchedPlayers = server.matchPlayer(name);
for (Player matchPlayer : matchedPlayers)
try
{
final User player = ess.getUser(matchPlayer);
if (skipHidden && player.isHidden())
{
continue;
}
foundUser = true;
try
{
healPlayer(matchPlayer);
sender.sendMessage(_("healOther", matchPlayer.getDisplayName()));
}
catch (QuietAbortException e)
{
//Handle Quietly
}
healPlayer(player);
sender.sendMessage(_("healOther", player.getDisplayName()));
}
if (!foundUser)
catch (QuietAbortException e)
{
throw new PlayerNotFoundException();
//Handle Quietly
}
}
private void healPlayer(final Player player) throws Exception
private void healPlayer(final User user) throws PlayerExemptException, QuietAbortException
{
final Player player = user.getBase();
if (player.getHealth() == 0)
{
throw new Exception(_("healDead"));
throw new PlayerExemptException(_("healDead"));
}
final double amount = player.getMaxHealth() - player.getHealth();
@ -107,7 +85,7 @@ public class Commandheal extends EssentialsCommand
player.setHealth(newAmount);
player.setFoodLevel(20);
player.setFireTicks(0);
player.sendMessage(_("heal"));
user.sendMessage(_("heal"));
for (PotionEffect effect : player.getActivePotionEffects())
{
player.removePotionEffect(effect.getType());

View File

@ -1,14 +1,14 @@
package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._;
import java.util.List;
import com.earth2me.essentials.User;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageEvent;
public class Commandkill extends EssentialsCommand
public class Commandkill extends EssentialsLoopCommand
{
public Commandkill()
{
@ -23,33 +23,30 @@ public class Commandkill extends EssentialsCommand
throw new NotEnoughArgumentsException();
}
//TODO: TL this
if (args[0].trim().length() < 2)
loopOnlinePlayers(server, sender, true, args[0], null);
}
@Override
protected void updatePlayer(final Server server, final CommandSender sender, final User user, final String[] args) throws PlayerExemptException
{
final Player matchPlayer = user.getBase();
if (sender instanceof Player && user.isAuthorized("essentials.kill.exempt") && !ess.getUser(sender).isAuthorized("essentials.kill.force"))
{
throw new NotEnoughArgumentsException("You need to specify a player to kill.");
throw new PlayerExemptException(_("killExempt", matchPlayer.getDisplayName()));
}
final EntityDamageEvent ede = new EntityDamageEvent(matchPlayer, sender instanceof Player && ((Player)sender).getName().equals(matchPlayer.getName()) ? EntityDamageEvent.DamageCause.SUICIDE : EntityDamageEvent.DamageCause.CUSTOM, Short.MAX_VALUE);
server.getPluginManager().callEvent(ede);
if (ede.isCancelled() && sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.kill.force"))
{
return;
}
matchPlayer.damage(Short.MAX_VALUE);
if (matchPlayer.getHealth() > 0)
{
matchPlayer.setHealth(0);
}
final List<Player> matchedPlayers = server.matchPlayer(args[0]);
for (Player matchPlayer : matchedPlayers)
{
if (sender instanceof Player && ess.getUser(matchPlayer).isAuthorized("essentials.kill.exempt") && !ess.getUser(sender).isAuthorized("essentials.kill.force"))
{
throw new Exception(_("killExempt", matchPlayer.getDisplayName()));
}
final EntityDamageEvent ede = new EntityDamageEvent(matchPlayer, sender instanceof Player && ((Player)sender).getName().equals(matchPlayer.getName()) ? EntityDamageEvent.DamageCause.SUICIDE : EntityDamageEvent.DamageCause.CUSTOM, Short.MAX_VALUE);
server.getPluginManager().callEvent(ede);
if (ede.isCancelled() && sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.kill.force"))
{
continue;
}
matchPlayer.damage(Short.MAX_VALUE);
if (matchPlayer.getHealth() > 0)
{
matchPlayer.setHealth(0);
}
sender.sendMessage(_("kill", matchPlayer.getDisplayName()));
}
sender.sendMessage(_("kill", matchPlayer.getDisplayName()));
}
}

View File

@ -2,15 +2,16 @@ package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User;
import java.util.List;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.LightningStrike;
import org.bukkit.entity.Player;
public class Commandlightning extends EssentialsCommand
public class Commandlightning extends EssentialsLoopCommand
{
int power = 5;
public Commandlightning()
{
super("lightning");
@ -19,7 +20,6 @@ public class Commandlightning extends EssentialsCommand
@Override
public void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception
{
User user = null;
if (sender instanceof Player)
{
@ -31,7 +31,6 @@ public class Commandlightning extends EssentialsCommand
}
}
int power = 5;
if (args.length > 1)
{
try
@ -42,28 +41,22 @@ public class Commandlightning extends EssentialsCommand
{
}
}
loopOnlinePlayers(server, sender, true, args[0], null);
}
if (args[0].trim().length() < 2)
@Override
protected void updatePlayer(final Server server, final CommandSender sender, final User matchUser, final String[] args)
{
sender.sendMessage(_("lightningUse", matchUser.getDisplayName()));
final LightningStrike strike = matchUser.getBase().getWorld().strikeLightningEffect(matchUser.getBase().getLocation());
if (!matchUser.isGodModeEnabled())
{
throw new PlayerNotFoundException();
matchUser.getBase().damage(power, strike);
}
final List<Player> matchedPlayers = server.matchPlayer(args[0]);
for (Player matchPlayer : matchedPlayers)
if (ess.getSettings().warnOnSmite())
{
User matchUser = ess.getUser(matchPlayer);
sender.sendMessage(_("lightningUse", matchPlayer.getDisplayName()));
final LightningStrike strike = matchPlayer.getWorld().strikeLightningEffect(matchPlayer.getLocation());
if (!matchUser.isGodModeEnabled())
{
matchPlayer.damage(power, strike);
}
if (ess.getSettings().warnOnSmite())
{
matchUser.sendMessage(_("lightningSmited"));
}
matchUser.sendMessage(_("lightningSmited"));
}
}
}

View File

@ -4,15 +4,17 @@ import com.earth2me.essentials.Console;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.IReplyTo;
import com.earth2me.essentials.User;
import static com.earth2me.essentials.commands.EssentialsCommand.getFinalArg;
import com.earth2me.essentials.utils.FormatUtil;
import java.util.List;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commandmsg extends EssentialsCommand
public class Commandmsg extends EssentialsLoopCommand
{
final String translatedMe = _("me");
public Commandmsg()
{
super("msg");
@ -41,13 +43,11 @@ public class Commandmsg extends EssentialsCommand
message = FormatUtil.replaceFormat(message);
}
final String translatedMe = _("me");
final IReplyTo replyTo = sender instanceof Player ? ess.getUser((Player)sender) : Console.getConsoleReplyTo();
final String senderName = sender instanceof Player ? ((Player)sender).getDisplayName() : Console.NAME;
if (args[0].equalsIgnoreCase(Console.NAME))
{
final IReplyTo replyTo = sender instanceof Player ? ess.getUser(sender) : Console.getConsoleReplyTo();
final String senderName = sender instanceof Player ? ((Player)sender).getDisplayName() : Console.NAME;
sender.sendMessage(_("msgFormat", translatedMe, Console.NAME, message));
CommandSender cs = Console.getCommandSender(server);
cs.sendMessage(_("msgFormat", senderName, translatedMe, message));
@ -56,38 +56,28 @@ public class Commandmsg extends EssentialsCommand
return;
}
boolean skipHidden = sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.vanish.interact");
boolean foundUser = false;
final List<Player> matchedPlayers = server.matchPlayer(args[0]);
loopOnlinePlayers(server, sender, true, args[0], new String[]{message});
}
for (Player matchPlayer : matchedPlayers)
@Override
protected void updatePlayer(final Server server, final CommandSender sender, final User matchedUser, final String[] args)
{
final IReplyTo replyTo = sender instanceof Player ? ess.getUser(sender) : Console.getConsoleReplyTo();
final String senderName = sender instanceof Player ? ((Player)sender).getDisplayName() : Console.NAME;
if (matchedUser.isAfk())
{
final User matchedUser = ess.getUser(matchPlayer);
if (skipHidden && matchedUser.isHidden())
{
continue;
}
foundUser = true;
if (matchedUser.isAfk())
{
sender.sendMessage(_("userAFK", matchPlayer.getDisplayName()));
}
sender.sendMessage(_("msgFormat", translatedMe, matchPlayer.getDisplayName(), message));
if (sender instanceof Player && matchedUser.isIgnoredPlayer(ess.getUser(sender)))
{
continue;
}
matchedUser.sendMessage(_("msgFormat", senderName, translatedMe, message));
replyTo.setReplyTo(matchPlayer);
matchedUser.setReplyTo(sender);
sender.sendMessage(_("userAFK", matchedUser.getDisplayName()));
}
if (!foundUser)
sender.sendMessage(_("msgFormat", translatedMe, matchedUser.getDisplayName(), args[0]));
if (sender instanceof Player && matchedUser.isIgnoredPlayer(ess.getUser(sender)))
{
throw new PlayerNotFoundException();
return;
}
matchedUser.sendMessage(_("msgFormat", senderName, translatedMe, args[0]));
replyTo.setReplyTo(matchedUser.getBase());
matchedUser.setReplyTo(sender);
}
}

View File

@ -9,7 +9,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commandnick extends EssentialsCommand
public class Commandnick extends EssentialsLoopCommand
{
public Commandnick()
{
@ -27,14 +27,16 @@ public class Commandnick extends EssentialsCommand
{
throw new Exception(_("nickDisplayName"));
}
final String[] nickname = formatNickname(user, args[1]).split(" ");
if (args.length > 1 && user.isAuthorized("essentials.nick.others"))
{
setNickname(server, getPlayer(server, user, args, 0), formatNickname(user, args[1]));
loopOfflinePlayers(server, user.getBase(), false, args[0], nickname);
user.sendMessage(_("nickChanged"));
}
else
{
setNickname(server, user, formatNickname(user, args[0]));
updatePlayer(server, user.getBase(), user, nickname);
}
}
@ -49,54 +51,16 @@ public class Commandnick extends EssentialsCommand
{
throw new Exception(_("nickDisplayName"));
}
if ((args[0].equalsIgnoreCase("*") || args[0].equalsIgnoreCase("all")) && args[1].equalsIgnoreCase("off"))
{
resetAllNicknames(server);
}
else
{
setNickname(server, getPlayer(server, args, 0, true, false), formatNickname(null, args[1]));
}
final String[] nickname = formatNickname(null, args[1]).split(" ");
loopOfflinePlayers(server, sender, false, args[0], nickname);
sender.sendMessage(_("nickChanged"));
}
private String formatNickname(final User user, final String nick)
@Override
protected void updatePlayer(final Server server, final CommandSender sender, final User target, final String[] args) throws NotEnoughArgumentsException
{
if (user == null)
{
return FormatUtil.replaceFormat(nick);
}
else
{
return FormatUtil.formatString(user, "essentials.nick", nick);
}
}
private void resetAllNicknames(final Server server)
{
for (Player player : server.getOnlinePlayers())
{
try
{
setNickname(server, ess.getUser(player), "off");
}
catch (Exception ex)
{
}
}
}
private void setNickname(final Server server, final User target, final String nick) throws Exception
{
if (!nick.matches("^[a-zA-Z_0-9\u00a7]+$"))
{
throw new Exception(_("nickNamesAlpha"));
}
else if (nick.length() > ess.getSettings().getMaxNickLength())
{
throw new Exception(_("nickTooLong"));
}
else if (target.getName().equalsIgnoreCase(nick))
final String nick = args[0];
if (target.getName().equalsIgnoreCase(nick))
{
target.setNickname(nick);
target.setDisplayNick();
@ -108,26 +72,47 @@ public class Commandnick extends EssentialsCommand
target.setDisplayNick();
target.sendMessage(_("nickNoMore"));
}
else if (nickInUse(server, target, nick))
{
throw new NotEnoughArgumentsException(_("nickInUse"));
}
else
{
for (Player onlinePlayer : server.getOnlinePlayers())
{
if (target.getBase() == onlinePlayer)
{
continue;
}
String displayName = onlinePlayer.getDisplayName().toLowerCase(Locale.ENGLISH);
String name = onlinePlayer.getName().toLowerCase(Locale.ENGLISH);
String lowerNick = nick.toLowerCase(Locale.ENGLISH);
if (lowerNick.equals(displayName) || lowerNick.equals(name))
{
throw new Exception(_("nickInUse"));
}
}
target.setNickname(nick);
target.setDisplayNick();
target.sendMessage(_("nickSet", target.getDisplayName() + "§7."));
target.sendMessage(_("nickSet", target.getDisplayName()));
}
}
private String formatNickname(final User user, final String nick) throws Exception
{
String newNick = user == null ? FormatUtil.replaceFormat(nick) : FormatUtil.formatString(user, "essentials.nick", nick);
if (!newNick.matches("^[a-zA-Z_0-9\u00a7]+$"))
{
throw new Exception(_("nickNamesAlpha"));
}
else if (newNick.length() > ess.getSettings().getMaxNickLength())
{
throw new Exception(_("nickTooLong"));
}
return newNick;
}
private boolean nickInUse(final Server server, final User target, String nick)
{
final String lowerNick = nick.toLowerCase(Locale.ENGLISH);
for (final Player onlinePlayer : server.getOnlinePlayers())
{
if (target.getBase() == onlinePlayer)
{
continue;
}
if (lowerNick.equals(onlinePlayer.getDisplayName().toLowerCase(Locale.ENGLISH))
|| lowerNick.equals(onlinePlayer.getName().toLowerCase(Locale.ENGLISH)))
{
return true;
}
}
return false;
}
}

View File

@ -1,55 +1,39 @@
package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User;
import java.math.BigDecimal;
import java.util.List;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.command.CommandSender;
public class Commandpay extends EssentialsCommand
public class Commandpay extends EssentialsLoopCommand
{
BigDecimal amount;
public Commandpay()
{
super("pay");
}
@Override
public void run(Server server, User user, String commandLabel, String[] args) throws Exception
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{
if (args.length < 2)
{
throw new NotEnoughArgumentsException();
}
//TODO: TL this.
if (args[0].trim().length() < 2)
{
throw new NotEnoughArgumentsException("You need to specify a player to pay.");
}
amount = new BigDecimal(args[1].replaceAll("[^0-9\\.]", ""));
loopOnlinePlayers(server, user.getBase(), true, args[0], args);
}
BigDecimal amount = new BigDecimal(args[1].replaceAll("[^0-9\\.]", ""));
boolean skipHidden = !user.isAuthorized("essentials.vanish.interact");
boolean foundUser = false;
final List<Player> matchedPlayers = server.matchPlayer(args[0]);
for (Player matchPlayer : matchedPlayers)
{
User player = ess.getUser(matchPlayer);
if (skipHidden && player.isHidden())
{
continue;
}
foundUser = true;
user.payUser(player, amount);
Trade.log("Command", "Pay", "Player", user.getName(), new Trade(amount, ess), player.getName(), new Trade(amount, ess), user.getLocation(), ess);
}
if (!foundUser)
{
throw new PlayerNotFoundException();
}
@Override
protected void updatePlayer(final Server server, final CommandSender sender, final User player, final String[] args) throws ChargeException
{
User user = ess.getUser(sender);
user.payUser(player, amount);
Trade.log("Command", "Pay", "Player", user.getName(), new Trade(amount, ess), player.getName(), new Trade(amount, ess), user.getLocation(), ess);
}
}

View File

@ -43,27 +43,30 @@ public abstract class EssentialsCommand implements IEssentialsCommand
return name;
}
protected User getPlayer(final Server server, final User user, final String[] args, final int pos) throws NoSuchFieldException, NotEnoughArgumentsException
{
return getPlayer(server, user, args, pos, user.isAuthorized("essentials.vanish.interact"), false);
}
protected User getPlayer(final Server server, final CommandSender sender, final String[] args, final int pos) throws NoSuchFieldException, NotEnoughArgumentsException
// Get online players - only show vanished if source has permission
protected User getPlayer(final Server server, final CommandSender sender, final String[] args, final int pos) throws PlayerNotFoundException, NotEnoughArgumentsException
{
if (sender instanceof Player)
{
User user = ess.getUser(sender);
return getPlayer(server, user, args, pos);
}
return getPlayer(server, null, args, pos, true, false);
return getPlayer(server, args, pos, true, false);
}
protected User getPlayer(final Server server, final String[] args, final int pos, boolean getHidden, final boolean getOffline) throws NoSuchFieldException, NotEnoughArgumentsException
// Get online players - only show vanished if source has permission
protected User getPlayer(final Server server, final User user, final String[] args, final int pos) throws PlayerNotFoundException, NotEnoughArgumentsException
{
return getPlayer(server, user, args, pos, user.isAuthorized("essentials.vanish.interact"), false);
}
// Get online or offline players, this method allows for raw access
protected User getPlayer(final Server server, final String[] args, final int pos, boolean getHidden, final boolean getOffline) throws PlayerNotFoundException, NotEnoughArgumentsException
{
return getPlayer(server, null, args, pos, getHidden, getOffline);
}
private User getPlayer(final Server server, final User sourceUser, final String[] args, final int pos, boolean getHidden, final boolean getOffline) throws NoSuchFieldException, NotEnoughArgumentsException
private User getPlayer(final Server server, final User sourceUser, final String[] args, final int pos, boolean getHidden, final boolean getOffline) throws PlayerNotFoundException, NotEnoughArgumentsException
{
if (args.length <= pos)
{
@ -73,7 +76,19 @@ public abstract class EssentialsCommand implements IEssentialsCommand
{
throw new PlayerNotFoundException();
}
final User user = ess.getUser(args[pos]);
return getPlayer(server, sourceUser, args[pos], getHidden, getOffline);
}
// Get online or offline players, this method allows for raw access
protected User getPlayer(final Server server, final String searchTerm, boolean getHidden, final boolean getOffline) throws PlayerNotFoundException
{
return getPlayer(server, null, searchTerm, getHidden, getOffline);
}
private User getPlayer(final Server server, final User sourceUser, final String searchTerm, boolean getHidden, final boolean getOffline) throws PlayerNotFoundException
{
final User user = ess.getUser(searchTerm);
if (user != null)
{
if (!getOffline && !user.isOnline())
@ -86,11 +101,11 @@ public abstract class EssentialsCommand implements IEssentialsCommand
}
return user;
}
final List<Player> matches = server.matchPlayer(args[pos]);
final List<Player> matches = server.matchPlayer(searchTerm);
if (matches.isEmpty())
{
final String matchText = args[pos].toLowerCase(Locale.ENGLISH);
final String matchText = searchTerm.toLowerCase(Locale.ENGLISH);
for (Player onlinePlayer : server.getOnlinePlayers())
{
final User userMatch = ess.getUser(onlinePlayer);
@ -109,7 +124,7 @@ public abstract class EssentialsCommand implements IEssentialsCommand
for (Player player : matches)
{
final User userMatch = ess.getUser(player);
if (userMatch.getDisplayName().startsWith(args[pos]) && (getHidden || !userMatch.isHidden() || userMatch.equals(sourceUser)))
if (userMatch.getDisplayName().startsWith(searchTerm) && (getHidden || !userMatch.isHidden() || userMatch.equals(sourceUser)))
{
return userMatch;
}

View File

@ -0,0 +1,126 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.User;
import java.util.List;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public abstract class EssentialsLoopCommand extends EssentialsCommand
{
public EssentialsLoopCommand(String command)
{
super(command);
}
protected void loopOfflinePlayers(final Server server, final CommandSender sender, final boolean multipleStringMatches, final String searchTerm, final String[] commandArgs)
throws PlayerNotFoundException, NotEnoughArgumentsException, PlayerExemptException, ChargeException
{
if (searchTerm.isEmpty())
{
throw new PlayerNotFoundException();
}
if (searchTerm.contentEquals("**"))
{
for (String sUser : ess.getUserMap().getAllUniqueUsers())
{
final User matchedUser = ess.getUser(sUser);
updatePlayer(server, sender, matchedUser, commandArgs);
}
}
else if (searchTerm.contentEquals("*"))
{
boolean skipHidden = sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.vanish.interact");
for (Player onlinePlayer : server.getOnlinePlayers())
{
final User onlineUser = ess.getUser(onlinePlayer);
if (skipHidden && onlineUser.isHidden())
{
continue;
}
updatePlayer(server, sender, onlineUser, commandArgs);
}
}
else if (multipleStringMatches)
{
if (searchTerm.trim().length() < 3)
{
throw new PlayerNotFoundException();
}
final List<Player> matchedPlayers = server.matchPlayer(searchTerm);
if (matchedPlayers.isEmpty())
{
final User matchedUser = getPlayer(server, searchTerm, true, true);
updatePlayer(server, sender, matchedUser, commandArgs);
}
for (Player matchPlayer : matchedPlayers)
{
final User matchedUser = ess.getUser(matchPlayer);
updatePlayer(server, sender, matchedUser, commandArgs);
}
}
else
{
final User user = getPlayer(server, searchTerm, true, true);
updatePlayer(server, sender, user, commandArgs);
}
}
protected void loopOnlinePlayers(final Server server, final CommandSender sender, final boolean multipleStringMatches, final String searchTerm, final String[] commandArgs)
throws PlayerNotFoundException, NotEnoughArgumentsException, PlayerExemptException, ChargeException
{
if (searchTerm.isEmpty())
{
throw new PlayerNotFoundException();
}
boolean skipHidden = sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.vanish.interact");
if (searchTerm.contentEquals("**") || searchTerm.contentEquals("*"))
{
for (Player onlinePlayer : server.getOnlinePlayers())
{
final User onlineUser = ess.getUser(onlinePlayer);
if (skipHidden && onlineUser.isHidden())
{
continue;
}
updatePlayer(server, sender, onlineUser, commandArgs);
}
}
else if (multipleStringMatches)
{
if (searchTerm.trim().length() < 2)
{
throw new PlayerNotFoundException();
}
boolean foundUser = false;
final List<Player> matchedPlayers = server.matchPlayer(searchTerm);
for (Player matchPlayer : matchedPlayers)
{
final User player = ess.getUser(matchPlayer);
if (skipHidden && player.isHidden())
{
continue;
}
foundUser = true;
updatePlayer(server, sender, player, commandArgs);
}
if (!foundUser)
{
throw new PlayerNotFoundException();
}
}
else
{
final User player = getPlayer(server, searchTerm, skipHidden, false);
updatePlayer(server, sender, player, commandArgs);
}
}
protected abstract void updatePlayer(Server server, CommandSender sender, User user, String[] args)
throws NotEnoughArgumentsException, PlayerExemptException, ChargeException;
}

View File

@ -0,0 +1,9 @@
package com.earth2me.essentials.commands;
public class PlayerExemptException extends NoSuchFieldException
{
public PlayerExemptException(String message)
{
super(message);
}
}