use of micdoodle8.mods.galacticraft.api.vector.Vector3 in project Galacticraft by micdoodle8.
the class NetworkUtil method encodeData.
public static void encodeData(ByteBuf buffer, Collection<Object> sendData) throws IOException {
for (Object dataValue : sendData) {
if (dataValue instanceof Integer) {
buffer.writeInt((Integer) dataValue);
} else if (dataValue instanceof Float) {
buffer.writeFloat((Float) dataValue);
} else if (dataValue instanceof Double) {
buffer.writeDouble((Double) dataValue);
} else if (dataValue instanceof Byte) {
buffer.writeByte((Byte) dataValue);
} else if (dataValue instanceof Boolean) {
buffer.writeBoolean((Boolean) dataValue);
} else if (dataValue instanceof String) {
ByteBufUtils.writeUTF8String(buffer, (String) dataValue);
} else if (dataValue instanceof Short) {
buffer.writeShort((Short) dataValue);
} else if (dataValue instanceof Long) {
buffer.writeLong((Long) dataValue);
} else if (dataValue instanceof EnergyStorage) {
EnergyStorage storage = (EnergyStorage) dataValue;
buffer.writeFloat(storage.getCapacityGC());
buffer.writeFloat(storage.getMaxReceive());
buffer.writeFloat(storage.getMaxExtract());
buffer.writeFloat(storage.getEnergyStoredGC());
} else if (dataValue instanceof NBTTagCompound) {
NetworkUtil.writeNBTTagCompound((NBTTagCompound) dataValue, buffer);
} else if (dataValue instanceof FluidTankGC) {
FluidTankGC tankGC = (FluidTankGC) dataValue;
BlockPos pos = tankGC.getTilePosition();
buffer.writeInt(pos.getX());
buffer.writeInt(pos.getY());
buffer.writeInt(pos.getZ());
NetworkUtil.writeFluidTank((FluidTank) dataValue, buffer);
} else if (dataValue instanceof FluidTank) {
NetworkUtil.writeFluidTank((FluidTank) dataValue, buffer);
} else if (dataValue instanceof Entity) {
buffer.writeInt(((Entity) dataValue).getEntityId());
} else if (dataValue instanceof Vector3) {
buffer.writeDouble(((Vector3) dataValue).x);
buffer.writeDouble(((Vector3) dataValue).y);
buffer.writeDouble(((Vector3) dataValue).z);
} else if (dataValue instanceof BlockVec3) {
buffer.writeInt(((BlockVec3) dataValue).x);
buffer.writeInt(((BlockVec3) dataValue).y);
buffer.writeInt(((BlockVec3) dataValue).z);
} else if (dataValue instanceof byte[]) {
int size = ((byte[]) dataValue).length;
buffer.writeInt(size);
int pos = buffer.writerIndex();
buffer.capacity(pos + size);
buffer.setBytes(pos, (byte[]) dataValue);
buffer.writerIndex(pos + size);
} else if (dataValue instanceof UUID) {
buffer.writeLong(((UUID) dataValue).getLeastSignificantBits());
buffer.writeLong(((UUID) dataValue).getMostSignificantBits());
} else if (dataValue instanceof Collection) {
NetworkUtil.encodeData(buffer, (Collection<Object>) dataValue);
} else if (dataValue instanceof FlagData) {
buffer.writeInt(((FlagData) dataValue).getWidth());
buffer.writeInt(((FlagData) dataValue).getHeight());
for (int i = 0; i < ((FlagData) dataValue).getWidth(); i++) {
for (int j = 0; j < ((FlagData) dataValue).getHeight(); j++) {
Vector3 vec = ((FlagData) dataValue).getColorAt(i, j);
buffer.writeByte((byte) (vec.x * 256 - 128));
buffer.writeByte((byte) (vec.y * 256 - 128));
buffer.writeByte((byte) (vec.z * 256 - 128));
}
}
} else if (dataValue instanceof Integer[]) {
Integer[] array = (Integer[]) dataValue;
buffer.writeInt(array.length);
for (int i = 0; i < array.length; i++) {
buffer.writeInt(array[i]);
}
} else if (dataValue instanceof String[]) {
String[] array = (String[]) dataValue;
buffer.writeInt(array.length);
for (int i = 0; i < array.length; i++) {
ByteBufUtils.writeUTF8String(buffer, array[i]);
}
} else if (dataValue instanceof Footprint[]) {
Footprint[] array = (Footprint[]) dataValue;
buffer.writeInt(array.length);
for (int i = 0; i < array.length; i++) {
buffer.writeInt(array[i].dimension);
buffer.writeFloat((float) array[i].position.x);
buffer.writeFloat((float) array[i].position.y + 1);
buffer.writeFloat((float) array[i].position.z);
buffer.writeFloat(array[i].rotation);
buffer.writeShort(array[i].age);
ByteBufUtils.writeUTF8String(buffer, array[i].owner);
}
} else if (dataValue instanceof EnumFacing) {
buffer.writeInt(((EnumFacing) dataValue).getIndex());
} else if (dataValue instanceof BlockPos) {
BlockPos pos = (BlockPos) dataValue;
buffer.writeInt(pos.getX());
buffer.writeInt(pos.getY());
buffer.writeInt(pos.getZ());
} else if (dataValue instanceof EnumDyeColor) {
buffer.writeInt(((EnumDyeColor) dataValue).getDyeDamage());
} else {
if (dataValue == null) {
GCLog.severe("Cannot construct PacketSimple with null data, this is a bug.");
}
GCLog.info("Could not find data type to encode!: " + dataValue);
}
}
}
use of micdoodle8.mods.galacticraft.api.vector.Vector3 in project Galacticraft by micdoodle8.
the class NetworkUtil method fuzzyEquals.
public static boolean fuzzyEquals(Object a, Object b) {
if ((a == null) != (b == null)) {
return false;
} else if (a == null) {
return true;
} else if (a instanceof Float && b instanceof Float) {
float af = (Float) a;
float bf = (Float) b;
return af == bf || Math.abs(af - bf) < 0.01F;
} else if (a instanceof Double && b instanceof Double) {
return DoubleMath.fuzzyEquals((Double) a, (Double) b, 0.01);
} else if (a instanceof Entity && b instanceof Entity) {
Entity a2 = (Entity) a;
Entity b2 = (Entity) b;
return fuzzyEquals(a2.getEntityId(), b2.getEntityId());
} else if (a instanceof Vector3 && b instanceof Vector3) {
Vector3 a2 = (Vector3) a;
Vector3 b2 = (Vector3) b;
return fuzzyEquals(a2.x, b2.x) && fuzzyEquals(a2.y, b2.y) && fuzzyEquals(a2.z, b2.z);
} else if (a instanceof EnergyStorage && b instanceof EnergyStorage) {
EnergyStorage a2 = (EnergyStorage) a;
EnergyStorage b2 = (EnergyStorage) b;
return fuzzyEquals(a2.getEnergyStoredGC(), b2.getEnergyStoredGC()) && fuzzyEquals(a2.getCapacityGC(), b2.getCapacityGC()) && fuzzyEquals(a2.getMaxReceive(), b2.getMaxReceive()) && fuzzyEquals(a2.getMaxExtract(), b2.getMaxExtract());
} else if (a instanceof FluidTank && b instanceof FluidTank) {
FluidTank a2 = (FluidTank) a;
FluidTank b2 = (FluidTank) b;
FluidStack fluidA = a2.getFluid();
FluidStack fluidB = b2.getFluid();
return fuzzyEquals(a2.getCapacity(), b2.getCapacity()) && fuzzyEquals(fluidA != null ? fluidA.getFluid().getName() : "", fluidB != null ? fluidB.getFluid().getName() : "") && fuzzyEquals(a2.getFluidAmount(), b2.getFluidAmount());
} else {
return a.equals(b);
}
}
use of micdoodle8.mods.galacticraft.api.vector.Vector3 in project Galacticraft by micdoodle8.
the class NetworkUtil method decodeData.
public static ArrayList<Object> decodeData(Class<?>[] types, ByteBuf buffer) {
ArrayList<Object> objList = new ArrayList<Object>();
for (Class clazz : types) {
if (clazz.equals(Integer.class)) {
objList.add(buffer.readInt());
} else if (clazz.equals(Float.class)) {
objList.add(buffer.readFloat());
} else if (clazz.equals(Double.class)) {
objList.add(buffer.readDouble());
} else if (clazz.equals(Byte.class)) {
objList.add(buffer.readByte());
} else if (clazz.equals(Boolean.class)) {
objList.add(buffer.readBoolean());
} else if (clazz.equals(String.class)) {
objList.add(ByteBufUtils.readUTF8String(buffer));
} else if (clazz.equals(Short.class)) {
objList.add(buffer.readShort());
} else if (clazz.equals(Long.class)) {
objList.add(buffer.readLong());
} else if (clazz.equals(byte[].class)) {
int size = buffer.readInt();
byte[] bytes = new byte[size];
buffer.readBytes(bytes, 0, size);
objList.add(bytes);
} else if (clazz.equals(EnergyStorage.class)) {
EnergyStorage storage = new EnergyStorage(buffer.readFloat(), buffer.readFloat(), buffer.readFloat());
storage.setEnergyStored(buffer.readFloat());
objList.add(storage);
} else if (clazz.equals(NBTTagCompound.class)) {
try {
objList.add(NetworkUtil.readNBTTagCompound(buffer));
} catch (IOException e) {
e.printStackTrace();
}
} else if (clazz.equals(BlockVec3.class)) {
objList.add(new BlockVec3(buffer.readInt(), buffer.readInt(), buffer.readInt()));
} else if (clazz.equals(UUID.class)) {
objList.add(new UUID(buffer.readLong(), buffer.readLong()));
} else if (clazz.equals(Vector3.class)) {
objList.add(new Vector3(buffer.readDouble(), buffer.readDouble(), buffer.readDouble()));
} else if (clazz.equals(FlagData.class)) {
int width = buffer.readInt();
int height = buffer.readInt();
FlagData flagData = new FlagData(width, height);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
flagData.setColorAt(i, j, new Vector3(buffer.readByte() + 128, buffer.readByte() + 128, buffer.readByte() + 128));
}
}
objList.add(flagData);
} else if (clazz.equals(Integer[].class)) {
int size = buffer.readInt();
for (int i = 0; i < size; i++) {
objList.add(buffer.readInt());
}
} else if (clazz.equals(String[].class)) {
int size = buffer.readInt();
for (int i = 0; i < size; i++) {
objList.add(ByteBufUtils.readUTF8String(buffer));
}
} else if (clazz.equals(Footprint[].class)) {
int size = buffer.readInt();
for (int i = 0; i < size; i++) {
objList.add(new Footprint(buffer.readInt(), new Vector3(buffer.readFloat(), buffer.readFloat(), buffer.readFloat()), buffer.readFloat(), buffer.readShort(), ByteBufUtils.readUTF8String(buffer)));
}
} else if (clazz.equals(EnumFacing.class)) {
objList.add(EnumFacing.getFront(buffer.readInt()));
} else if (clazz.equals(BlockPos.class)) {
objList.add(new BlockPos(buffer.readInt(), buffer.readInt(), buffer.readInt()));
} else if (clazz.equals(EnumDyeColor.class)) {
objList.add(EnumDyeColor.byDyeDamage(buffer.readInt()));
}
}
return objList;
}
use of micdoodle8.mods.galacticraft.api.vector.Vector3 in project Galacticraft by micdoodle8.
the class PacketEntityUpdate method decodeInto.
@Override
public void decodeInto(ByteBuf buffer) {
super.decodeInto(buffer);
this.entityID = buffer.readInt();
this.position = new Vector3(buffer.readDouble(), buffer.readDouble(), buffer.readDouble());
this.rotationYaw = buffer.readFloat();
this.rotationPitch = buffer.readFloat();
this.motion = new Vector3(buffer.readDouble(), buffer.readDouble(), buffer.readDouble());
this.onGround = buffer.readBoolean();
}
use of micdoodle8.mods.galacticraft.api.vector.Vector3 in project Galacticraft by micdoodle8.
the class TileEntityOxygenStorageModule method update.
@Override
public void update() {
if (!this.worldObj.isRemote) {
ItemStack oxygenItemStack = this.getStackInSlot(0);
if (oxygenItemStack != null && oxygenItemStack.getItem() instanceof IItemOxygenSupply) {
IItemOxygenSupply oxygenItem = (IItemOxygenSupply) oxygenItemStack.getItem();
int oxygenDraw = (int) Math.floor(Math.min(this.oxygenPerTick * 2.5F, this.getMaxOxygenStored() - this.getOxygenStored()));
this.setOxygenStored(getOxygenStored() + oxygenItem.discharge(oxygenItemStack, oxygenDraw));
if (this.getOxygenStored() > this.getMaxOxygenStored()) {
this.setOxygenStored(this.getOxygenStored());
}
}
}
super.update();
this.scaledOxygenLevel = this.getScaledOxygenLevel(16);
if (this.scaledOxygenLevel != this.lastScaledOxygenLevel) {
this.worldObj.notifyLightSet(this.getPos());
}
this.lastScaledOxygenLevel = this.scaledOxygenLevel;
this.produceOxygen(getFront().rotateY().getOpposite());
// if (!this.worldObj.isRemote)
// {
// int gasToSend = Math.min(this.storedOxygen,
// GCCoreTileEntityOxygenStorageModule.OUTPUT_PER_TICK);
// GasStack toSend = new GasStack(GalacticraftCore.gasOxygen,
// gasToSend);
// this.storedOxygen -= GasTransmission.emitGasToNetwork(toSend, this,
// this.getOxygenOutputDirection());
//
// Vector3 thisVec = new Vector3(this);
// TileEntity tileEntity =
// thisVec.modifyPositionFromSide(this.getOxygenOutputDirection()).getTileEntity(this.worldObj);
//
// if (tileEntity instanceof IGasAcceptor)
// {
// if (((IGasAcceptor)
// tileEntity).canReceiveGas(this.getOxygenInputDirection(),
// GalacticraftCore.gasOxygen))
// {
// double sendingGas = 0;
//
// if (this.storedOxygen >=
// GCCoreTileEntityOxygenStorageModule.OUTPUT_PER_TICK)
// {
// sendingGas = GCCoreTileEntityOxygenStorageModule.OUTPUT_PER_TICK;
// }
// else
// {
// sendingGas = this.storedOxygen;
// }
//
// this.storedOxygen -= sendingGas - ((IGasAcceptor)
// tileEntity).receiveGas(new GasStack(GalacticraftCore.gasOxygen, (int)
// Math.floor(sendingGas)));
// }
// }
// }
this.lastScaledOxygenLevel = this.scaledOxygenLevel;
}
Aggregations