diff --git a/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java b/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java index dad16a7e2..317a2c1c6 100644 --- a/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java +++ b/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java @@ -16,6 +16,9 @@ package net.fabricmc.fabric.test.attachment; +import java.io.File; +import java.io.IOException; + import com.mojang.serialization.Codec; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,6 +29,7 @@ import net.minecraft.registry.RegistryKey; import net.minecraft.registry.RegistryKeys; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Identifier; +import net.minecraft.util.WorldSavePath; import net.minecraft.util.math.ChunkPos; import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.ChunkStatus; @@ -54,9 +58,9 @@ public class AttachmentTestMod implements ModInitializer { new Identifier(MOD_ID, "feature") ); - public static final ChunkPos FAR_CHUNK_POS = new ChunkPos(30, 0); + public static final ChunkPos FAR_CHUNK_POS = new ChunkPos(300, 0); - private boolean firstLaunch = true; + private boolean serverStarted = false; public static boolean featurePlaced = false; @Override @@ -70,11 +74,18 @@ public class AttachmentTestMod implements ModInitializer { ); ServerLifecycleEvents.SERVER_STARTED.register(server -> { - ServerWorld overworld; - WorldChunk chunk; + File saveRoot = server.getSavePath(WorldSavePath.ROOT).toFile(); + File markerFile = new File(saveRoot, MOD_ID + "_MARKER"); + boolean firstLaunch; - overworld = server.getOverworld(); - chunk = overworld.getChunk(0, 0); + try { + firstLaunch = markerFile.createNewFile(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + ServerWorld overworld = server.getOverworld(); + WorldChunk chunk = overworld.getChunk(0, 0); if (firstLaunch) { LOGGER.info("First launch, testing attachment by feature"); @@ -98,30 +109,36 @@ public class AttachmentTestMod implements ModInitializer { } else { LOGGER.info("Second launch, testing persistent attachments"); - if (!"world_data".equals(overworld.getAttached(PERSISTENT))) throw new AssertionError("World attachement did not persist"); - if (!"chunk_data".equals(chunk.getAttached(PERSISTENT))) throw new AssertionError("WorldChunk attachement did not persist"); + if (!"world_data".equals(overworld.getAttached(PERSISTENT))) throw new AssertionError("World attachment did not persist"); + if (!"chunk_data".equals(chunk.getAttached(PERSISTENT))) throw new AssertionError("WorldChunk attachment did not persist"); WrapperProtoChunk wrapperProtoChunk = (WrapperProtoChunk) overworld.getChunkManager().getChunk(0, 0, ChunkStatus.EMPTY, true); - if (!"chunk_data".equals(wrapperProtoChunk.getAttached(PERSISTENT))) throw new AssertionError("Attachement is not accessible through WrapperProtoChunk"); + if (!"chunk_data".equals(wrapperProtoChunk.getAttached(PERSISTENT))) throw new AssertionError("Attachment is not accessible through WrapperProtoChunk"); Chunk farChunk = overworld.getChunkManager().getChunk(FAR_CHUNK_POS.x, FAR_CHUNK_POS.z, ChunkStatus.EMPTY, true); if (farChunk instanceof WrapperProtoChunk) { - LOGGER.warn("Far chunk alread generated, can't test persistence in ProtoChunk."); + LOGGER.warn("Far chunk already generated, can't test persistence in ProtoChunk."); } else { - if (!"protochunk_data".equals(farChunk.getAttached(PERSISTENT))) throw new AssertionError("ProtoChunk attachement did not persist"); + if (!"protochunk_data".equals(farChunk.getAttached(PERSISTENT))) throw new AssertionError("ProtoChunk attachment did not persist"); } } - }); - ServerLifecycleEvents.SERVER_STOPPING.register(server -> firstLaunch = false); - // Testing hint: load far chunk by running /tp @s 480 ~ 0 + serverStarted = true; + }); + + // Testing hint: load far chunk by running /tp @s 4800 ~ 0 ServerChunkEvents.CHUNK_LOAD.register(((world, chunk) -> { if (!chunk.getPos().equals(FAR_CHUNK_POS)) return; + if (!serverStarted) { + LOGGER.warn("Chunk {} loaded before server started, can't test transfer of attachments to WorldChunk", FAR_CHUNK_POS); + return; + } + LOGGER.info("Loaded chunk {}, testing transfer of attachments to WorldChunk", FAR_CHUNK_POS); - if (!"protochunk_data".equals(chunk.getAttached(PERSISTENT))) throw new AssertionError("ProtoChunk attachement was not transfered to WorldChunk"); + if (!"protochunk_data".equals(chunk.getAttached(PERSISTENT))) throw new AssertionError("ProtoChunk attachment was not transfered to WorldChunk"); })); } }