refactor: reorganise the package structure

This commit is contained in:
Sekwah 2022-04-23 16:07:16 +01:00
parent b63169ca58
commit 47ca225be7
No known key found for this signature in database
GPG Key ID: 9E0D654FC942286D
13 changed files with 281 additions and 40 deletions

View File

@ -6,6 +6,9 @@ public class AdvancedPortalsCore {
private Injector injector;
/**
* For some platforms we could do this on construction but this just allows for a bit more control
*/
public void onEnable() {
AdvancedPortalsModule module = new AdvancedPortalsModule(this);
injector = module.getInjector();

View File

@ -0,0 +1,24 @@
package com.sekwah.advancedportals.core.commands;
import com.sekwah.advancedportals.core.connector.containers.CommandSenderContainer;
import java.util.List;
/**
* Already know spigot's auto complete possibilities
*
* Sponge https://docs.spongepowered.org/stable/en/plugin/commands/arguments.html#custom-command-elements
*/
public interface CommandTemplate {
void onCommand(CommandSenderContainer sender, String commandExecuted, String[] args);
/**
* Fired when someone asks for a tab complete action.
* @param sender whoever triggered the command e.g. command block, server or player
* @param args arguments for the command
* @return a lot of strings that are possible completions
*/
List<String> onTabComplete(CommandSenderContainer sender, String[] args);
}

View File

@ -1,36 +1,36 @@
package com.sekwah.advancedportals.core.commands;
import com.sekwah.advancedportals.connector.container.CommandSenderContainer;
import java.util.List;
public interface SubCommand {
/**
* @param sender
* @param args arguments including the subcommand that has been specified.
* @return if the command has worked (if false it will just display a message from the command suggesting to check help)
*/
void onCommand(CommandSenderContainer sender, String[] args);
boolean hasPermission(CommandSenderContainer sender);
/**
*
*
* @param sender
* @param args arguments including the subcommand that has been specified.
* @return tab completion for the subcommand
*/
List<String> onTabComplete(CommandSenderContainer sender, String[] args);
/**
* @return the string to show next to the tag on the help menu.
*/
String getBasicHelpText();
/**
* @return the string to show if help then the tag is listed.
*/
String getDetailedHelpText();
}
package com.sekwah.advancedportals.core.commands;
import com.sekwah.advancedportals.core.connector.containers.CommandSenderContainer;
import java.util.List;
public interface SubCommand {
/**
* @param sender
* @param args arguments including the subcommand that has been specified.
* @return if the command has worked (if false it will just display a message from the command suggesting to check help)
*/
void onCommand(CommandSenderContainer sender, String[] args);
boolean hasPermission(CommandSenderContainer sender);
/**
*
*
* @param sender
* @param args arguments including the subcommand that has been specified.
* @return tab completion for the subcommand
*/
List<String> onTabComplete(CommandSenderContainer sender, String[] args);
/**
* @return the string to show next to the tag on the help menu.
*/
String getBasicHelpText();
/**
* @return the string to show if help then the tag is listed.
*/
String getDetailedHelpText();
}

View File

@ -0,0 +1,14 @@
package com.sekwah.advancedportals.core.connector.commands;
import com.sekwah.advancedportals.core.commands.CommandTemplate;
public abstract class CommandHandler {
private final CommandTemplate commandExecutor;
public CommandHandler(CommandTemplate commandExecutor) {
this.commandExecutor = commandExecutor;
}
}

View File

@ -0,0 +1,14 @@
package com.sekwah.advancedportals.core.connector.commands;
import com.sekwah.advancedportals.core.commands.CommandTemplate;
public interface CommandRegister {
/**
* Registers the command to the appropriate system
* @param commandName
* @param commandExecutor
*/
void registerCommand(String commandName, CommandTemplate commandExecutor);
}

View File

@ -1,4 +1,4 @@
package com.sekwah.advancedportals.connector.container;
package com.sekwah.advancedportals.core.connector.containers;
public interface CommandSenderContainer {

View File

@ -1,4 +1,4 @@
package com.sekwah.advancedportals.connector.container;
package com.sekwah.advancedportals.core.connector.containers;
import com.sekwah.advancedportals.core.data.BlockLocation;
import com.sekwah.advancedportals.core.data.PlayerLocation;

View File

@ -1,4 +1,4 @@
package com.sekwah.advancedportals.connector.container;
package com.sekwah.advancedportals.core.connector.containers;
public interface ServerContainer {

View File

@ -1,4 +1,4 @@
package com.sekwah.advancedportals.connector.container;
package com.sekwah.advancedportals.core.connector.containers;
import com.sekwah.advancedportals.core.data.BlockLocation;

View File

@ -0,0 +1,16 @@
package com.sekwah.advancedportals.util;
public abstract class InfoLogger {
/**
* Problematic messages
* @param s warning message
*/
public abstract void logWarning(String s);
/**
* General information logging
* @param s info message
*/
public abstract void log(String s);
}

View File

@ -0,0 +1,112 @@
package com.sekwah.advancedportals.util;
import com.sekwah.advancedportals.core.AdvancedPortalsCore;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* @author sekwah41
* <p>
* The language translation file for the game. Will always load english first
* so that if the translations are missing any then they are still readable and can then be translated.
* (Its better than a raw translate string)
* <p>
* TODO add a loaddefault where it only loads from the plugins version of the data rather than paying attention to any
* possible changed versions in the lang folder.
*/
public class Lang {
private static final Lang instance = new Lang();
private final HashMap<String, String> languageMap = new HashMap<>();
//private final String DEFAULT_LANG = "en_GB";
/*public Lang() {
injectTranslations(this, DEFAULT_LANG);
}*/
public static void loadLanguage(String fileName) {
instance.injectTranslations(instance, fileName);
}
public static String translate(String s) {
if (instance.languageMap.containsKey(s)) {
return instance.languageMap.get(s);
} else {
return s;
}
}
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-1].toString());
}
return translation;
}
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-1].toString());
}
return translation;
}
public static String translateColor(String s) {
String translation = instance.translate(s);
translation = translation.replaceAll("\\\\u00A7", "\u00A7");
return translation;
}
private void injectTranslations(Lang lang, String fileName) {
try {
//URL url = lang.getClass().getClassLoader().getResource("lang/" + fileName + ".lang");
//System.out.println(url);
//Map<String, String> newLangMap = lang.parseLang(url.openStream());
InputStream stream = AdvancedPortalsCore.getInstance().getDataStorage().loadResource("lang/" + fileName + ".lang");
if (stream != null) {
Map<String, String> newLangMap = lang.parseLang(stream);
if (newLangMap != null) {
lang.languageMap.putAll(newLangMap);
}
}
} catch (NullPointerException e) {
e.printStackTrace();
AdvancedPortalsCore.getInstance().getInfoLogger().logWarning("Could not load " + fileName + ".lang The file does" +
"not exist or there has been an error reading the file. Canceled loading language file.");
}
}
private Map<String, String> parseLang(InputStream inputStream) {
Scanner scanner = new Scanner(inputStream, "UTF-8");
String line = getNextLine(scanner);
HashMap<String, String> newMap = new HashMap<>();
while (scanner != null && line != null) {
//System.out.println(line);
if (!line.startsWith("#") && line.indexOf('=') > -1) {
int split = line.indexOf('=');
String key = line.substring(0, split);
String value = line.substring(split + 1, line.length());
newMap.put(key, value);
}
line = getNextLine(scanner);
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return newMap;
}
private String getNextLine(Scanner scanner) {
if (scanner.hasNextLine()) {
return scanner.nextLine();
}
return null;
}
}

View File

@ -0,0 +1,51 @@
package com.sekwah.advancedportals.util;
import com.sekwah.advancedportals.core.data.DataTag;
import java.util.ArrayList;
public class TagReader {
public static ArrayList<DataTag> getTagsFromArgs(String[] args) {
ArrayList<DataTag> tags = new ArrayList<>();
boolean partingValueWithSpaces = false;
String argBeingParsed = "";
String currentParsedValue = "";
for (int i = 1; i < args.length; i++) {
if(partingValueWithSpaces) {
if(args[i].charAt(args[i].length() - 1) == '"') {
args[i] = args[i].substring(0, args[i].length() - 1);
partingValueWithSpaces = false;
tags.add(new DataTag(argBeingParsed.toLowerCase(), currentParsedValue));
}
else {
currentParsedValue += " " + args[i];
}
}
else {
String detectedTag = TagReader.getTag(args[i].toLowerCase());
if(detectedTag != null) {
String arg = args[i].substring(detectedTag.length() + 1);
if(arg.length() > 0 && arg.charAt(0) == '"') {
argBeingParsed = detectedTag;
currentParsedValue = arg;
}
else {
tags.add(new DataTag(detectedTag.toLowerCase(), arg));
}
}
}
}
return tags;
}
public static String getTag(String arg) {
int splitLoc = arg.indexOf(":");
if(splitLoc != -1) {
return arg.substring(0,splitLoc);
}
return null;
}
}

View File

@ -1,13 +1,20 @@
package com.sekwah.advancedportals.spigot;
import com.sekwah.advancedportals.core.AdvancedPortalsCore;
import com.sekwah.advancedportals.spigot.metrics.Metrics;
import org.bukkit.plugin.java.JavaPlugin;
public class AdvancedPortalsPlugin extends JavaPlugin {
private AdvancedPortalsCore portalsCore;
@Override
public void onEnable() {
this.portalsCore = new AdvancedPortalsCore();
this.portalsCore.onEnable();
new Metrics(this);
this.getServer().getConsoleSender().sendMessage("\u00A7aAdvanced portals have been successfully enabled!");