TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/commands/Command_adminworld.java

249 lines
8.3 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.commands;
import me.totalfreedom.totalfreedommod.rank.PlayerRank;
import me.totalfreedom.totalfreedommod.util.FUtil;
import me.totalfreedom.totalfreedommod.world.WorldTime;
import me.totalfreedom.totalfreedommod.world.WorldWeather;
2013-08-22 00:07:14 +00:00
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@CommandPermissions(level = PlayerRank.OP, source = SourceType.BOTH)
@CommandParameters(description = "Go to the AdminWorld.",
usage = "/<command> [guest < list | purge | add <player> | remove <player> > | time <morning | noon | evening | night> | weather <off | on | storm>]")
public class Command_adminworld extends FreedomCommand
{
2013-08-15 21:40:35 +00:00
private enum CommandMode
{
TELEPORT, GUEST, TIME, WEATHER;
2013-08-15 21:40:35 +00:00
}
@Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
2013-08-15 21:40:35 +00:00
CommandMode commandMode = null;
if (args.length == 0)
{
commandMode = CommandMode.TELEPORT;
}
else if (args.length >= 2)
2013-08-12 16:51:30 +00:00
{
2013-08-15 21:40:35 +00:00
if ("guest".equalsIgnoreCase(args[0]))
{
commandMode = CommandMode.GUEST;
}
else if ("time".equalsIgnoreCase(args[0]))
{
commandMode = CommandMode.TIME;
}
2013-08-17 01:36:25 +00:00
else if ("weather".equalsIgnoreCase(args[0]))
{
commandMode = CommandMode.WEATHER;
}
2013-08-12 16:51:30 +00:00
}
2013-08-15 21:40:35 +00:00
if (commandMode == null)
2013-08-12 16:51:30 +00:00
{
2013-08-15 21:40:35 +00:00
return false;
2013-08-12 16:51:30 +00:00
}
2013-08-15 21:40:35 +00:00
2013-08-23 18:43:58 +00:00
try
2013-08-15 21:40:35 +00:00
{
2013-08-23 18:43:58 +00:00
switch (commandMode)
2013-08-15 21:40:35 +00:00
{
2013-08-23 18:43:58 +00:00
case TELEPORT:
2013-08-22 00:07:14 +00:00
{
if (!(sender instanceof Player) || playerSender == null)
2013-08-23 20:59:31 +00:00
{
return true;
}
2013-08-23 18:43:58 +00:00
World adminWorld = null;
try
{
adminWorld = plugin.wm.adminworld.getWorld();
2013-08-23 18:43:58 +00:00
}
catch (Exception ex)
{
}
2013-08-15 21:40:35 +00:00
if (adminWorld == null || playerSender.getWorld() == adminWorld)
2013-08-15 21:40:35 +00:00
{
2013-08-23 18:43:58 +00:00
playerMsg("Going to the main world.");
playerSender.teleport(server.getWorlds().get(0).getSpawnLocation());
2013-08-15 21:40:35 +00:00
}
2013-08-23 18:43:58 +00:00
else
2013-08-22 19:26:12 +00:00
{
if (plugin.wm.adminworld.canAccessWorld(playerSender))
2013-08-23 20:59:31 +00:00
{
playerMsg("Going to the AdminWorld.");
plugin.wm.adminworld.sendToWorld(playerSender);
2013-08-23 20:59:31 +00:00
}
else
{
playerMsg("You don't have permission to access the AdminWorld.");
}
2013-08-22 19:26:12 +00:00
}
2013-08-23 18:43:58 +00:00
break;
}
case GUEST:
{
if (args.length == 2)
2013-08-15 21:40:35 +00:00
{
2013-08-23 18:43:58 +00:00
if ("list".equalsIgnoreCase(args[1]))
{
playerMsg("AdminWorld guest list: " + plugin.wm.adminworld.guestListToString());
2013-08-23 18:43:58 +00:00
}
else if ("purge".equalsIgnoreCase(args[1]))
{
assertCommandPerms(sender, playerSender);
plugin.wm.adminworld.purgeGuestList();
FUtil.adminAction(sender.getName(), "AdminWorld guest list purged.", false);
2013-08-23 18:43:58 +00:00
}
else
{
return false;
}
2013-08-15 21:40:35 +00:00
}
2013-08-23 18:43:58 +00:00
else if (args.length == 3)
2013-08-15 21:40:35 +00:00
{
assertCommandPerms(sender, playerSender);
2013-08-23 18:43:58 +00:00
if ("add".equalsIgnoreCase(args[1]))
{
final Player player = getPlayer(args[2]);
if (player == null)
2013-08-23 18:43:58 +00:00
{
sender.sendMessage(FreedomCommand.PLAYER_NOT_FOUND);
2013-08-23 18:43:58 +00:00
return true;
}
2013-08-23 20:29:46 +00:00
if (plugin.wm.adminworld.addGuest(player, playerSender))
2013-08-23 20:29:46 +00:00
{
FUtil.adminAction(sender.getName(), "AdminWorld guest added: " + player.getName(), false);
2013-08-23 20:29:46 +00:00
}
else
{
playerMsg("Could not add player to guest list.");
}
2013-08-23 18:43:58 +00:00
}
else if ("remove".equals(args[1]))
2013-08-23 18:43:58 +00:00
{
final Player player = plugin.wm.adminworld.removeGuest(args[2]);
2013-08-23 18:43:58 +00:00
if (player != null)
{
FUtil.adminAction(sender.getName(), "AdminWorld guest removed: " + player.getName(), false);
2013-08-23 18:43:58 +00:00
}
else
{
playerMsg("Can't find guest entry for: " + args[2]);
}
}
else
{
return false;
}
2013-08-15 21:40:35 +00:00
}
2013-08-23 18:43:58 +00:00
break;
2013-08-15 21:40:35 +00:00
}
2013-08-23 18:43:58 +00:00
case TIME:
{
assertCommandPerms(sender, playerSender);
2013-08-15 21:40:35 +00:00
2013-08-23 18:43:58 +00:00
if (args.length == 2)
{
WorldTime timeOfDay = WorldTime.getByAlias(args[1]);
if (timeOfDay != null)
{
plugin.wm.adminworld.setTimeOfDay(timeOfDay);
playerMsg("AdminWorld time set to: " + timeOfDay.name());
}
else
{
playerMsg("Invalid time of day. Can be: sunrise, noon, sunset, midnight");
}
2013-08-23 18:43:58 +00:00
}
else
{
return false;
}
2013-08-22 19:26:12 +00:00
2013-08-23 18:43:58 +00:00
break;
2013-08-15 21:40:35 +00:00
}
2013-08-23 18:43:58 +00:00
case WEATHER:
{
assertCommandPerms(sender, playerSender);
2013-08-15 21:40:35 +00:00
2013-08-23 18:43:58 +00:00
if (args.length == 2)
{
WorldWeather weatherMode = WorldWeather.getByAlias(args[1]);
if (weatherMode != null)
{
plugin.wm.adminworld.setWeatherMode(weatherMode);
playerMsg("AdminWorld weather set to: " + weatherMode.name());
}
else
{
playerMsg("Invalid weather mode. Can be: off, rain, storm");
}
2013-08-23 18:43:58 +00:00
}
else
{
return false;
}
2013-08-22 19:26:12 +00:00
2013-08-23 18:43:58 +00:00
break;
}
default:
2013-08-17 01:36:25 +00:00
{
2013-08-23 18:43:58 +00:00
return false;
2013-08-17 01:36:25 +00:00
}
2013-08-15 21:40:35 +00:00
}
}
2013-08-23 18:43:58 +00:00
catch (PermissionDeniedException ex)
{
if (ex.getMessage().isEmpty())
{
return noPerms();
}
2013-08-23 18:43:58 +00:00
sender.sendMessage(ex.getMessage());
return true;
2013-08-23 18:43:58 +00:00
}
2013-08-15 21:40:35 +00:00
return true;
}
2013-08-23 18:43:58 +00:00
// TODO: Redo this properly
private void assertCommandPerms(CommandSender sender, Player playerSender) throws PermissionDeniedException
2013-08-23 18:43:58 +00:00
{
if (!(sender instanceof Player) || playerSender == null || !isAdmin(sender))
2013-08-23 18:43:58 +00:00
{
throw new PermissionDeniedException();
2013-08-23 18:43:58 +00:00
}
}
private class PermissionDeniedException extends Exception
{
private static final long serialVersionUID = 1L;
private PermissionDeniedException()
{
super("");
}
private PermissionDeniedException(String string)
2013-08-23 18:43:58 +00:00
{
super(string);
}
}
}