Added documentation, error checks

This commit is contained in:
Norbi Peti 2016-11-03 21:56:03 +01:00
parent d99f163500
commit f9c764fed6
2 changed files with 26 additions and 0 deletions

View file

@ -75,6 +75,8 @@ public class TBMCChatAPI {
continue;
TBMCCommandBase c = cmd.newInstance();
c.plugin = plugin;
if (!CheckForNulls(plugin, c))
continue;
commands.put(c.GetCommandPath(), c);
} catch (InstantiationException e) {
TBMCCoreAPI.SendException("An error occured while registering command " + cmd.getName(), e);
@ -110,6 +112,8 @@ public class TBMCChatAPI {
else
c = thecmdclass.newInstance();
c.plugin = plugin;
if (!CheckForNulls(plugin, c))
return;
commands.put(c.GetCommandPath(), c);
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while registering command " + thecmdclass.getSimpleName(), e);
@ -133,6 +137,8 @@ public class TBMCChatAPI {
* The command to add
*/
public static void AddCommand(JavaPlugin plugin, TBMCCommandBase cmd) {
if (!CheckForNulls(plugin, cmd))
return;
plugin.getLogger().info("Registering command /" + cmd.GetCommandPath() + " for " + plugin.getName());
try {
cmd.plugin = plugin;
@ -141,4 +147,17 @@ public class TBMCChatAPI {
TBMCCoreAPI.SendException("An error occured while registering command " + cmd.GetCommandPath(), e);
}
}
private static boolean CheckForNulls(JavaPlugin plugin, TBMCCommandBase cmd) {
if (cmd == null) {
TBMCCoreAPI.SendException("An error occured while registering a command for plugin " + plugin.getName(),
new Exception("The command is null!"));
return false;
} else if (cmd.GetCommandPath() == null) {
TBMCCoreAPI.SendException("An error occured while registering command " + cmd.getClass().getSimpleName()
+ " for plugin " + plugin.getName(), new Exception("The command path is null!"));
return false;
}
return true;
}
}

View file

@ -3,6 +3,13 @@ package buttondevteam.lib.chat;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
/**
* Extend this class to create new TBMCCommand and use {@link TBMCChatAPI#AddCommand(org.bukkit.plugin.java.JavaPlugin, TBMCCommandBase)} to add it. <u><b>Note:</b></u> The command path (command name
* and subcommand arguments) will be the class name by default, removing any "command" from it. To change it (especially for subcommands), override {@link #GetCommandPath()}.
*
* @author Norbi
*
*/
public abstract class TBMCCommandBase {
public TBMCCommandBase() {