Skip to content

Commit

Permalink
[no ci] Fix classloading and gametests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Nov 12, 2023
1 parent 88aa4fc commit 84d8f19
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<LevelStem> key, RegistryAccess access) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandSourceStack> dispatcher) {
dispatcher.register(
Commands.literal(Utils.MOD_ID)
Expand Down Expand Up @@ -114,8 +118,12 @@ public static void register(CommandDispatcher<CommandSourceStack> 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();
}
}
)
)
Expand All @@ -125,14 +133,18 @@ public static void register(CommandDispatcher<CommandSourceStack> 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();
}
}
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public interface Platform {

Path gameDir();
Optional<Path> getRootResource(String resource);

boolean isClient();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static Collection<TestFunction> 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);
Expand All @@ -72,7 +73,7 @@ public static Collection<TestFunction> 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);
Expand All @@ -91,10 +92,21 @@ public static Collection<TestFunction> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,4 +20,9 @@ public Path gameDir() {
public Optional<Path> getRootResource(String resource) {
return FabricLoader.getInstance().getModContainer(Utils.MOD_ID).orElseThrow().findPath(resource);
}

@Override
public boolean isClient() {
return FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,4 +27,9 @@ public Optional<Path> getRootResource(String resource) {
}
return Optional.empty();
}

@Override
public boolean isClient() {
return FMLEnvironment.dist == Dist.CLIENT;
}
}

0 comments on commit 84d8f19

Please sign in to comment.