refactor: reorganise the package structure

This commit is contained in:
Sekwah 2022-04-24 03:30:26 +01:00
parent 47ca225be7
commit c5f37138e0
No known key found for this signature in database
GPG Key ID: 9E0D654FC942286D
9 changed files with 215 additions and 137 deletions

View File

@ -27,7 +27,7 @@ jobs:
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
restore-keys: ${{ runner.os }}-gradle
- name: Build with Gradle
run: ./gradlew build
run: ./gradlew buildSubmodules build
- name: Upload to Discord (If pre-release tag)
if: "startsWith(github.ref, 'refs/tags/v') && contains(github.ref, '-')"
env:

View File

@ -22,6 +22,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
}
@ -79,9 +86,9 @@ dependencies {
jar {
// Filters the files out that are in the build folders. Look to see if there is a better way to do this?
from configurations.includeLibs.filter {
it.path.contains("\\build\\libs\\")
it.path.contains("${File.separator}build${File.separator}libs")
} .collect {
println("Including: ${it.name}")
println("Will Include: ${it.name}")
it.isDirectory() ? it : zipTree(it)
}
}
@ -271,80 +278,3 @@ task curseforge {
// releaseType = 'release'
}
task copyPlugin {
doLast {
copy {
if(System.env.MC_SERVER_LOC == null) {
throw new Exception('You must set the server location and jar to use')
}
println "$buildDir/libs/Advanced-Portals-${version}.jar"
println "${System.env.MC_SERVER_LOC}/plugins/Advanced-Portals-${version}.jar"
try {
delete fileTree("${System.env.MC_SERVER_LOC}/plugins/") {
include "*.jar"
}
}
catch(RuntimeException e) {
println e.getLocalizedMessage()
}
from file("$buildDir/libs/Advanced-Portals-${version}.jar")
into file("${System.env.MC_SERVER_LOC}/plugins/")
}
}
}
// Set SPIGOT_LOC to the location of your server and SPIGOT_JAR as the name of the jar file in the server you want to run
// DIReallyKnowWhatIAmDoingISwear is to remove the stupid pause spigot has at the start
task runJar() {
doLast {
if(System.env.MC_SERVER_LOC == null || System.env.MC_SERVER_JAR == null) {
throw new Exception('You must set the server location and jar to use MC_SERVER_LOC and MC_SERVER_JAR')
}
javaexec {
main "-jar"
args "${System.env.MC_SERVER_LOC}\\${System.env.MC_SERVER_JAR}.jar"
jvmArgs = ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005", "-DIReallyKnowWhatIAmDoingISwear=true"]
workingDir "${System.env.MC_SERVER_LOC}"
}
}
}
/**
* These are needed as standard-version doesnt allow for the ability to skip tag versions for the changelog.
* Well it does but not on purpose and it breaks things.
*
* Tagging is skipped so that the release can be merged and confirmed (A little long winded but just to stop mistakes)
*/
task updateChangelog {
doLast{
exec {
commandLine 'cmd', '/c', 'npx standard-version@9.3.0 -i docs/CHANGELOG.md -t (v)[0-9]+.[0-0]+.[0-0]+(?!-) --skip.tag'
ext.output = {
return standardOutput.toString()
}
}
exec {
commandLine 'cmd', '/c', 'npx standard-version@9.3.0 --skip.changelog --skip.bump --skip.tag'
ext.output = {
return standardOutput.toString()
}
}
}
}
task updateChangelogPreRelease(type: Exec) {
commandLine 'cmd', '/c', 'npx standard-version@9.3.0 --prerelease -i docs/SNAPSHOT_CHANGELOG.md --skip.tag'
ext.output = {
return standardOutput.toString()
}
}
/**
* Just to stop having to manually tagging. Probably could do this easier but meh stops typos or other issues.
*/
task tagRelease(type: Exec) {
commandLine 'cmd', '/c', 'npx standard-version@9.3.0 --prerelease -i docs/SNAPSHOT_CHANGELOG.md --skip.changelog --skip.bump'
ext.output = {
return standardOutput.toString()
}
}

View File

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

View File

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

View File

@ -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> 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> T loadJson(Class<T> 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.
* <p>
* @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;
}
}
}
}

View File

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

View File

@ -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<String, String> 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<String, String> 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<String, String> 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.");
}
}

View File

@ -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: /<command>
destination:
description: Can be used to access portal destinations.
aliases: [desti]
usage: /<command>
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

View File

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