From dcc955a816bc80fa320dec2d383735eb17731d45 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 9 Nov 2016 12:30:11 +0100 Subject: [PATCH] Fixed NPE with an NPE (#51) From my phone. :P Also it's a more descriptive NPE. --- .../chat/commands/CommandCaller.java | 194 +++++++++--------- 1 file changed, 100 insertions(+), 94 deletions(-) diff --git a/src/main/java/buttondevteam/chat/commands/CommandCaller.java b/src/main/java/buttondevteam/chat/commands/CommandCaller.java index 58cdccc..1b29675 100644 --- a/src/main/java/buttondevteam/chat/commands/CommandCaller.java +++ b/src/main/java/buttondevteam/chat/commands/CommandCaller.java @@ -1,94 +1,100 @@ -package buttondevteam.chat.commands; - -import java.util.Arrays; -import java.util.Map.Entry; - -import org.bukkit.Bukkit; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.command.PluginCommand; -import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; -import buttondevteam.chat.PluginMain; -import buttondevteam.lib.TBMCCoreAPI; -import buttondevteam.lib.chat.TBMCChatAPI; -import buttondevteam.lib.chat.TBMCCommandBase; - -public class CommandCaller implements CommandExecutor { - - private CommandCaller() { - } - - private static CommandCaller instance; - - public static void RegisterCommands() { - if (instance == null) - instance = new CommandCaller(); - for (Entry entry : TBMCChatAPI.GetCommands().entrySet()) { - TBMCCommandBase c = entry.getValue(); - if (c == null) { - TBMCCoreAPI.SendException("An error occured while registering commands", - new Exception("Null command found at " + entry.getKey() + "!")); - continue; - } - if (c.GetCommandPath() == null) { - TBMCCoreAPI.SendException("An error occured while registering commands", - new Exception("Command " + entry.getKey() + " has no command path!")); - continue; - } - if (c.getPlugin() == null) { - TBMCCoreAPI.SendException("An error occured while registering commands", - new Exception("Command " + entry.getKey() + " has no plugin!")); - continue; - } - if (!c.GetCommandPath().contains(" ")) // Top-level command - { - PluginCommand pc = ((JavaPlugin) c.getPlugin()).getCommand(c.GetCommandPath()); - if (pc == null) - TBMCCoreAPI.SendException("An error occured while registering commands", - new Exception("Can't find top-level command: " + c.GetCommandPath() + " for plugin: " - + c.getPlugin().getName())); - else - pc.setExecutor(instance); - } - } - } - - @Override - public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) { - String path = command.getName(); - for (String arg : args) - path += " " + arg; - TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path); - int argc = 0; - while (cmd == null && path.contains(" ")) { - path = path.substring(0, path.lastIndexOf(' ')); - argc++; - cmd = TBMCChatAPI.GetCommands().get(path); - } - if (cmd == null) { - sender.sendMessage("§cInternal error: Command not registered to CommandCaller"); - if (sender != Bukkit.getConsoleSender()) - Bukkit.getConsoleSender().sendMessage("§cInternal error: Command not registered to CommandCaller"); - return true; - } - if (cmd.GetModOnly() && !PluginMain.permission.has(sender, "tbmc.admin")) { - sender.sendMessage("§cYou need to be a mod to use this command."); - return true; - } - if (cmd.GetPlayerOnly() && !(sender instanceof Player)) { - sender.sendMessage("§cOnly ingame players can use this command."); - return true; - } - final String[] cmdargs = args.length > 0 ? Arrays.copyOfRange(args, args.length - argc, args.length) : args; - try { - if (!cmd.OnCommand(sender, alias, cmdargs)) - sender.sendMessage(cmd.GetHelpText(alias)); - } catch (Exception e) { - TBMCCoreAPI.SendException("Failed to execute command /" + cmd.GetCommandPath() + " with arguments " - + Arrays.toString(cmdargs), e); - } - return true; - } -} +package buttondevteam.chat.commands; + +import java.util.Arrays; +import java.util.Map.Entry; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginCommand; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import buttondevteam.chat.PluginMain; +import buttondevteam.lib.TBMCCoreAPI; +import buttondevteam.lib.chat.TBMCChatAPI; +import buttondevteam.lib.chat.TBMCCommandBase; + +public class CommandCaller implements CommandExecutor { + + private CommandCaller() { + } + + private static CommandCaller instance; + + public static void RegisterCommands() { + if (instance == null) + instance = new CommandCaller(); + for (Entry entry : TBMCChatAPI.GetCommands().entrySet()) { + TBMCCommandBase c = entry.getValue(); + if (c == null) { + TBMCCoreAPI.SendException("An error occured while registering commands", + new Exception("Null command found at " + entry.getKey() + "!")); + continue; + } + if (c.GetCommandPath() == null) { + TBMCCoreAPI.SendException("An error occured while registering commands", + new Exception("Command " + entry.getKey() + " has no command path!")); + continue; + } + if (c.getPlugin() == null) { + TBMCCoreAPI.SendException("An error occured while registering commands", + new Exception("Command " + entry.getKey() + " has no plugin!")); + continue; + } + if (!c.GetCommandPath().contains(" ")) // Top-level command + { + PluginCommand pc = ((JavaPlugin) c.getPlugin()).getCommand(c.GetCommandPath()); + if (pc == null) + TBMCCoreAPI.SendException("An error occured while registering commands", + new Exception("Can't find top-level command: " + c.GetCommandPath() + " for plugin: " + + c.getPlugin().getName())); + else + pc.setExecutor(instance); + } + } + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) { + String path = command.getName(); + for (String arg : args) + path += " " + arg; + TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path); + int argc = 0; + while (cmd == null && path.contains(" ")) { + path = path.substring(0, path.lastIndexOf(' ')); + argc++; + cmd = TBMCChatAPI.GetCommands().get(path); + } + if (cmd == null) { + sender.sendMessage("§cInternal error: Command not registered to CommandCaller"); + if (sender != Bukkit.getConsoleSender()) + Bukkit.getConsoleSender().sendMessage("§cInternal error: Command not registered to CommandCaller"); + return true; + } + if (cmd.GetModOnly() && !PluginMain.permission.has(sender, "tbmc.admin")) { + sender.sendMessage("§cYou need to be a mod to use this command."); + return true; + } + if (cmd.GetPlayerOnly() && !(sender instanceof Player)) { + sender.sendMessage("§cOnly ingame players can use this command."); + return true; + } + final String[] cmdargs = args.length > 0 ? Arrays.copyOfRange(args, args.length - argc, args.length) : args; + try { + if (!cmd.OnCommand(sender, alias, cmdargs)) { + if(cmd.GetHelpText()==null) { + sender.sendMessage("Wrong usage, but there's no help text! Error is being reported to devs."); + throw new NullPointerException("GetHelpText is null for comand /" + cmd.GetCommandPath()); + } + else + sender.sendMessage(cmd.GetHelpText(alias)); + } + } catch (Exception e) { + TBMCCoreAPI.SendException("Failed to execute command /" + cmd.GetCommandPath() + " with arguments " + + Arrays.toString(cmdargs), e); + } + return true; + } +}