I'm writing a simple tile entity that stores a char[0x10000] array. Each 16-bit char is represented as two 8-bit bytes within the NBT format.
However, when saving the world, the console reports that it has written 65536 words of null (0) data. Always. Even when there is data in the array. NBTExplorer confirms this.
My code (messy version):
public class ComputerTileEntity extends TileEntity {
public static final int MEMORY_SIZE = 0x10000;
public char[] memory = new char[MEMORY_SIZE];
public int PC = 0, SP = 0, EX = 0, IA = 0;
public int A = 0, B = 0, C = 0, X = 0, Y = 0, Z = 0, I = 0, J = 0;
public String status = "LOC: " + String.format("%04X", PC);
public String input = "";
public void resetStatus() {
status = "LOC: " + String.format("%04X", PC);
}
public void commandLOC(String[] args) {
if (args.length > 1) {
try {
int newLoc = Integer.parseInt(args[1], 16);
if (newLoc > MEMORY_SIZE || newLoc < 0)
status = "? Address";
else {
PC = newLoc;
resetStatus();
input = "";
}
} catch (NumberFormatException e) {
status = "? Address";
}
} else {
resetStatus();
input = "";
}
}
public void commandMEM(String[] args) {
char[] newData = new char[args.length - 1];
try {
for (int i = 1; i < args.length; i++) {
int parsed = Integer.parseInt(args[i], 16);
if (parsed < 0 || parsed > 65535)
throw new NumberFormatException("Too big for char");
newData[i - 1] = (char) parsed;
}
for (int i = 0; i < newData.length; i++) {
memory[PC] = newData[i];
PC++;
}
resetStatus();
input = "";
} catch (NumberFormatException e) {
status = "? Value";
}
}
public void commandDMP(String[] args) {
status = "";
for (int i = 0; i < 6; i++) {
status += String.format("%04X", (int) memory[PC + i]);
if (i != 5)
status += " ";
}
input = "";
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
byte[] mem8 = new byte[MEMORY_SIZE * 2];
int bytes = 0;
for (int i = 0; i < MEMORY_SIZE; i++) {
byte lS = (byte) (memory[i] & 0xFF);
byte mS = (byte) (memory[i] >> 8 & 0xFF);
mem8[2 * i] = lS;
mem8[2 * i + 1] = mS;
if (memory[i] == 0)
bytes += 1;
}
nbt.setByteArray("memory", mem8);
System.out.println("Wrote " + bytes + "w null data");
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
byte[] mem8 = nbt.getByteArray("memory");
int bytes = 0;
for (int i = 0; i < MEMORY_SIZE; i++) {
int lSi = mem8[2 * i] & 0xFF;
int mSi = mem8[2 * i + 1] & 0xFF;
char m = (char) (mSi << 8 | lSi);
memory[i] = m;
if (memory[i] == 0)
bytes += 1;
}
System.out.println("Read " + bytes + "w null data");
}
@Override
public String toString() {
return "ComputerTileEntity instance";
}
}
My code (SSCCE version):
public class ComputerTileEntity extends TileEntity {
public static final int MEMORY_SIZE = 0x10000;
public char[] memory = new char[MEMORY_SIZE];
public int PC = 0, SP = 0, EX = 0, IA = 0;
public int A = 0, B = 0, C = 0, X = 0, Y = 0, Z = 0, I = 0, J = 0;
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
byte[] mem8 = new byte[MEMORY_SIZE * 2];
int bytes = 0;
for (int i = 0; i < MEMORY_SIZE; i++) {
byte lS = (byte) (memory[i] & 0xFF);
byte mS = (byte) (memory[i] >> 8 & 0xFF);
mem8[2 * i] = lS;
mem8[2 * i + 1] = mS;
if (memory[i] == 0)
bytes += 1;
}
nbt.setByteArray("memory", mem8);
System.out.println("Wrote " + bytes + "w null data");
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
byte[] mem8 = nbt.getByteArray("memory");
int bytes = 0;
for (int i = 0; i < MEMORY_SIZE; i++) {
int lSi = mem8[2 * i] & 0xFF;
int mSi = mem8[2 * i + 1] & 0xFF;
char m = (char) (mSi << 8 | lSi);
memory[i] = m;
if (memory[i] == 0)
bytes += 1;
}
System.out.println("Read " + bytes + "w null data");
}
}
Can someone please tell me what's going on here? It's like there's two instances of my tile entity or something?!
I'm writing a simple tile entity that stores a char[0x10000] array. Each 16-bit char is represented as two 8-bit bytes within the NBT format.
However, when saving the world, the console reports that it has written 65536 words of null (0) data. Always. Even when there is data in the array. NBTExplorer confirms this.
My code (messy version):
My code (SSCCE version):
Can someone please tell me what's going on here? It's like there's two instances of my tile entity or something?!
Derp! Of course! How did I not think of that? Things were so much simpler back in Beta...
(Now... where's that packet handling documentation.............)