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

125 lines
3.2 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
2011-06-13 13:05:11 +00:00
import com.earth2me.essentials.Trade;
import org.bukkit.Server;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import com.earth2me.essentials.Warps;
2011-08-21 19:27:36 +00:00
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.bukkit.command.CommandSender;
public class Commandwarp extends EssentialsCommand
{
2011-08-21 19:27:36 +00:00
private final static int WARPS_PER_PAGE = 20;
public Commandwarp()
{
super("warp");
}
@Override
public void run(Server server, User user, String commandLabel, String[] args) throws Exception
{
2011-08-21 19:27:36 +00:00
if (args.length == 0 || args[0].matches("[0-9]+"))
{
if (!user.isAuthorized("essentials.warp.list"))
{
2011-08-30 02:42:31 +00:00
throw new Exception(Util.i18n("warpListPermission"));
}
warpList(user, args);
2011-08-30 02:42:31 +00:00
throw new NoChargeException();
}
if (args.length > 0)
{
User otherUser = null;
if (args.length == 2 && user.isAuthorized("essentials.warp.otherplayers"))
{
otherUser = ess.getUser(server.getPlayer(args[1]));
2011-08-21 19:27:36 +00:00
if (otherUser == null)
{
2011-08-30 02:42:31 +00:00
throw new Exception(Util.i18n("playerNotFound"));
}
warpUser(otherUser, args[0]);
2011-08-30 02:42:31 +00:00
throw new NoChargeException();
}
warpUser(user, args[0]);
2011-08-30 02:42:31 +00:00
throw new NoChargeException();
}
}
public void run(Server server, CommandSender sender, String commandLabel, String[] args) throws Exception
{
if (args.length < 2 || args[0].matches("[0-9]+"))
{
warpList(null, args);
throw new NoChargeException();
}
User otherUser = ess.getUser(server.getPlayer(args[1]));
if (otherUser == null)
{
throw new Exception(Util.i18n("playerNotFound"));
}
warpUser(otherUser, args[0]);
throw new NoChargeException();
}
private void warpList(User user, String[] args) throws Exception
{
Warps warps = ess.getWarps();
if (warps.isEmpty())
{
throw new Exception(Util.i18n("noWarpsDefined"));
}
final List<String> warpNameList = new ArrayList<String>(warps.getWarpNames());
if (user != null)
{
final Iterator<String> iterator = warpNameList.iterator();
while (iterator.hasNext())
{
final String warpName = iterator.next();
if (ess.getSettings().getPerWarpPermission() && !user.isAuthorized("essentials.warp." + warpName))
{
iterator.remove();
}
}
}
int page = 1;
if (args.length > 0)
{
page = Integer.parseInt(args[0]);
}
2011-11-04 23:41:39 +00:00
final int warpPage = (page - 1) * WARPS_PER_PAGE;
final String warpList = Util.joinList(warpNameList.subList(warpPage, warpPage + Math.min(warpNameList.size() - warpPage, WARPS_PER_PAGE)));
if (warpNameList.size() > WARPS_PER_PAGE)
{
user.sendMessage(Util.format("warpsCount", warpNameList.size(), page, (int)Math.ceil(warpNameList.size() / (double)WARPS_PER_PAGE)));
2011-11-04 23:41:39 +00:00
user.sendMessage(warpList);
}
else {
user.sendMessage(Util.format("warps", warpList));
}
}
private void warpUser(User user, String name) throws Exception
{
2011-06-13 13:05:11 +00:00
Trade charge = new Trade(this.getName(), ess);
charge.isAffordableFor(user);
if (ess.getSettings().getPerWarpPermission())
{
if (user.isAuthorized("essentials.warp." + name))
{
user.getTeleport().warp(name, charge);
return;
}
2011-08-30 02:42:31 +00:00
throw new Exception(Util.i18n("warpUsePermission"));
}
user.getTeleport().warp(name, charge);
}
}