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

208 lines
6.5 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.commands;
2011-10-19 00:37:00 +00:00
import me.totalfreedom.totalfreedommod.rank.PlayerRank;
import java.nio.charset.StandardCharsets;
2014-07-26 03:11:44 +00:00
import java.util.Iterator;
import java.util.UUID;
import me.totalfreedom.totalfreedommod.banning.Ban;
import me.totalfreedom.totalfreedommod.freeze.FreezeData;
import me.totalfreedom.totalfreedommod.util.FUtil;
2011-10-19 00:37:00 +00:00
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@CommandPermissions(level = PlayerRank.SUPER_ADMIN, source = SourceType.BOTH, blockHostConsole = true)
@CommandParameters(
2014-07-26 03:11:44 +00:00
description = "Use admin commands on someone by hash. Use mode 'list' to get a player's hash. Other modes are kick, nameban, ipban, ban, op, deop, ci, fr, smite.",
usage = "/<command> [list | [<kick | nameban | ipban | ban | op | deop | ci | fr | smite> <targethash>] ]")
public class Command_gadmin extends FreedomCommand
2011-10-19 00:37:00 +00:00
{
2014-07-26 03:11:44 +00:00
private enum GadminMode
{
LIST("list"),
KICK("kick"),
NAMEBAN("nameban"),
IPBAN("ipban"),
BAN("ban"),
OP("op"),
DEOP("deop"),
CI("ci"),
FR("fr"),
SMITE("smite");
private final String modeName;
private GadminMode(String command)
{
this.modeName = command;
}
public String getModeName()
{
return modeName;
}
public static GadminMode findMode(String needle)
{
for (final GadminMode mode : GadminMode.values())
{
if (needle.equalsIgnoreCase(mode.getModeName()))
{
return mode;
}
}
return null;
}
}
public String getPlayerHash(Player player)
{
return UUID.nameUUIDFromBytes(player.getName().toLowerCase().getBytes(StandardCharsets.UTF_8)).toString().substring(0, 4);
}
2011-10-19 00:37:00 +00:00
@Override
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
if (args.length == 0)
{
return false;
}
2014-07-26 03:11:44 +00:00
final GadminMode mode = GadminMode.findMode(args[0].toLowerCase());
if (mode == null)
2011-10-19 00:37:00 +00:00
{
2014-07-26 03:11:44 +00:00
playerMsg("Invalid mode: " + args[0], ChatColor.RED);
return true;
}
final Iterator<? extends Player> it = server.getOnlinePlayers().iterator();
2014-07-26 03:11:44 +00:00
if (mode == GadminMode.LIST)
{
2014-07-26 03:11:44 +00:00
playerMsg("[ Real Name ] : [ Display Name ] - Hash:");
while (it.hasNext())
2011-10-19 00:37:00 +00:00
{
2014-07-26 03:11:44 +00:00
final Player player = it.next();
sender.sendMessage(ChatColor.GRAY + String.format("[ %s ] : [ %s ] - %s",
2013-08-14 14:01:42 +00:00
player.getName(),
ChatColor.stripColor(player.getDisplayName()),
getPlayerHash(player)));
2011-10-19 00:37:00 +00:00
}
2014-07-26 03:11:44 +00:00
return true;
}
2014-07-26 03:11:44 +00:00
if (args.length < 2)
{
return false;
}
Player target = null;
while (it.hasNext() && target == null)
{
final Player player = it.next();
final String hash = getPlayerHash(player);
2014-07-26 03:11:44 +00:00
if (hash.equalsIgnoreCase(args[1]))
{
2014-07-26 03:11:44 +00:00
target = player;
}
2014-07-26 03:11:44 +00:00
}
2014-07-26 03:11:44 +00:00
if (target == null)
{
playerMsg("Invalid player hash: " + args[1], ChatColor.RED);
return true;
}
2014-07-26 03:11:44 +00:00
switch (mode)
{
case KICK:
2011-10-19 00:37:00 +00:00
{
FUtil.adminAction(sender.getName(), String.format("Kicking: %s.", target.getName()), false);
2014-07-26 03:11:44 +00:00
target.kickPlayer("Kicked by Administrator");
2014-06-29 08:53:39 +00:00
2014-07-26 03:11:44 +00:00
break;
}
case NAMEBAN:
{
FUtil.adminAction(sender.getName(), String.format("Banning Name: %s.", target.getName()), true);
plugin.bm.addBan(Ban.forPlayerName(target, sender, null, null));
2014-07-26 03:11:44 +00:00
target.kickPlayer("Username banned by Administrator.");
break;
}
case IPBAN:
{
String ip = target.getAddress().getAddress().getHostAddress();
String[] ip_parts = ip.split("\\.");
if (ip_parts.length == 4)
{
2014-07-26 03:11:44 +00:00
ip = String.format("%s.%s.*.*", ip_parts[0], ip_parts[1]);
}
FUtil.adminAction(sender.getName(), String.format("Banning IP: %s.", ip), true);
plugin.bm.addBan(Ban.forPlayerIp(ip, sender, null, null));
2014-07-26 03:11:44 +00:00
target.kickPlayer("IP address banned by Administrator.");
break;
}
case BAN:
{
String ip = target.getAddress().getAddress().getHostAddress();
String[] ip_parts = ip.split("\\.");
if (ip_parts.length == 4)
{
2014-07-26 03:11:44 +00:00
ip = String.format("%s.%s.*.*", ip_parts[0], ip_parts[1]);
}
FUtil.adminAction(sender.getName(), String.format("Banning Name: %s, IP: %s.", target.getName(), ip), true);
2011-10-19 00:37:00 +00:00
plugin.bm.addBan(Ban.forPlayer(target, sender));
2011-10-19 00:37:00 +00:00
2014-07-26 03:11:44 +00:00
target.kickPlayer("IP and username banned by Administrator.");
break;
2011-10-19 00:37:00 +00:00
}
2014-07-26 03:11:44 +00:00
case OP:
{
FUtil.adminAction(sender.getName(), String.format("Opping %s.", target.getName()), false);
2014-07-26 03:11:44 +00:00
target.setOp(false);
target.sendMessage(FreedomCommand.YOU_ARE_OP);
2014-07-26 03:11:44 +00:00
break;
}
case DEOP:
{
FUtil.adminAction(sender.getName(), String.format("Deopping %s.", target.getName()), false);
2014-07-26 03:11:44 +00:00
target.setOp(false);
target.sendMessage(FreedomCommand.YOU_ARE_NOT_OP);
2014-07-26 03:11:44 +00:00
break;
}
case CI:
{
target.getInventory().clear();
break;
}
case FR:
{
FreezeData fd = plugin.pl.getPlayer(target).getFreezeData();
fd.setFrozen(!fd.isFrozen());
2014-07-26 03:11:44 +00:00
playerMsg(target.getName() + " has been " + (fd.isFrozen() ? "frozen" : "unfrozen") + ".");
target.sendMessage(ChatColor.AQUA + "You have been " + (fd.isFrozen() ? "frozen" : "unfrozen") + ".");
2014-07-26 03:11:44 +00:00
break;
}
case SMITE:
{
Command_smite.smite(target);
break;
}
2011-10-19 00:37:00 +00:00
}
return true;
}
}