Fix forced chunks never being ticked by the server (#10325)

This commit is contained in:
Jonathing authored and GitHub committed 2025-02-03 13:47:36 -08:00
1 parent 464483db5b
commit 79efe2ac36
5 files changed
+182 -4

No files matched your search

@@ -13,6 +13,9 @@ import java.util.function.Supplier;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.ChatFormatting;
import net.minecraft.Util;
import net.minecraft.network.chat.MutableComponent;
import org.jetbrains.annotations.Nullable;
import com.mojang.authlib.GameProfile;
@@ -46,14 +49,25 @@ public interface IForgeGameTestHelper {
}
default void say(String message, Style style) {
var component = ForgeI18n.getPattern(message) != null ? Component.translatable(message) : Component.literal(message);
this.say(component.withStyle(style));
this.say(this.getMessage(message).withStyle(style));
}
default void say(String message, ChatFormatting style) {
this.say(this.getMessage(message).withStyle(style));
}
default void say(String message, int color) {
this.say(this.getMessage(message).withColor(color));
}
default void say(Component component) {
this.self().getLevel().players().forEach(p -> p.sendSystemMessage(component));
}
private MutableComponent getMessage(String message) {
return ForgeI18n.getPattern(message) != null ? Component.translatable(message) : Component.literal(message);
}
default void assertTrue(boolean value, Supplier<String> message) {
if (!value)
throw new GameTestAssertException(message.get());
@@ -180,6 +194,14 @@ public interface IForgeGameTestHelper {
return new IntFlag(name);
}
default IntFlag intFlag(String name, int value) {
return this.intFlag(name, (long) value);
}
default IntFlag intFlag(String name, long value) {
return Util.make(new IntFlag(name), flag -> flag.set(value));
}
default BoolFlag boolFlag(String name) {
return new BoolFlag(name);
}
@@ -258,6 +280,32 @@ public interface IForgeGameTestHelper {
super.set(value);
}
public void increment() {
this.decrement(1L);
}
public void increment(int amount) {
this.decrement((long) amount);
}
public void increment(long amount) {
if (this.value != null)
this.set(this.value + amount);
}
public void decrement() {
this.decrement(1L);
}
public void decrement(int amount) {
this.decrement((long) amount);
}
public void decrement(long amount) {
if (this.value != null)
this.set(this.value - amount);
}
public byte getByte() {
return this.value == null ? -1 : this.value.byteValue();
}
@@ -0,0 +1,73 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.debug.gameplay.level;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.gametest.framework.GameTest;
import net.minecraft.gametest.framework.GameTestHelper;
import net.minecraft.server.level.ChunkLevel;
import net.minecraft.server.level.FullChunkStatus;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.TicketType;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.gametest.GameTestHolder;
import net.minecraftforge.test.BaseTestMod;
import org.jetbrains.annotations.Nullable;
@GameTestHolder("forge." + ForcedChunkLoadingTest.MOD_ID)
@Mod(ForcedChunkLoadingTest.MOD_ID)
public class ForcedChunkLoadingTest extends BaseTestMod {
static final String MOD_ID = "forced_chunk_loading";
public ForcedChunkLoadingTest(FMLJavaModLoadingContext context) {
super(context);
}
private static final int MAX_CHUNK_LOCATION_ATTEMPTS = 5;
@GameTest(template = "forge:empty3x3x3")
public static void force_far_away_chunk(GameTestHelper helper) {
var random = RandomSource.create();
var level = helper.getLevel();
var chunkSource = level.getChunkSource();
// try to find a far away chunk that is not already ticking
int attempts;
var origin = helper.absolutePos(BlockPos.ZERO);
ChunkAccess chunk = null;
for (attempts = 0; attempts < MAX_CHUNK_LOCATION_ATTEMPTS && chunk == null; attempts++) {
int x = random.nextInt(1, 10) * 10000;
int z = random.nextInt(1, 10) * 10000;
chunk = level.getChunk(origin.offset(x, 0, z));
// If the chunk is already ticking, we can't force it
chunk = chunkSource.isPositionTicking(chunk.getPos().toLong()) ? null : chunk;
}
if (chunk == null) {
helper.fail("Failed to find a far away chunk that is not already ticking after " + MAX_CHUNK_LOCATION_ATTEMPTS + " attempts");
return;
} else if (attempts > 1) {
helper.say("WARNING: Finding a far away chunk took " + attempts + " attempts.", ChatFormatting.YELLOW);
}
var pos = chunk.getPos();
helper.say("Attempting to force far away chunk: " + pos, ChatFormatting.YELLOW);
chunkSource.chunkMap.getDistanceManager().addRegionTicket(TicketType.FORCED, pos, ChunkLevel.byStatus(FullChunkStatus.FULL), pos, true);
helper.runAfterDelay(20, () -> {
helper.assertTrue(chunkSource.chunkMap.getDistanceManager().shouldForceTicks(pos.toLong()), "Chunk is not ticketed as a forced loaded chunk");
helper.assertTrue(chunkSource.chunkMap.getDistanceManager().inEntityTickingRange(pos.toLong()), "Forced chunk cannot tick entities");
helper.assertTrue(chunkSource.chunkMap.getDistanceManager().inBlockTickingRange(pos.toLong()), "Forced chunk cannot tick blocks");
helper.assertTrue(chunkSource.hasChunk(pos.x, pos.z), "Chunk is not loaded despite being ticketed as a force loaded chunk");
helper.succeed();
});
}
}