Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.mrkirby153.quarxelnetwork.core.command;
- import me.mrkirby153.quarxelnetwork.core.Core;
- import me.mrkirby153.quarxelnetwork.core.error.ErrorType;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.PluginDescriptionFile;
- import org.bukkit.plugin.java.JavaPlugin;
- import java.util.ArrayList;
- /**
- * CommandExecutor class to execute reflection-based command construction
- */
- public class CommandExecutor implements org.bukkit.command.CommandExecutor {
- /**
- * The plugin that "owns" the class. Passed through to run method
- */
- private JavaPlugin owner;
- private ArrayList<String> paths = new ArrayList<>();
- public CommandExecutor(JavaPlugin owner, String commandPath) {
- this.owner = owner;
- this.paths.add(commandPath);
- registerCommands();
- }
- @Override
- public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
- BaseCommand cmd;
- Player player = null;
- if (sender instanceof Player) {
- player = (Player) sender;
- }
- try {
- Class<?> commandClass = findCmdClass(command.getName().toLowerCase());
- if (commandClass == null)
- throw new CommandException("That command does not have an associated BaseCommand! This should never happen");
- Object cmdObj = commandClass.newInstance();
- if (!(cmdObj instanceof BaseCommand)) {
- throw new CommandException("The command class " + commandClass.getName() + " is not a base command!");
- }
- cmd = (BaseCommand) cmdObj;
- if (player == null) {
- cmd.run(Bukkit.getServer(), sender, command, label, args);
- } else {
- cmd.run(Bukkit.getServer(), player, command, label, args);
- }
- } catch (CommandException cmdException) {
- if (cmdException.getMessage() != null && cmdException.getMessage().length() > 0) {
- sender.sendMessage(ChatColor.RED + "" + cmdException.getMessage());
- }
- } catch (Exception e) {
- sender.sendMessage(ChatColor.RED + "An internal error occurred when preforming that command!");
- }
- return true;
- }
- /**
- * Attempts to find the command class from the registered paths
- *
- * @param commandName The command name to look for
- * @return The class if found
- */
- private Class<?> findCmdClass(String commandName) {
- for (String path : paths) {
- Class cmdClass = findCmdClass(path, commandName);
- if (cmdClass != null)
- return cmdClass;
- }
- return null;
- }
- /**
- * Attempts to construct a class from the arguments
- *
- * @param basePath The package to look in
- * @param commandName The command name to look for
- * @return the class, if found
- */
- private Class<?> findCmdClass(String basePath, String commandName) {
- try {
- return this.getClass().getClassLoader().loadClass(basePath + ".Command" + commandName.toLowerCase());
- } catch (Exception e) {
- return null;
- }
- }
- /**
- * Attempts to register all the commands in plugin.yml to this command executor
- */
- private void registerCommands() {
- PluginDescriptionFile pdfFile = this.owner.getDescription();
- for (String cmd : pdfFile.getCommands().keySet()) {
- try {
- if (findCmdClass(cmd) == null)
- throw new CommandException("That command could not be found!");
- this.owner.getCommand(cmd).setExecutor(this);
- Core.getPlugin(Core.class).getLogger().info("Registered command " + cmd);
- } catch (CommandException ex) {
- (Core.getPlugin(Core.class)).reporter.report("A command does not have a class associated with it", ex, ErrorType.GENERAL);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment