Started designing the start of commands

This commit is contained in:
sekwah 2018-01-22 05:00:35 +00:00
parent 56d46a763f
commit 970db237de
14 changed files with 113 additions and 92 deletions

View File

@ -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.

View File

@ -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() {

View File

@ -28,8 +28,13 @@ public interface SubCommand {
List<String> 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();
}

View File

@ -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 {
}

View File

@ -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 {
}

View File

@ -17,37 +17,35 @@ import java.util.Map;
*/
public class SubCommandRegistry {
private Map<String, SubCommand> subCommandMap = new HashMap<>();
private static final SubCommandRegistry instance = new SubCommandRegistry();
protected Map<String, SubCommand> subCommandMap = new HashMap<>();
/**
* List of subcommand names which should be in order alphabetically
*/
private ArrayList<String> subCommands = new ArrayList<>();
protected ArrayList<String> 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<String> getSubCommands(){
return instance.subCommands;
public ArrayList<String> 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;
}

View File

@ -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<String> onTabComplete(CommandHandler sender, String[] args);
List<String> onTabComplete(CommandSenderContainer sender, String[] args);
}

View File

@ -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<String> 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<String> onTabComplete(CommandSenderContainer sender, String[] args) {
return null;
}
}

View File

@ -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<String> onTabComplete(CommandHandler sender, String[] args) {
return null;
}
}

View File

@ -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<String> onTabComplete(CommandHandler sender, String[] args) {
return null;
}
}

View File

@ -19,6 +19,8 @@ public class Config {
private String selectionBlock = "STAINED_GLASS";
private String translationFile = "en_GB";
private int blockSubID = 14;
public boolean getUseOnlySpecialAxe() {

View File

@ -33,7 +33,10 @@ public class DataStorage {
}
public <T> T loadJson(Class<T> dataHolder, String location) {
// TODO get json
return this.loadJson(dataHolder, location, false);
}
public <T> T loadJson(Class<T> dataHolder, String location, boolean loadDefaultIfMissing) {
// TODO get json and if file doesnt exist create default class if true
return gson.fromJson("", dataHolder);
}

View File

@ -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

View File

@ -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) {
}
}