mrkirby153

Untitled

Apr 7th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.11 KB | None | 0 0
  1. package me.mrkirby153.quarxelnetwork.core.command;
  2.  
  3. import me.mrkirby153.quarxelnetwork.core.Core;
  4. import me.mrkirby153.quarxelnetwork.core.error.ErrorType;
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.ChatColor;
  7. import org.bukkit.command.Command;
  8. import org.bukkit.command.CommandSender;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.plugin.PluginDescriptionFile;
  11. import org.bukkit.plugin.java.JavaPlugin;
  12.  
  13. import java.util.ArrayList;
  14.  
  15. /**
  16.  * CommandExecutor class to execute reflection-based command construction
  17.  */
  18. public class CommandExecutor implements org.bukkit.command.CommandExecutor {
  19.  
  20.     /**
  21.      * The plugin that "owns" the class. Passed through to run method
  22.      */
  23.     private JavaPlugin owner;
  24.     private ArrayList<String> paths = new ArrayList<>();
  25.  
  26.     public CommandExecutor(JavaPlugin owner, String commandPath) {
  27.         this.owner = owner;
  28.         this.paths.add(commandPath);
  29.         registerCommands();
  30.     }
  31.  
  32.     @Override
  33.     public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  34.         BaseCommand cmd;
  35.         Player player = null;
  36.  
  37.         if (sender instanceof Player) {
  38.             player = (Player) sender;
  39.         }
  40.         try {
  41.             Class<?> commandClass = findCmdClass(command.getName().toLowerCase());
  42.             if (commandClass == null)
  43.                 throw new CommandException("That command does not have an associated BaseCommand! This should never happen");
  44.             Object cmdObj = commandClass.newInstance();
  45.             if (!(cmdObj instanceof BaseCommand)) {
  46.                 throw new CommandException("The command class " + commandClass.getName() + " is not a base command!");
  47.             }
  48.             cmd = (BaseCommand) cmdObj;
  49.             if (player == null) {
  50.                 cmd.run(Bukkit.getServer(), sender, command, label, args);
  51.             } else {
  52.                 cmd.run(Bukkit.getServer(), player, command, label, args);
  53.             }
  54.         } catch (CommandException cmdException) {
  55.             if (cmdException.getMessage() != null && cmdException.getMessage().length() > 0) {
  56.                 sender.sendMessage(ChatColor.RED + "" + cmdException.getMessage());
  57.             }
  58.         } catch (Exception e) {
  59.             sender.sendMessage(ChatColor.RED + "An internal error occurred when preforming that command!");
  60.         }
  61.         return true;
  62.     }
  63.  
  64.     /**
  65.      * Attempts to find the command class from the registered paths
  66.      *
  67.      * @param commandName The command name to look for
  68.      * @return The class if found
  69.      */
  70.     private Class<?> findCmdClass(String commandName) {
  71.         for (String path : paths) {
  72.             Class cmdClass = findCmdClass(path, commandName);
  73.             if (cmdClass != null)
  74.                 return cmdClass;
  75.         }
  76.         return null;
  77.     }
  78.  
  79.     /**
  80.      * Attempts to construct a class from the arguments
  81.      *
  82.      * @param basePath    The package to look in
  83.      * @param commandName The command name to look for
  84.      * @return the class, if found
  85.      */
  86.     private Class<?> findCmdClass(String basePath, String commandName) {
  87.         try {
  88.             return this.getClass().getClassLoader().loadClass(basePath + ".Command" + commandName.toLowerCase());
  89.         } catch (Exception e) {
  90.             return null;
  91.         }
  92.     }
  93.  
  94.     /**
  95.      * Attempts to register all the commands in plugin.yml to this command executor
  96.      */
  97.     private void registerCommands() {
  98.         PluginDescriptionFile pdfFile = this.owner.getDescription();
  99.         for (String cmd : pdfFile.getCommands().keySet()) {
  100.             try {
  101.                 if (findCmdClass(cmd) == null)
  102.                     throw new CommandException("That command could not be found!");
  103.                 this.owner.getCommand(cmd).setExecutor(this);
  104.                 Core.getPlugin(Core.class).getLogger().info("Registered command " + cmd);
  105.             } catch (CommandException ex) {
  106.                 (Core.getPlugin(Core.class)).reporter.report("A command does not have a class associated with it", ex, ErrorType.GENERAL);
  107.             }
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment