Add isNewChunk to ChunkEvent.Load (#9369)

This commit is contained in:
Jason authored and GitHub committed 2023-03-20 14:15:33 -07:00
1 parent 576ff81856
commit 7b8ca60d32
5 files changed
+84 -2

No files matched your search

@@ -12,7 +12,7 @@
}
this.f_104411_.m_171649_(chunkpos);
+ net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.level.ChunkEvent.Load(levelchunk));
+ net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.level.ChunkEvent.Load(levelchunk, false));
return levelchunk;
}
}
@@ -24,7 +24,7 @@
+ p_140384_.currentlyLoading = levelchunk; // Forge - bypass the future chain when getChunk is called, this prevents deadlocks.
levelchunk.m_156369_();
levelchunk.m_187958_(this.f_140133_);
+ net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.level.ChunkEvent.Load(levelchunk));
+ net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.level.ChunkEvent.Load(levelchunk, !(protochunk instanceof ImposterProtoChunk)));
+ } finally {
+ p_140384_.currentlyLoading = null; // Forge - Stop bypassing the future chain.
+ }
@@ -12,6 +12,7 @@ import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
import org.jetbrains.annotations.ApiStatus;
/**
* ChunkEvent is fired when an event involving a chunk occurs.<br>
@@ -58,9 +59,32 @@ public class ChunkEvent extends LevelEvent
**/
public static class Load extends ChunkEvent
{
private final boolean newChunk;
@ApiStatus.Internal
@Deprecated(forRemoval = true, since = "1.19.3")
public Load(ChunkAccess chunk)
{
this(chunk, false);
}
@ApiStatus.Internal
public Load(ChunkAccess chunk, boolean newChunk)
{
super(chunk);
this.newChunk = newChunk;
}
/**
* Check whether the Chunk is newly generated, and being loaded for the first time.
*
* <p>Will only ever return {@code true} on the {@linkplain net.minecraftforge.fml.LogicalSide#SERVER logical server}.</p>
*
* @return whether the Chunk is newly generated
*/
public boolean isNewChunk()
{
return newChunk;
}
}
@@ -0,0 +1,56 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.debug.world;
import com.mojang.logging.LogUtils;
import net.minecraft.server.level.ServerLevel;
import net.minecraftforge.event.level.ChunkEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.slf4j.Logger;
/**
* Simple test for {@link ChunkEvent.Load#isNewChunk()}. Will log a message to console each time
* the event is fired for a newly generated chunk.
*
* <p>A simple way to check that it's behaving as expected is as follows:</p>
* <ul>
* <li>Generate a new world (and log in if using dedicated server). You should see many log messages about
* the spawn chunks being generated.</li>
* <li>Don't move around in game, to make sure we only generate the spawn chunks, and any chunks loaded
* by the player chunk loader for the players initial login position. (to make this test as
* reproducible as possible without lots of complexity)</li>
* <li>Once a couple seconds pass without any new chunk log messages, close out of the world/stop the server.</li>
* <li>Load the world/start the server (and log in if using dedicated server) again. For the same reasons as before,
* don't move around.</li>
* <li>You shouldn't see any log messages for new chunks this time.
* (until you start moving around to generate more chunks)</li>
* </ul>
*/
@Mod(ChunkEventLoadNewChunkTest.MODID)
@Mod.EventBusSubscriber
public class ChunkEventLoadNewChunkTest
{
static final String MODID = "chunk_event_load_new_chunk_test";
private static final boolean ENABLED = true;
private static final Logger LOGGER = LogUtils.getLogger();
@SubscribeEvent
public static void onChunkLoad(final ChunkEvent.Load event)
{
if (!ENABLED)
{
return;
}
if (!event.isNewChunk())
{
return;
}
LOGGER.info("Loaded freshly generated chunk at {}{}", ((ServerLevel) event.getLevel()).dimension().location(), event.getChunk().getPos());
}
}
+2
View File
@@ -280,6 +280,8 @@ modId="creative_mode_tab_test"
modId="render_level_stages_test"
[[mods]]
modId="trade_with_villager_event_test"
[[mods]]
modId="chunk_event_load_new_chunk_test"
# ADD ABOVE THIS LINE