Improved inconsistent NBT read/write

This commit is contained in:
LemADEC 2017-02-09 21:31:33 +01:00
parent 1d9409f5ef
commit fffe79ebba
3 changed files with 54 additions and 53 deletions

View file

@ -35,7 +35,7 @@ public class TileEntityForceField extends TileEntityAbstractBase {
public void readFromNBT(NBTTagCompound tag) {
super.readFromNBT(tag);
if (tag.hasKey("projector")) {
vProjector = VectorI.readFromNBT(tag.getCompoundTag("projector"));
vProjector = VectorI.createFromNBT(tag.getCompoundTag("projector"));
cache_beamFrequency = tag.getInteger("beamFrequency");
if (tag.hasKey("projector")) {
try {

View file

@ -71,15 +71,6 @@ public class Vector3 implements Cloneable {
z = direction.offsetZ;
}
/**
* Loads a Vector3 from an NBT compound.
*/
public Vector3(NBTTagCompound nbt) {
x = nbt.getDouble("x");
y = nbt.getDouble("y");
z = nbt.getDouble("z");
}
/**
* Returns the coordinates as integers, ideal for block placement.
*/
@ -527,25 +518,24 @@ public class Vector3 implements Cloneable {
return Math.acos(vec1.clone().dotProduct(vec2));
}
/**
* Loads a Vector3 from an NBT compound.
*/
@Deprecated
public static Vector3 readFromNBT(NBTTagCompound nbt) {
return new Vector3(nbt);
public static Vector3 createFromNBT(NBTTagCompound nbtTagCompound) {
Vector3 vector = new Vector3();
vector.readFromNBT(nbtTagCompound);
return vector;
}
/**
* Saves this Vector3 to disk
*
* @param nbt
* - The NBT compound object to save the data in
*/
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
nbt.setDouble("x", x);
nbt.setDouble("y", y);
nbt.setDouble("z", z);
return nbt;
public void readFromNBT(NBTTagCompound nbtTagCompound) {
x = nbtTagCompound.getDouble("x");
y = nbtTagCompound.getDouble("y");
z = nbtTagCompound.getDouble("z");
}
public NBTTagCompound writeToNBT(NBTTagCompound nbtTagCompound) {
nbtTagCompound.setDouble("x", x);
nbtTagCompound.setDouble("y", y);
nbtTagCompound.setDouble("z", z);
return nbtTagCompound;
}
public static Vector3 UP() {

View file

@ -93,6 +93,27 @@ public class VectorI implements Cloneable {
return world.getBlock(x, y, z);
}
public boolean isChunkLoaded(IBlockAccess world) {
return isChunkLoaded(world, x, z);
}
static public boolean isChunkLoaded(IBlockAccess world, final int x, final int z) {
if (world instanceof WorldServer) {
if (((WorldServer)world).getChunkProvider() instanceof ChunkProviderServer) {
ChunkProviderServer chunkProviderServer = (ChunkProviderServer) ((WorldServer)world).getChunkProvider();
try {
Chunk chunk = (Chunk) chunkProviderServer.loadedChunkHashMap.getValueByKey(ChunkCoordIntPair.chunkXZ2Int(x >> 4, z >> 4));
return chunk != null && chunk.isChunkLoaded;
} catch (NoSuchFieldError exception) {
return chunkProviderServer.chunkExists(x >> 4, z >> 4);
}
} else {
return ((WorldServer)world).getChunkProvider().chunkExists(x >> 4, z >> 4);
}
}
return true;
}
public Block getBlock_noChunkLoading(IBlockAccess world, ForgeDirection side) {
return getBlock_noChunkLoading(world, x + side.offsetX, y + side.offsetY, z + side.offsetZ);
}
@ -106,23 +127,9 @@ public class VectorI implements Cloneable {
if (world == null) {
return null;
}
if (world instanceof WorldServer) {
boolean isLoaded;
if (((WorldServer)world).getChunkProvider() instanceof ChunkProviderServer) {
ChunkProviderServer chunkProviderServer = (ChunkProviderServer) ((WorldServer)world).getChunkProvider();
try {
Chunk chunk = (Chunk) chunkProviderServer.loadedChunkHashMap.getValueByKey(ChunkCoordIntPair.chunkXZ2Int(x >> 4, z >> 4));
isLoaded = chunk != null && chunk.isChunkLoaded;
} catch (NoSuchFieldError exception) {
isLoaded = chunkProviderServer.chunkExists(x >> 4, z >> 4);
}
} else {
isLoaded = ((WorldServer)world).getChunkProvider().chunkExists(x >> 4, z >> 4);
}
// skip unloaded chunks
if (!isLoaded) {
return null;
}
// skip unloaded chunks
if (!isChunkLoaded(world, x, z)) {
return null;
}
return world.getBlock(x, y, z);
}
@ -260,19 +267,23 @@ public class VectorI implements Cloneable {
}
public static VectorI readFromNBT(NBTTagCompound nbtCompound) {
public static VectorI createFromNBT(NBTTagCompound nbtTagCompound) {
VectorI vector = new VectorI();
vector.x = nbtCompound.getInteger("x");
vector.y = nbtCompound.getInteger("y");
vector.z = nbtCompound.getInteger("z");
vector.readFromNBT(nbtTagCompound);
return vector;
}
public NBTTagCompound writeToNBT(NBTTagCompound nbtCompound) {
nbtCompound.setInteger("x", x);
nbtCompound.setInteger("y", y);
nbtCompound.setInteger("z", z);
return nbtCompound;
public void readFromNBT(NBTTagCompound nbtTagCompound) {
x = nbtTagCompound.getInteger("x");
y = nbtTagCompound.getInteger("y");
z = nbtTagCompound.getInteger("z");
}
public NBTTagCompound writeToNBT(NBTTagCompound nbtTagCompound) {
nbtTagCompound.setInteger("x", x);
nbtTagCompound.setInteger("y", y);
nbtTagCompound.setInteger("z", z);
return nbtTagCompound;
}
// Square roots are evil, avoid them at all cost