From cf9965a5985ebb32c93b3958a91c1b698d36d3c3 Mon Sep 17 00:00:00 2001 From: vladislemon Date: Sun, 9 Jan 2022 19:10:32 +0300 Subject: [PATCH] Fixes #2680. Chunks that had tickets before world save/load now takes tickets after world load. Chunk Loaders now works without players in world. (#2735) --- .../src/main/java/reborncore/RebornCore.java | 7 ++++++- .../chunkloading/ChunkLoaderManager.java | 21 ++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/RebornCore/src/main/java/reborncore/RebornCore.java b/RebornCore/src/main/java/reborncore/RebornCore.java index 34cceb460..b93771521 100644 --- a/RebornCore/src/main/java/reborncore/RebornCore.java +++ b/RebornCore/src/main/java/reborncore/RebornCore.java @@ -27,6 +27,8 @@ package reborncore; import net.fabricmc.api.EnvType; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerBlockEntityEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents; import net.fabricmc.fabric.api.event.world.WorldTickCallback; import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage; import net.fabricmc.loader.api.FabricLoader; @@ -39,9 +41,9 @@ import reborncore.common.RebornCoreCommands; import reborncore.common.RebornCoreConfig; import reborncore.common.blockentity.MachineBaseBlockEntity; import reborncore.common.blocks.BlockWrenchEventHandler; +import reborncore.common.chunkloading.ChunkLoaderManager; import reborncore.common.config.Configuration; import reborncore.common.crafting.ingredient.IngredientManager; -import reborncore.common.fluid.RebornFluidManager; import reborncore.common.misc.ModSounds; import reborncore.common.misc.RebornCoreTags; import reborncore.common.multiblock.MultiblockRegistry; @@ -118,6 +120,9 @@ public class RebornCore implements ModInitializer { if (blockEntity instanceof UnloadHandler) ((UnloadHandler) blockEntity).onUnload(); }); + ServerWorldEvents.LOAD.register((server, world) -> ChunkLoaderManager.get(world).onServerWorldLoad(world)); + ServerTickEvents.START_WORLD_TICK.register(world -> ChunkLoaderManager.get(world).onServerWorldTick(world)); + FluidStorage.SIDED.registerFallback((world, pos, state, be, direction) -> { if (be instanceof MachineBaseBlockEntity machineBase) { return machineBase.getTank(); diff --git a/RebornCore/src/main/java/reborncore/common/chunkloading/ChunkLoaderManager.java b/RebornCore/src/main/java/reborncore/common/chunkloading/ChunkLoaderManager.java index 1ac6a7d44..1083d5a60 100644 --- a/RebornCore/src/main/java/reborncore/common/chunkloading/ChunkLoaderManager.java +++ b/RebornCore/src/main/java/reborncore/common/chunkloading/ChunkLoaderManager.java @@ -27,6 +27,7 @@ package reborncore.common.chunkloading; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtElement; import net.minecraft.nbt.NbtOps; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ChunkTicketType; @@ -70,7 +71,7 @@ public class ChunkLoaderManager extends PersistentState { chunkLoaderManager.loadedChunks.clear(); - List chunks = CODEC.parse(NbtOps.INSTANCE, tag.getCompound("loadedchunks")) + List chunks = CODEC.parse(NbtOps.INSTANCE, tag.getList("loadedchunks", NbtElement.COMPOUND_TYPE)) .result() .orElse(Collections.emptyList()); @@ -123,8 +124,7 @@ public class ChunkLoaderManager extends PersistentState { LoadedChunk loadedChunk = new LoadedChunk(chunkPos, getWorldName(world), player, chunkLoader); loadedChunks.add(loadedChunk); - final ServerChunkManager serverChunkManager = ((ServerWorld) world).getChunkManager(); - serverChunkManager.addTicket(ChunkLoaderManager.CHUNK_LOADER, loadedChunk.getChunk(), RADIUS, loadedChunk.getChunk()); + loadChunk((ServerWorld) world, loadedChunk); markDirty(); } @@ -148,6 +148,16 @@ public class ChunkLoaderManager extends PersistentState { markDirty(); } + public void onServerWorldLoad(ServerWorld world) { + loadedChunks.forEach(loadedChunk -> loadChunk(world, loadedChunk)); + } + + public void onServerWorldTick(ServerWorld world) { + if (!loadedChunks.isEmpty()) { + world.resetIdleTimeout(); + } + } + public static Identifier getWorldName(World world){ return world.getRegistryKey().getValue(); } @@ -172,6 +182,11 @@ public class ChunkLoaderManager extends PersistentState { NetworkManager.sendToPlayer(ClientBoundPackets.createPacketSyncLoadedChunks(chunks), serverPlayerEntity); } + private void loadChunk(ServerWorld world, LoadedChunk loadedChunk) { + ChunkPos chunkPos = loadedChunk.getChunk(); + world.getChunkManager().addTicket(ChunkLoaderManager.CHUNK_LOADER, chunkPos, RADIUS, chunkPos); + } + public static class LoadedChunk { public static Codec CHUNK_POS_CODEC = RecordCodecBuilder.create(instance ->