Did a change to NBT saving and syncing that might fix weird bugs that are happening :v

This commit is contained in:
Ellpeck 2016-09-01 17:08:34 +02:00
parent 41e2daa0be
commit a89ff4bc29
3 changed files with 60 additions and 69 deletions

View file

@ -59,7 +59,7 @@ public final class PacketHandler{
if(world != null){
TileEntity tile = world.getTileEntity(new BlockPos(compound.getInteger("X"), compound.getInteger("Y"), compound.getInteger("Z")));
if(tile != null && tile instanceof TileEntityBase){
((TileEntityBase)tile).receiveSyncCompound(compound.getCompoundTag("Data"));
((TileEntityBase)tile).readSyncableNBT(compound.getCompoundTag("Data"), TileEntityBase.NBTType.SYNC);
}
}
}

View file

@ -26,7 +26,6 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
@ -130,41 +129,30 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
}
@Override
public void readFromNBT(NBTTagCompound compound){
this.readSyncableNBT(compound, NBTType.SAVE_TILE);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound){
public final NBTTagCompound writeToNBT(NBTTagCompound compound){
this.writeSyncableNBT(compound, NBTType.SAVE_TILE);
return compound;
}
@Override
public SPacketUpdateTileEntity getUpdatePacket(){
NBTTagCompound compound = this.getUpdateTag();
if(compound != null){
return new SPacketUpdateTileEntity(this.pos, 0, compound);
}
else{
return null;
}
public final void readFromNBT(NBTTagCompound compound){
this.readSyncableNBT(compound, NBTType.SAVE_TILE);
}
@Override
public final SPacketUpdateTileEntity getUpdatePacket(){
NBTTagCompound compound = new NBTTagCompound();
this.writeSyncableNBT(compound, NBTType.SYNC);
return new SPacketUpdateTileEntity(this.pos, -1, compound);
}
@Override
public final void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt){
if(pkt != null){
NBTTagCompound compound = pkt.getNbtCompound();
this.receiveSyncCompound(compound);
}
}
public void receiveSyncCompound(NBTTagCompound compound){
this.readSyncableNBT(compound, NBTType.SYNC);
this.readSyncableNBT(pkt.getNbtCompound(), NBTType.SYNC);
}
@Override
public NBTTagCompound getUpdateTag(){
public final NBTTagCompound getUpdateTag(){
NBTTagCompound compound = new NBTTagCompound();
this.writeSyncableNBT(compound, NBTType.SYNC);
return compound;
@ -172,12 +160,21 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
@Override
public final void handleUpdateTag(NBTTagCompound compound){
this.receiveSyncCompound(compound);
this.readSyncableNBT(compound, NBTType.SYNC);
}
@Override
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState){
return !oldState.getBlock().isAssociatedBlock(newState.getBlock());
public final void sendUpdate(){
if(!this.worldObj.isRemote){
NBTTagCompound compound = new NBTTagCompound();
this.writeSyncableNBT(compound, NBTType.SYNC);
NBTTagCompound data = new NBTTagCompound();
data.setTag("Data", compound);
data.setInteger("X", this.pos.getX());
data.setInteger("Y", this.pos.getY());
data.setInteger("Z", this.pos.getZ());
PacketHandler.theNetwork.sendToAllAround(new PacketServerToClient(data, PacketHandler.TILE_ENTITY_HANDLER), new NetworkRegistry.TargetPoint(this.worldObj.provider.getDimension(), this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), 128));
}
}
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
@ -192,11 +189,6 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
}
}
@Override
public ITextComponent getDisplayName(){
return new TextComponentTranslation("container."+ModUtil.MOD_ID+"."+this.name+".name");
}
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
super.readFromNBT(compound);
@ -210,7 +202,17 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
}
@Override
public void update(){
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState){
return !oldState.getBlock().isAssociatedBlock(newState.getBlock());
}
@Override
public ITextComponent getDisplayName(){
return new TextComponentTranslation("container."+ModUtil.MOD_ID+"."+this.name+".name");
}
@Override
public final void update(){
this.updateEntity();
}
@ -275,20 +277,6 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
}
}
public void sendUpdate(){
if(!this.worldObj.isRemote){
NBTTagCompound compound = this.getUpdateTag();
if(compound != null){
NBTTagCompound data = new NBTTagCompound();
data.setTag("Data", compound);
data.setInteger("X", this.pos.getX());
data.setInteger("Y", this.pos.getY());
data.setInteger("Z", this.pos.getZ());
PacketHandler.theNetwork.sendToAllAround(new PacketServerToClient(data, PacketHandler.TILE_ENTITY_HANDLER), new NetworkRegistry.TargetPoint(this.worldObj.provider.getDimension(), this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), 128));
}
}
}
@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing){
return this.getCapability(capability, facing) != null;

View file

@ -48,35 +48,38 @@ public abstract class TileEntityLaserRelay extends TileEntityBase{
}
@Override
public void receiveSyncCompound(NBTTagCompound compound){
ActuallyAdditionsAPI.connectionHandler.removeRelayFromNetwork(this.pos, this.worldObj);
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
super.readSyncableNBT(compound, type);
NBTTagList list = compound.getTagList("Connections", 10);
if(!list.hasNoTags()){
for(int i = 0; i < list.tagCount(); i++){
ConnectionPair pair = ConnectionPair.readFromNBT(list.getCompoundTagAt(i));
ActuallyAdditionsAPI.connectionHandler.addConnection(pair.positions[0], pair.positions[1], this.worldObj, pair.suppressConnectionRender);
if(type == NBTType.SYNC){
ActuallyAdditionsAPI.connectionHandler.removeRelayFromNetwork(this.pos, this.worldObj);
NBTTagList list = compound.getTagList("Connections", 10);
if(!list.hasNoTags()){
for(int i = 0; i < list.tagCount(); i++){
ConnectionPair pair = ConnectionPair.readFromNBT(list.getCompoundTagAt(i));
ActuallyAdditionsAPI.connectionHandler.addConnection(pair.positions[0], pair.positions[1], this.worldObj, pair.suppressConnectionRender);
}
}
}
super.receiveSyncCompound(compound);
}
@Override
public NBTTagCompound getUpdateTag(){
NBTTagCompound compound = super.getUpdateTag();
NBTTagList list = new NBTTagList();
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
super.writeSyncableNBT(compound, type);
ConcurrentSet<ConnectionPair> connections = ActuallyAdditionsAPI.connectionHandler.getConnectionsFor(this.pos, this.worldObj);
if(connections != null && !connections.isEmpty()){
for(ConnectionPair pair : connections){
list.appendTag(pair.writeToNBT());
if(type == NBTType.SYNC){
NBTTagList list = new NBTTagList();
ConcurrentSet<ConnectionPair> connections = ActuallyAdditionsAPI.connectionHandler.getConnectionsFor(this.pos, this.worldObj);
if(connections != null && !connections.isEmpty()){
for(ConnectionPair pair : connections){
list.appendTag(pair.writeToNBT());
}
}
}
compound.setTag("Connections", list);
return compound;
compound.setTag("Connections", list);
}
}
@Override