Fixed a bug with Laser Relays sometimes randomly disconnecting on chunk load.

*Fuck this game.*
This commit is contained in:
Ellpeck 2016-06-05 16:01:10 +02:00
parent 9ad6dfcd0f
commit d46bb306d8

View file

@ -12,6 +12,7 @@ package de.ellpeck.actuallyadditions.mod.tile;
import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues; import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler; import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler;
import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler.ConnectionPair;
import de.ellpeck.actuallyadditions.mod.network.PacketParticle; import de.ellpeck.actuallyadditions.mod.network.PacketParticle;
import de.ellpeck.actuallyadditions.mod.util.PosUtil; import de.ellpeck.actuallyadditions.mod.util.PosUtil;
import de.ellpeck.actuallyadditions.mod.util.Util; import de.ellpeck.actuallyadditions.mod.util.Util;
@ -22,6 +23,8 @@ import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.Set;
public abstract class TileEntityLaserRelay extends TileEntityBase{ public abstract class TileEntityLaserRelay extends TileEntityBase{
public static final int MAX_DISTANCE = 15; public static final int MAX_DISTANCE = 15;
@ -30,6 +33,8 @@ public abstract class TileEntityLaserRelay extends TileEntityBase{
public final boolean isItem; public final boolean isItem;
private Set<ConnectionPair> tempConnectionStorage;
public TileEntityLaserRelay(String name, boolean isItem){ public TileEntityLaserRelay(String name, boolean isItem){
super(name); super(name);
this.isItem = isItem; this.isItem = isItem;
@ -42,7 +47,7 @@ public abstract class TileEntityLaserRelay extends TileEntityBase{
NBTTagList list = compound.getTagList("Connections", 10); NBTTagList list = compound.getTagList("Connections", 10);
if(!list.hasNoTags()){ if(!list.hasNoTags()){
for(int i = 0; i < list.tagCount(); i++){ for(int i = 0; i < list.tagCount(); i++){
LaserRelayConnectionHandler.ConnectionPair pair = LaserRelayConnectionHandler.ConnectionPair.readFromNBT(list.getCompoundTagAt(i)); ConnectionPair pair = ConnectionPair.readFromNBT(list.getCompoundTagAt(i));
LaserRelayConnectionHandler.addConnection(pair.positions[0], pair.positions[1], this.worldObj); LaserRelayConnectionHandler.addConnection(pair.positions[0], pair.positions[1], this.worldObj);
} }
} }
@ -56,9 +61,9 @@ public abstract class TileEntityLaserRelay extends TileEntityBase{
NBTTagCompound compound = super.getUpdateTag(); NBTTagCompound compound = super.getUpdateTag();
NBTTagList list = new NBTTagList(); NBTTagList list = new NBTTagList();
ConcurrentSet<LaserRelayConnectionHandler.ConnectionPair> connections = LaserRelayConnectionHandler.getConnectionsFor(this.pos, this.worldObj); ConcurrentSet<ConnectionPair> connections = LaserRelayConnectionHandler.getConnectionsFor(this.pos, this.worldObj);
if(connections != null && !connections.isEmpty()){ if(connections != null && !connections.isEmpty()){
for(LaserRelayConnectionHandler.ConnectionPair pair : connections){ for(ConnectionPair pair : connections){
list.appendTag(pair.writeToNBT()); list.appendTag(pair.writeToNBT());
} }
} }
@ -81,7 +86,7 @@ public abstract class TileEntityLaserRelay extends TileEntityBase{
BlockPos thisPos = this.pos; BlockPos thisPos = this.pos;
LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getNetworkFor(thisPos, this.worldObj); LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getNetworkFor(thisPos, this.worldObj);
if(network != null){ if(network != null){
for(LaserRelayConnectionHandler.ConnectionPair aPair : network.connections){ for(ConnectionPair aPair : network.connections){
if(aPair.contains(thisPos) && PosUtil.areSamePos(thisPos, aPair.positions[0])){ if(aPair.contains(thisPos) && PosUtil.areSamePos(thisPos, aPair.positions[0])){
PacketParticle.renderParticlesFromAToB(aPair.positions[0].getX(), aPair.positions[0].getY(), aPair.positions[0].getZ(), aPair.positions[1].getX(), aPair.positions[1].getY(), aPair.positions[1].getZ(), ConfigBoolValues.LESS_PARTICLES.isEnabled() ? 1 : Util.RANDOM.nextInt(3)+1, 0.8F, this.isItem ? COLOR_ITEM : COLOR, 1F); PacketParticle.renderParticlesFromAToB(aPair.positions[0].getX(), aPair.positions[0].getY(), aPair.positions[0].getZ(), aPair.positions[1].getX(), aPair.positions[1].getY(), aPair.positions[1].getZ(), ConfigBoolValues.LESS_PARTICLES.isEnabled() ? 1 : Util.RANDOM.nextInt(3)+1, 0.8F, this.isItem ? COLOR_ITEM : COLOR, 1F);
} }
@ -93,7 +98,22 @@ public abstract class TileEntityLaserRelay extends TileEntityBase{
@Override @Override
public void invalidate(){ public void invalidate(){
super.invalidate(); super.invalidate();
//This is because Minecraft randomly invalidates tiles on world join and then validates them again
//We need to compensate for this so that connections don't get broken randomly
this.tempConnectionStorage = LaserRelayConnectionHandler.getConnectionsFor(this.pos, this.worldObj);
LaserRelayConnectionHandler.removeRelayFromNetwork(this.pos, this.worldObj); LaserRelayConnectionHandler.removeRelayFromNetwork(this.pos, this.worldObj);
} }
@Override
public void validate(){
if(this.tempConnectionStorage != null){
for(ConnectionPair pair : this.tempConnectionStorage){
LaserRelayConnectionHandler.addConnection(pair.positions[0], pair.positions[1], this.worldObj);
}
this.tempConnectionStorage = null;
}
super.validate();
}
} }