SkyeDarkhawk

New PlayerCommands.java

May 9th, 2011
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.52 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.logging.Logger;
  3. import java.util.Map;
  4. import java.util.Map.Entry;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. public class PlayerCommands {
  9.     private static final Logger                      log      = Logger.getLogger("Minecraft");
  10.     private static PlayerCommands             instance;
  11.     private final LinkedHashMap<String, BaseCommand> commands = new LinkedHashMap<String, BaseCommand>();
  12.     private static List<String>        onlyOneUseKits     = new ArrayList<String>();
  13.  
  14.     public PlayerCommands() {
  15.         add("/help", help);
  16.         add("/playerlist", playerlist);
  17.         add("/banlist", banlist);
  18.         add("/banip", banip);
  19.         add("/unbanip", unbanip);
  20.         add("/ban", ban);
  21.         add("/unban", unban);
  22.         add("/mute", mute);
  23.         add("/tp", tp);
  24.         add("/tphere", tphere);
  25.         add("/kick", kick);
  26.         add("/item", item);
  27.         add("/cloth", cloth);
  28.         add("/kit", kit);
  29.         add("/listwarps", listwarps);
  30.         add("/home", home);
  31.         add("/sethome", sethome);
  32.         add("/setspawn", setspawn);
  33.         add("/me", me);
  34.         add("/msg", msg);
  35.         add("/tell", msg);
  36.         add("/m", msg);
  37.         add("/spawn", spawn);
  38.         add("/warp", warp);
  39.         add("/setwarp", setwarp);
  40.         add("/removewarp", removewarp);
  41.         add("/getpos", getpos);
  42.         add("/compass", compass);
  43.         add("/time", currentTime);
  44.         add("/lighter", lighter);
  45.         add("/motd", motd);
  46.         add("/clearinventory", clearInventory);
  47.         add("/update", update);
  48.        
  49.     }
  50.  
  51.     /**
  52.      * Add a command to the player list.
  53.      *
  54.      * @param name
  55.      * @param cmd
  56.      */
  57.     public void add(String name, BaseCommand cmd) {
  58.         if (name != null && cmd != null)
  59.             add(name, cmd);
  60.     }
  61.  
  62.     /**
  63.      * Remove a command from the server list.
  64.      *
  65.      * @param name
  66.      */
  67.     public void remove(String name) {
  68.         if (name != null) {
  69.             etc.getInstance().removeCommand(name);
  70.             commands.remove(name);
  71.         }
  72.     }
  73.  
  74.     /**
  75.      * Performs a lookup for a command of the given name and executes it if
  76.      * found. Returns false if command not found.
  77.      *
  78.      * @param command
  79.      * @param player
  80.      * @param parameters
  81.      * @return
  82.      */
  83.     public static boolean parseCommand(MessageReceiver caller, String command, String[] args) {
  84.         if (instance == null)
  85.             instance = new PlayerCommands();
  86.  
  87.         BaseCommand cmd = instance.getCommand(command);
  88.         if (cmd != null) {
  89.             cmd.parseCommand(caller, args);
  90.             // Inform caller a matching command was found.
  91.             return true;
  92.         }
  93.         return false;
  94.     }
  95.  
  96.     public BaseCommand getCommand(String command) {
  97.         return commands.get(command);
  98.     }
  99.  
  100.     public static final BaseCommand help         = new BaseCommand("[Page] - Shows a list of commands. 7 per page.") {
  101.                                                      @Override
  102.                                                      void execute (MessageReceiver caller, String[] parameters) {
  103.                                                          List<String> availableCommands = new ArrayList<String>();
  104.                                                          for (Entry<String, String> entry : etc.getInstance().getCommands().entrySet())
  105.                                                              if (etc.getServer().getPlayer(caller.getName()).canUseCommand(entry.getKey())) {
  106.                                                                  if (entry.getKey().equals("/kit") && !etc.getDataSource().hasKits())
  107.                                                                     continue;
  108.                                                                  if (entry.getKey().equals("/listwarps") && !etc.getDataSource().hasWarps())
  109.                                                                     continue;
  110.  
  111.                                                                  availableCommands.add(entry.getKey() + " " + entry.getValue());
  112.                                                              }
  113.  
  114.                                                          caller.notify(Colors.Blue + "Available commands (Page " + (parameters.length == 2 ? parameters[1] : "1") + " of " + (int) Math.ceil((double) availableCommands.size() / (double) 7) + ") [] = required <> = optional:");
  115.                                                          if (parameters.length == 2)
  116.                                                              try {
  117.                                                                  int amount = Integer.parseInt(parameters[1]);
  118.  
  119.                                                                  if (amount > 0)
  120.                                                                      amount = (amount - 1) * 7;
  121.                                                                  else
  122.                                                                      amount = 0;
  123.  
  124.                                                                  for (int i = amount; i < amount + 7; i++)
  125.                                                                      if (availableCommands.size() > i)
  126.                                                                          caller.notify(Colors.Rose + availableCommands.get(i));
  127.                                                              } catch (NumberFormatException ex) {
  128.                                                                  caller.notify(Colors.Rose + "Not a valid page number.");
  129.                                                              }
  130.                                                          else
  131.                                                              for (int i = 0; i < 7; i++)
  132.                                                                  if (availableCommands.size() > i)
  133.                                                                      caller.notify(Colors.Rose + availableCommands.get(i));
  134.                                                      }
  135.                                                  };
  136.     public static final BaseCommand mute         = new BaseCommand("[Player] - Toggles mute on player.") {
  137.                                                      @Override
  138.                                                      void execute(MessageReceiver caller, String[] parameters){
  139.                                                          if (parameters.length != 2) {
  140.                                                              caller.notify(Colors.Rose + "Correct usage is: /mute [player]");
  141.                                                              return;
  142.                                                          }
  143.  
  144.                                                      Player player = etc.getServer().matchPlayer(parameters[1]);
  145.  
  146.                                                      if (player != null) {
  147.                                                         if (player.toggleMute())
  148.                                                             caller.notify(Colors.Rose + "player was muted");
  149.                                                         else
  150.                                                             caller.notify(Colors.Rose + "player was unmuted");
  151.                                                     } else
  152.                                                         caller.notify(Colors.Rose + "Can't find player " + parameters[1]);
  153.                                                     }
  154.                                                  };
  155.     public static final BaseCommand msg          = new BaseCommand("[Player] [Message] - Sends a message to player") {
  156.                                                      @Override
  157.                                                      void execute(MessageReceiver caller, String[] parameters) {
  158.                                                          if (parameters.length < 3) {
  159.                                                              caller.notify(Colors.Rose + "Correct usage is: /msg [player] [message]");
  160.                                                              return;
  161.                                                          }
  162.                                                          if (etc.getServer().getPlayer(caller.getName()).isMuted()) {
  163.                                                              caller.notify(Colors.Rose + "You are currently muted.");
  164.                                                              return;
  165.                                                          }
  166.                                                          
  167.                                                          Player player = etc.getServer().matchPlayer(parameters[1]);
  168.  
  169.                                                          if (player != null) {
  170.                                                              if (player.getName().equals(caller.getName())) {
  171.                                                                  caller.notify(Colors.Rose + "You can't message yourself!");
  172.                                                                  return;
  173.                                                              }
  174.  
  175.                                                              player.sendMessage("(MSG) " + etc.getServer().getPlayer(caller.getName()).getColor() + "<" + etc.getServer().getPlayer(caller.getName()) + "> " + Colors.White + etc.combineSplit(2, parameters, " "));
  176.                                                              caller.notify("(MSG) " + etc.getServer().getPlayer(caller.getName()).getColor() + "<" + etc.getServer().getPlayer(caller.getName()) + "> " + Colors.White + etc.combineSplit(2, parameters, " "));
  177.                                                          } else
  178.                                                              caller.notify(Colors.Rose + "Couldn't find player " + parameters[1]);
  179.                                                      }
  180.                                                  };
  181.  
  182.     public static final BaseCommand kit              = new BaseCommand ("[Kit] - Gives a kit. To get a list of kits type /kit") {
  183.                                                        @Override
  184.                                                        void execute(MessageReceiver caller, String[] parameters) {
  185.                                                             if (etc.getDataSource().hasKits()) {
  186.                                                                 if (parameters.length != 2 && parameters.length != 3) {
  187.                                                                  caller.notify(Colors.Rose + "Available kits" + Colors.White + ": " + etc.getDataSource().getKitNames(etc.getServer().getPlayer(caller.getName())));
  188.                                                                   return;
  189.                                                                 }
  190.                                                                
  191.                                                             Player toGive = etc.getServer().getPlayer(caller.getName());
  192.                                                             if (parameters.length > 2 && etc.getServer().getPlayer(caller.getName()).canIgnoreRestrictions())
  193.                                                                 toGive = etc.getServer().matchPlayer(parameters[2]);
  194.  
  195.                                                              Kit kit = etc.getDataSource().getKit(parameters[1]);
  196.                                                              if (toGive != null) {
  197.                                                                 if (kit != null) {
  198.                                                                    if (!etc.getServer().getPlayer(caller.getName()).isInGroup(kit.Group) && !kit.Group.equals(""))
  199.                                                                       caller.notify(Colors.Rose + "That kit does not exist.");
  200.                                                                    else if (onlyOneUseKits.contains(kit.Name))
  201.                                                                        caller.notify(Colors.Rose + "You can only get this kit once per login.");
  202.                                                                    else if (etc.getMCServer().b.containsKey(etc.getServer().getPlayer(caller.getName()).getName() + " " + kit.Name))
  203.                                                                            caller.notify(Colors.Rose + "You can't get this kit again for a while.");
  204.                                                                    else {
  205.                                                                         if (!etc.getServer().getPlayer(caller.getName()).canIgnoreRestrictions())
  206.                                                                            if (kit.Delay >= 0)
  207.                                                                               etc.getMCServer().b.put(etc.getServer().getPlayer(caller.getName()).getName() + " " + kit.Name, kit.Delay);
  208.                                                                            else
  209.                                                                               onlyOneUseKits.add(kit.Name);
  210.                                                                        
  211.                                                                 log.info(etc.getServer().getPlayer(caller.getName()).getName() + " got a kit!");
  212.                                                                 toGive.notify(Colors.Rose + "Enjoy this kit!");
  213.                                                                 for (Map.Entry<String, Integer> entry : kit.IDs.entrySet())
  214.                                                                    try {
  215.                                                                        int itemId = 0;
  216.                                                                        try {
  217.                                                                            itemId = Integer.parseInt(entry.getKey());
  218.                                                                        } catch (NumberFormatException n) {
  219.                                                                                itemId = etc.getDataSource().getItem(entry.getKey());
  220.                                                                        }
  221.  
  222.                                                                        toGive.giveItem(itemId, kit.IDs.get(entry.getKey()));
  223.                                                                        } catch (Exception e1) {
  224.                                                                                log.info("Got an exception while giving out a kit (Kit name \"" + kit.Name + "\"). Are you sure all the Ids are numbers?");
  225.                                                                                caller.notify(Colors.Rose + "The server encountered a problem while giving the kit :(");
  226.                                                                        }
  227.                                                                     }
  228.                                                                 } else
  229.                                                             caller.notify(Colors.Rose + "That kit does not exist.");
  230.                                                          } else
  231.                                                          caller.notify(Colors.Rose + "That user does not exist.");
  232.                                                        }
  233.                                                      }
  234.                                                  };
  235.  
  236.  
  237.  
  238.  
  239.    
  240.    
  241.    
  242. }
Advertisement
Add Comment
Please, Sign In to add comment