gt22

Untitled

Nov 6th, 2016
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. package com.projectbronze.pbbot.console;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import com.projectbronze.pbbot.Core;
  6. import com.projectbronze.pbbot.console.ConsoleCommandManager.ConsoleCommand.ConsoleCommandAction;
  7.  
  8. public class ConsoleCommandManager
  9. {
  10.  
  11.     public static class ConsoleCommand
  12.     {
  13.  
  14.         public static interface ConsoleCommandAction
  15.         {
  16.             public void performCommand(String[] args);
  17.         }
  18.  
  19.         public String name, shortname;
  20.         public ConsoleCommandAction action;
  21.         public int level;
  22.  
  23.         public ConsoleCommand(String name, String shortname, ConsoleCommandAction action)
  24.         {
  25.             this.name = name;
  26.             this.shortname = shortname;
  27.             this.action = action;
  28.         }
  29.  
  30.     }
  31.  
  32.     private static List<ConsoleCommand> commands = new ArrayList<ConsoleCommand>();
  33.  
  34.     public static void addCommand(ConsoleCommand cm)
  35.     {
  36.         commands.add(cm);
  37.     }
  38.  
  39.     public static void addCommand(String name, String shortname, ConsoleCommandAction action)
  40.     {
  41.         addCommand(new ConsoleCommand(name, shortname, action));
  42.     }
  43.  
  44.     public static ConsoleCommand getCommandByName(String name)
  45.     {
  46.         for (ConsoleCommand cm : commands)
  47.         {
  48.             if (cm.name.equals(name) || cm.shortname.equals(name))
  49.             {
  50.                 return cm;
  51.             }
  52.         }
  53.         return null;
  54.     }
  55.  
  56.     public static List<ConsoleCommand> getCommands()
  57.     {
  58.         return commands;
  59.     }
  60.  
  61.     public static void reset()
  62.     {
  63.         commands.clear();
  64.         ConsoleCommands.init();
  65.     }
  66.  
  67.     public static void tryExecuteCommand(String text)
  68.     {
  69.         String[] t = text.split("\\s", 2);
  70.         String name = t[0].toLowerCase();
  71.         String[] args = t.length == 1 ? new String[]
  72.         {} : t[1].split("\\s");
  73.         ConsoleCommand cm = getCommandByName(name);
  74.         if (cm == null)
  75.         {
  76.             Core.log.info("Неизвестная консольная команда");
  77.         }
  78.         else
  79.         {
  80.             cm.action.performCommand(args);
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment