Fixed NPE with an NPE (#51)

From my phone. :P Also it's a more descriptive NPE.
This commit is contained in:
Norbi Peti 2016-11-09 12:30:11 +01:00
parent 795eacf38d
commit dcc955a816

View file

@ -1,94 +1,100 @@
package buttondevteam.chat.commands; package buttondevteam.chat.commands;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import buttondevteam.chat.PluginMain; import buttondevteam.chat.PluginMain;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.TBMCCommandBase; import buttondevteam.lib.chat.TBMCCommandBase;
public class CommandCaller implements CommandExecutor { public class CommandCaller implements CommandExecutor {
private CommandCaller() { private CommandCaller() {
} }
private static CommandCaller instance; private static CommandCaller instance;
public static void RegisterCommands() { public static void RegisterCommands() {
if (instance == null) if (instance == null)
instance = new CommandCaller(); instance = new CommandCaller();
for (Entry<String, TBMCCommandBase> entry : TBMCChatAPI.GetCommands().entrySet()) { for (Entry<String, TBMCCommandBase> entry : TBMCChatAPI.GetCommands().entrySet()) {
TBMCCommandBase c = entry.getValue(); TBMCCommandBase c = entry.getValue();
if (c == null) { if (c == null) {
TBMCCoreAPI.SendException("An error occured while registering commands", TBMCCoreAPI.SendException("An error occured while registering commands",
new Exception("Null command found at " + entry.getKey() + "!")); new Exception("Null command found at " + entry.getKey() + "!"));
continue; continue;
} }
if (c.GetCommandPath() == null) { if (c.GetCommandPath() == null) {
TBMCCoreAPI.SendException("An error occured while registering commands", TBMCCoreAPI.SendException("An error occured while registering commands",
new Exception("Command " + entry.getKey() + " has no command path!")); new Exception("Command " + entry.getKey() + " has no command path!"));
continue; continue;
} }
if (c.getPlugin() == null) { if (c.getPlugin() == null) {
TBMCCoreAPI.SendException("An error occured while registering commands", TBMCCoreAPI.SendException("An error occured while registering commands",
new Exception("Command " + entry.getKey() + " has no plugin!")); new Exception("Command " + entry.getKey() + " has no plugin!"));
continue; continue;
} }
if (!c.GetCommandPath().contains(" ")) // Top-level command if (!c.GetCommandPath().contains(" ")) // Top-level command
{ {
PluginCommand pc = ((JavaPlugin) c.getPlugin()).getCommand(c.GetCommandPath()); PluginCommand pc = ((JavaPlugin) c.getPlugin()).getCommand(c.GetCommandPath());
if (pc == null) if (pc == null)
TBMCCoreAPI.SendException("An error occured while registering commands", TBMCCoreAPI.SendException("An error occured while registering commands",
new Exception("Can't find top-level command: " + c.GetCommandPath() + " for plugin: " new Exception("Can't find top-level command: " + c.GetCommandPath() + " for plugin: "
+ c.getPlugin().getName())); + c.getPlugin().getName()));
else else
pc.setExecutor(instance); pc.setExecutor(instance);
} }
} }
} }
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) { public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
String path = command.getName(); String path = command.getName();
for (String arg : args) for (String arg : args)
path += " " + arg; path += " " + arg;
TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path); TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path);
int argc = 0; int argc = 0;
while (cmd == null && path.contains(" ")) { while (cmd == null && path.contains(" ")) {
path = path.substring(0, path.lastIndexOf(' ')); path = path.substring(0, path.lastIndexOf(' '));
argc++; argc++;
cmd = TBMCChatAPI.GetCommands().get(path); cmd = TBMCChatAPI.GetCommands().get(path);
} }
if (cmd == null) { if (cmd == null) {
sender.sendMessage("§cInternal error: Command not registered to CommandCaller"); sender.sendMessage("§cInternal error: Command not registered to CommandCaller");
if (sender != Bukkit.getConsoleSender()) if (sender != Bukkit.getConsoleSender())
Bukkit.getConsoleSender().sendMessage("§cInternal error: Command not registered to CommandCaller"); Bukkit.getConsoleSender().sendMessage("§cInternal error: Command not registered to CommandCaller");
return true; return true;
} }
if (cmd.GetModOnly() && !PluginMain.permission.has(sender, "tbmc.admin")) { if (cmd.GetModOnly() && !PluginMain.permission.has(sender, "tbmc.admin")) {
sender.sendMessage("§cYou need to be a mod to use this command."); sender.sendMessage("§cYou need to be a mod to use this command.");
return true; return true;
} }
if (cmd.GetPlayerOnly() && !(sender instanceof Player)) { if (cmd.GetPlayerOnly() && !(sender instanceof Player)) {
sender.sendMessage("§cOnly ingame players can use this command."); sender.sendMessage("§cOnly ingame players can use this command.");
return true; return true;
} }
final String[] cmdargs = args.length > 0 ? Arrays.copyOfRange(args, args.length - argc, args.length) : args; final String[] cmdargs = args.length > 0 ? Arrays.copyOfRange(args, args.length - argc, args.length) : args;
try { try {
if (!cmd.OnCommand(sender, alias, cmdargs)) if (!cmd.OnCommand(sender, alias, cmdargs)) {
sender.sendMessage(cmd.GetHelpText(alias)); if(cmd.GetHelpText()==null) {
} catch (Exception e) { sender.sendMessage("Wrong usage, but there's no help text! Error is being reported to devs.");
TBMCCoreAPI.SendException("Failed to execute command /" + cmd.GetCommandPath() + " with arguments " throw new NullPointerException("GetHelpText is null for comand /" + cmd.GetCommandPath());
+ Arrays.toString(cmdargs), e); }
} else
return true; sender.sendMessage(cmd.GetHelpText(alias));
} }
} } catch (Exception e) {
TBMCCoreAPI.SendException("Failed to execute command /" + cmd.GetCommandPath() + " with arguments "
+ Arrays.toString(cmdargs), e);
}
return true;
}
}