Command system under construction

This commit is contained in:
Norbi Peti 2016-06-24 19:32:34 +02:00
parent 2d7447a46f
commit 47315bbdaf
22 changed files with 48 additions and 77 deletions

View file

@ -30,7 +30,7 @@ public final class ChatonlyCommand extends TBMCCommandBase {
}
@Override
public String GetCommandName() {
public String GetCommandPath() {
return "chatonly";
}

View file

@ -2,6 +2,7 @@ package io.github.norbipeti.thebuttonmcchat.commands;
import io.github.norbipeti.thebuttonmcchat.PluginMain;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Set;
@ -12,13 +13,10 @@ import org.bukkit.command.CommandSender;
import org.reflections.Reflections;
public class CommandCaller implements CommandExecutor {
private CommandCaller() {
}
private static HashMap<String, TBMCCommandBase> commands = new HashMap<String, TBMCCommandBase>();
private static HashMap<String, TBMCSubCommandBase> subcommands = new HashMap<String, TBMCSubCommandBase>();
public static void RegisterCommands(PluginMain plugin) {
public void RegisterCommands(PluginMain plugin) {
System.out.println("Registering commands...");
Reflections rf = new Reflections(
"io.github.norbipeti.thebuttonmcchat.commands");
@ -27,28 +25,14 @@ public class CommandCaller implements CommandExecutor {
for (Class<? extends TBMCCommandBase> cmd : cmds) {
try {
TBMCCommandBase c = cmd.newInstance();
commands.put(c.GetCommandName(), c);
plugin.getCommand(c.GetCommandName()).setExecutor(c);
commands.put(c.GetCommandPath(), c);
plugin.getCommand(c.GetCommandPath()).setExecutor(this);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
System.out.println("Registering subcommands...");
Set<Class<? extends TBMCSubCommandBase>> subcmds = rf
.getSubTypesOf(TBMCSubCommandBase.class);
for (Class<? extends TBMCSubCommandBase> subcmd : subcmds) {
try {
TBMCSubCommandBase sc = subcmd.newInstance();
subcommands.put(sc.GetCommandPath(), sc);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
System.out.println("Done registering");
}
@Override
@ -60,17 +44,23 @@ public class CommandCaller implements CommandExecutor {
String path = command.getName();
for (String arg : args)
path += "/" + arg;
TBMCSubCommandBase cmd = subcommands.get(path);
while (cmd == null && path.contains("/"))
TBMCCommandBase cmd = commands.get(path);
int argc = 0;
while (cmd == null && path.contains("/")) {
path = path.substring(0, path.indexOf('/'));
argc++;
cmd = commands.get(path);
}
if (cmd == null) {
sender.sendMessage("§cInternal error: Subcommand not registered to CommandCaller");
sender.sendMessage("§cInternal error: Command not registered to CommandCaller");
if (sender != Bukkit.getConsoleSender())
Bukkit.getConsoleSender()
.sendMessage(
"§cInternal error: Subcommand not registered to CommandCaller");
"§cInternal error: Command not registered to CommandCaller");
return true;
}
cmd.OnCommand(sender, alias,
Arrays.copyOfRange(args, argc, args.length - 1));
return true;
}
}

View file

@ -30,7 +30,7 @@ public class MWikiCommand extends TBMCCommandBase {
}
@Override
public String GetCommandName() {
public String GetCommandPath() {
return "mwiki";
}

View file

@ -32,7 +32,7 @@ public final class OOCCommand extends TBMCCommandBase {
}
@Override
public String GetCommandName() {
public String GetCommandPath() {
return "ooc";
}

View file

@ -42,7 +42,7 @@ public final class UnlolCommand extends TBMCCommandBase {
}
@Override
public String GetCommandName() {
public String GetCommandPath() {
return "unlol";
}

View file

@ -25,7 +25,7 @@ public class YeehawCommand extends TBMCCommandBase {
}
@Override
public String GetCommandName() {
public String GetCommandPath() {
return "yeehaw";
}

View file

@ -20,8 +20,6 @@ public abstract class AppendTextCommandBase extends TBMCCommandBase {
return true;
}
public abstract String GetCommandName();
@Override
public boolean GetPlayerOnly() {
return false;

View file

@ -16,7 +16,7 @@ public final class ShrugCommand extends AppendTextCommandBase {
}
@Override
public String GetCommandName() {
public String GetCommandPath() {
return "shrug";
}

View file

@ -16,7 +16,7 @@ public final class TableflipCommand extends AppendTextCommandBase {
}
@Override
public String GetCommandName() {
public String GetCommandPath() {
return "tableflip";
}
}

View file

@ -16,7 +16,7 @@ public final class UnflipCommand extends AppendTextCommandBase {
}
@Override
public String GetCommandName() {
public String GetCommandPath() {
return "unflip";
}

View file

@ -20,7 +20,7 @@ public class AcceptCommand extends UCommandBase {
}
@Override
public boolean OnUCommand(CommandSender sender, String alias, String[] args) {
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
final Player player = (Player) sender;
MaybeOfflinePlayer p = MaybeOfflinePlayer.GetFromPlayer(player);
if (args.length < 1 && p.UserNames.size() > 1) {
@ -88,7 +88,7 @@ public class AcceptCommand extends UCommandBase {
}
@Override
public String GetUCommandName() {
public String GetUCommandPath() {
return "accept";
}

View file

@ -16,7 +16,7 @@ public class CCommand extends UCommandBase {
}
@Override
public boolean OnUCommand(CommandSender sender, String alias, String[] args) {
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
Player player = (Player) sender;
MaybeOfflinePlayer p = MaybeOfflinePlayer.AddPlayerIfNeeded(player
.getUniqueId());
@ -50,7 +50,7 @@ public class CCommand extends UCommandBase {
}
@Override
public String GetUCommandName() {
public String GetUCommandPath() {
return "c";
}

View file

@ -13,7 +13,7 @@ public final class HelpCommand extends UCommandBase {
}
@Override
public boolean OnUCommand(CommandSender sender, String alias, String[] args) {
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
if (args.length == 0) {
sender.sendMessage(new String[] {
"§6---- TBMC Help ----",
@ -47,7 +47,7 @@ public final class HelpCommand extends UCommandBase {
}
@Override
public String GetUCommandName() {
public String GetUCommandPath() {
return "help";
}
}

View file

@ -17,7 +17,7 @@ public final class IgnoreCommand extends UCommandBase {
}
@Override
public boolean OnUCommand(CommandSender sender, String alias, String[] args) {
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
final Player player = (Player) sender;
MaybeOfflinePlayer p = MaybeOfflinePlayer.GetFromPlayer(player);
if (p.FlairState.equals(FlairStates.Accepted)) {
@ -39,7 +39,7 @@ public final class IgnoreCommand extends UCommandBase {
}
@Override
public String GetUCommandName() {
public String GetUCommandPath() {
return "ignore";
}

View file

@ -35,7 +35,7 @@ public class KittycannonCommand extends UCommandBase {
}
@Override
public boolean OnUCommand(CommandSender sender, String alias, String[] args) {
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
Player player = (Player) sender;
MinigamePlayer mp = Minigames.plugin.pdata.getMinigamePlayer(player);
if (!(mp.isInMinigame() && mp.getMinigame().getName(false)
@ -84,7 +84,7 @@ public class KittycannonCommand extends UCommandBase {
}
@Override
public String GetUCommandName() {
public String GetUCommandPath() {
return "kittycannon";
}

View file

@ -15,7 +15,7 @@ public class NameCommand extends UCommandBase {
}
@Override
public boolean OnUCommand(CommandSender sender, String alias, String[] args) {
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
if (args.length == 1) {
return false;
}
@ -30,7 +30,7 @@ public class NameCommand extends UCommandBase {
}
@Override
public String GetUCommandName() {
public String GetUCommandPath() {
return "name";
}

View file

@ -10,13 +10,13 @@ public class OpmeCommand extends UCommandBase {
}
@Override
public boolean OnUCommand(CommandSender sender, String alias, String[] args) {
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
sender.sendMessage("It would be nice, wouldn't it?");
return true;
}
@Override
public String GetUCommandName() {
public String GetUCommandPath() {
return "opme";
}

View file

@ -9,16 +9,16 @@ public final class UCommand extends UCommandBase {
@Override
public String[] GetHelpText(String alias) {
return new String[] { "§6---- U commands ----",
"Subcommands: help, accept, ignore, admin" };
"Subcommands: help, accept, ignore, admin" }; //TODO
}
@Override
public boolean OnUCommand(CommandSender sender, String alias, String[] args) {
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
return false;
}
@Override
public String GetUCommandName() {
public String GetUCommandPath() {
return "u"; // TODO: Same as at AdminCommand
}

View file

@ -5,7 +5,6 @@ import java.util.Arrays;
import org.bukkit.command.CommandSender;
import io.github.norbipeti.thebuttonmcchat.commands.TBMCCommandBase;
import io.github.norbipeti.thebuttonmcchat.commands.TBMCSubCommandBase;
public abstract class UCommandBase extends TBMCCommandBase {
@ -13,7 +12,9 @@ public abstract class UCommandBase extends TBMCCommandBase {
@Override
public String GetCommandPath() {
return "u/" + GetUCommandPath();
if (GetUCommandPath().equals("u"))
return "u";
return "u/" + GetUCommandPath(); //TODO: This for others
}
public abstract String GetUCommandPath(); // TODO: Help for /u commands

View file

@ -11,7 +11,7 @@ public final class AdminCommand extends AdminCommandBase {
}
@Override
public boolean OnAdminCommand(CommandSender sender, String alias,
public boolean OnCommand(CommandSender sender, String alias,
String[] args) {
return false;
}

View file

@ -11,27 +11,10 @@ public abstract class AdminCommandBase extends UCommandBase {
public abstract String[] GetHelpText(String alias);
@Override
public boolean OnUCommand(CommandSender sender, String alias, String[] args) { // TODO:
// Only
// mods/admins
// should
// be
// able
// to
// use
// these
if (args.length == 0)
return false;
return OnAdminCommand(sender, alias,
Arrays.copyOfRange(args, 1, args.length));
}
public abstract boolean OnAdminCommand(CommandSender sender, String alias,
String[] args); // TODO: Actually call subcommands
@Override
public String GetUCommandName() {
return "admin";
public String GetUCommandPath() {
if (GetAdminCommandName().equals("admin"))
return "admin";
return "admin/" + GetAdminCommandName();
}
@Override

View file

@ -13,8 +13,7 @@ public class ConfirmCommand extends AdminCommandBase {
}
@Override
public boolean OnAdminCommand(CommandSender sender, String alias,
String[] args) {
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
if (ReloadCommand.Reloader == sender) {
try {
if (sender != PluginMain.Console)