From 336102f88b38900b60888ab85ea13b388d4fe0dc Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 5 Jan 2025 14:11:11 +0000 Subject: [PATCH] Upgrade spark-sponge to API 12 --- .../spark/fabric/FabricWorldInfoProvider.java | 3 +- .../spark/forge/ForgeWorldInfoProvider.java | 3 +- .../neoforge/NeoForgeWorldInfoProvider.java | 3 +- spark-sponge/build.gradle | 8 ++++- .../spark/sponge/SpongeClassSourceLookup.java | 17 +++++------ .../spark/sponge/SpongeCommandSender.java | 9 ++++-- .../sponge/SpongePlayerPingProvider.java | 10 ++++++- .../spark/sponge/SpongeWorldInfoProvider.java | 30 ++++++++++--------- .../resources/META-INF/sponge_plugins.json | 2 +- 9 files changed, 53 insertions(+), 32 deletions(-) diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricWorldInfoProvider.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricWorldInfoProvider.java index f66906ff..5484c7d6 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricWorldInfoProvider.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricWorldInfoProvider.java @@ -134,13 +134,14 @@ public GameRulesResult pollGameRules() { Iterable worlds = this.server.getWorlds(); for (ServerWorld world : worlds) { + String worldName = world.getRegistryKey().getValue().getPath(); + world.getGameRules().accept(new GameRules.Visitor() { @Override public > void visit(GameRules.Key key, GameRules.Type type) { String defaultValue = type.createRule().serialize(); data.putDefault(key.getName(), defaultValue); - String worldName = world.getRegistryKey().getValue().getPath(); String value = world.getGameRules().get(key).serialize(); data.put(key.getName(), worldName, value); } diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeWorldInfoProvider.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeWorldInfoProvider.java index 61bea72f..a87aa035 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeWorldInfoProvider.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeWorldInfoProvider.java @@ -121,13 +121,14 @@ public GameRulesResult pollGameRules() { Iterable levels = this.server.getAllLevels(); for (ServerLevel level : levels) { + String levelName = level.dimension().location().getPath(); + level.getGameRules().visitGameRuleTypes(new GameRules.GameRuleTypeVisitor() { @Override public > void visit(GameRules.Key key, GameRules.Type type) { String defaultValue = type.createRule().serialize(); data.putDefault(key.getId(), defaultValue); - String levelName = level.dimension().location().getPath(); String value = level.getGameRules().getRule(key).serialize(); data.put(key.getId(), levelName, value); } diff --git a/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeWorldInfoProvider.java b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeWorldInfoProvider.java index 73b9e7b0..d85cfb36 100644 --- a/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeWorldInfoProvider.java +++ b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeWorldInfoProvider.java @@ -129,13 +129,14 @@ public GameRulesResult pollGameRules() { Iterable levels = this.server.getAllLevels(); for (ServerLevel level : levels) { + String levelName = level.dimension().location().getPath(); + level.getGameRules().visitGameRuleTypes(new GameRules.GameRuleTypeVisitor() { @Override public > void visit(GameRules.Key key, GameRules.Type type) { String defaultValue = type.createRule().serialize(); data.putDefault(key.getId(), defaultValue); - String levelName = level.dimension().location().getPath(); String value = level.getGameRules().getRule(key).serialize(); data.put(key.getId(), levelName, value); } diff --git a/spark-sponge/build.gradle b/spark-sponge/build.gradle index de2a1d37..75356a54 100644 --- a/spark-sponge/build.gradle +++ b/spark-sponge/build.gradle @@ -2,9 +2,15 @@ plugins { id 'com.gradleup.shadow' version '8.3.0' } +tasks.withType(JavaCompile) { + // override, compile targeting J21 + options.release = 21 +} + dependencies { implementation project(':spark-common') - compileOnly "org.spongepowered:spongeapi:8.0.0-SNAPSHOT" + compileOnly "org.spongepowered:spongeapi:12.0.0" + compileOnly "com.google.guava:guava:33.3.1-jre" } repositories { diff --git a/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeClassSourceLookup.java b/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeClassSourceLookup.java index 0820ae34..1effb194 100644 --- a/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeClassSourceLookup.java +++ b/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeClassSourceLookup.java @@ -25,12 +25,12 @@ import org.spongepowered.api.Game; import org.spongepowered.plugin.PluginCandidate; import org.spongepowered.plugin.PluginContainer; -import org.spongepowered.plugin.builtin.jvm.JVMPluginContainer; -import org.spongepowered.plugin.builtin.jvm.locator.JVMPluginResource; +import org.spongepowered.plugin.builtin.StandardPluginContainer; import java.lang.reflect.Field; import java.nio.file.Path; import java.util.Collection; +import java.util.HashMap; import java.util.Map; public class SpongeClassSourceLookup extends ClassSourceLookup.ByCodeSource { @@ -57,26 +57,25 @@ public String identifyFile(Path path) { } // pretty nasty, but if it fails it doesn't really matter - @SuppressWarnings("unchecked") private static Map constructPathToPluginIdMap(Collection plugins) { - ImmutableMap.Builder builder = ImmutableMap.builder(); + Map map = new HashMap<>(); try { - Field candidateField = JVMPluginContainer.class.getDeclaredField("candidate"); + Field candidateField = StandardPluginContainer.class.getDeclaredField("candidate"); candidateField.setAccessible(true); for (PluginContainer plugin : plugins) { - if (plugin instanceof JVMPluginContainer) { - PluginCandidate candidate = (PluginCandidate) candidateField.get(plugin); + if (plugin instanceof StandardPluginContainer) { + PluginCandidate candidate = (PluginCandidate) candidateField.get(plugin); Path path = candidate.resource().path().toAbsolutePath().normalize(); - builder.put(path, plugin.metadata().id()); + map.putIfAbsent(path, plugin.metadata().id()); } } } catch (Exception e) { // ignore } - return builder.build(); + return ImmutableMap.copyOf(map); } } diff --git a/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeCommandSender.java b/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeCommandSender.java index aa634cc4..beae7349 100644 --- a/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeCommandSender.java +++ b/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeCommandSender.java @@ -50,7 +50,8 @@ public SpongeCommandSender(T cause) { @Override public String getName() { - return super.delegate.friendlyIdentifier().orElse(super.delegate.identifier()); + String name = super.delegate.friendlyIdentifier().orElse(super.delegate.identifier()); + return name.equals("console") ? "Console" : name; } @Override @@ -64,9 +65,11 @@ public UUID getUniqueId() { try { return UUID.fromString(super.delegate.identifier()); - } catch (Exception e) { - return UUID.nameUUIDFromBytes(super.delegate.identifier().getBytes(UTF_8)); + } catch (IllegalArgumentException e) { + // ignore } + + return null; } @Override diff --git a/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongePlayerPingProvider.java b/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongePlayerPingProvider.java index b327a0a7..3aa598ff 100644 --- a/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongePlayerPingProvider.java +++ b/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongePlayerPingProvider.java @@ -24,6 +24,9 @@ import me.lucko.spark.common.monitor.ping.PlayerPingProvider; import org.spongepowered.api.Server; import org.spongepowered.api.entity.living.player.server.ServerPlayer; +import org.spongepowered.api.network.EngineConnectionState; +import org.spongepowered.api.network.ServerConnectionState; +import org.spongepowered.api.network.ServerSideConnection; import java.util.Map; @@ -38,7 +41,12 @@ public SpongePlayerPingProvider(Server server) { public Map poll() { ImmutableMap.Builder builder = ImmutableMap.builder(); for (ServerPlayer player : this.server.onlinePlayers()) { - builder.put(player.name(), player.connection().latency()); + ServerSideConnection connection = player.connection(); + EngineConnectionState connectionState = connection.state().orElse(null); + if (connectionState instanceof ServerConnectionState.Game) { + int latency = ((ServerConnectionState.Game) connectionState).latency(); + builder.put(player.name(), latency); + } } return builder.build(); } diff --git a/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeWorldInfoProvider.java b/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeWorldInfoProvider.java index 15f15f83..2269e6f3 100644 --- a/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeWorldInfoProvider.java +++ b/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeWorldInfoProvider.java @@ -31,8 +31,6 @@ import org.spongepowered.api.entity.EntityType; import org.spongepowered.api.entity.EntityTypes; import org.spongepowered.api.world.chunk.WorldChunk; -import org.spongepowered.api.world.gamerule.GameRule; -import org.spongepowered.api.world.gamerule.GameRules; import org.spongepowered.api.world.server.ServerWorld; import java.util.ArrayList; @@ -65,15 +63,15 @@ public CountsResult pollCounts() { } @Override - public ChunksResult pollChunks() { - ChunksResult data = new ChunksResult<>(); + public ChunksResult pollChunks() { + ChunksResult data = new ChunksResult<>(); for (ServerWorld world : this.server.worldManager().worlds()) { List chunks = Lists.newArrayList(world.loadedChunks()); - List list = new ArrayList<>(chunks.size()); + List list = new ArrayList<>(chunks.size()); for (WorldChunk chunk : chunks) { - list.add(new Sponge7ChunkInfo(chunk)); + list.add(new SpongeChunkInfo(chunk)); } data.put(world.key().value(), list); @@ -86,12 +84,16 @@ public ChunksResult pollChunks() { public GameRulesResult pollGameRules() { GameRulesResult data = new GameRulesResult(); - List> rules = GameRules.registry().stream().collect(Collectors.toList()); - for (GameRule rule : rules) { - data.putDefault(rule.name(), rule.defaultValue().toString()); - for (ServerWorld world : this.server.worldManager().worlds()) { - data.put(rule.name(), world.key().value(), world.properties().gameRule(rule).toString()); - } + Collection worlds = this.server.worldManager().worlds(); + for (ServerWorld world : worlds) { + String worldName = world.key().value(); + + world.properties().gameRules().forEach((gameRule, value) -> { + String defaultValue = gameRule.defaultValue().toString(); + data.putDefault(gameRule.name(), defaultValue); + + data.put(gameRule.name(), worldName, value.toString()); + }); } return data; @@ -108,10 +110,10 @@ public Collection pollDataPacks() { .collect(Collectors.toList()); } - static final class Sponge7ChunkInfo extends AbstractChunkInfo> { + static final class SpongeChunkInfo extends AbstractChunkInfo> { private final CountMap> entityCounts; - Sponge7ChunkInfo(WorldChunk chunk) { + SpongeChunkInfo(WorldChunk chunk) { super(chunk.chunkPosition().x(), chunk.chunkPosition().z()); this.entityCounts = new CountMap.Simple<>(new HashMap<>()); diff --git a/spark-sponge/src/main/resources/META-INF/sponge_plugins.json b/spark-sponge/src/main/resources/META-INF/sponge_plugins.json index 195d0b99..7c467469 100644 --- a/spark-sponge/src/main/resources/META-INF/sponge_plugins.json +++ b/spark-sponge/src/main/resources/META-INF/sponge_plugins.json @@ -25,7 +25,7 @@ "dependencies": [ { "id": "spongeapi", - "version": "8.0.0" + "version": "12.0.0" } ] }