Skip to content

Commit

Permalink
fix issues for dimension based WorldSaveData
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss committed Jul 5, 2021
1 parent 02541b6 commit af8296e
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 12 deletions.
18 changes: 12 additions & 6 deletions src/main/java/gregtech/api/pipenet/WorldPipeNet.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import net.minecraft.world.storage.WorldSavedData;
import net.minecraftforge.common.util.Constants.NBT;

import java.lang.ref.WeakReference;
import java.util.*;

public abstract class WorldPipeNet<NodeDataType, T extends PipeNet<NodeDataType>> extends WorldSavedData {

private World world;
protected boolean isFirstTick = true;
private WeakReference<World> worldRef = new WeakReference<>(null);
protected List<T> pipeNets = new ArrayList<>();
protected Map<ChunkPos, List<T>> pipeNetsByChunk = new HashMap<>();

Expand All @@ -23,17 +23,23 @@ public WorldPipeNet(String name) {
}

public World getWorld() {
return world;
return this.worldRef.get();
}

protected void setWorldAndInit(World world) {
if (isFirstTick) {
this.world = world;
this.isFirstTick = false;
if (world != this.worldRef.get()) {
this.worldRef = new WeakReference<>(world);
onWorldSet();
}
}

public static String getDataID(final String baseID, final World world) {
if (world == null || world.isRemote)
throw new RuntimeException("WorldPipeNet should only be created on the server!");
int dimension = world.provider.getDimension();
return dimension == 0 ? baseID : baseID + '.' + dimension;
}

protected void onWorldSet() {
this.pipeNets.forEach(PipeNet::onConnectionsUpdate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,25 @@ public static void registerTickablePipeNet(Function<World, TickableWorldPipeNet<

@SubscribeEvent
public static void onWorldTick(WorldTickEvent event) {
getPipeNetsForWorld(event.world).forEach(TickableWorldPipeNet::update);
World world = event.world;
if (world.isRemote)
return;
getPipeNetsForWorld(world).forEach(TickableWorldPipeNet::update);
}

@SubscribeEvent
public static void onChunkLoad(ChunkEvent.Load event) {
getPipeNetsForWorld(event.getWorld()).forEach(it -> it.onChunkLoaded(event.getChunk()));
World world = event.getWorld();
if (world.isRemote)
return;
getPipeNetsForWorld(world).forEach(it -> it.onChunkLoaded(event.getChunk()));
}

@SubscribeEvent
public static void onChunkUnload(ChunkEvent.Unload event) {
getPipeNetsForWorld(event.getWorld()).forEach(it -> it.onChunkUnloaded(event.getChunk()));
World world = event.getWorld();
if (world.isRemote)
return;
getPipeNetsForWorld(world).forEach(it -> it.onChunkUnloaded(event.getChunk()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public int onPipeToolUsed(ItemStack stack, EnumFacing coverSide, IPipeTile<Insul

@Override
public void onEntityCollision(World worldIn, BlockPos pos, IBlockState state, Entity entityIn) {
if (worldIn.isRemote) return;
Insulation insulation = getPipeTileEntity(worldIn, pos).getPipeType();
boolean damageOnLossless = ConfigHolder.doLosslessWiresDamage;
if (!worldIn.isRemote && insulation.insulationLevel == -1 && entityIn instanceof EntityLivingBase) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

public class WorldENet extends WorldPipeNet<WireProperties, EnergyNet> {

private static final String DATA_ID = "gregtech.e_net";
private static final String DATA_ID_BASE = "gregtech.e_net";

public static WorldENet getWorldENet(World world) {
final String DATA_ID = getDataID(DATA_ID_BASE, world);
WorldENet eNetWorldData = (WorldENet) world.loadData(WorldENet.class, DATA_ID);
if (eNetWorldData == null) {
eNetWorldData = new WorldENet(DATA_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public boolean canPushIntoFluidHandler(IPipeTile<FluidPipeType, FluidPipePropert

@Override
public void onEntityCollision(World worldIn, BlockPos pos, IBlockState state, Entity entityIn) {
if (worldIn.isRemote) return;
if (entityIn instanceof EntityLivingBase && entityIn.world.getWorldTime() % 20 == 0L) {
EntityLivingBase entityLiving = (EntityLivingBase) entityIn;
FluidPipeNet pipeNet = getWorldPipeNet(worldIn).getNetFromPos(pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,18 @@ protected FluidPipeProperties readNodeData(NBTTagCompound tagCompound) {
return new FluidPipeProperties(maxTemperature, throughput, gasProof);
}

@Override
public NBTTagCompound serializeNBT() {
NBTTagCompound nbt = super.serializeNBT();
nbt.setTag("FluidTankNet", this.fluidNetTank.writeToNBT(new NBTTagCompound()));
return nbt;
}

@Override
public void deserializeNBT(NBTTagCompound nbt) {
super.deserializeNBT(nbt);
if (nbt.hasKey("FluidTankNet"))
this.fluidNetTank.readFromNBT(nbt.getCompoundTag("FluidTankNet"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

public class WorldFluidPipeNet extends WorldPipeNet<FluidPipeProperties, FluidPipeNet> {

private static final String DATA_ID = "gregtech.fluid_pipe_net";
private static final String DATA_ID_BASE = "gregtech.fluid_pipe_net";

public static WorldFluidPipeNet getWorldPipeNet(World world) {
final String DATA_ID = getDataID(DATA_ID_BASE, world);
WorldFluidPipeNet netWorldData = (WorldFluidPipeNet) world.loadData(WorldFluidPipeNet.class, DATA_ID);
if (netWorldData == null) {
netWorldData = new WorldFluidPipeNet(DATA_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

public class WorldInventoryPipeNet extends TickableWorldPipeNet<EmptyNodeData, InventoryPipeNet> {

private static final String DATA_ID = "gregtech.inventory_pipe_net";
private static final String DATA_ID_BASE = "gregtech.inventory_pipe_net";

public static WorldInventoryPipeNet getWorldPipeNet(World world) {
final String DATA_ID = getDataID(DATA_ID_BASE, world);
WorldInventoryPipeNet netWorldData = (WorldInventoryPipeNet) world.loadData(WorldInventoryPipeNet.class, DATA_ID);
if (netWorldData == null) {
netWorldData = new WorldInventoryPipeNet(DATA_ID);
Expand Down

0 comments on commit af8296e

Please sign in to comment.