From f7ea2d45c58e03d5e685aee3f0fcdb0ddeaacbb3 Mon Sep 17 00:00:00 2001 From: shartte Date: Wed, 7 Aug 2024 10:34:13 +0200 Subject: [PATCH] Fix problems with cable unloading (#181) --- .../owmii/powah/block/cable/CableNet.java | 22 +++++++++++++------ .../java/owmii/powah/fabric/PowahFabric.java | 2 -- .../fabric/client/PowahFabricClient.java | 4 ++++ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/common/src/main/java/owmii/powah/block/cable/CableNet.java b/common/src/main/java/owmii/powah/block/cable/CableNet.java index 54fdd721..77247023 100644 --- a/common/src/main/java/owmii/powah/block/cable/CableNet.java +++ b/common/src/main/java/owmii/powah/block/cable/CableNet.java @@ -39,21 +39,29 @@ static void addCable(CableTile cable) { } static void removeCable(CableTile cable) { - var levelMap = loadedCables.get(cable.getLevel()); - var chunkPos = ChunkPos.asLong(cable.getBlockPos()); + var level = cable.getLevel(); + if (level == null) { + return; + } + + var levelMap = loadedCables.get(level); + if (levelMap == null) { + return; // The full-chunk cleanup already took care of the entire level + } + var blockPos = cable.getBlockPos(); + var chunkPos = ChunkPos.asLong(blockPos); var chunkMap = levelMap.get(chunkPos); if (chunkMap == null) { - LOG.error("Ignoring to unload cable @ {} since chunk is already gone.", cable.getBlockPos()); - return; + return; // The full-chunk cleanup already took care of the entire chunk } - if (chunkMap.remove(cable.getBlockPos().asLong()) != cable) { - throw new RuntimeException("Removed wrong cable from position %s".formatted(cable.getBlockPos())); + if (chunkMap.remove(blockPos.asLong()) != cable) { + throw new RuntimeException("Removed wrong cable from position %s".formatted(blockPos)); } if (chunkMap.isEmpty()) { levelMap.remove(chunkPos); } if (levelMap.isEmpty()) { - loadedCables.remove(cable.getLevel()); + loadedCables.remove(level); } updateAdjacentCables(cable); diff --git a/fabric/src/main/java/owmii/powah/fabric/PowahFabric.java b/fabric/src/main/java/owmii/powah/fabric/PowahFabric.java index 26dfdacc..e8511ed2 100644 --- a/fabric/src/main/java/owmii/powah/fabric/PowahFabric.java +++ b/fabric/src/main/java/owmii/powah/fabric/PowahFabric.java @@ -1,7 +1,6 @@ package owmii.powah.fabric; import net.fabricmc.api.ModInitializer; -import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientChunkEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerChunkEvents; import net.fabricmc.fabric.api.event.player.UseBlockCallback; import net.minecraft.SharedConstants; @@ -23,7 +22,6 @@ public void onInitialize() { SharedConstants.CHECK_DATA_FIXER_SCHEMA = checkDataFixer; ServerChunkEvents.CHUNK_UNLOAD.register(CableNet::removeChunk); - ClientChunkEvents.CHUNK_UNLOAD.register(CableNet::removeChunk); UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> { if (Wrench.removeWithWrench(player, world, hand, hitResult)) { diff --git a/fabric/src/main/java/owmii/powah/fabric/client/PowahFabricClient.java b/fabric/src/main/java/owmii/powah/fabric/client/PowahFabricClient.java index ece8e4c2..da880642 100644 --- a/fabric/src/main/java/owmii/powah/fabric/client/PowahFabricClient.java +++ b/fabric/src/main/java/owmii/powah/fabric/client/PowahFabricClient.java @@ -1,6 +1,7 @@ package owmii.powah.fabric.client; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientChunkEvents; import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents; import net.fabricmc.fabric.api.event.client.player.ClientPickBlockGatherCallback; @@ -8,6 +9,7 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.phys.BlockHitResult; import owmii.powah.block.Blcks; +import owmii.powah.block.cable.CableNet; import owmii.powah.client.PowahClient; import owmii.powah.client.handler.ReactorOverlayHandler; import owmii.powah.client.render.tile.ReactorItemRenderer; @@ -20,6 +22,8 @@ public void onInitializeClient() { PowahClient.init(); PowahClient.clientSetup(); + ClientChunkEvents.CHUNK_UNLOAD.register(CableNet::removeChunk); + var reactorRenderer = new ReactorItemRenderer(); Blcks.REACTOR.getAll().forEach(block -> { var item = (ReactorItem) BuiltInRegistries.ITEM.get(BuiltInRegistries.BLOCK.getKey(block));