diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..e623c5e --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,29 @@ +name: Gradle Publish + +on: + release: + types: + - published + +jobs: + gradle-publish-paper: + name: Publish Paper Plugin + runs-on: ubuntu-latest + permissions: + packages: write + + steps: + - uses: actions/checkout@v4.1.7 + - uses: gradle/wrapper-validation-action@v3 + - name: Set up JDK + uses: actions/setup-java@v4 + with: + distribution: adopt + java-version: 21 + - name: Build with Gradle + run: ./gradlew --build-cache test build + - name: Publish + run: ./gradlew :plugin-paper:publishMavenPublicationToEldonexusRepository + env: + NEXUS_USERNAME: ${{ secrets.NEXUS_USERNAME }} + NEXUS_PASSWORD: ${{ secrets.NEXUS_PASSWORD }} diff --git a/bot/src/main/java/de/chojo/gamejam/server/TeamServer.java b/bot/src/main/java/de/chojo/gamejam/server/TeamServer.java index d0d12e4..96c3fb8 100644 --- a/bot/src/main/java/de/chojo/gamejam/server/TeamServer.java +++ b/bot/src/main/java/de/chojo/gamejam/server/TeamServer.java @@ -125,9 +125,9 @@ private void writeTemplate() throws IOException { var sourceDir = Path.of(configuration.serverTemplate().templateDir()); var symlinks = configuration.serverTemplate().symLinks() - .stream() - .map(sourceDir::resolve) - .collect(Collectors.toSet()); + .stream() + .map(sourceDir::resolve) + .collect(Collectors.toSet()); try (var files = Files.walk(sourceDir)) { for (var sourceTarget : files.toList()) { // skip root dir @@ -153,6 +153,7 @@ private void writeTemplate() throws IOException { /** * Delete all the server data. + * * @return true when server was deleted. * @throws IOException */ @@ -196,7 +197,8 @@ public boolean start() { .start(); running = true; } catch (IOException e) { - throw new RuntimeException(e); + log.error("Could not start server", e); + return false; } return true; } @@ -370,8 +372,8 @@ public boolean replaceWorld(Path newWorld) { public boolean deleteDirectory(Path path) { try (var files = Files.walk(path)) { files.sorted(Comparator.reverseOrder()) - .map(Path::toFile) - .forEach(File::delete); + .map(Path::toFile) + .forEach(File::delete); } catch (NoSuchFileException e) { return true; } catch (IOException e) { @@ -411,18 +413,18 @@ public CompletableFuture detailStatus(EventContext context) { } else { if (running()) { builder.setDescription("teamserver.message.detailstatus.existing.description") - .addField("word.ports", "$word.server$: %d%n$word.api$: %d".formatted(port, apiPort), true); + .addField("word.ports", "$word.server$: %d%n$word.api$: %d".formatted(port, apiPort), true); stats().ifPresent(stats -> { var memory = stats.memory(); builder.addField("word.memory", "$word.used$ %d%n$word.total$: %d%n$word.max$: %d".formatted(memory.usedMb(), memory.totalMb(), memory.maxMb()), true) - .addField("word.tps", "1 $word.min$: %.2f%n5 $word.min$: %.2f%n 15 $word.min$: %.2f%n$word.averageticktime$ %.2f".formatted( - stats.tps()[0], stats.tps()[1], stats.tps()[2], stats.averageTickTime()), true) - .addField("word.players", String.valueOf(stats.onlinePlayers()), true) - .addField("word.system", "$word.activethreads$: %d".formatted(stats.activeThreads()), true); + .addField("word.tps", "1 $word.min$: %.2f%n5 $word.min$: %.2f%n 15 $word.min$: %.2f%n$word.averageticktime$ %.2f".formatted( + stats.tps()[0], stats.tps()[1], stats.tps()[2], stats.averageTickTime()), true) + .addField("word.players", String.valueOf(stats.onlinePlayers()), true) + .addField("word.system", "$word.activethreads$: %d".formatted(stats.activeThreads()), true); }); } else { builder.setDescription("word.serversetup") - .addField("word.ports", "word.notrunning", true); + .addField("word.ports", "word.notrunning", true); } } diff --git a/plugin-api/src/main/java/de/chojo/pluginjam/payload/Registration.java b/plugin-api/src/main/java/de/chojo/pluginjam/payload/Registration.java index 7f5b90a..bd93cbd 100644 --- a/plugin-api/src/main/java/de/chojo/pluginjam/payload/Registration.java +++ b/plugin-api/src/main/java/de/chojo/pluginjam/payload/Registration.java @@ -6,5 +6,5 @@ package de.chojo.pluginjam.payload; -public record Registration(int id, String name, int port, int apiPort) { +public record Registration(int id, String name, String host, int port, int apiPort) { } diff --git a/plugin-paper/build.gradle.kts b/plugin-paper/build.gradle.kts index 6504f67..2e31394 100644 --- a/plugin-paper/build.gradle.kts +++ b/plugin-paper/build.gradle.kts @@ -2,6 +2,7 @@ plugins { java alias(libs.plugins.pluginyml) alias(libs.plugins.shadow) + `maven-publish` } group = "de.chojo" @@ -37,6 +38,24 @@ tasks { } } +publishing { + repositories { + maven { + name = "Eldonexus" + url = uri("https://eldonexus.de/repository/maven-releases") + credentials { + username = System.getenv("NEXUS_USERNAME") + password = System.getenv("NEXUS_PASSWORD") + } + } + } + publications { + create("maven") { + from(components["java"]) + } + } +} + bukkit { name = "PluginJam" main = "de.chojo.pluginjam.PluginJam" diff --git a/plugin-paper/src/main/java/de/chojo/pluginjam/velocity/ReportService.java b/plugin-paper/src/main/java/de/chojo/pluginjam/velocity/ReportService.java index 8719ac2..056c4d9 100644 --- a/plugin-paper/src/main/java/de/chojo/pluginjam/velocity/ReportService.java +++ b/plugin-paper/src/main/java/de/chojo/pluginjam/velocity/ReportService.java @@ -9,6 +9,7 @@ import com.google.gson.Gson; import de.chojo.pluginjam.payload.Registration; import org.bukkit.plugin.Plugin; +import org.eclipse.jetty.http.HttpStatus; import org.slf4j.Logger; import java.net.URI; @@ -30,6 +31,7 @@ public class ReportService implements Runnable { private final int velocityPort; private final int apiPort; private final String velocityHost; + private final String host = System.getenv("HOSTNAME"); private ReportService(Plugin plugin) { this.plugin = plugin; @@ -55,14 +57,15 @@ public void run() { private void register() { log.info("Registering server at velocity instance"); - var registration = new Registration(id, name, plugin.getServer().getPort(), apiPort); + var registration = new Registration(id, name, host, plugin.getServer().getPort(), apiPort); var builder = HttpRequest.newBuilder(apiUrl("v1", "server")) .POST(HttpRequest.BodyPublishers.ofString(gson.toJson(registration))) .build(); + log.info("Posting to {}", builder.uri().toString()); client.sendAsync(builder, HttpResponse.BodyHandlers.discarding()) .whenComplete((res, err) -> { - if (err == null) { - log.info("Registered server at velocity instance"); + if (err == null && res.statusCode() == HttpStatus.ACCEPTED_202) { + log.info("Registered server at velocity instance."); } else { log.error("Could not register", err); } @@ -70,7 +73,7 @@ private void register() { } private void ping() { - var registration = new Registration(id, name, plugin.getServer().getPort(), apiPort); + var registration = new Registration(id, name, host, plugin.getServer().getPort(), apiPort); var builder = HttpRequest.newBuilder(apiUrl("v1", "server")) .method("PATCH", HttpRequest.BodyPublishers.ofString(gson.toJson(registration))) .build(); @@ -84,9 +87,9 @@ private void ping() { private void unregister() { log.info("Unregistering server at velocity instance."); - var builder = HttpRequest.newBuilder(queryApiUrl("id=%s&port=%s".formatted(id, + var builder = HttpRequest.newBuilder(queryApiUrl("id=%s&port=%s&host=%s".formatted(id, plugin.getServer() - .getPort()), + .getPort(), host), "v1", "server")) .DELETE() .build(); diff --git a/plugin-velocity/build.gradle.kts b/plugin-velocity/build.gradle.kts index c1d1a2e..ec6b07b 100644 --- a/plugin-velocity/build.gradle.kts +++ b/plugin-velocity/build.gradle.kts @@ -3,6 +3,8 @@ plugins { alias(libs.plugins.shadow) } +version = "1.0.0" + repositories { mavenCentral() maven("https://repo.papermc.io/repository/maven-public/") diff --git a/plugin-velocity/src/main/java/de/chojo/pluginjam/servers/ServerRegistry.java b/plugin-velocity/src/main/java/de/chojo/pluginjam/servers/ServerRegistry.java index f08b15a..0166d86 100644 --- a/plugin-velocity/src/main/java/de/chojo/pluginjam/servers/ServerRegistry.java +++ b/plugin-velocity/src/main/java/de/chojo/pluginjam/servers/ServerRegistry.java @@ -17,7 +17,6 @@ import java.time.temporal.ChronoUnit; import java.util.Collection; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -71,7 +70,7 @@ public void register(Registration registration) { ids.put(registration.id(), registration); ports.put(registration.port(), registration); - proxy.registerServer(new ServerInfo(registration.name(), new InetSocketAddress("localhost", registration.port()))); + proxy.registerServer(new ServerInfo(registration.name(), new InetSocketAddress(registration.host(), registration.port()))); ping(registration); } @@ -87,7 +86,7 @@ public void ping(Registration registration) { public void unregister(Registration registration) { var removed = ids.remove(registration.id()); - if(removed == null){ + if (removed == null) { log.warn("Unregistered server {} from port {}, but this server is not known.", registration.id(), registration.port()); return; } diff --git a/plugin-velocity/src/main/java/de/chojo/pluginjam/web/Api.java b/plugin-velocity/src/main/java/de/chojo/pluginjam/web/Api.java index fc817a4..2f92557 100644 --- a/plugin-velocity/src/main/java/de/chojo/pluginjam/web/Api.java +++ b/plugin-velocity/src/main/java/de/chojo/pluginjam/web/Api.java @@ -32,7 +32,8 @@ public static Api create(ServerRegistry registry) { var classLoader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(PluginJam.class.getClassLoader()); var javalin = Javalin.create(); - javalin.start("0.0.0.0", Integer.parseInt(System.getProperty("javalin.port", "30000"))); + int port = Integer.parseInt(System.getProperty("javalin.port", "30000")); + javalin.start("0.0.0.0", port); Thread.currentThread().setContextClassLoader(classLoader); var api = new Api(registry, javalin); api.ignite(); diff --git a/plugin-velocity/src/main/java/de/chojo/pluginjam/web/server/Server.java b/plugin-velocity/src/main/java/de/chojo/pluginjam/web/server/Server.java index 8b2645b..6ca9388 100644 --- a/plugin-velocity/src/main/java/de/chojo/pluginjam/web/server/Server.java +++ b/plugin-velocity/src/main/java/de/chojo/pluginjam/web/server/Server.java @@ -38,10 +38,12 @@ public void buildRoutes() { delete("", ctx -> { var id = ctx.queryParam("id"); var port = ctx.queryParam("port"); - var registration = new Registration(Integer.parseInt(id), "", Integer.parseInt(port), 0); + var host = ctx.queryParam("host"); + var registration = new Registration(Integer.parseInt(id), "", host, Integer.parseInt(port), 0); registry.unregister(registration); ctx.status(HttpCode.ACCEPTED); }); + get("", ctx -> { ctx.json(registry.server()); ctx.status(HttpCode.OK);