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

93 lines
2.5 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;
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"));
}
Warps warps = ess.getWarps();
if (warps.isEmpty())
{
throw new Exception(Util.i18n("noWarpsDefined"));
}
2011-08-21 19:27:36 +00:00
final List<String> warpNameList = new ArrayList<String>(warps.getWarpNames());
final Iterator<String> iterator = warpNameList.iterator();
while (iterator.hasNext())
{
2011-08-21 19:27:36 +00:00
final String warpName = iterator.next();
if (ess.getSettings().getPerWarpPermission() && !user.isAuthorized("essentials.warp." + warpName))
{
2011-08-21 19:27:36 +00:00
iterator.remove();
}
2011-08-21 19:27:36 +00:00
}
int page = 1;
if (args.length > 0)
{
page = Integer.parseInt(args[0]);
}
if (warpNameList.size() > WARPS_PER_PAGE)
{
user.sendMessage(Util.format("warpsCount", warpNameList.size(), page, (int)Math.ceil(warpNameList.size() / (double)WARPS_PER_PAGE)));
}
final int warpPage = (page - 1) * WARPS_PER_PAGE;
user.sendMessage(Util.joinList(warpNameList.subList(warpPage, warpPage+Math.min(warpNameList.size() - warpPage, WARPS_PER_PAGE))));
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();
}
}
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);
}
}