Commit for archival reasons.
This commit is contained in:
43
src/main/java/xyz/reslate/mcswebapi/MCSWebAPI.java
Normal file
43
src/main/java/xyz/reslate/mcswebapi/MCSWebAPI.java
Normal file
@@ -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();
|
||||
}
|
||||
|
||||
}
|
@@ -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<WebControllable> 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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -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<String> 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);
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
5
src/main/resources/plugin.yml
Normal file
5
src/main/resources/plugin.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
main: xyz.reslate.mcswebapi.MCSWebAPI
|
||||
name: MCSWebAPI
|
||||
version: 0.0.1
|
||||
author: reslate
|
||||
description: "A Minecraft Server Web API."
|
38
src/test/java/xyz/reslate/mcswebapi/AppTest.java
Normal file
38
src/test/java/xyz/reslate/mcswebapi/AppTest.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package xyz.reslate.mcswebapi;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user