Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedHashMap;
- import java.util.logging.Logger;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.ArrayList;
- import java.util.List;
- public class PlayerCommands {
- private static final Logger log = Logger.getLogger("Minecraft");
- private static PlayerCommands instance;
- private final LinkedHashMap<String, BaseCommand> commands = new LinkedHashMap<String, BaseCommand>();
- private static List<String> onlyOneUseKits = new ArrayList<String>();
- public PlayerCommands() {
- add("/help", help);
- add("/playerlist", playerlist);
- add("/banlist", banlist);
- add("/banip", banip);
- add("/unbanip", unbanip);
- add("/ban", ban);
- add("/unban", unban);
- add("/mute", mute);
- add("/tp", tp);
- add("/tphere", tphere);
- add("/kick", kick);
- add("/item", item);
- add("/cloth", cloth);
- add("/kit", kit);
- add("/listwarps", listwarps);
- add("/home", home);
- add("/sethome", sethome);
- add("/setspawn", setspawn);
- add("/me", me);
- add("/msg", msg);
- add("/tell", msg);
- add("/m", msg);
- add("/spawn", spawn);
- add("/warp", warp);
- add("/setwarp", setwarp);
- add("/removewarp", removewarp);
- add("/getpos", getpos);
- add("/compass", compass);
- add("/time", currentTime);
- add("/lighter", lighter);
- add("/motd", motd);
- add("/clearinventory", clearInventory);
- add("/update", update);
- }
- /**
- * Add a command to the player list.
- *
- * @param name
- * @param cmd
- */
- public void add(String name, BaseCommand cmd) {
- if (name != null && cmd != null)
- add(name, cmd);
- }
- /**
- * Remove a command from the server list.
- *
- * @param name
- */
- public void remove(String name) {
- if (name != null) {
- etc.getInstance().removeCommand(name);
- commands.remove(name);
- }
- }
- /**
- * Performs a lookup for a command of the given name and executes it if
- * found. Returns false if command not found.
- *
- * @param command
- * @param player
- * @param parameters
- * @return
- */
- public static boolean parseCommand(MessageReceiver caller, String command, String[] args) {
- if (instance == null)
- instance = new PlayerCommands();
- BaseCommand cmd = instance.getCommand(command);
- if (cmd != null) {
- cmd.parseCommand(caller, args);
- // Inform caller a matching command was found.
- return true;
- }
- return false;
- }
- public BaseCommand getCommand(String command) {
- return commands.get(command);
- }
- public static final BaseCommand help = new BaseCommand("[Page] - Shows a list of commands. 7 per page.") {
- @Override
- void execute (MessageReceiver caller, String[] parameters) {
- List<String> availableCommands = new ArrayList<String>();
- for (Entry<String, String> entry : etc.getInstance().getCommands().entrySet())
- if (etc.getServer().getPlayer(caller.getName()).canUseCommand(entry.getKey())) {
- if (entry.getKey().equals("/kit") && !etc.getDataSource().hasKits())
- continue;
- if (entry.getKey().equals("/listwarps") && !etc.getDataSource().hasWarps())
- continue;
- availableCommands.add(entry.getKey() + " " + entry.getValue());
- }
- caller.notify(Colors.Blue + "Available commands (Page " + (parameters.length == 2 ? parameters[1] : "1") + " of " + (int) Math.ceil((double) availableCommands.size() / (double) 7) + ") [] = required <> = optional:");
- if (parameters.length == 2)
- try {
- int amount = Integer.parseInt(parameters[1]);
- if (amount > 0)
- amount = (amount - 1) * 7;
- else
- amount = 0;
- for (int i = amount; i < amount + 7; i++)
- if (availableCommands.size() > i)
- caller.notify(Colors.Rose + availableCommands.get(i));
- } catch (NumberFormatException ex) {
- caller.notify(Colors.Rose + "Not a valid page number.");
- }
- else
- for (int i = 0; i < 7; i++)
- if (availableCommands.size() > i)
- caller.notify(Colors.Rose + availableCommands.get(i));
- }
- };
- public static final BaseCommand mute = new BaseCommand("[Player] - Toggles mute on player.") {
- @Override
- void execute(MessageReceiver caller, String[] parameters){
- if (parameters.length != 2) {
- caller.notify(Colors.Rose + "Correct usage is: /mute [player]");
- return;
- }
- Player player = etc.getServer().matchPlayer(parameters[1]);
- if (player != null) {
- if (player.toggleMute())
- caller.notify(Colors.Rose + "player was muted");
- else
- caller.notify(Colors.Rose + "player was unmuted");
- } else
- caller.notify(Colors.Rose + "Can't find player " + parameters[1]);
- }
- };
- public static final BaseCommand msg = new BaseCommand("[Player] [Message] - Sends a message to player") {
- @Override
- void execute(MessageReceiver caller, String[] parameters) {
- if (parameters.length < 3) {
- caller.notify(Colors.Rose + "Correct usage is: /msg [player] [message]");
- return;
- }
- if (etc.getServer().getPlayer(caller.getName()).isMuted()) {
- caller.notify(Colors.Rose + "You are currently muted.");
- return;
- }
- Player player = etc.getServer().matchPlayer(parameters[1]);
- if (player != null) {
- if (player.getName().equals(caller.getName())) {
- caller.notify(Colors.Rose + "You can't message yourself!");
- return;
- }
- player.sendMessage("(MSG) " + etc.getServer().getPlayer(caller.getName()).getColor() + "<" + etc.getServer().getPlayer(caller.getName()) + "> " + Colors.White + etc.combineSplit(2, parameters, " "));
- caller.notify("(MSG) " + etc.getServer().getPlayer(caller.getName()).getColor() + "<" + etc.getServer().getPlayer(caller.getName()) + "> " + Colors.White + etc.combineSplit(2, parameters, " "));
- } else
- caller.notify(Colors.Rose + "Couldn't find player " + parameters[1]);
- }
- };
- public static final BaseCommand kit = new BaseCommand ("[Kit] - Gives a kit. To get a list of kits type /kit") {
- @Override
- void execute(MessageReceiver caller, String[] parameters) {
- if (etc.getDataSource().hasKits()) {
- if (parameters.length != 2 && parameters.length != 3) {
- caller.notify(Colors.Rose + "Available kits" + Colors.White + ": " + etc.getDataSource().getKitNames(etc.getServer().getPlayer(caller.getName())));
- return;
- }
- Player toGive = etc.getServer().getPlayer(caller.getName());
- if (parameters.length > 2 && etc.getServer().getPlayer(caller.getName()).canIgnoreRestrictions())
- toGive = etc.getServer().matchPlayer(parameters[2]);
- Kit kit = etc.getDataSource().getKit(parameters[1]);
- if (toGive != null) {
- if (kit != null) {
- if (!etc.getServer().getPlayer(caller.getName()).isInGroup(kit.Group) && !kit.Group.equals(""))
- caller.notify(Colors.Rose + "That kit does not exist.");
- else if (onlyOneUseKits.contains(kit.Name))
- caller.notify(Colors.Rose + "You can only get this kit once per login.");
- else if (etc.getMCServer().b.containsKey(etc.getServer().getPlayer(caller.getName()).getName() + " " + kit.Name))
- caller.notify(Colors.Rose + "You can't get this kit again for a while.");
- else {
- if (!etc.getServer().getPlayer(caller.getName()).canIgnoreRestrictions())
- if (kit.Delay >= 0)
- etc.getMCServer().b.put(etc.getServer().getPlayer(caller.getName()).getName() + " " + kit.Name, kit.Delay);
- else
- onlyOneUseKits.add(kit.Name);
- log.info(etc.getServer().getPlayer(caller.getName()).getName() + " got a kit!");
- toGive.notify(Colors.Rose + "Enjoy this kit!");
- for (Map.Entry<String, Integer> entry : kit.IDs.entrySet())
- try {
- int itemId = 0;
- try {
- itemId = Integer.parseInt(entry.getKey());
- } catch (NumberFormatException n) {
- itemId = etc.getDataSource().getItem(entry.getKey());
- }
- toGive.giveItem(itemId, kit.IDs.get(entry.getKey()));
- } catch (Exception e1) {
- log.info("Got an exception while giving out a kit (Kit name \"" + kit.Name + "\"). Are you sure all the Ids are numbers?");
- caller.notify(Colors.Rose + "The server encountered a problem while giving the kit :(");
- }
- }
- } else
- caller.notify(Colors.Rose + "That kit does not exist.");
- } else
- caller.notify(Colors.Rose + "That user does not exist.");
- }
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment