Skip to content

Commit

Permalink
invalidate pipenet routepaths on chunk unload (#2303)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechLord22 authored and serenibyss committed Dec 18, 2023
1 parent 028562c commit d41a036
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import org.jetbrains.annotations.MustBeInvokedByOverriders;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -28,24 +29,34 @@ protected void invalidateNeighbors() {
}
}

@MustBeInvokedByOverriders
@Override
public void setWorld(@NotNull World worldIn) {
super.setWorld(worldIn);
invalidateNeighbors();
}

@MustBeInvokedByOverriders
@Override
public void setPos(@NotNull BlockPos posIn) {
super.setPos(posIn);
invalidateNeighbors();
}

@MustBeInvokedByOverriders
@Override
public void invalidate() {
super.invalidate();
invalidateNeighbors();
}

@MustBeInvokedByOverriders
@Override
public void onChunkUnload() {
super.onChunkUnload();
invalidateNeighbors();
}

@Override
public @Nullable TileEntity getNeighbor(@NotNull EnumFacing facing) {
if (world == null || pos == null) return null;
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/gregtech/api/pipenet/PipeNet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;

import java.util.*;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public abstract class PipeNet<NodeDataType> implements INBTSerializable<NBTTagCompound> {

Expand Down Expand Up @@ -63,6 +68,11 @@ public void onPipeConnectionsUpdate() {}

public void onNeighbourUpdate(BlockPos fromPos) {}

/**
* Is called when any Pipe TE in the PipeNet is unloaded
*/
public void onChunkUnload() {}

public Map<BlockPos, Node<NodeDataType>> getAllNodes() {
return unmodifiableNodeByBlockPos;
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/gregtech/api/pipenet/tile/TileEntityPipeBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.Constants.NBT;

import org.jetbrains.annotations.MustBeInvokedByOverriders;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -539,6 +540,17 @@ public boolean shouldRefresh(@NotNull World world, @NotNull BlockPos pos, IBlock
return oldState.getBlock() != newSate.getBlock();
}

@MustBeInvokedByOverriders
@Override
public void onChunkUnload() {
super.onChunkUnload();
WorldPipeNet<?, ?> worldPipeNet = getPipeBlock().getWorldPipeNet(getWorld());
PipeNet<?> net = worldPipeNet.getNetFromPos(pos);
if (net != null) {
net.onChunkUnload();
}
}

public void doExplosion(float explosionPower) {
getWorld().setBlockToAir(getPos());
if (!getWorld().isRemote) {
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/gregtech/common/pipelike/cable/net/EnergyNet.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;

import java.util.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public class EnergyNet extends PipeNet<WireProperties> {

Expand Down Expand Up @@ -67,6 +70,11 @@ public void onPipeConnectionsUpdate() {
NET_DATA.clear();
}

@Override
public void onChunkUnload() {
NET_DATA.clear();
}

@Override
protected void transferNodeData(Map<BlockPos, Node<WireProperties>> transferredNodes,
PipeNet<WireProperties> parentNet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public <T> T getCapabilityInternal(Capability<T> capability, @Nullable EnumFacin
if (capability == GregtechCapabilities.CAPABILITY_ENERGY_CONTAINER) {
if (world.isRemote)
return GregtechCapabilities.CAPABILITY_ENERGY_CONTAINER.cast(clientCapability);
if (handlers.size() == 0)
if (handlers.isEmpty())
initHandlers();
checkNetwork();
return GregtechCapabilities.CAPABILITY_ENERGY_CONTAINER.cast(handlers.getOrDefault(facing, defaultHandler));
Expand Down Expand Up @@ -293,6 +293,12 @@ private EnergyNet getEnergyNet() {
return currentEnergyNet;
}

@Override
public void onChunkUnload() {
super.onChunkUnload();
this.handlers.clear();
}

@Override
public int getDefaultPaintingColor() {
return 0x404040;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;

import java.util.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ItemPipeNet extends PipeNet<ItemPipeProperties> {

Expand Down Expand Up @@ -43,6 +47,11 @@ public void onPipeConnectionsUpdate() {
NET_DATA.clear();
}

@Override
public void onChunkUnload() {
NET_DATA.clear();
}

@Override
protected void transferNodeData(Map<BlockPos, Node<ItemPipeProperties>> transferredNodes,
PipeNet<ItemPipeProperties> parentNet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,10 @@ public int getTransferredItems() {
updateTransferredState();
return this.transferredItems;
}

@Override
public void onChunkUnload() {
super.onChunkUnload();
this.handlers.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public void onPipeConnectionsUpdate() {
netData.clear();
}

@Override
public void onChunkUnload() {
netData.clear();
}

@Override
protected void transferNodeData(Map<BlockPos, Node<LaserPipeProperties>> transferredNodes,
PipeNet<LaserPipeProperties> parentNet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ public void readFromNBT(@NotNull NBTTagCompound compound) {
}
}

@Override
public void onChunkUnload() {
super.onChunkUnload();
this.handlers.clear();
}

private static class DefaultLaserContainer implements ILaserContainer {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public void onPipeConnectionsUpdate() {
NET_DATA.clear();
}

@Override
public void onChunkUnload() {
NET_DATA.clear();
}

@Override
protected void transferNodeData(Map<BlockPos, Node<OpticalPipeProperties>> transferredNodes,
PipeNet<OpticalPipeProperties> parentNet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ public void receiveCustomData(int discriminator, PacketBuffer buf) {
}
}

@Override
public void onChunkUnload() {
super.onChunkUnload();
this.handlers.clear();
}

private static class DefaultDataHandler implements IDataAccessHatch {

@Override
Expand Down

0 comments on commit d41a036

Please sign in to comment.