Skip to content

Commit

Permalink
Fix data attachment testmod (#3586)
Browse files Browse the repository at this point in the history
* fix data attachment testmod: use marker file to determine first/second launch

* fix typos

* one more typo

(cherry picked from commit 4e3ec63)
  • Loading branch information
jacobsjo authored and modmuss50 committed Feb 12, 2024
1 parent 3335cde commit 2ee1668
Showing 1 changed file with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +29,7 @@
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;
Expand Down Expand Up @@ -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
Expand All @@ -70,11 +74,18 @@ public void onInitialize() {
);

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;

try {
firstLaunch = markerFile.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
}

overworld = server.getOverworld();
chunk = overworld.getChunk(0, 0);
ServerWorld overworld = server.getOverworld();
WorldChunk chunk = overworld.getChunk(0, 0);

if (firstLaunch) {
LOGGER.info("First launch, testing attachment by feature");
Expand All @@ -98,30 +109,36 @@ public void onInitialize() {
} 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");
}
}

serverStarted = true;
});
ServerLifecycleEvents.SERVER_STOPPING.register(server -> firstLaunch = false);

// Testing hint: load far chunk by running /tp @s 480 ~ 0
// 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");
}));
}
}

0 comments on commit 2ee1668

Please sign in to comment.