Skip to content

Commit

Permalink
Fix problems with cable unloading (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte authored Aug 7, 2024
1 parent b37e940 commit f7ea2d4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
22 changes: 15 additions & 7 deletions common/src/main/java/owmii/powah/block/cable/CableNet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions fabric/src/main/java/owmii/powah/fabric/PowahFabric.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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;
import net.minecraft.core.registries.BuiltInRegistries;
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;
Expand All @@ -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));
Expand Down

0 comments on commit f7ea2d4

Please sign in to comment.