the ByteBuf shenanigans begin!!

This commit is contained in:
BallOfEnergy 2024-09-02 21:39:28 -05:00
parent bd25da4c1a
commit 05ca9203e9
13 changed files with 323 additions and 205 deletions

View File

@ -11,6 +11,7 @@ import com.hbm.tileentity.TileEntityMachineBase;
import api.hbm.energymk2.IBatteryItem;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
@ -54,22 +55,29 @@ public class TileEntityNukeBalefire extends TileEntityMachineBase implements IGU
if(timer <= 0) {
explode();
}
NBTTagCompound data = new NBTTagCompound();
data.setInteger("timer", timer);
data.setBoolean("loaded", this.isLoaded());
data.setBoolean("started", started);
networkPack(data, 250);
networkPackNT(250);
}
}
public void networkUnpack(NBTTagCompound data) {
super.networkUnpack(data);
timer = data.getInteger("timer");
started = data.getBoolean("started");
loaded = data.getBoolean("loaded");
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
buf.writeInt(this.timer);
buf.writeBoolean(this.started);
buf.writeBoolean(this.loaded);
}
@Override
public void deserialize(ByteBuf buf) {
super.deserialize(buf);
this.timer = buf.readInt();
this.started = buf.readBoolean();
this.loaded = buf.readBoolean();
}
public void handleButtonPacket(int value, int meta) {
if(meta == 0 && this.isLoaded()) {

View File

@ -15,6 +15,7 @@ import com.hbm.tileentity.TileEntityMachineBase;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
@ -103,11 +104,8 @@ public class TileEntityAshpit extends TileEntityMachineBase implements IGUIProvi
for(int i = 0; i < 5; i++) {
if(slots[i] != null) isFull = true;
}
NBTTagCompound data = new NBTTagCompound();
data.setInteger("playersUsing", this.playersUsing);
data.setBoolean("isFull", this.isFull);
this.networkPack(data, 50);
this.networkPackNT(50);
} else {
this.prevDoorAngle = this.doorAngle;
@ -142,10 +140,19 @@ public class TileEntityAshpit extends TileEntityMachineBase implements IGUIProvi
}
@Override
public void networkUnpack(NBTTagCompound nbt) {
super.networkUnpack(nbt);
this.playersUsing = nbt.getInteger("playersUsing");
this.isFull = nbt.getBoolean("isFull");
public void serialize(ByteBuf buf) {
super.serialize(buf);
buf.writeInt(this.playersUsing);
buf.writeBoolean(this.isFull);
}
@Override
public void deserialize(ByteBuf buf) {
super.deserialize(buf);
this.playersUsing = buf.readInt();
this.isFull = buf.readBoolean();
}
@Override

View File

@ -7,13 +7,16 @@ import com.hbm.inventory.recipes.PressRecipes;
import com.hbm.items.machine.ItemStamp;
import com.hbm.lib.Library;
import com.hbm.tileentity.TileEntityMachineBase;
import com.hbm.util.BufferUtil;
import com.hbm.util.fauxpointtwelve.DirPos;
import api.hbm.energymk2.IEnergyReceiverMK2;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.util.ForgeDirection;
@ -84,16 +87,7 @@ public class TileEntityConveyorPress extends TileEntityMachineBase implements IE
delay--;
}
NBTTagCompound data = new NBTTagCompound();
data.setLong("power", power);
data.setDouble("press", press);
if(slots[0] != null) {
NBTTagCompound stack = new NBTTagCompound();
slots[0].writeToNBT(stack);
data.setTag("stack", stack);
}
this.networkPack(data, 50);
this.networkPackNT(50);
} else {
// approach-based interpolation, GO!
@ -174,23 +168,26 @@ public class TileEntityConveyorPress extends TileEntityMachineBase implements IE
}
public boolean canRetract() {
if(this.power < usage) return false;
return true;
return this.power >= usage;
}
@Override
public void networkUnpack(NBTTagCompound nbt) {
super.networkUnpack(nbt);
this.power = nbt.getLong("power");
this.syncPress = nbt.getInteger("press");
if(nbt.hasKey("stack")) {
NBTTagCompound stack = nbt.getCompoundTag("stack");
this.syncStack = ItemStack.loadItemStackFromNBT(stack);
} else {
this.syncStack = null;
}
public void serialize(ByteBuf buf) {
super.serialize(buf);
buf.writeLong(this.power);
buf.writeDouble(this.syncPress);
BufferUtil.writeItemStack(buf, syncStack);
}
@Override
public void deserialize(ByteBuf buf) {
super.deserialize(buf);
this.power = buf.readLong();
this.syncPress = buf.readDouble();
this.syncStack = BufferUtil.readItemStack(buf);
this.turnProgress = 2;
}

View File

@ -21,11 +21,13 @@ import com.hbm.lib.ModDamageSource;
import com.hbm.tileentity.IGUIProvider;
import com.hbm.tileentity.TileEntityMachineBase;
import com.hbm.util.ArmorUtil;
import com.hbm.util.BufferUtil;
import com.hbm.util.CompatEnergyControl;
import api.hbm.tile.IInfoProviderEC;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
@ -138,15 +140,8 @@ public class TileEntityCore extends TileEntityMachineBase implements IGUIProvide
if(heat > 0)
radiation();
NBTTagCompound data = new NBTTagCompound();
tanks[0].writeToNBT(data, "t0");
tanks[1].writeToNBT(data, "t1");
data.setInteger("field", field);
data.setInteger("heat", heat);
data.setInteger("color", color);
data.setBoolean("melt", meltdownTick);
networkPack(data, 250);
networkPackNT(250);
heat = 0;
@ -162,15 +157,28 @@ public class TileEntityCore extends TileEntityMachineBase implements IGUIProvide
}
public void networkUnpack(NBTTagCompound data) {
super.networkUnpack(data);
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
tanks[0].readFromNBT(data, "t0");
tanks[1].readFromNBT(data, "t1");
field = data.getInteger("field");
heat = data.getInteger("heat");
color = data.getInteger("color");
meltdownTick = data.getBoolean("melt");
tanks[0].serialize(buf);
tanks[1].serialize(buf);
buf.writeInt(field);
buf.writeInt(heat);
buf.writeInt(color);
buf.writeBoolean(meltdownTick);
}
@Override
public void deserialize(ByteBuf buf) {
super.deserialize(buf);
tanks[0].deserialize(buf);
tanks[1].deserialize(buf);
this.field = buf.readInt();
this.heat = buf.readInt();
this.color = buf.readInt();
this.meltdownTick = buf.readBoolean();
}
private void radiation() {

View File

@ -18,6 +18,7 @@ import com.hbm.util.CompatEnergyControl;
import cpw.mods.fml.common.Optional;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import li.cil.oc.api.machine.Arguments;
import li.cil.oc.api.machine.Callback;
import li.cil.oc.api.machine.Context;
@ -169,26 +170,32 @@ public class TileEntityCoreEmitter extends TileEntityMachineBase implements IEne
this.markDirty();
NBTTagCompound data = new NBTTagCompound();
data.setLong("power", power);
data.setInteger("watts", watts);
data.setLong("prev", prev);
data.setInteger("beam", beam);
data.setBoolean("isOn", isOn);
tank.writeToNBT(data, "tank");
this.networkPack(data, 250);
this.networkPackNT(250);
}
}
public void networkUnpack(NBTTagCompound data) {
super.networkUnpack(data);
power = data.getLong("power");
watts = data.getInteger("watts");
prev = data.getLong("prev");
beam = data.getInteger("beam");
isOn = data.getBoolean("isOn");
tank.readFromNBT(data, "tank");
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
buf.writeLong(power);
buf.writeInt(watts);
buf.writeLong(prev);
buf.writeInt(beam);
buf.writeBoolean(isOn);
tank.serialize(buf);
}
@Override
public void deserialize(ByteBuf buf) {
super.deserialize(buf);
this.power = buf.readLong();
this.watts = buf.readInt();
this.prev = buf.readLong();
this.beam = buf.readInt();
this.isOn = buf.readBoolean();
tank.deserialize(buf);
}
public long getPowerScaled(long i) {

View File

@ -12,6 +12,7 @@ import api.hbm.fluid.IFluidStandardReceiver;
import cpw.mods.fml.common.Optional;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import li.cil.oc.api.machine.Arguments;
import li.cil.oc.api.machine.Callback;
import li.cil.oc.api.machine.Context;
@ -101,19 +102,26 @@ public class TileEntityCoreInjector extends TileEntityMachineBase implements IFl
this.markDirty();
NBTTagCompound data = new NBTTagCompound();
data.setInteger("beam", beam);
tanks[0].writeToNBT(data, "t0");
tanks[1].writeToNBT(data, "t1");
this.networkPack(data, 250);
this.networkPackNT(250);
}
}
public void networkUnpack(NBTTagCompound data) {
super.networkUnpack(data);
beam = data.getInteger("beam");
tanks[0].readFromNBT(data, "t0");
tanks[1].readFromNBT(data, "t1");
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
buf.writeInt(beam);
tanks[0].serialize(buf);
tanks[1].serialize(buf);
}
@Override
public void deserialize(ByteBuf buf) {
super.deserialize(buf);
this.beam = buf.readInt();
tanks[0].deserialize(buf);
tanks[1].deserialize(buf);
}
@Override

View File

@ -16,6 +16,7 @@ import api.hbm.tile.IInfoProviderEC;
import cpw.mods.fml.common.Optional;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import li.cil.oc.api.machine.Arguments;
import li.cil.oc.api.machine.Callback;
import li.cil.oc.api.machine.Context;
@ -69,19 +70,26 @@ public class TileEntityCoreReceiver extends TileEntityMachineBase implements IEn
}
}
NBTTagCompound data = new NBTTagCompound();
data.setLong("joules", joules);
tank.writeToNBT(data, "t");
this.networkPack(data, 50);
this.networkPackNT(50);
joules = 0;
}
}
public void networkUnpack(NBTTagCompound data) {
super.networkUnpack(data);
joules = data.getLong("joules");
tank.readFromNBT(data, "t");
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
buf.writeLong(joules);
tank.serialize(buf);
}
@Override
public void deserialize(ByteBuf buf) {
super.deserialize(buf);
joules = buf.readLong();
tank.deserialize(buf);
}
@Override

View File

@ -14,6 +14,7 @@ import api.hbm.tile.IInfoProviderEC;
import cpw.mods.fml.common.Optional;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import li.cil.oc.api.machine.Arguments;
import li.cil.oc.api.machine.Callback;
import li.cil.oc.api.machine.Context;
@ -93,11 +94,7 @@ public class TileEntityCoreStabilizer extends TileEntityMachineBase implements I
}
}
NBTTagCompound data = new NBTTagCompound();
data.setLong("power", power);
data.setInteger("watts", watts);
data.setInteger("beam", beam);
this.networkPack(data, 250);
this.networkPackNT(250);
}
}
@ -106,13 +103,23 @@ public class TileEntityCoreStabilizer extends TileEntityMachineBase implements I
for(ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
this.trySubscribe(worldObj, xCoord + dir.offsetX, yCoord + dir.offsetY, zCoord + dir.offsetZ, dir);
}
public void networkUnpack(NBTTagCompound data) {
super.networkUnpack(data);
power = data.getLong("power");
watts = data.getInteger("watts");
beam = data.getInteger("beam");
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
buf.writeLong(power);
buf.writeInt(watts);
buf.writeInt(beam);
}
@Override
public void deserialize(ByteBuf buf) {
super.deserialize(buf);
this.power = buf.readLong();
this.watts = buf.readInt();
this.beam = buf.readInt();
}
public long getPowerScaled(long i) {

View File

@ -22,6 +22,7 @@ import com.hbm.module.ModulePatternMatcher;
import com.hbm.tileentity.IGUIProvider;
import com.hbm.tileentity.TileEntityMachinePolluting;
import com.hbm.tileentity.TileEntityProxyBase;
import com.hbm.util.BufferUtil;
import com.hbm.util.Compat;
import com.hbm.util.fauxpointtwelve.BlockPos;
import com.hbm.util.fauxpointtwelve.DirPos;
@ -31,6 +32,7 @@ import api.hbm.energymk2.IEnergyReceiverMK2;
import api.hbm.fluid.IFluidStandardTransceiver;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
@ -221,23 +223,44 @@ public class TileEntityCustomMachine extends TileEntityMachinePolluting implemen
} else {
this.progress = 0;
}
NBTTagCompound data = new NBTTagCompound();
data.setString("type", this.machineType);
data.setLong("power", power);
data.setBoolean("structureOK", structureOK);
data.setInteger("flux", flux);
data.setInteger("heat", heat);
data.setInteger("progress", progress);
data.setInteger("maxProgress", maxProgress);
for (int i = 0; i < inputTanks.length; i++) inputTanks[i].writeToNBT(data, "i" + i);
for (int i = 0; i < outputTanks.length; i++) outputTanks[i].writeToNBT(data, "o" + i);
this.matcher.writeToNBT(data);
this.networkPack(data, 50);
this.networkPackNT(50);
}
}
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
buf.writeLong(power);
buf.writeInt(progress);
buf.writeInt(flux);
buf.writeInt(heat);
buf.writeBoolean(structureOK);
buf.writeInt(maxProgress);
for (FluidTank inputTank : inputTanks) inputTank.serialize(buf);
for (FluidTank outputTank : outputTanks) outputTank.serialize(buf);
this.matcher.serialize(buf);
}
@Override
public void deserialize(ByteBuf buf) {
super.deserialize(buf);
this.machineType = BufferUtil.readString(buf);
if(this.config == null) this.init();
this.power = buf.readLong();
this.progress = buf.readInt();
this.flux = buf.readInt();
this.heat = buf.readInt();
this.structureOK = buf.readBoolean();
this.maxProgress = buf.readInt();
for (FluidTank inputTank : inputTanks) inputTank.deserialize(buf);
for (FluidTank outputTank : outputTanks) outputTank.deserialize(buf);
this.matcher.deserialize(buf);
}
/** Only accepts inputs in a fixed order, saves a ton of performance because there's no permutations to check for */
public CustomMachineRecipe getMatchingRecipe() {
List<CustomMachineRecipe> recipes = CustomMachineRecipes.recipes.get(this.config.recipeKey);
@ -459,25 +482,6 @@ public class TileEntityCustomMachine extends TileEntityMachinePolluting implemen
return matcher.isValidForFilter(slots[filterSlot], index, stack);
}
@Override
public void networkUnpack(NBTTagCompound nbt) {
super.networkUnpack(nbt);
this.machineType = nbt.getString("type");
if(this.config == null) this.init();
this.power = nbt.getLong("power");
this.progress = nbt.getInteger("progress");
this.flux = nbt.getInteger("flux");
this.heat = nbt.getInteger("heat");
this.structureOK = nbt.getBoolean("structureOK");
this.maxProgress = nbt.getInteger("maxProgress");
for(int i = 0; i < inputTanks.length; i++) inputTanks[i].readFromNBT(nbt, "i" + i);
for(int i = 0; i < outputTanks.length; i++) outputTanks[i].readFromNBT(nbt, "o" + i);
this.matcher.readFromNBT(nbt);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {

View File

@ -18,6 +18,7 @@ import com.hbm.util.fauxpointtwelve.DirPos;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
@ -110,19 +111,18 @@ public class TileEntityMachinePumpjack extends TileEntityOilDrillBase {
}
}
}
@Override
public void networkPack(NBTTagCompound nbt, int range) {
nbt.setFloat("speed", this.indicator == 0 ? (5F + (2F * this.speedLevel)) + (this.overLevel - 1F) * 10: 0F);
super.networkPack(nbt, range);
}
@Override
public void networkUnpack(NBTTagCompound nbt) {
super.networkUnpack(nbt);
this.speed = nbt.getFloat("speed");
public void serialize(ByteBuf buf) {
super.serialize(buf);
buf.writeFloat(this.indicator == 0 ? (5F + (2F * this.speedLevel)) + (this.overLevel - 1F) * 10: 0F);
}
@Override
public void deserialize(ByteBuf buf) {
super.deserialize(buf);
this.speed = buf.readFloat();
}
@Override

View File

@ -23,6 +23,7 @@ import api.hbm.energymk2.IEnergyReceiverMK2;
import api.hbm.fluid.IFluidStandardTransceiver;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
@ -90,83 +91,87 @@ public abstract class TileEntityOilDrillBase extends TileEntityMachineBase imple
@Override
public void updateEntity() {
if(!worldObj.isRemote) {
if (!worldObj.isRemote) {
this.updateConnections();
this.tanks[0].unloadTank(1, 2, slots);
this.tanks[1].unloadTank(3, 4, slots);
UpgradeManager.eval(slots, 5, 7);
this.speedLevel = Math.min(UpgradeManager.getLevel(UpgradeType.SPEED), 3);
this.energyLevel = Math.min(UpgradeManager.getLevel(UpgradeType.POWER), 3);
this.overLevel = Math.min(UpgradeManager.getLevel(UpgradeType.OVERDRIVE), 3) + 1;
int abLevel = Math.min(UpgradeManager.getLevel(UpgradeType.AFTERBURN), 3);
int toBurn = Math.min(tanks[1].getFill(), abLevel * 10);
if(toBurn > 0) {
if (toBurn > 0) {
tanks[1].setFill(tanks[1].getFill() - toBurn);
this.power += toBurn * 5;
if(this.power > this.getMaxPower())
if (this.power > this.getMaxPower())
this.power = this.getMaxPower();
}
power = Library.chargeTEFromItems(slots, 0, power, this.getMaxPower());
for(DirPos pos : getConPos()) {
if(tanks[0].getFill() > 0) this.sendFluid(tanks[0], worldObj, pos.getX(), pos.getY(), pos.getZ(), pos.getDir());
if(tanks[1].getFill() > 0) this.sendFluid(tanks[1], worldObj, pos.getX(), pos.getY(), pos.getZ(), pos.getDir());
for (DirPos pos : getConPos()) {
if (tanks[0].getFill() > 0)
this.sendFluid(tanks[0], worldObj, pos.getX(), pos.getY(), pos.getZ(), pos.getDir());
if (tanks[1].getFill() > 0)
this.sendFluid(tanks[1], worldObj, pos.getX(), pos.getY(), pos.getZ(), pos.getDir());
}
if(this.power >= this.getPowerReqEff() && this.tanks[0].getFill() < this.tanks[0].getMaxFill() && this.tanks[1].getFill() < this.tanks[1].getMaxFill()) {
if (this.power >= this.getPowerReqEff() && this.tanks[0].getFill() < this.tanks[0].getMaxFill() && this.tanks[1].getFill() < this.tanks[1].getMaxFill()) {
this.power -= this.getPowerReqEff();
if(worldObj.getTotalWorldTime() % getDelayEff() == 0) {
if (worldObj.getTotalWorldTime() % getDelayEff() == 0) {
this.indicator = 0;
for(int y = yCoord - 1; y >= getDrillDepth(); y--) {
if(worldObj.getBlock(xCoord, y, zCoord) != ModBlocks.oil_pipe) {
if(trySuck(y)) {
for (int y = yCoord - 1; y >= getDrillDepth(); y--) {
if (worldObj.getBlock(xCoord, y, zCoord) != ModBlocks.oil_pipe) {
if (trySuck(y)) {
break;
} else {
tryDrill(y);
break;
}
}
if(y == getDrillDepth())
if (y == getDrillDepth())
this.indicator = 1;
}
}
} else {
this.indicator = 2;
}
this.sendUpdate();
this.networkPackNT(25);
}
}
public void sendUpdate() {
NBTTagCompound data = new NBTTagCompound();
data.setLong("power", power);
data.setInteger("indicator", this.indicator);
for(int i = 0; i < tanks.length; i++) tanks[i].writeToNBT(data, "t" + i);
this.networkPack(data, 25);
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
buf.writeLong(this.power);
buf.writeInt(this.indicator);
for (FluidTank tank : tanks) tank.serialize(buf);
}
public void networkUnpack(NBTTagCompound nbt) {
super.networkUnpack(nbt);
this.power = nbt.getLong("power");
this.indicator = nbt.getInteger("indicator");
for(int i = 0; i < tanks.length; i++) tanks[i].readFromNBT(nbt, "t" + i);
@Override
public void deserialize(ByteBuf buf) {
super.deserialize(buf);
this.power = buf.readLong();
this.indicator = buf.readInt();
for (FluidTank tank : tanks) tank.deserialize(buf);
}
public boolean canPump() {

View File

@ -118,9 +118,7 @@ public abstract class TileEntityRBMKBase extends TileEntityLoadedBase implements
this.worldObj.theProfiler.endStartSection("rbmkBase_rpassive_cooling");
coolPassively();
this.worldObj.theProfiler.endSection();
NBTTagCompound data = new NBTTagCompound();
this.writeToNBT(data);
this.networkPackNT(trackingRange());
}
}

View File

@ -1,15 +1,22 @@
package com.hbm.util;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import io.netty.buffer.ByteBuf;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTSizeTracker;
import net.minecraft.nbt.NBTTagCompound;
public class BufferUtil {
private static final Charset CHARSET = Charset.forName("UTF-8");
private static final Charset CHARSET = StandardCharsets.UTF_8;
// Writes a string to a byte buffer by encoding the length and raw bytes
public static final void writeString(ByteBuf buf, String value) {
public static void writeString(ByteBuf buf, String value) {
if(value == null) {
buf.writeInt(-1);
return;
@ -20,7 +27,7 @@ public class BufferUtil {
}
// Reads a string from a byte buffer via the written length and raw bytes
public static final String readString(ByteBuf buf) {
public static String readString(ByteBuf buf) {
final int count = buf.readInt();
if(count < 0) return null;
@ -30,4 +37,58 @@ public class BufferUtil {
return new String(bytes, CHARSET);
}
}
/**
* Writes the ItemStack to the buffer.
*/
public static void writeItemStack(ByteBuf buf, ItemStack item) {
if (item == null)
buf.writeShort(-1);
else {
buf.writeShort(Item.getIdFromItem(item.getItem()));
buf.writeByte(item.stackSize);
buf.writeShort(item.getItemDamage());
NBTTagCompound nbtTagCompound = null;
if (item.getItem().isDamageable() || item.getItem().getShareTag())
nbtTagCompound = item.stackTagCompound;
if(nbtTagCompound != null) {
byte[] nbtData = new byte[0];
try {
nbtData = CompressedStreamTools.compress(nbtTagCompound);
} catch(IOException e) {
e.printStackTrace();
}
buf.writeShort((short) nbtData.length);
buf.writeBytes(nbtData);
} else {
buf.writeShort(-1);
}
}
}
/**
* Reads an ItemStack from a buffer
*/
public static ItemStack readItemStack(ByteBuf buf) {
ItemStack item = null;
short id = buf.readShort();
if (id >= 0) {
byte quantity = buf.readByte();
short meta = buf.readShort();
item = new ItemStack(Item.getItemById(id), quantity, meta);
short nbtLength = buf.readByte();
byte[] tags = new byte[nbtLength];
buf.readBytes(tags);
try {
item.stackTagCompound = CompressedStreamTools.func_152457_a(tags, new NBTSizeTracker(2097152L));
} catch(IOException e) {
e.printStackTrace();
}
}
return item;
}
}