Moved some command stuff

It took a while
This commit is contained in:
Norbi Peti 2016-10-28 21:42:34 +02:00
parent 4d5c3db239
commit caa63f7912
14 changed files with 32 additions and 131 deletions

View file

@ -16,7 +16,9 @@ import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import buttondevteam.chat.commands.CommandCaller;
import buttondevteam.chat.commands.YeehawCommand;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.TBMCChatAPI;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
@ -66,7 +68,7 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
Instance = this;
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
CommandCaller.RegisterChatCommands(this);
TBMCChatAPI.AddCommands(this, YeehawCommand.class);
Console = this.getServer().getConsoleSender();
LoadFiles(false);
@ -101,6 +103,7 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
};
t = new Thread(r);
t.start();
Bukkit.getScheduler().runTaskLater(this, () -> CommandCaller.RegisterCommands(), 0);
}
public Boolean stop = false;

View file

@ -1,56 +0,0 @@
package buttondevteam.chat;
import org.bukkit.plugin.java.JavaPlugin;
import buttondevteam.chat.commands.CommandCaller;
import buttondevteam.chat.commands.TBMCCommandBase;
import buttondevteam.chat.commands.ucmds.admin.PlayerInfoCommand;
public class TBMCChatAPI {
/**
* <p>
* This method adds a plugin's commands to help and sets their executor.
* </p>
* <p>
* The <u>command must be registered</u> in the caller plugin's plugin.yml.
* Otherwise the plugin will output a messsage to console.
* </p>
*
* @param plugin
* The caller plugin
* @param acmdclass
* A command's class to get the package name for commands. The
* provided class's package and subpackages are scanned for
* commands.
*/
public void AddCommands(JavaPlugin plugin, Class<? extends TBMCCommandBase> acmdclass) {
CommandCaller.AddCommands(plugin, acmdclass); // TODO: Make it scan for all "buttondevteam" packages
}
/**
* <p>
* Add player information for {@link PlayerInfoCommand}. Only mods can see
* the given information.
* </p>
*
* @param player
* @param infoline
*/
public void AddPlayerInfoForMods(ChatPlayer player, String infoline) {
// TODO
}
/**
* <p>
* Add player information for hover text at {@link ChatProcessing}. Every
* online player can see the given information.
* </p>
*
* @param player
* @param infoline
*/
public void AddPlayerInfoForHover(ChatPlayer player, String infoline) {
// TODO
}
}

View file

@ -5,6 +5,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import buttondevteam.chat.ChatPlayer;
import buttondevteam.lib.chat.TBMCCommandBase;
public final class ChatonlyCommand extends TBMCCommandBase {

View file

@ -1,11 +1,7 @@
package buttondevteam.chat.commands;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -13,58 +9,29 @@ import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import buttondevteam.chat.PluginMain;
import buttondevteam.discordplugin.TBMCDiscordAPI;
import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.TBMCCommandBase;
public class CommandCaller implements CommandExecutor {
private CommandCaller() {
}
private static HashMap<String, TBMCCommandBase> commands = new HashMap<String, TBMCCommandBase>();
public static HashMap<String, TBMCCommandBase> GetCommands() {
return commands;
}
private static CommandCaller instance;
public static void RegisterChatCommands(PluginMain plugin) {
AddCommands(plugin, TBMCCommandBase.class);
}
public static void AddCommands(JavaPlugin plugin, Class<? extends TBMCCommandBase> acmdclass) {
plugin.getLogger().info("Registering commands for " + plugin.getName());
public static void RegisterCommands() {
if (instance == null)
instance = new CommandCaller();
Reflections rf = new Reflections(
new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader(plugin.getClass().getClassLoader()))
.addClassLoader(plugin.getClass().getClassLoader()).addScanners(new SubTypesScanner())
.filterInputsBy((String pkg) -> pkg.contains(acmdclass.getPackage().getName())));
Set<Class<? extends TBMCCommandBase>> cmds = rf.getSubTypesOf(TBMCCommandBase.class);
for (Class<? extends TBMCCommandBase> cmd : cmds) {
try {
if (Modifier.isAbstract(cmd.getModifiers()))
continue;
TBMCCommandBase c = cmd.newInstance();
commands.put(c.GetCommandPath(), c);
if (!c.GetCommandPath().contains("/")) // Top-level command
{
PluginCommand pc = plugin.getCommand(c.GetCommandPath());
if (pc == null)
new Exception("Can't find top-level command: " + c.GetCommandPath()).printStackTrace();
else
pc.setExecutor(instance);
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
for (TBMCCommandBase c : TBMCChatAPI.GetCommands().values()) {
if (!c.GetCommandPath().contains("/")) // Top-level command
{
PluginCommand pc = ((JavaPlugin) c.getPlugin()).getCommand(c.GetCommandPath());
if (pc == null)
new Exception("Can't find top-level command: " + c.GetCommandPath()).printStackTrace();
else
pc.setExecutor(instance);
}
}
}
@ -74,12 +41,12 @@ public class CommandCaller implements CommandExecutor {
String path = command.getName();
for (String arg : args)
path += "/" + arg;
TBMCCommandBase cmd = commands.get(path);
TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path);
int argc = 0;
while (cmd == null && path.contains("/")) {
path = path.substring(0, path.lastIndexOf('/'));
argc++;
cmd = commands.get(path);
cmd = TBMCChatAPI.GetCommands().get(path);
}
if (cmd == null) {
sender.sendMessage("§cInternal error: Command not registered to CommandCaller");
@ -109,7 +76,7 @@ public class CommandCaller implements CommandExecutor {
public static String[] GetSubCommands(TBMCCommandBase command) {
ArrayList<String> cmds = new ArrayList<String>();
cmds.add("§6---- Subcommands ----");
for (TBMCCommandBase cmd : CommandCaller.GetCommands().values()) {
for (TBMCCommandBase cmd : TBMCChatAPI.GetCommands().values()) {
if (cmd.GetCommandPath().startsWith(command.GetCommandPath() + "/")) {
int ind = cmd.GetCommandPath().indexOf('/', command.GetCommandPath().length() + 2);
if (ind >= 0)

View file

@ -2,6 +2,8 @@ package buttondevteam.chat.commands;
import org.bukkit.command.CommandSender;
import buttondevteam.lib.chat.TBMCCommandBase;
public class MWikiCommand extends TBMCCommandBase {
@Override

View file

@ -4,6 +4,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import buttondevteam.chat.ChatPlayer;
import buttondevteam.lib.chat.TBMCCommandBase;
public final class OOCCommand extends TBMCCommandBase {

View file

@ -1,19 +0,0 @@
package buttondevteam.chat.commands;
import org.bukkit.command.CommandSender;
public abstract class TBMCCommandBase {
public TBMCCommandBase() {
}
public abstract String[] GetHelpText(String alias);
public abstract boolean OnCommand(CommandSender sender, String alias, String[] args);
public abstract String GetCommandPath();
public abstract boolean GetPlayerOnly();
public abstract boolean GetModOnly();
}

View file

@ -7,6 +7,7 @@ import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import buttondevteam.chat.PluginMain;
import buttondevteam.lib.chat.TBMCCommandBase;
public final class UnlolCommand extends TBMCCommandBase {

View file

@ -4,6 +4,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import buttondevteam.chat.PluginMain;
import buttondevteam.lib.chat.TBMCCommandBase;
public class YeehawCommand extends TBMCCommandBase {

View file

@ -3,7 +3,7 @@ package buttondevteam.chat.commands.appendtext;
import org.bukkit.command.CommandSender;
import buttondevteam.chat.ChatProcessing;
import buttondevteam.chat.commands.TBMCCommandBase;
import buttondevteam.lib.chat.TBMCCommandBase;
public abstract class AppendTextCommandBase extends TBMCCommandBase {

View file

@ -4,8 +4,8 @@ import java.util.ArrayList;
import org.bukkit.command.CommandSender;
import buttondevteam.chat.commands.CommandCaller;
import buttondevteam.chat.commands.TBMCCommandBase;
import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.TBMCCommandBase;
public final class HelpCommand extends UCommandBase {
@ -32,7 +32,7 @@ public final class HelpCommand extends UCommandBase {
else if (args[0].equalsIgnoreCase("commands")) {
ArrayList<String> text = new ArrayList<String>();
text.add("§6---- Command list ----");
for (TBMCCommandBase cmd : CommandCaller.GetCommands().values())
for (TBMCCommandBase cmd : TBMCChatAPI.GetCommands().values())
if (!cmd.GetCommandPath().contains("/"))
text.add("/" + cmd.GetCommandPath());
sender.sendMessage(text.toArray(new String[text.size()]));
@ -55,7 +55,7 @@ public final class HelpCommand extends UCommandBase {
String path = args[0];
for (int i = 1; i < args.length; i++)
path += "/" + args[i];
TBMCCommandBase cmd = CommandCaller.GetCommands().get(path);
TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path);
if (cmd == null)
sender.sendMessage(new String[] { "§cError: Command not found: " + path.replace('/', ' '),
"Usage example: /u accept --> /u help u accept" });

View file

@ -3,7 +3,7 @@ package buttondevteam.chat.commands.ucmds;
import org.bukkit.command.CommandSender;
import buttondevteam.chat.commands.CommandCaller;
import buttondevteam.chat.commands.TBMCCommandBase;
import buttondevteam.lib.chat.TBMCCommandBase;
public final class UCommand extends TBMCCommandBase {

View file

@ -1,6 +1,6 @@
package buttondevteam.chat.commands.ucmds;
import buttondevteam.chat.commands.TBMCCommandBase;
import buttondevteam.lib.chat.TBMCCommandBase;
public abstract class UCommandBase extends TBMCCommandBase {

View file

@ -2,8 +2,8 @@ package buttondevteam.chat.commands.ucmds.announce;
import org.bukkit.command.CommandSender;
import buttondevteam.chat.commands.CommandCaller;
import buttondevteam.chat.commands.ucmds.UCommandBase;
import buttondevteam.chat.commands.CommandCaller;
public class AnnounceCommand extends UCommandBase {