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)

This commit is contained in:
vladislemon 2022-01-09 19:10:32 +03:00 committed by GitHub
parent 347117b95a
commit cf9965a598
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View file

@ -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();

View file

@ -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<LoadedChunk> chunks = CODEC.parse(NbtOps.INSTANCE, tag.getCompound("loadedchunks"))
List<LoadedChunk> 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<ChunkPos> CHUNK_POS_CODEC = RecordCodecBuilder.create(instance ->