This commit is contained in:
asiekierka 2014-10-27 08:57:21 +01:00
parent b2135c37b6
commit 1e007896e0
5 changed files with 45 additions and 36 deletions

View file

@ -12,9 +12,9 @@ import java.util.BitSet;
import java.util.Random;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.core.NetworkData;
import buildcraft.core.utils.BitSetUtils;
public class ZoneChunk {
@ -71,7 +71,7 @@ public class ZoneChunk {
nbt.setBoolean("fullSet", fullSet);
if (property != null) {
nbt.setByteArray("bits", property.toByteArray());
nbt.setByteArray("bits", BitSetUtils.toByteArray(property));
}
}
@ -79,7 +79,7 @@ public class ZoneChunk {
fullSet = nbt.getBoolean("fullSet");
if (nbt.hasKey("bits")) {
property = BitSet.valueOf(nbt.getByteArray("bits"));
property = BitSetUtils.fromByteArray(nbt.getByteArray("bits"));
}
}

View file

@ -9,9 +9,10 @@
package buildcraft.core.network.serializers;
import java.util.BitSet;
import io.netty.buffer.ByteBuf;
import buildcraft.core.utils.BitSetUtils;
public class SerializerBitSet extends ClassSerializer {
@Override
@ -22,7 +23,7 @@ public class SerializerBitSet extends ClassSerializer {
data.writeBoolean(true);
BitSet set = (BitSet) o;
byte[] bytes = set.toByteArray();
byte[] bytes = BitSetUtils.toByteArray(set);
data.writeInt(bytes.length);
data.writeBytes(bytes);
}
@ -38,8 +39,7 @@ public class SerializerBitSet extends ClassSerializer {
byte[] bytes = new byte[actualSize];
data.readBytes(bytes);
BitSet set = new BitSet();
set = BitSet.valueOf(bytes);
BitSet set = BitSetUtils.fromByteArray(bytes);
return set;
}

View file

@ -0,0 +1,30 @@
package buildcraft.core.utils;
import java.util.BitSet;
public final class BitSetUtils {
private BitSetUtils() {
}
public static BitSet fromByteArray(byte[] bytes) {
BitSet bits = new BitSet();
for (int i = 0; i < bytes.length * 8; i++) {
if ((bytes[bytes.length - (i >> 3) - 1] & (1 << (i & 7))) > 0) {
bits.set(i);
}
}
return bits;
}
public static byte[] toByteArray(BitSet bits) {
byte[] bytes = new byte[(int) Math.ceil(bits.length() / 8)];
for (int i = 0; i < bits.length(); i++) {
if (bits.get(i)) {
bytes[bytes.length - (i >> 3) - 1] |= 1 << (i & 7);
}
}
return bytes;
}
}

View file

@ -18,6 +18,7 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.transport.IPipeTile.PipeType;
import buildcraft.core.utils.BitSetUtils;
public abstract class PipeTransport {
@ -41,8 +42,8 @@ public abstract class PipeTransport {
public void readFromNBT(NBTTagCompound nbt) {
if (nbt.hasKey("inputOpen") && nbt.hasKey("outputOpen")) {
BitSet inputBuf = BitSet.valueOf(new byte[] {nbt.getByte("inputOpen")});
BitSet outputBuf = BitSet.valueOf(new byte[] {nbt.getByte("outputOpen")});
BitSet inputBuf = BitSetUtils.fromByteArray(new byte[] {nbt.getByte("inputOpen")});
BitSet outputBuf = BitSetUtils.fromByteArray(new byte[] {nbt.getByte("outputOpen")});
for (int b = 0; b < ForgeDirection.VALID_DIRECTIONS.length; b++) {
inputsOpen[b] = inputBuf.get(b);
@ -69,8 +70,8 @@ public abstract class PipeTransport {
}
}
nbt.setByte("inputOpen", inputBuf.toByteArray()[0]);
nbt.setByte("outputOpen", outputBuf.toByteArray()[0]);
nbt.setByte("inputOpen", BitSetUtils.toByteArray(inputBuf)[0]);
nbt.setByte("outputOpen", BitSetUtils.toByteArray(outputBuf)[0]);
}
public void updateEntity() {

View file

@ -11,16 +11,14 @@ package buildcraft.transport.network;
import java.util.BitSet;
import io.netty.buffer.ByteBuf;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidStack;
import buildcraft.core.network.PacketCoordinates;
import buildcraft.core.network.PacketIds;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.BitSetUtils;
import buildcraft.transport.PipeTransportFluids;
import buildcraft.transport.TileGenericPipe;
@ -76,7 +74,7 @@ public class PacketFluidUpdate extends PacketCoordinates {
byte[] dBytes = new byte[2];
data.readBytes(dBytes);
delta = fromByteArray(dBytes);
delta = BitSetUtils.fromByteArray(dBytes);
// System.out.printf("read %d, %d, %d = %s, %s%n", posX, posY, posZ, Arrays.toString(dBytes), delta);
@ -99,7 +97,7 @@ public class PacketFluidUpdate extends PacketCoordinates {
public void writeData(ByteBuf data) {
super.writeData(data);
byte[] dBytes = toByteArray(delta);
byte[] dBytes = BitSetUtils.toByteArray(delta);
// System.out.printf("write %d, %d, %d = %s, %s%n", posX, posY, posZ, Arrays.toString(dBytes), delta);
data.writeBytes(dBytes);
@ -125,26 +123,6 @@ public class PacketFluidUpdate extends PacketCoordinates {
}
}
public static BitSet fromByteArray(byte[] bytes) {
BitSet bits = new BitSet();
for (int i = 0; i < bytes.length * 8; i++) {
if ((bytes[bytes.length - i / 8 - 1] & (1 << (i % 8))) > 0) {
bits.set(i);
}
}
return bits;
}
public static byte[] toByteArray(BitSet bits) {
byte[] bytes = new byte[2];
for (int i = 0; i < bits.length(); i++) {
if (bits.get(i)) {
bytes[bytes.length - i / 8 - 1] |= 1 << (i % 8);
}
}
return bytes;
}
@Override
public int getID() {
return PacketIds.PIPE_LIQUID;