From 56e0c8f22a7242ef1270986efadcb2810877cbc1 Mon Sep 17 00:00:00 2001 From: Harrison Deng Date: Mon, 16 May 2022 01:33:14 -0500 Subject: [PATCH] Commit for archival reasons. --- .vscode/launch.json | 22 ++++++ .vscode/settings.json | 6 ++ .vscode/tasks.json | 45 +++++++++++ Jenkinsfile | 10 +++ mcswebapi/pom.xml | 17 ---- .../main/java/xyz/reslate/mcswebapi/App.java | 13 ---- pom.xml | 78 +++++++++++++++++++ .../java/xyz/reslate/mcswebapi/MCSWebAPI.java | 43 ++++++++++ .../controllers/ControllerGroup.java | 22 ++++++ .../controllers/PublicServerInformation.java | 38 +++++++++ .../controllers/WebControllable.java | 11 +++ src/main/resources/plugin.yml | 5 ++ .../java/xyz/reslate/mcswebapi/AppTest.java | 0 13 files changed, 280 insertions(+), 30 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json create mode 100644 Jenkinsfile delete mode 100644 mcswebapi/pom.xml delete mode 100644 mcswebapi/src/main/java/xyz/reslate/mcswebapi/App.java create mode 100644 pom.xml create mode 100644 src/main/java/xyz/reslate/mcswebapi/MCSWebAPI.java create mode 100644 src/main/java/xyz/reslate/mcswebapi/controllers/ControllerGroup.java create mode 100644 src/main/java/xyz/reslate/mcswebapi/controllers/PublicServerInformation.java create mode 100644 src/main/java/xyz/reslate/mcswebapi/controllers/WebControllable.java create mode 100644 src/main/resources/plugin.yml rename {mcswebapi/src => src}/test/java/xyz/reslate/mcswebapi/AppTest.java (100%) diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..927caa9 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,22 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "Launch App", + "request": "launch", + "mainClass": "xyz.reslate.mcswebapi.App", + "projectName": "mcswebapi" + }, + { + "type": "java", + "name": "Attach", + "request": "attach", + "hostName": "localhost", + "port": "25577" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..175c09a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "java.configuration.updateBuildConfiguration": "automatic", + "yaml.schemas": { + "https://json.schemastore.org/bukkit-plugin.json": "file:///home/ydeng/MCSWebAPI/plugin.yml" + } +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..9b46c86 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,45 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "verify", + "type": "shell", + "command": "mvn -B verify", + "group": "build" + }, + { + "label": "package", + "type": "shell", + "command": "mvn -B package", + "group": "build" + }, + { + "label": "test", + "type": "shell", + "command": "mvn -B test", + "group": "test" + }, + { + "label": "load latest build", + "type": "shell", + "command": "python3", + "args": [ + "${workspaceFolder}/SpigotPluginBaseTools/load_plugin.py" + ], + "dependsOn": "package", + "problemMatcher": [] + }, + { + "label": "launch dev. server", + "type": "shell", + "command": "python3", + "dependsOn": "load latest build", + "args": [ + "${workspaceFolder}/SpigotPluginBaseTools/start_server.py" + ], + "problemMatcher": [] + }, + ] +} \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..93ab1eb --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,10 @@ +pipeline { + agent any + stages { + stage('tests') { + steps { + sh 'mvn -B test' + } + } + } +} \ No newline at end of file diff --git a/mcswebapi/pom.xml b/mcswebapi/pom.xml deleted file mode 100644 index 310da05..0000000 --- a/mcswebapi/pom.xml +++ /dev/null @@ -1,17 +0,0 @@ - - 4.0.0 - xyz.reslate.mcswebapi - mcswebapi - jar - 1.0-SNAPSHOT - mcswebapi - http://maven.apache.org - - - junit - junit - 3.8.1 - test - - org.spigotmcspigot-apiLATESTjarprovided -spigot-repohttps://hub.spigotmc.org/nexus/content/repositories/public/ \ No newline at end of file diff --git a/mcswebapi/src/main/java/xyz/reslate/mcswebapi/App.java b/mcswebapi/src/main/java/xyz/reslate/mcswebapi/App.java deleted file mode 100644 index 1afd183..0000000 --- a/mcswebapi/src/main/java/xyz/reslate/mcswebapi/App.java +++ /dev/null @@ -1,13 +0,0 @@ -package xyz.reslate.mcswebapi; - -/** - * Hello world! - * - */ -public class App -{ - public static void main( String[] args ) - { - System.out.println( "Hello World!" ); - } -} diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7c84593 --- /dev/null +++ b/pom.xml @@ -0,0 +1,78 @@ + + 4.0.0 + xyz.reslate.mcswebapi + mcswebapi + jar + 1.0-SNAPSHOT + + 17 + 17 + + mcswebapi + http://maven.apache.org + + + + org.reflections + reflections + 0.10.2 + + + + + org.pac4j + javalin-pac4j + 4.0.0 + + + + + io.javalin + javalin + 4.3.0 + + + + + org.pac4j + pac4j-oauth + 5.3.0 + + + + + org.junit.jupiter + junit-jupiter + 5.8.2 + test + + + + + org.spigotmc + spigot-api + LATEST + jar + provided + + + + + + + maven-surefire-plugin + 2.22.2 + + + maven-failsafe-plugin + 2.22.2 + + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/public/ + + + \ No newline at end of file diff --git a/src/main/java/xyz/reslate/mcswebapi/MCSWebAPI.java b/src/main/java/xyz/reslate/mcswebapi/MCSWebAPI.java new file mode 100644 index 0000000..7aadc14 --- /dev/null +++ b/src/main/java/xyz/reslate/mcswebapi/MCSWebAPI.java @@ -0,0 +1,43 @@ +package xyz.reslate.mcswebapi; + +import java.util.logging.Logger; + +import org.bukkit.plugin.java.JavaPlugin; + +import io.javalin.Javalin; +import xyz.reslate.mcswebapi.controllers.ControllerGroup; +import xyz.reslate.mcswebapi.controllers.PublicServerInformation; + +public class MCSWebAPI extends JavaPlugin +{ + private Javalin javalin; + + @Override + public void onEnable() { + Logger logger = getLogger(); + logger.info("Enabling MCSWebAPI..."); + javalin = Javalin.create(); + javalin.start(8080); + prepareControllers(javalin); + super.onEnable(); + logger.info("Enabled MCSWebAPI."); + } + + @Override + public void onDisable() { + Logger logger = getLogger(); + logger.info("Disabling MCSWebAPI..."); + javalin.close(); + super.onDisable(); + logger.info("Disabled MCSWebAPI."); + } + + + public void prepareControllers(Javalin javalin) { + ControllerGroup controllerGroup = new ControllerGroup(javalin, + new PublicServerInformation() + ); + controllerGroup.loadAll(); + } + +} diff --git a/src/main/java/xyz/reslate/mcswebapi/controllers/ControllerGroup.java b/src/main/java/xyz/reslate/mcswebapi/controllers/ControllerGroup.java new file mode 100644 index 0000000..493497e --- /dev/null +++ b/src/main/java/xyz/reslate/mcswebapi/controllers/ControllerGroup.java @@ -0,0 +1,22 @@ +package xyz.reslate.mcswebapi.controllers; + +import java.util.Arrays; +import java.util.HashSet; + +import io.javalin.Javalin; + +public class ControllerGroup { + private final HashSet controllers; + private final Javalin javalin; + public ControllerGroup(Javalin javalin, WebControllable... controllers) { + super(); + this.javalin = javalin; + this.controllers = new HashSet<>(Arrays.asList(controllers)); + } + + public void loadAll() { + for (WebControllable controller : controllers) { + controller.register(javalin); + } + } +} diff --git a/src/main/java/xyz/reslate/mcswebapi/controllers/PublicServerInformation.java b/src/main/java/xyz/reslate/mcswebapi/controllers/PublicServerInformation.java new file mode 100644 index 0000000..1065a35 --- /dev/null +++ b/src/main/java/xyz/reslate/mcswebapi/controllers/PublicServerInformation.java @@ -0,0 +1,38 @@ +package xyz.reslate.mcswebapi.controllers; + +import java.util.ArrayList; + +import org.bukkit.Bukkit; + +import io.javalin.Javalin; +import io.javalin.http.Context; + +public class PublicServerInformation implements WebControllable { + private String route = "/api/GeneralServerInfo"; + + public void getNumPlayersOnline(Context ctx) { + ctx.json(Bukkit.getOnlinePlayers().size()); + } + + public void getMaximumNumPlayers(Context ctx) { + ctx.json(Bukkit.getMaxPlayers()); + } + + public void getServerVersion(Context ctx) { + ctx.json(Bukkit.getBukkitVersion()); + } + + public void getWhitelistedPlayers(Context ctx) { + ArrayList playerNames = new ArrayList<>(); + Bukkit.getWhitelistedPlayers().forEach(player -> playerNames.add(player.getName())); + ctx.json(playerNames); + } + + @Override + public void register(Javalin javalin) { + javalin.get(route + "/numPlayersOnline", this::getNumPlayersOnline); + javalin.get(route + "/maximumNumPlayers", this::getMaximumNumPlayers); + javalin.get(route + "/serverVersion", this::getServerVersion); + javalin.get(route + "/whitelistedPlayernames", this::getWhitelistedPlayers); + } +} diff --git a/src/main/java/xyz/reslate/mcswebapi/controllers/WebControllable.java b/src/main/java/xyz/reslate/mcswebapi/controllers/WebControllable.java new file mode 100644 index 0000000..a42c206 --- /dev/null +++ b/src/main/java/xyz/reslate/mcswebapi/controllers/WebControllable.java @@ -0,0 +1,11 @@ +package xyz.reslate.mcswebapi.controllers; + +import io.javalin.Javalin; + +public interface WebControllable { + /** + * Register the endpoints with their respective functions. + * @param javalin The running Javalin instance these endpoints will be hosted on. + */ + public void register(Javalin javalin); +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..f179739 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,5 @@ +main: xyz.reslate.mcswebapi.MCSWebAPI +name: MCSWebAPI +version: 0.0.1 +author: reslate +description: "A Minecraft Server Web API." \ No newline at end of file diff --git a/mcswebapi/src/test/java/xyz/reslate/mcswebapi/AppTest.java b/src/test/java/xyz/reslate/mcswebapi/AppTest.java similarity index 100% rename from mcswebapi/src/test/java/xyz/reslate/mcswebapi/AppTest.java rename to src/test/java/xyz/reslate/mcswebapi/AppTest.java