From 182fdb846d755480e1dc1128bf596a52aee28334 Mon Sep 17 00:00:00 2001 From: Sekwah Date: Sun, 24 Apr 2022 03:30:26 +0100 Subject: [PATCH] refactor: reorganise the package structure --- build.gradle | 7 + .../core/AdvancedPortalsCore.java | 8 +- .../core/config/CoreModule.java | 33 +++++ .../core/data/DataStorage.java | 138 +++++++++++++++++- .../advancedportals/core/data/DataTag.java | 13 ++ .../com/sekwah/advancedportals/util/Lang.java | 16 +- core/src/main/resources/plugin.yml | 52 ------- gradle.properties | 2 +- 8 files changed, 212 insertions(+), 57 deletions(-) create mode 100644 core/src/main/java/com/sekwah/advancedportals/core/config/CoreModule.java create mode 100644 core/src/main/java/com/sekwah/advancedportals/core/data/DataTag.java delete mode 100644 core/src/main/resources/plugin.yml diff --git a/build.gradle b/build.gradle index e91fc7e0..fbacc733 100644 --- a/build.gradle +++ b/build.gradle @@ -59,6 +59,13 @@ allprojects { options.encoding = 'UTF-8' } + if (project.name != "advanced-portals") { + task buildSubmodules doLast { + task -> println "Building $task.project.name" + } + buildSubmodule.finalizedBy build + } + sourceCompatibility = 1.8 targetCompatibility = 1.8 } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java b/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java index 390c28bc..74342239 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java @@ -1,10 +1,16 @@ package com.sekwah.advancedportals.core; +import com.google.inject.Guice; import com.google.inject.Injector; +import com.sekwah.advancedportals.core.config.CoreModule; public class AdvancedPortalsCore { - private Injector injector; + /** + * https://github.com/google/guice/wiki/GettingStarted + * + */ + private Injector injector = Guice.createInjector(new CoreModule(this)); /** * For some platforms we could do this on construction but this just allows for a bit more control diff --git a/core/src/main/java/com/sekwah/advancedportals/core/config/CoreModule.java b/core/src/main/java/com/sekwah/advancedportals/core/config/CoreModule.java new file mode 100644 index 00000000..db9e45d2 --- /dev/null +++ b/core/src/main/java/com/sekwah/advancedportals/core/config/CoreModule.java @@ -0,0 +1,33 @@ +package com.sekwah.advancedportals.core.config; + +import com.google.inject.AbstractModule; +import com.google.inject.Provides; +import com.sekwah.advancedportals.core.AdvancedPortalsCore; + +public class CoreModule extends AbstractModule { + + private final AdvancedPortalsCore portalsCore; + + /** + * Parts provided by the core module. Check the implementation for its individual integrations. + * @param portalsCore + */ + public CoreModule(AdvancedPortalsCore portalsCore) { + this.portalsCore = portalsCore; + } + + @Override + protected void configure() { + // bind(IPortalRepository.class).to(PortalRepository.class).in(Scopes.SINGLETON); + // bind(IDestinationRepository.class).to(DestinationRepository.class).in(Scopes.SINGLETON); + // bind(IPortalRepository.class).to(PortalRepository.class).in(Scopes.SINGLETON); + // bind(ConfigRepository.class).to(ConfigRepositoryImpl.class).in(Scopes.SINGLETON); + //bindListener(Matchers.Any(), new Log4JTypeListenr()); + } + + // https://github.com/google/guice/wiki/GettingStarted + @Provides + AdvancedPortalsCore providePortalsCore() { + return this.portalsCore; + } +} diff --git a/core/src/main/java/com/sekwah/advancedportals/core/data/DataStorage.java b/core/src/main/java/com/sekwah/advancedportals/core/data/DataStorage.java index 77f5b87f..ea323b2e 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/data/DataStorage.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/data/DataStorage.java @@ -2,12 +2,148 @@ package com.sekwah.advancedportals.core.data; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.inject.Inject; +import com.sekwah.advancedportals.core.AdvancedPortalsCore; +import com.sekwah.advancedportals.util.InfoLogger; -import java.io.File; +import java.io.*; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Type; public class DataStorage { private Gson gson = new GsonBuilder().setPrettyPrinting().create(); private File dataFolder; + + @Inject + private AdvancedPortalsCore portalsCore; + + @Inject + private InfoLogger infoLogger; + + public DataStorage(File dataStorageLoc) { + this.dataFolder = dataStorageLoc; + } + + /** + * Copies the default file, defaults to true to keep true to the name + * + * @param fileLoc + * @return + */ + public boolean copyDefaultFile(String fileLoc) { + return this.copyDefaultFile(fileLoc, true); + } + + public void copyDefaultFiles(boolean override, String... fileLocs) { + for (String fileLoc : fileLocs) { + this.copyDefaultFile(fileLoc, override); + } + } + + public T loadJson(Type dataHolder, String location) { + InputStream jsonResource = this.loadResource(location); + if(jsonResource == null) { + return null; + } + BufferedReader bufReader = new BufferedReader(new InputStreamReader(jsonResource)); + T object = gson.fromJson(bufReader, dataHolder); + return object; + } + public T loadJson(Class dataHolder, String location) { + InputStream jsonResource = this.loadResource(location); + if(jsonResource == null) { + try { + return dataHolder.getDeclaredConstructor().newInstance(); + } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + e.printStackTrace(); + } + return null; + } + BufferedReader bufReader = new BufferedReader(new InputStreamReader(jsonResource)); + return gson.fromJson(bufReader, dataHolder); + } + + public void storeJson(Object dataHolder, String location) { + String json = gson.toJson(dataHolder); + try { + FileWriter fileWriter = new FileWriter(new File(this.dataFolder, location)); + fileWriter.write(json); + fileWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Copies the specified file out of the plugin and into the plugins folder. + * + * @param fileLoc + * @return if the file is copied, will be false if override is false and the file already existed. + */ + public boolean copyDefaultFile(String fileLoc, boolean overwrite) { + File outFile = new File(this.dataFolder, fileLoc); + if (!outFile.exists()) { + outFile.getParentFile().mkdirs(); + } + if (!outFile.exists() || overwrite) { + try { + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileLoc); + if(inputStream == null) { + return false; + } + + FileOutputStream outStream = new FileOutputStream(outFile); + + byte[] buf = new byte[1024]; + int len; + while ((len = inputStream.read(buf)) > 0) { + outStream.write(buf, 0, len); + } + inputStream.close(); + outStream.close(); + } catch (NullPointerException e) { + e.printStackTrace(); + this.infoLogger.logWarning("Could not load " + fileLoc + ". The file does" + + "not exist or there has been an error reading the file."); + return false; + } catch (FileNotFoundException e) { + e.printStackTrace(); + this.infoLogger.logWarning("Could not create " + fileLoc); + } catch (IOException e) { + e.printStackTrace(); + this.infoLogger.logWarning("File error reading " + fileLoc); + } + } + return true; + } + + /** + * A method to try to grab the files from the plugin and if its in the plugin folder load from there instead. + *

+ * @param location + * @return + */ + public InputStream loadResource(String location) { + File inFile = new File(dataFolder, location); + if (inFile.exists() && !inFile.isDirectory()) { + try { + return new FileInputStream(inFile); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return null; + } + } else { + try { + copyDefaultFile(location, false); + return this.getClass().getClassLoader().getResourceAsStream(location); + } catch (NullPointerException e) { + e.printStackTrace(); + this.infoLogger.logWarning("Could not load " + location + ". The file does" + + "not exist or there has been an error reading the file."); + return null; + } + } + } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/data/DataTag.java b/core/src/main/java/com/sekwah/advancedportals/core/data/DataTag.java new file mode 100644 index 00000000..6e55b31e --- /dev/null +++ b/core/src/main/java/com/sekwah/advancedportals/core/data/DataTag.java @@ -0,0 +1,13 @@ +package com.sekwah.advancedportals.core.data; + +public class DataTag { + + public final String NAME; + public final String VALUE; + + public DataTag(String argName, String value) { + this.NAME = argName; + this.VALUE = value; + } + +} diff --git a/core/src/main/java/com/sekwah/advancedportals/util/Lang.java b/core/src/main/java/com/sekwah/advancedportals/util/Lang.java index 67e903f1..dbd89489 100644 --- a/core/src/main/java/com/sekwah/advancedportals/util/Lang.java +++ b/core/src/main/java/com/sekwah/advancedportals/util/Lang.java @@ -1,6 +1,8 @@ package com.sekwah.advancedportals.util; +import com.google.inject.Inject; import com.sekwah.advancedportals.core.AdvancedPortalsCore; +import com.sekwah.advancedportals.core.data.DataStorage; import java.io.IOException; import java.io.InputStream; @@ -22,6 +24,16 @@ public class Lang { private static final Lang instance = new Lang(); private final HashMap languageMap = new HashMap<>(); + + @Inject + private AdvancedPortalsCore portalsCore; + + @Inject + private DataStorage dataStorage; + + @Inject + private InfoLogger infoLogger; + //private final String DEFAULT_LANG = "en_GB"; /*public Lang() { @@ -67,7 +79,7 @@ public class Lang { //URL url = lang.getClass().getClassLoader().getResource("lang/" + fileName + ".lang"); //System.out.println(url); //Map newLangMap = lang.parseLang(url.openStream()); - InputStream stream = AdvancedPortalsCore.getInstance().getDataStorage().loadResource("lang/" + fileName + ".lang"); + InputStream stream = this.dataStorage.loadResource("lang/" + fileName + ".lang"); if (stream != null) { Map newLangMap = lang.parseLang(stream); if (newLangMap != null) { @@ -76,7 +88,7 @@ public class Lang { } } catch (NullPointerException e) { e.printStackTrace(); - AdvancedPortalsCore.getInstance().getInfoLogger().logWarning("Could not load " + fileName + ".lang The file does" + + this.infoLogger.logWarning("Could not load " + fileName + ".lang The file does" + "not exist or there has been an error reading the file. Canceled loading language file."); } } diff --git a/core/src/main/resources/plugin.yml b/core/src/main/resources/plugin.yml deleted file mode 100644 index 8375320e..00000000 --- a/core/src/main/resources/plugin.yml +++ /dev/null @@ -1,52 +0,0 @@ -main: com.sekwah.advancedportals.spigot.AdvancedPortalsPlugin -name: AdvancedPortals -version: 1.0.0 -author: sekwah41 -description: An advanced portals plugin for bukkit. -api-version: 1.13 -commands: - portal: - description: The main command for the advanced portals - aliases: [portals, aportals, advancedportals] - usage: / - destination: - description: Can be used to access portal destinations. - aliases: [desti] - usage: / -permissions: - advancedportals.*: - description: Gives access to all commands - default: op - children: - advancedportals.createportal: true - advancedportals.portal: true - advancedportals.build: true - advancedportals.desti: true - advancedportals.createportal: - description: Allows you to create portals - default: op - advancedportals.createportal.commandlevel.*: - description: Gives access to all level raisers - default: false - children: - advancedportals.createportal.commandlevel.op: true - advancedportals.createportal.commandlevel.perms: true - advancedportals.createportal.commandlevel.console: true - advancedportals.createportal.commandlevel.op: - description: Allows you to increase the users level temporaily to op - default: false - advancedportals.createportal.commandlevel.perms: - description: Allows you to increase the users level temporaily to have all perms - default: false - advancedportals.createportal.commandlevel.console: - description: Executes command in the console - default: false - advancedportals.portal: - description: Allows use of portal commands - default: op - advancedportals.build: - description: Allows you to build in the portal regions - default: op - advancedportals.desti: - description: Gives access to all desti commands - default: op diff --git a/gradle.properties b/gradle.properties index 4f29706e..201ce75d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,7 +1,7 @@ z# https://docs.gradle.org/current/userguide/build_environment.html # Disable with --no-build-cache org.gradle.caching=true -version=1.0.0-0 +version=1.0.0 github=https://github.com/sekwah41/Advanced-Portals curse_project_id=86001