From 84d8f19348c6ace4a2ae714e16a4c4f9f613b05b Mon Sep 17 00:00:00 2001 From: Luke Bemish Date: Sun, 12 Nov 2023 02:15:35 +0000 Subject: [PATCH] [no ci] Fix classloading and gametests --- .../biomesquisher/impl/BiomeSquisher.java | 4 ++- .../impl/BiomeSquisherCommands.java | 32 +++++++++++++------ .../biomesquisher/impl/Platform.java | 2 ++ .../assets/biomesquisher/lang/en_us.json | 3 +- .../test/BiomeSquisherGameTests.java | 18 +++++++++-- .../impl/fabric/BiomeSquisherMod.java | 7 +++- .../impl/fabric/PlatformImpl.java | 6 ++++ .../impl/neoforge/BiomeSquisherMod.java | 7 +++- .../impl/neoforge/PlatformImpl.java | 7 ++++ 9 files changed, 69 insertions(+), 17 deletions(-) diff --git a/common/src/main/java/dev/lukebemish/biomesquisher/impl/BiomeSquisher.java b/common/src/main/java/dev/lukebemish/biomesquisher/impl/BiomeSquisher.java index 7499428..4040f22 100644 --- a/common/src/main/java/dev/lukebemish/biomesquisher/impl/BiomeSquisher.java +++ b/common/src/main/java/dev/lukebemish/biomesquisher/impl/BiomeSquisher.java @@ -19,7 +19,9 @@ public final class BiomeSquisher { private BiomeSquisher() {} public static void init() { - Runtime.getRuntime().addShutdownHook(new Thread(WebServerThread::waitOnStopServer)); + if (Platform.INSTANCE.isClient()) { + Runtime.getRuntime().addShutdownHook(new Thread(WebServerThread::waitOnStopServer)); + } } public static void squishBiomeSource(ResourceManager resourceManager, @Nullable NoiseBasedChunkGenerator generator, MultiNoiseBiomeSource multiNoiseBiomeSource, ResourceKey key, RegistryAccess access) { diff --git a/common/src/main/java/dev/lukebemish/biomesquisher/impl/BiomeSquisherCommands.java b/common/src/main/java/dev/lukebemish/biomesquisher/impl/BiomeSquisherCommands.java index 971c11f..4ec52ca 100644 --- a/common/src/main/java/dev/lukebemish/biomesquisher/impl/BiomeSquisherCommands.java +++ b/common/src/main/java/dev/lukebemish/biomesquisher/impl/BiomeSquisherCommands.java @@ -38,6 +38,10 @@ public class BiomeSquisherCommands { Component.translatable("commands.biomesquisher.serve.failed_to_start") ); + private static final SimpleCommandExceptionType ERROR_ON_DEDICATED_SERVER = new SimpleCommandExceptionType( + Component.translatable("commands.biomesquisher.on_dedicated_server") + ); + public static void register(CommandDispatcher dispatcher) { dispatcher.register( Commands.literal(Utils.MOD_ID) @@ -114,8 +118,12 @@ public static void register(CommandDispatcher dispatcher) { Commands.literal("stop") .executes( commandContext -> { - WebServerThread.stopServer(); - return Command.SINGLE_SUCCESS; + if (Platform.INSTANCE.isClient()) { + WebServerThread.stopServer(); + return Command.SINGLE_SUCCESS; + } else { + throw ERROR_ON_DEDICATED_SERVER.create(); + } } ) ) @@ -125,14 +133,18 @@ public static void register(CommandDispatcher dispatcher) { Commands.argument("port", IntegerArgumentType.integer()) .executes( commandContext -> { - int port = commandContext.getArgument("port", Integer.class); - setupServer(commandContext, port); - commandContext.getSource().sendSuccess(() -> Component.translatable("commands.biomesquisher.serve.on_port", - Component.literal("http://localhost:"+port+"/").withStyle( - Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "http://localhost:"+port+"/")).withUnderlined(true) - ) - ), true); - return Command.SINGLE_SUCCESS; + if (Platform.INSTANCE.isClient()) { + int port = commandContext.getArgument("port", Integer.class); + setupServer(commandContext, port); + commandContext.getSource().sendSuccess(() -> Component.translatable("commands.biomesquisher.serve.on_port", + Component.literal("http://localhost:" + port + "/").withStyle( + Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "http://localhost:" + port + "/")).withUnderlined(true) + ) + ), true); + return Command.SINGLE_SUCCESS; + } else { + throw ERROR_ON_DEDICATED_SERVER.create(); + } } ) ) diff --git a/common/src/main/java/dev/lukebemish/biomesquisher/impl/Platform.java b/common/src/main/java/dev/lukebemish/biomesquisher/impl/Platform.java index f4de572..1bb7d13 100644 --- a/common/src/main/java/dev/lukebemish/biomesquisher/impl/Platform.java +++ b/common/src/main/java/dev/lukebemish/biomesquisher/impl/Platform.java @@ -8,4 +8,6 @@ public interface Platform { Path gameDir(); Optional getRootResource(String resource); + + boolean isClient(); } diff --git a/common/src/main/resources/assets/biomesquisher/lang/en_us.json b/common/src/main/resources/assets/biomesquisher/lang/en_us.json index 7bbb72c..ca15650 100644 --- a/common/src/main/resources/assets/biomesquisher/lang/en_us.json +++ b/common/src/main/resources/assets/biomesquisher/lang/en_us.json @@ -3,5 +3,6 @@ "commands.biomesquisher.dump.failed_to_save": "Failed to save biome dump - PNGJ may not be present; check the logs for more information", "commands.biomesquisher.dump.failed_to_generate": "Failed to generate biome dump - check the logs for more information", "commands.biomesquisher.serve.on_port": "Serving biome dump at %s", - "commands.biomesquisher.serve.failed_to_start": "Failed to start biome dump server - check the logs for more information" + "commands.biomesquisher.serve.failed_to_start": "Failed to start biome dump server - check the logs for more information", + "commands.biomesquisher.on_dedicated_server": "At present, this command cannot be run on the dedicated server" } diff --git a/common/src/test/java/dev/lukebemish/biomesquisher/test/BiomeSquisherGameTests.java b/common/src/test/java/dev/lukebemish/biomesquisher/test/BiomeSquisherGameTests.java index d3096b9..b40aba5 100644 --- a/common/src/test/java/dev/lukebemish/biomesquisher/test/BiomeSquisherGameTests.java +++ b/common/src/test/java/dev/lukebemish/biomesquisher/test/BiomeSquisherGameTests.java @@ -60,6 +60,7 @@ public static Collection testLayoutsGenerator() { try (var stream = Files.walk(root)) { stream.filter(path -> path.getFileName().toString().endsWith(".json") && !Files.isDirectory(path)).forEach(path -> { Path png = path.getParent().resolve(path.getFileName().toString().replace(".json", ".png")); + if (!Files.exists(png)) { String message = "Missing png for test layout: " + path; Utils.LOGGER.error(message); @@ -72,7 +73,7 @@ public static Collection testLayoutsGenerator() { String testName = String.join("/", parts).replace(".json", ""); try (var pngStream = Files.newInputStream(png); var jsonReader = Files.newBufferedReader(path)) { - PngReader r = new PngReader(pngStream); + PngReader r = new PngReader(pngStream, false); if (r.imgInfo.cols != 1024 || r.imgInfo.rows != 1024) { var message = "Invalid png size for test layout: " + testName; Utils.LOGGER.error(message); @@ -91,10 +92,21 @@ public static Collection testLayoutsGenerator() { for (int i = 0; i < 1024; i++) { int[] row = data[i]; ImageLineInt rRow = (ImageLineInt) r.readRow(i); - for (int j = 0; j < 1024; j++) { - row[j] = rRow.getElem(j); + if (rRow.getSize() == 1024*4) { + for (int j = 0; j < 1024*4; j+=4) { + row[j/4] = rRow.getElem(j+2) | rRow.getElem(j+1) << 8 | rRow.getElem(j) << 16 | rRow.getElem(j+3) << 24; + } + } else if (rRow.getSize() == 1024*3) { + for (int j = 0; j < 1024*3; j+=3) { + row[j/3] = rRow.getElem(j+2) | rRow.getElem(j+1) << 8 | rRow.getElem(j) << 16 | 0xFF << 24; + } + } else { + for (int j = 0; j < 1024; j++) { + row[j] = rRow.getElem(j); + } } } + r.close(); layouts.add(new LayoutTest(testName, specs, new LayoutTest.Layout(data))); } catch (IOException e) { Utils.LOGGER.error("Failed to load test layout: " + testName, e); diff --git a/fabric/src/main/java/dev/lukebemish/biomesquisher/impl/fabric/BiomeSquisherMod.java b/fabric/src/main/java/dev/lukebemish/biomesquisher/impl/fabric/BiomeSquisherMod.java index ccdcfd9..d002c25 100644 --- a/fabric/src/main/java/dev/lukebemish/biomesquisher/impl/fabric/BiomeSquisherMod.java +++ b/fabric/src/main/java/dev/lukebemish/biomesquisher/impl/fabric/BiomeSquisherMod.java @@ -7,10 +7,12 @@ import dev.lukebemish.biomesquisher.impl.BiomeSquisherCommands; import dev.lukebemish.biomesquisher.impl.InternalScalingSampler; import dev.lukebemish.biomesquisher.impl.server.WebServerThread; +import net.fabricmc.api.EnvType; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.registry.DynamicRegistries; +import net.fabricmc.loader.api.FabricLoader; import net.minecraft.core.Registry; import net.minecraft.core.registries.BuiltInRegistries; @@ -24,6 +26,9 @@ public void onInitialize() { BiomeSquisherCommands.register(dispatcher)); DynamicRegistries.register(BiomeSquisherRegistries.SERIES, Series.CODEC); DynamicRegistries.register(BiomeSquisherRegistries.SQUISHER, Squisher.CODEC); - ServerLifecycleEvents.SERVER_STOPPING.register(server -> WebServerThread.stopServer()); + + if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { + ServerLifecycleEvents.SERVER_STOPPING.register(server -> WebServerThread.stopServer()); + } } } diff --git a/fabric/src/main/java/dev/lukebemish/biomesquisher/impl/fabric/PlatformImpl.java b/fabric/src/main/java/dev/lukebemish/biomesquisher/impl/fabric/PlatformImpl.java index fd75816..a9d9eb0 100644 --- a/fabric/src/main/java/dev/lukebemish/biomesquisher/impl/fabric/PlatformImpl.java +++ b/fabric/src/main/java/dev/lukebemish/biomesquisher/impl/fabric/PlatformImpl.java @@ -3,6 +3,7 @@ import com.google.auto.service.AutoService; import dev.lukebemish.biomesquisher.impl.Platform; import dev.lukebemish.biomesquisher.impl.Utils; +import net.fabricmc.api.EnvType; import net.fabricmc.loader.api.FabricLoader; import java.nio.file.Path; @@ -19,4 +20,9 @@ public Path gameDir() { public Optional getRootResource(String resource) { return FabricLoader.getInstance().getModContainer(Utils.MOD_ID).orElseThrow().findPath(resource); } + + @Override + public boolean isClient() { + return FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT; + } } diff --git a/neoforge/src/main/java/dev/lukebemish/biomesquisher/impl/neoforge/BiomeSquisherMod.java b/neoforge/src/main/java/dev/lukebemish/biomesquisher/impl/neoforge/BiomeSquisherMod.java index f4a720d..c635903 100644 --- a/neoforge/src/main/java/dev/lukebemish/biomesquisher/impl/neoforge/BiomeSquisherMod.java +++ b/neoforge/src/main/java/dev/lukebemish/biomesquisher/impl/neoforge/BiomeSquisherMod.java @@ -11,8 +11,10 @@ import dev.lukebemish.biomesquisher.impl.server.WebServerThread; import net.minecraft.core.registries.Registries; import net.minecraft.world.level.levelgen.DensityFunction; +import net.neoforged.api.distmarker.Dist; import net.neoforged.fml.common.Mod; import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext; +import net.neoforged.fml.loading.FMLEnvironment; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.RegisterCommandsEvent; import net.neoforged.neoforge.event.server.ServerStoppingEvent; @@ -33,7 +35,10 @@ public BiomeSquisherMod() { modBus.addListener(DataPackRegistryEvent.NewRegistry.class, this::createDatapackRegistries); gameBus.addListener(RegisterCommandsEvent.class, this::registerCommands); - gameBus.addListener(ServerStoppingEvent.class, event -> WebServerThread.stopServer()); + + if (FMLEnvironment.dist == Dist.CLIENT) { + gameBus.addListener(ServerStoppingEvent.class, event -> WebServerThread.stopServer()); + } } private void createDatapackRegistries(DataPackRegistryEvent.NewRegistry event) { diff --git a/neoforge/src/main/java/dev/lukebemish/biomesquisher/impl/neoforge/PlatformImpl.java b/neoforge/src/main/java/dev/lukebemish/biomesquisher/impl/neoforge/PlatformImpl.java index 6e22256..fa98e33 100644 --- a/neoforge/src/main/java/dev/lukebemish/biomesquisher/impl/neoforge/PlatformImpl.java +++ b/neoforge/src/main/java/dev/lukebemish/biomesquisher/impl/neoforge/PlatformImpl.java @@ -3,7 +3,9 @@ import com.google.auto.service.AutoService; import dev.lukebemish.biomesquisher.impl.Platform; import dev.lukebemish.biomesquisher.impl.Utils; +import net.neoforged.api.distmarker.Dist; import net.neoforged.fml.ModList; +import net.neoforged.fml.loading.FMLEnvironment; import net.neoforged.fml.loading.FMLPaths; import java.nio.file.Files; @@ -25,4 +27,9 @@ public Optional getRootResource(String resource) { } return Optional.empty(); } + + @Override + public boolean isClient() { + return FMLEnvironment.dist == Dist.CLIENT; + } }