From 56d46a763f92fd8d8c99bfe3e11db6b2284f18db Mon Sep 17 00:00:00 2001 From: sekwah Date: Fri, 19 Jan 2018 04:14:54 +0000 Subject: [PATCH 1/4] Started adding the base of the command registry --- .../core/AdvancedPortalsCore.java | 14 +++++++++++--- .../core/commands/DestiCommand.java | 18 ++++++++++++++++++ .../core/commands/PortalCommand.java | 16 +++++++++++++++- .../command/CommandRegister.java | 19 +++++++++++++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 src/com/sekwah/advancedportals/core/commands/DestiCommand.java create mode 100644 src/com/sekwah/advancedportals/coreconnector/command/CommandRegister.java diff --git a/src/com/sekwah/advancedportals/core/AdvancedPortalsCore.java b/src/com/sekwah/advancedportals/core/AdvancedPortalsCore.java index f3a4a54..2be9047 100644 --- a/src/com/sekwah/advancedportals/core/AdvancedPortalsCore.java +++ b/src/com/sekwah/advancedportals/core/AdvancedPortalsCore.java @@ -1,26 +1,34 @@ package com.sekwah.advancedportals.core; +import com.sekwah.advancedportals.core.commands.DestiCommand; +import com.sekwah.advancedportals.core.commands.PortalCommand; import com.sekwah.advancedportals.core.util.Config; import com.sekwah.advancedportals.core.util.DataStorage; import com.sekwah.advancedportals.core.util.InfoLogger; +import com.sekwah.advancedportals.coreconnector.command.CommandRegister; public class AdvancedPortalsCore { private static AdvancedPortalsCore instance; - private DataStorage dataStorage; - private InfoLogger infoLogger; + private final CommandRegister commandRegister; + private final DataStorage dataStorage; + private final InfoLogger infoLogger; private Config config; - public AdvancedPortalsCore(DataStorage dataStorage, InfoLogger infoLogger) { + public AdvancedPortalsCore(DataStorage dataStorage, InfoLogger infoLogger, CommandRegister commandRegister) { this.dataStorage = dataStorage; this.infoLogger = infoLogger; this.instance = this; + this.commandRegister = commandRegister; this.onEnable(); } private void onEnable() { this.loadPortalData(); infoLogger.log("Advanced portals have been successfully enabled!"); + + this.commandRegister.registerCommand("portal", new PortalCommand()); + this.commandRegister.registerCommand("destination", new DestiCommand()); } /** diff --git a/src/com/sekwah/advancedportals/core/commands/DestiCommand.java b/src/com/sekwah/advancedportals/core/commands/DestiCommand.java new file mode 100644 index 0000000..e0197a1 --- /dev/null +++ b/src/com/sekwah/advancedportals/core/commands/DestiCommand.java @@ -0,0 +1,18 @@ +package com.sekwah.advancedportals.core.commands; + +import com.sun.corba.se.impl.activation.CommandHandler; + +import java.util.List; + +public class DestiCommand implements CommandTemplate { + + @Override + public void onCommand(CommandHandler sender, String[] args) { + + } + + @Override + public List onTabComplete(CommandHandler sender, String[] args) { + return null; + } +} diff --git a/src/com/sekwah/advancedportals/core/commands/PortalCommand.java b/src/com/sekwah/advancedportals/core/commands/PortalCommand.java index fa36282..d02c205 100644 --- a/src/com/sekwah/advancedportals/core/commands/PortalCommand.java +++ b/src/com/sekwah/advancedportals/core/commands/PortalCommand.java @@ -1,4 +1,18 @@ package com.sekwah.advancedportals.core.commands; -public class PortalCommand { +import com.sun.corba.se.impl.activation.CommandHandler; + +import java.util.List; + +public class PortalCommand implements CommandTemplate { + + @Override + public void onCommand(CommandHandler sender, String[] args) { + + } + + @Override + public List onTabComplete(CommandHandler sender, String[] args) { + return null; + } } diff --git a/src/com/sekwah/advancedportals/coreconnector/command/CommandRegister.java b/src/com/sekwah/advancedportals/coreconnector/command/CommandRegister.java new file mode 100644 index 0000000..b31dde2 --- /dev/null +++ b/src/com/sekwah/advancedportals/coreconnector/command/CommandRegister.java @@ -0,0 +1,19 @@ +package com.sekwah.advancedportals.coreconnector.command; + +import com.sekwah.advancedportals.core.commands.CommandTemplate; +import com.sekwah.advancedportals.core.commands.PortalCommand; + +/** + * Register the CommandTemplate files to the appropriate system + */ +public class CommandRegister { + + /** + * Registers the command to the appropriate system + * @param commandName + * @param commandExecutor + */ + public void registerCommand(String commandName, CommandTemplate commandExecutor) { + + } +} From 970db237dec3f5c13c62a251ff114408dd39b7b7 Mon Sep 17 00:00:00 2001 From: sekwah Date: Mon, 22 Jan 2018 05:00:35 +0000 Subject: [PATCH 2/4] Started designing the start of commands --- Resources/lang/en_GB.lang | 8 ++- .../core/AdvancedPortalsCore.java | 29 ++++++---- .../core/api/commands/SubCommand.java | 9 ++- .../DestinationSubCommandRegistry.java | 9 --- .../registry/PortalSubCommandRegistry.java | 11 ---- .../core/api/registry/SubCommandRegistry.java | 30 +++++----- .../core/commands/CommandTemplate.java | 6 +- .../core/commands/CommandWithSubCommands.java | 55 +++++++++++++++++++ .../core/commands/DestiCommand.java | 18 ------ .../core/commands/PortalCommand.java | 18 ------ .../advancedportals/core/util/Config.java | 2 + .../core/util/DataStorage.java | 5 +- .../command/CommandRegister.java | 1 - .../container/CommandSenderContainer.java | 4 ++ 14 files changed, 113 insertions(+), 92 deletions(-) delete mode 100644 src/com/sekwah/advancedportals/core/api/registry/DestinationSubCommandRegistry.java delete mode 100644 src/com/sekwah/advancedportals/core/api/registry/PortalSubCommandRegistry.java create mode 100644 src/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java delete mode 100644 src/com/sekwah/advancedportals/core/commands/DestiCommand.java delete mode 100644 src/com/sekwah/advancedportals/core/commands/PortalCommand.java diff --git a/Resources/lang/en_GB.lang b/Resources/lang/en_GB.lang index 2a0188b..476ba64 100644 --- a/Resources/lang/en_GB.lang +++ b/Resources/lang/en_GB.lang @@ -15,6 +15,8 @@ messageprefix.positive=\u00Aa[\u00AeAdvancedPortals\u00Aa] messageprefix.negative=\u00Ac[\u00A7AdvancedPortals\u00Ac] -logger.pluginenable=\u00A7aAdvanced portals have been successfully enabled! -logger.plugindisable=\u00A7cAdvanced portals are being disabled! -logger.plugincrafterr=This version of craftbukkit is not yet supported or something went wrong, please post this message with the version number and the above stacktrace in an issue on GitHub v:%1$s +logger.pluginenable=Advanced portals have been successfully enabled! +logger.plugindisable=Advanced portals are being disabled! +logger.plugincrafterror=This version of craftbukkit is not yet supported or something went wrong, please post this message with the version number and the above stacktrace in an issue on GitHub v:%1$s + +command.noargs=%1$s Sorry but you need to specify a sub command, please use \u00Ae/%2$s help \u00Acif you would like a list of possible sub commands. diff --git a/src/com/sekwah/advancedportals/core/AdvancedPortalsCore.java b/src/com/sekwah/advancedportals/core/AdvancedPortalsCore.java index 2be9047..5947c59 100644 --- a/src/com/sekwah/advancedportals/core/AdvancedPortalsCore.java +++ b/src/com/sekwah/advancedportals/core/AdvancedPortalsCore.java @@ -1,10 +1,10 @@ package com.sekwah.advancedportals.core; -import com.sekwah.advancedportals.core.commands.DestiCommand; -import com.sekwah.advancedportals.core.commands.PortalCommand; +import com.sekwah.advancedportals.core.commands.CommandWithSubCommands; import com.sekwah.advancedportals.core.util.Config; import com.sekwah.advancedportals.core.util.DataStorage; import com.sekwah.advancedportals.core.util.InfoLogger; +import com.sekwah.advancedportals.core.util.Lang; import com.sekwah.advancedportals.coreconnector.command.CommandRegister; public class AdvancedPortalsCore { @@ -13,8 +13,12 @@ public class AdvancedPortalsCore { private final CommandRegister commandRegister; private final DataStorage dataStorage; private final InfoLogger infoLogger; + private Config config; + private CommandWithSubCommands portalCommand; + private CommandWithSubCommands destiCommand; + public AdvancedPortalsCore(DataStorage dataStorage, InfoLogger infoLogger, CommandRegister commandRegister) { this.dataStorage = dataStorage; this.infoLogger = infoLogger; @@ -24,23 +28,28 @@ public class AdvancedPortalsCore { } private void onEnable() { - this.loadPortalData(); - infoLogger.log("Advanced portals have been successfully enabled!"); + Lang.loadLanguage("en_GB"); + this.loadPortalConfig(); - this.commandRegister.registerCommand("portal", new PortalCommand()); - this.commandRegister.registerCommand("destination", new DestiCommand()); + this.portalCommand = new CommandWithSubCommands(); + this.destiCommand = new CommandWithSubCommands(); + this.commandRegister.registerCommand("portal", this.portalCommand); + this.commandRegister.registerCommand("destination", this.destiCommand); + + infoLogger.log(Lang.translate("logger.pluginenable")); } /** - * Can be used for in /portal reload as well. + * Loads the portal config into the memory and saves from the memory to check in case certain things have changed + * (basically if values are missing or whatever) */ - private void loadPortalData() { - this.config = this.dataStorage.loadJson(Config.class, "config.json"); + private void loadPortalConfig() { + this.config = this.dataStorage.loadJson(Config.class, "config.json", true); this.dataStorage.storeJson(this.config, "config.json"); } public void onDisable() { - infoLogger.log("Advanced portals are being disabled!"); + infoLogger.log(Lang.translate("logger.plugindisable")); } private static AdvancedPortalsCore getInstance() { diff --git a/src/com/sekwah/advancedportals/core/api/commands/SubCommand.java b/src/com/sekwah/advancedportals/core/api/commands/SubCommand.java index 385b589..00956dd 100644 --- a/src/com/sekwah/advancedportals/core/api/commands/SubCommand.java +++ b/src/com/sekwah/advancedportals/core/api/commands/SubCommand.java @@ -28,8 +28,13 @@ public interface SubCommand { List onTabComplete(CommandSenderContainer sender, String[] args); /** - * @return the string to show on the above help menu. (describing the subcommand) + * @return the string to show next to the tag on the help menu. */ - String getHelpText(); + String getBasicHelpText(); + + /** + * @return the string to show if help then the tag is listed. + */ + String getDetailedHelpText(); } diff --git a/src/com/sekwah/advancedportals/core/api/registry/DestinationSubCommandRegistry.java b/src/com/sekwah/advancedportals/core/api/registry/DestinationSubCommandRegistry.java deleted file mode 100644 index 8546a2f..0000000 --- a/src/com/sekwah/advancedportals/core/api/registry/DestinationSubCommandRegistry.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.sekwah.advancedportals.core.api.registry; - -/** - * Designed to let addons add new command sections to access, edit or add new functonality. - * - * @author sekwah41 - */ -public class DestinationSubCommandRegistry extends SubCommandRegistry { -} diff --git a/src/com/sekwah/advancedportals/core/api/registry/PortalSubCommandRegistry.java b/src/com/sekwah/advancedportals/core/api/registry/PortalSubCommandRegistry.java deleted file mode 100644 index f379f7a..0000000 --- a/src/com/sekwah/advancedportals/core/api/registry/PortalSubCommandRegistry.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.sekwah.advancedportals.core.api.registry; - -/** - * Designed to let addons add new command sections to access, edit or add new functonality. - * - * @author sekwah41 - */ -public class PortalSubCommandRegistry extends SubCommandRegistry { - - -} diff --git a/src/com/sekwah/advancedportals/core/api/registry/SubCommandRegistry.java b/src/com/sekwah/advancedportals/core/api/registry/SubCommandRegistry.java index b0205ae..e54d1f7 100644 --- a/src/com/sekwah/advancedportals/core/api/registry/SubCommandRegistry.java +++ b/src/com/sekwah/advancedportals/core/api/registry/SubCommandRegistry.java @@ -17,37 +17,35 @@ import java.util.Map; */ public class SubCommandRegistry { - private Map subCommandMap = new HashMap<>(); - - private static final SubCommandRegistry instance = new SubCommandRegistry(); + protected Map subCommandMap = new HashMap<>(); /** * List of subcommand names which should be in order alphabetically */ - private ArrayList subCommands = new ArrayList<>(); + protected ArrayList subCommands = new ArrayList<>(); /** * @param arg argument needed to activate * @param subCommand * @return if the subcommand is registered or not */ - public static boolean registerSubCommand(String arg, SubCommand subCommand) { + public boolean registerSubCommand(String arg, SubCommand subCommand) { if (subCommand == null) { AdvancedPortalsCore.getInfoLogger().logWarning("The subcommand '" + arg + "' cannot be null."); return false; } - if(!instance.subCommandMap.containsKey(arg)){ + if(!this.subCommandMap.containsKey(arg)){ AdvancedPortalsCore.getInfoLogger().logWarning("The subcommand '" + arg + "' already exists."); return false; } - instance.subCommandMap.put(arg.toLowerCase(), subCommand); + this.subCommandMap.put(arg.toLowerCase(), subCommand); - instance.subCommands.add(arg.toLowerCase()); + this.subCommands.add(arg.toLowerCase()); - Collections.sort(instance.subCommands); + Collections.sort(this.subCommands); return true; } @@ -55,8 +53,8 @@ public class SubCommandRegistry { /** * @return a list of arguments of registered subcommands */ - public static ArrayList getSubCommands(){ - return instance.subCommands; + public ArrayList getSubCommands(){ + return this.subCommands; } /** @@ -66,8 +64,8 @@ public class SubCommandRegistry { * @param arg * @return if the argument is registered */ - public static boolean isArgRegistered(String arg){ - return instance.subCommandMap.containsKey(arg.toLowerCase()); + public boolean isArgRegistered(String arg){ + return this.subCommandMap.containsKey(arg.toLowerCase()); } /** @@ -75,9 +73,9 @@ public class SubCommandRegistry { * @param arg * @return the subcommand linked to the arg */ - public static SubCommand getSubCommand(String arg){ - if(instance.subCommandMap.containsKey(arg.toLowerCase())){ - return instance.subCommandMap.get(arg.toLowerCase()); + public SubCommand getSubCommand(String arg){ + if(this.subCommandMap.containsKey(arg.toLowerCase())){ + return this.subCommandMap.get(arg.toLowerCase()); } return null; } diff --git a/src/com/sekwah/advancedportals/core/commands/CommandTemplate.java b/src/com/sekwah/advancedportals/core/commands/CommandTemplate.java index 1d5c2bd..7ccf62a 100644 --- a/src/com/sekwah/advancedportals/core/commands/CommandTemplate.java +++ b/src/com/sekwah/advancedportals/core/commands/CommandTemplate.java @@ -1,6 +1,6 @@ package com.sekwah.advancedportals.core.commands; -import com.sun.corba.se.impl.activation.CommandHandler; +import com.sekwah.advancedportals.coreconnector.container.CommandSenderContainer; import java.util.List; @@ -11,7 +11,7 @@ import java.util.List; */ public interface CommandTemplate { - void onCommand(CommandHandler sender, String[] args); + void onCommand(CommandSenderContainer sender, String commandExecuted, String[] args); /** * Fired when someone asks for a tab complete action. @@ -19,6 +19,6 @@ public interface CommandTemplate { * @param args * @return */ - List onTabComplete(CommandHandler sender, String[] args); + List onTabComplete(CommandSenderContainer sender, String[] args); } diff --git a/src/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java b/src/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java new file mode 100644 index 0000000..0a556e1 --- /dev/null +++ b/src/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java @@ -0,0 +1,55 @@ +package com.sekwah.advancedportals.core.commands; + +import com.sekwah.advancedportals.core.api.commands.SubCommand; +import com.sekwah.advancedportals.core.api.registry.SubCommandRegistry; +import com.sekwah.advancedportals.core.util.Lang; +import com.sekwah.advancedportals.coreconnector.container.CommandSenderContainer; + +import java.util.ArrayList; +import java.util.List; + +public class CommandWithSubCommands implements CommandTemplate { + + private final SubCommandRegistry subCommandRegistry; + + public CommandWithSubCommands() { + this.subCommandRegistry = new SubCommandRegistry(); + } + + public boolean registerSubCommand(String arg, SubCommand subCommand) { + return this.subCommandRegistry.registerSubCommand(arg,subCommand); + } + + public ArrayList getSubCommands(){ + return this.subCommandRegistry.getSubCommands(); + } + + public boolean isArgRegistered(String arg){ + return this.subCommandRegistry.isArgRegistered(arg); + } + + public SubCommand getSubCommand(String arg){ + return this.subCommandRegistry.getSubCommand(arg); + } + + + @Override + public void onCommand(CommandSenderContainer sender, String commandExecuted, String[] args) { + if(args.length > 0) { + if(args[0].equalsIgnoreCase("help")) { + // TODO do help menu here sorted alphabetically + } + else { + + } + } + else { + sender.sendMessage(Lang.translateInsertVariablesColor(Lang.translate("command.noargs"), Lang.translate("messageprefix.negative"), commandExecuted)); + } + } + + @Override + public List onTabComplete(CommandSenderContainer sender, String[] args) { + return null; + } +} diff --git a/src/com/sekwah/advancedportals/core/commands/DestiCommand.java b/src/com/sekwah/advancedportals/core/commands/DestiCommand.java deleted file mode 100644 index e0197a1..0000000 --- a/src/com/sekwah/advancedportals/core/commands/DestiCommand.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.sekwah.advancedportals.core.commands; - -import com.sun.corba.se.impl.activation.CommandHandler; - -import java.util.List; - -public class DestiCommand implements CommandTemplate { - - @Override - public void onCommand(CommandHandler sender, String[] args) { - - } - - @Override - public List onTabComplete(CommandHandler sender, String[] args) { - return null; - } -} diff --git a/src/com/sekwah/advancedportals/core/commands/PortalCommand.java b/src/com/sekwah/advancedportals/core/commands/PortalCommand.java deleted file mode 100644 index d02c205..0000000 --- a/src/com/sekwah/advancedportals/core/commands/PortalCommand.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.sekwah.advancedportals.core.commands; - -import com.sun.corba.se.impl.activation.CommandHandler; - -import java.util.List; - -public class PortalCommand implements CommandTemplate { - - @Override - public void onCommand(CommandHandler sender, String[] args) { - - } - - @Override - public List onTabComplete(CommandHandler sender, String[] args) { - return null; - } -} diff --git a/src/com/sekwah/advancedportals/core/util/Config.java b/src/com/sekwah/advancedportals/core/util/Config.java index 76ad5b5..7593096 100644 --- a/src/com/sekwah/advancedportals/core/util/Config.java +++ b/src/com/sekwah/advancedportals/core/util/Config.java @@ -19,6 +19,8 @@ public class Config { private String selectionBlock = "STAINED_GLASS"; + private String translationFile = "en_GB"; + private int blockSubID = 14; public boolean getUseOnlySpecialAxe() { diff --git a/src/com/sekwah/advancedportals/core/util/DataStorage.java b/src/com/sekwah/advancedportals/core/util/DataStorage.java index 822b351..1e5e19d 100644 --- a/src/com/sekwah/advancedportals/core/util/DataStorage.java +++ b/src/com/sekwah/advancedportals/core/util/DataStorage.java @@ -33,7 +33,10 @@ public class DataStorage { } public T loadJson(Class dataHolder, String location) { - // TODO get json + return this.loadJson(dataHolder, location, false); + } + public T loadJson(Class dataHolder, String location, boolean loadDefaultIfMissing) { + // TODO get json and if file doesnt exist create default class if true return gson.fromJson("", dataHolder); } diff --git a/src/com/sekwah/advancedportals/coreconnector/command/CommandRegister.java b/src/com/sekwah/advancedportals/coreconnector/command/CommandRegister.java index b31dde2..feeaf6f 100644 --- a/src/com/sekwah/advancedportals/coreconnector/command/CommandRegister.java +++ b/src/com/sekwah/advancedportals/coreconnector/command/CommandRegister.java @@ -1,7 +1,6 @@ package com.sekwah.advancedportals.coreconnector.command; import com.sekwah.advancedportals.core.commands.CommandTemplate; -import com.sekwah.advancedportals.core.commands.PortalCommand; /** * Register the CommandTemplate files to the appropriate system diff --git a/src/com/sekwah/advancedportals/coreconnector/container/CommandSenderContainer.java b/src/com/sekwah/advancedportals/coreconnector/container/CommandSenderContainer.java index b4b037a..afe6cfd 100644 --- a/src/com/sekwah/advancedportals/coreconnector/container/CommandSenderContainer.java +++ b/src/com/sekwah/advancedportals/coreconnector/container/CommandSenderContainer.java @@ -1,4 +1,8 @@ package com.sekwah.advancedportals.coreconnector.container; +import com.sekwah.advancedportals.core.util.Lang; + public class CommandSenderContainer { + public void sendMessage(String message) { + } } From 310740879ee2c6241c7846354d537e1bbb9f8e97 Mon Sep 17 00:00:00 2001 From: sekwah Date: Mon, 22 Jan 2018 05:12:53 +0000 Subject: [PATCH 3/4] Added loops for the subcommands and more to the todo --- TODO.md | 2 ++ .../core/commands/CommandWithSubCommands.java | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/TODO.md b/TODO.md index 33ae588..d5d8975 100644 --- a/TODO.md +++ b/TODO.md @@ -6,6 +6,8 @@ Tag registration system (Mostly done, just needs to be tested and implemented) Portal loading system +Portal trigger system + Configs for language files to allow all messages to change Destination Command diff --git a/src/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java b/src/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java index 0a556e1..0755ebe 100644 --- a/src/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java +++ b/src/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java @@ -37,10 +37,15 @@ public class CommandWithSubCommands implements CommandTemplate { public void onCommand(CommandSenderContainer sender, String commandExecuted, String[] args) { if(args.length > 0) { if(args[0].equalsIgnoreCase("help")) { - // TODO do help menu here sorted alphabetically + // TODO start making a help menu } else { - + for(String subCommand : this.subCommandRegistry.getSubCommands()) { + if(subCommand.equalsIgnoreCase(args[0])) { + this.getSubCommand(subCommand).onCommand(sender, args); + return; + } + } } } else { @@ -50,6 +55,13 @@ public class CommandWithSubCommands implements CommandTemplate { @Override public List onTabComplete(CommandSenderContainer sender, String[] args) { + if(args.length > 0) { + for (String subCommand : this.subCommandRegistry.getSubCommands()) { + if (subCommand.equalsIgnoreCase(args[0])) { + return this.getSubCommand(subCommand).onTabComplete(sender, args); + } + } + } return null; } } From d8acf54d40c888bf1ca1da90608dd758dd0eca17 Mon Sep 17 00:00:00 2001 From: sekwah Date: Mon, 22 Jan 2018 20:59:39 +0000 Subject: [PATCH 4/4] Started help menu, not sure exactly what it looks like yet --- Resources/lang/en_GB.lang | 7 ++- .../core/api/commands/SubCommand.java | 2 + .../core/commands/CommandWithSubCommands.java | 61 ++++++++++++++++--- .../advancedportals/core/util/Lang.java | 8 +-- 4 files changed, 65 insertions(+), 13 deletions(-) diff --git a/Resources/lang/en_GB.lang b/Resources/lang/en_GB.lang index 476ba64..52a7ee7 100644 --- a/Resources/lang/en_GB.lang +++ b/Resources/lang/en_GB.lang @@ -19,4 +19,9 @@ logger.pluginenable=Advanced portals have been successfully enabled! logger.plugindisable=Advanced portals are being disabled! logger.plugincrafterror=This version of craftbukkit is not yet supported or something went wrong, please post this message with the version number and the above stacktrace in an issue on GitHub v:%1$s -command.noargs=%1$s Sorry but you need to specify a sub command, please use \u00Ae/%2$s help \u00Acif you would like a list of possible sub commands. +command.noargs=Sorry but you need to specify a sub command, please use \u00Ae/%1$s help \u00Acif you would like a list of possible sub commands. + +command.help.header=\u00Ae--------- \u00Aa%1$s Help - Page %2$s of %3$s\u00Ae --------------- +command.help.invalidnum=Sorry but \u00Ae%1$s\u00Ac is not a valid page number. + +command.subcommand.nopermission=Sorry but you don't have permission for that, please use \u00Ae/%1$s help \u00Acif you would like a list of possible sub commands. diff --git a/src/com/sekwah/advancedportals/core/api/commands/SubCommand.java b/src/com/sekwah/advancedportals/core/api/commands/SubCommand.java index 00956dd..a7131ff 100644 --- a/src/com/sekwah/advancedportals/core/api/commands/SubCommand.java +++ b/src/com/sekwah/advancedportals/core/api/commands/SubCommand.java @@ -18,6 +18,8 @@ public interface SubCommand { */ boolean onCommand(CommandSenderContainer sender, String[] args); + boolean hasPermission(CommandSenderContainer sender); + /** * * diff --git a/src/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java b/src/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java index 0755ebe..f8dbecb 100644 --- a/src/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java +++ b/src/com/sekwah/advancedportals/core/commands/CommandWithSubCommands.java @@ -12,6 +12,8 @@ public class CommandWithSubCommands implements CommandTemplate { private final SubCommandRegistry subCommandRegistry; + private final int subCommandsPerPage = 5; + public CommandWithSubCommands() { this.subCommandRegistry = new SubCommandRegistry(); } @@ -37,31 +39,74 @@ public class CommandWithSubCommands implements CommandTemplate { public void onCommand(CommandSenderContainer sender, String commandExecuted, String[] args) { if(args.length > 0) { if(args[0].equalsIgnoreCase("help")) { - // TODO start making a help menu + int helpPage = 1; + String[] subCommands = this.subCommandRegistry.getSubCommands().toArray(new String[0]); + int pages = (int) Math.ceil(subCommands.length / (float) this.subCommandsPerPage); + if(args.length > 1) { + try { + helpPage = Integer.parseInt(args[1]); + if(helpPage < 0) { + helpPage = 1; + } + else if(helpPage > pages) { + helpPage = pages; + } + } + catch(NumberFormatException e) { + sender.sendMessage(Lang.translateInsertVariablesColor("command.help.invalidnum", args[1])); + return; + } + } + sender.sendMessage(Lang.translateInsertVariablesColor("command.help.header", commandExecuted, helpPage, pages)); + int subCommandOffset = (helpPage - 1) * this.subCommandsPerPage; + int displayEnd = subCommandOffset + this.subCommandsPerPage; + if(displayEnd > subCommands.length) { + displayEnd = subCommands.length; + } + for(; subCommandOffset < displayEnd; subCommandOffset++) { + sender.sendMessage("\u00A76/" + commandExecuted + " " + subCommands[subCommandOffset] + + "\u00A7a - " + this.getSubCommand(subCommands[subCommandOffset]).getBasicHelpText()); + } } else { - for(String subCommand : this.subCommandRegistry.getSubCommands()) { - if(subCommand.equalsIgnoreCase(args[0])) { - this.getSubCommand(subCommand).onCommand(sender, args); + for(String subCommandName : this.subCommandRegistry.getSubCommands()) { + if(subCommandName.equalsIgnoreCase(args[0])) { + SubCommand subCommand = this.getSubCommand(subCommandName); + if(subCommand.hasPermission(sender)) { + subCommand.onCommand(sender, args); + } + else { + sender.sendMessage(Lang.translateColor("messageprefix.negative") + Lang.translateInsertVariablesColor("command.subcommand.nopermission", + commandExecuted)); + } return; } } } } else { - sender.sendMessage(Lang.translateInsertVariablesColor(Lang.translate("command.noargs"), Lang.translate("messageprefix.negative"), commandExecuted)); + sender.sendMessage(Lang.translateColor("messageprefix.negative") + Lang.translateInsertVariablesColor("command.noargs", commandExecuted)); } } @Override public List onTabComplete(CommandSenderContainer sender, String[] args) { if(args.length > 0) { - for (String subCommand : this.subCommandRegistry.getSubCommands()) { - if (subCommand.equalsIgnoreCase(args[0])) { - return this.getSubCommand(subCommand).onTabComplete(sender, args); + for (String subCommandName : this.subCommandRegistry.getSubCommands()) { + if (subCommandName.equalsIgnoreCase(args[0])) { + SubCommand subCommand = this.getSubCommand(subCommandName); + if(subCommand.hasPermission(sender)) { + this.getSubCommand(subCommandName).onTabComplete(sender, args); + } + else { + return null; + } } } } + else { + return this.subCommandRegistry.getSubCommands(); + } return null; } } diff --git a/src/com/sekwah/advancedportals/core/util/Lang.java b/src/com/sekwah/advancedportals/core/util/Lang.java index 73383f5..2342040 100644 --- a/src/com/sekwah/advancedportals/core/util/Lang.java +++ b/src/com/sekwah/advancedportals/core/util/Lang.java @@ -39,18 +39,18 @@ public class Lang { } } - public static String translateInsertVariables(String s, String... args) { + public static String translateInsertVariables(String s, Object... args) { String translation = instance.translate(s); for (int i = 1; i <= args.length; i++) { - translation = translation.replaceAll("%" + i + "$s", args[i]); + translation = translation.replaceAll("%" + i + "$s", args[i].toString()); } return translation; } - public static String translateInsertVariablesColor(String s, String... args) { + public static String translateInsertVariablesColor(String s, Object... args) { String translation = instance.translateColor(s); for (int i = 1; i <= args.length; i++) { - translation = translation.replaceAll("%" + i + "$s", args[i]); + translation = translation.replaceAll("%" + i + "$s", args[i].toString()); } return translation; }