Skip to content

Commit

Permalink
Upgrade spark-sponge to API 12
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Jan 5, 2025
1 parent 4cd34dc commit 336102f
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,14 @@ public GameRulesResult pollGameRules() {
Iterable<ServerWorld> worlds = this.server.getWorlds();

for (ServerWorld world : worlds) {
String worldName = world.getRegistryKey().getValue().getPath();

world.getGameRules().accept(new GameRules.Visitor() {
@Override
public <T extends GameRules.Rule<T>> void visit(GameRules.Key<T> key, GameRules.Type<T> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ public GameRulesResult pollGameRules() {
Iterable<ServerLevel> levels = this.server.getAllLevels();

for (ServerLevel level : levels) {
String levelName = level.dimension().location().getPath();

level.getGameRules().visitGameRuleTypes(new GameRules.GameRuleTypeVisitor() {
@Override
public <T extends GameRules.Value<T>> void visit(GameRules.Key<T> key, GameRules.Type<T> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,14 @@ public GameRulesResult pollGameRules() {
Iterable<ServerLevel> levels = this.server.getAllLevels();

for (ServerLevel level : levels) {
String levelName = level.dimension().location().getPath();

level.getGameRules().visitGameRuleTypes(new GameRules.GameRuleTypeVisitor() {
@Override
public <T extends GameRules.Value<T>> void visit(GameRules.Key<T> key, GameRules.Type<T> 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);
}
Expand Down
8 changes: 7 additions & 1 deletion spark-sponge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Path, String> constructPathToPluginIdMap(Collection<PluginContainer> plugins) {
ImmutableMap.Builder<Path, String> builder = ImmutableMap.builder();
Map<Path, String> 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<JVMPluginResource> candidate = (PluginCandidate<JVMPluginResource>) 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public <T extends Subject & Audience> 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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,7 +41,12 @@ public SpongePlayerPingProvider(Server server) {
public Map<String, Integer> poll() {
ImmutableMap.Builder<String, Integer> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,15 +63,15 @@ public CountsResult pollCounts() {
}

@Override
public ChunksResult<Sponge7ChunkInfo> pollChunks() {
ChunksResult<Sponge7ChunkInfo> data = new ChunksResult<>();
public ChunksResult<SpongeChunkInfo> pollChunks() {
ChunksResult<SpongeChunkInfo> data = new ChunksResult<>();

for (ServerWorld world : this.server.worldManager().worlds()) {
List<WorldChunk> chunks = Lists.newArrayList(world.loadedChunks());

List<Sponge7ChunkInfo> list = new ArrayList<>(chunks.size());
List<SpongeChunkInfo> 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);
Expand All @@ -86,12 +84,16 @@ public ChunksResult<Sponge7ChunkInfo> pollChunks() {
public GameRulesResult pollGameRules() {
GameRulesResult data = new GameRulesResult();

List<GameRule<?>> 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<ServerWorld> 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;
Expand All @@ -108,10 +110,10 @@ public Collection<DataPackInfo> pollDataPacks() {
.collect(Collectors.toList());
}

static final class Sponge7ChunkInfo extends AbstractChunkInfo<EntityType<?>> {
static final class SpongeChunkInfo extends AbstractChunkInfo<EntityType<?>> {
private final CountMap<EntityType<?>> entityCounts;

Sponge7ChunkInfo(WorldChunk chunk) {
SpongeChunkInfo(WorldChunk chunk) {
super(chunk.chunkPosition().x(), chunk.chunkPosition().z());

this.entityCounts = new CountMap.Simple<>(new HashMap<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"dependencies": [
{
"id": "spongeapi",
"version": "8.0.0"
"version": "12.0.0"
}
]
}
Expand Down

0 comments on commit 336102f

Please sign in to comment.