Fix: Inform block entities when chunk is unloaded.

Forge's ITileEntity interface adds a onChunkUnloaded handler method into
block entities. The fabric port doesn't have a re-implementation of this
feature, which meant TileMonitors weren't releasing their buffers when
unloaded. This commit adds that handler method back into TileGeneric,
which all CC block entities inherit from. Handler logic for the four
block entities that use this feature were copied over from the forge
repo.
This commit is contained in:
ToadDev committed 2021-06-03 13:48:34 -07:00
1 parent c820e051b3
commit 1f117b7c47
7 files changed
+56 -9

No files matched your search

@@ -24,6 +24,7 @@ import dan200.computercraft.events.ClientUnloadWorldEvent;
import dan200.computercraft.shared.ComputerCraftRegistry;
import dan200.computercraft.shared.common.ContainerHeldItem;
import dan200.computercraft.shared.common.IColouredItem;
import dan200.computercraft.shared.common.TileGeneric;
import dan200.computercraft.shared.computer.inventory.ContainerComputer;
import dan200.computercraft.shared.computer.inventory.ContainerViewComputer;
import dan200.computercraft.shared.peripheral.diskdrive.ContainerDiskDrive;
@@ -33,6 +34,7 @@ import dan200.computercraft.shared.pocket.inventory.ContainerPocketComputer;
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
import dan200.computercraft.shared.turtle.inventory.ContainerTurtle;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientBlockEntityEvents;
import net.minecraft.client.item.ModelPredicateProvider;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.item.Item;
@@ -54,6 +56,12 @@ import net.fabricmc.fabric.mixin.object.builder.ModelPredicateProviderRegistrySp
public final class ComputerCraftProxyClient implements ClientModInitializer {
private static void initEvents() {
ClientBlockEntityEvents.BLOCK_ENTITY_UNLOAD.register( ( blockEntity, world ) -> {
if(blockEntity instanceof TileGeneric ) {
((TileGeneric)blockEntity).onChunkUnloaded();
}
});
ClientUnloadWorldEvent.EVENT.register( () -> ClientMonitor.destroyAll() );
}
@@ -28,6 +28,9 @@ public abstract class TileGeneric extends BlockEntity implements BlockEntityClie
public void destroy() {
}
public void onChunkUnloaded() {
}
public final void updateBlock() {
this.markDirty();
BlockPos pos = this.getPos();
@@ -77,6 +77,12 @@ public abstract class TileComputerBase extends TileGeneric implements IComputerT
}
}
@Override
public void onChunkUnloaded()
{
unload();
}
protected void unload() {
if (this.m_instanceID >= 0) {
if (!this.getWorld().isClient) {
@@ -83,6 +83,13 @@ public class TileCable extends TileGeneric implements IPeripheralTile {
}
}
@Override
public void onChunkUnloaded()
{
super.onChunkUnloaded();
onRemove();
}
private void onRemove() {
if (this.world == null || !this.world.isClient) {
this.m_node.remove();
@@ -74,6 +74,13 @@ public class TileWiredModemFull extends TileGeneric implements IPeripheralTile {
super.destroy();
}
@Override
public void onChunkUnloaded()
{
super.onChunkUnloaded();
doRemove();
}
private void doRemove() {
if (this.world == null || !this.world.isClient) {
this.m_node.remove();
@@ -79,6 +79,23 @@ public class TileMonitor extends TileGeneric implements IPeripheralTile {
}
}
@Override
public void markRemoved() {
super.markRemoved();
if (this.m_clientMonitor != null && this.m_xIndex == 0 && this.m_yIndex == 0) {
this.m_clientMonitor.destroy();
}
}
@Override
public void onChunkUnloaded() {
super.onChunkUnloaded();
if (this.m_clientMonitor != null && this.m_xIndex == 0 && this.m_yIndex == 0) {
this.m_clientMonitor.destroy();
}
this.m_clientMonitor = null;
}
@Nonnull
@Override
public ActionResult onActivate(PlayerEntity player, Hand hand, BlockHitResult hit) {
@@ -262,15 +279,6 @@ public class TileMonitor extends TileGeneric implements IPeripheralTile {
return ComputerCraft.monitorDistanceSq;
}
@Override
@Environment (EnvType.CLIENT)
public void markRemoved() {
super.markRemoved();
if (this.m_clientMonitor != null && this.m_xIndex == 0 && this.m_yIndex == 0) {
this.m_clientMonitor.destroy();
}
}
// Sizing and placement stuff
@Override
@@ -17,6 +17,7 @@ import dan200.computercraft.shared.TurtlePermissions;
import dan200.computercraft.shared.command.CommandComputerCraft;
import dan200.computercraft.shared.command.arguments.ArgumentSerializers;
import dan200.computercraft.shared.common.DefaultBundledRedstoneProvider;
import dan200.computercraft.shared.common.TileGeneric;
import dan200.computercraft.shared.data.BlockNamedEntityLootCondition;
import dan200.computercraft.shared.data.HasComputerIdLootCondition;
import dan200.computercraft.shared.data.PlayerCreativeLootCondition;
@@ -28,6 +29,7 @@ import dan200.computercraft.shared.turtle.FurnaceRefuelHandler;
import dan200.computercraft.shared.turtle.SignInspectHandler;
import dan200.computercraft.shared.util.TickScheduler;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerBlockEntityEvents;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.CommandBlockBlockEntity;
import net.minecraft.item.Item;
@@ -106,6 +108,12 @@ public final class ComputerCraftProxyCommon {
ComputerCraftProxyCommon.server = null;
});
ServerBlockEntityEvents.BLOCK_ENTITY_UNLOAD.register( ( blockEntity, world ) -> {
if(blockEntity instanceof TileGeneric ) {
((TileGeneric)blockEntity).onChunkUnloaded();
}
});
TurtleEvent.EVENT_BUS.register(FurnaceRefuelHandler.INSTANCE);
TurtleEvent.EVENT_BUS.register(new TurtlePermissions());
TurtleEvent.EVENT_BUS.register(new SignInspectHandler());