TF-EssentialsX/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java

149 lines
4.2 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.IEssentialsModule;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User;
import java.util.List;
2011-11-18 17:42:26 +00:00
import java.util.logging.Logger;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public abstract class EssentialsCommand implements IEssentialsCommand
{
private final transient String name;
protected transient IEssentials ess;
protected transient IEssentialsModule module;
protected final static Logger logger = Logger.getLogger("Minecraft");
protected EssentialsCommand(final String name)
{
this.name = name;
}
2011-11-18 18:07:28 +00:00
@Override
public void setEssentials(final IEssentials ess)
{
this.ess = ess;
}
@Override
public void setEssentialsModule(final IEssentialsModule module)
{
this.module = module;
}
2011-11-18 18:07:28 +00:00
@Override
public String getName()
{
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
{
if (sender instanceof Player)
{
User user = ess.getUser(sender);
return getPlayer(server, user, args, pos);
}
return getPlayer(server, null, args, pos, true, false);
}
protected User getPlayer(final Server server, final String[] args, final int pos, boolean getHidden, final boolean getOffline) throws NoSuchFieldException, NotEnoughArgumentsException
2013-05-20 17:50:14 +00:00
{
return getPlayer(server, null, args, pos, getHidden, getOffline);
}
2013-05-20 17:50:14 +00:00
private User getPlayer(final Server server, final User sourceUser, final String[] args, final int pos, boolean getHidden, final boolean getOffline) throws NoSuchFieldException, NotEnoughArgumentsException
{
if (args.length <= pos)
{
throw new NotEnoughArgumentsException();
}
2011-11-26 23:31:14 +00:00
if (args[pos].isEmpty())
{
2012-10-14 12:04:00 +00:00
throw new PlayerNotFoundException();
}
final User user = ess.getUser(args[pos]);
if (user != null)
{
if (!getOffline && !user.isOnline())
{
throw new PlayerNotFoundException();
}
if (!getHidden && user.isHidden() && !user.equals(sourceUser))
{
2012-10-14 12:04:00 +00:00
throw new PlayerNotFoundException();
}
return user;
}
final List<Player> matches = server.matchPlayer(args[pos]);
if (!matches.isEmpty())
{
for (Player player : matches)
{
final User userMatch = ess.getUser(player);
2013-05-20 17:50:14 +00:00
if (userMatch.getDisplayName().startsWith(args[pos]) && (getHidden || !userMatch.isHidden() || userMatch.equals(sourceUser)))
{
return userMatch;
}
}
final User userMatch = ess.getUser(matches.get(0));
2013-05-20 17:50:14 +00:00
if (getHidden || !userMatch.isHidden() || userMatch.equals(sourceUser))
{
return userMatch;
}
}
2012-10-14 12:04:00 +00:00
throw new PlayerNotFoundException();
}
@Override
public final void run(final Server server, final User user, final String commandLabel, final Command cmd, final String[] args) throws Exception
{
final Trade charge = new Trade(this.getName(), ess);
charge.isAffordableFor(user);
run(server, user, commandLabel, args);
charge.charge(user);
}
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{
run(server, (CommandSender)user.getBase(), commandLabel, args);
}
@Override
public final void run(final Server server, final CommandSender sender, final String commandLabel, final Command cmd, final String[] args) throws Exception
{
run(server, sender, commandLabel, args);
}
protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception
{
throw new Exception(_("onlyPlayers", commandLabel));
}
public static String getFinalArg(final String[] args, final int start)
{
final StringBuilder bldr = new StringBuilder();
for (int i = start; i < args.length; i++)
{
if (i != start)
{
bldr.append(" ");
}
bldr.append(args[i]);
}
return bldr.toString();
}
}