Skip to content

Commit

Permalink
Add publish for paper plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowdashlabs committed Aug 19, 2024
1 parent e38beb1 commit 7b84ebc
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 24 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
- 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 }}
26 changes: 14 additions & 12 deletions bot/src/main/java/de/chojo/gamejam/server/TeamServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -153,6 +153,7 @@ private void writeTemplate() throws IOException {

/**
* Delete all the server data.
*
* @return true when server was deleted.
* @throws IOException
*/
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -411,18 +413,18 @@ public CompletableFuture<MessageEmbed> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
19 changes: 19 additions & 0 deletions plugin-paper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
java
alias(libs.plugins.pluginyml)
alias(libs.plugins.shadow)
`maven-publish`
}

group = "de.chojo"
Expand Down Expand Up @@ -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<MavenPublication>("maven") {
from(components["java"])
}
}
}

bukkit {
name = "PluginJam"
main = "de.chojo.pluginjam.PluginJam"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -55,22 +57,23 @@ 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);
}
});
}

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();
Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions plugin-velocity/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ plugins {
alias(libs.plugins.shadow)
}

version = "1.0.0"

repositories {
mavenCentral()
maven("https://repo.papermc.io/repository/maven-public/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7b84ebc

Please sign in to comment.