add method for sending storage item over net
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package storagecraft.storage;
|
||||
|
||||
import cpw.mods.fml.common.network.ByteBufUtils;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
@@ -15,6 +17,14 @@ public class StorageItem {
|
||||
@SideOnly(Side.CLIENT)
|
||||
private int id;
|
||||
|
||||
public StorageItem(ByteBuf buf) {
|
||||
this.id = buf.readInt();
|
||||
this.type = Item.getItemById(buf.readInt());
|
||||
this.quantity = buf.readInt();
|
||||
this.damage = buf.readInt();
|
||||
this.tag = buf.readBoolean() ? ByteBufUtils.readTag(buf) : null;
|
||||
}
|
||||
|
||||
public StorageItem(Item type, int quantity, int damage, NBTTagCompound tag) {
|
||||
this.type = type;
|
||||
this.quantity = quantity;
|
||||
@@ -32,6 +42,18 @@ public class StorageItem {
|
||||
this(stack.getItem(), stack.stackSize, stack.getItemDamage(), stack.stackTagCompound);
|
||||
}
|
||||
|
||||
public void toBytes(ByteBuf buf, int id) {
|
||||
buf.writeInt(id);
|
||||
buf.writeInt(Item.getIdFromItem(type));
|
||||
buf.writeInt(quantity);
|
||||
buf.writeInt(damage);
|
||||
buf.writeBoolean(tag != null);
|
||||
|
||||
if (tag != null) {
|
||||
ByteBufUtils.writeTag(buf, tag);
|
||||
}
|
||||
}
|
||||
|
||||
public Item getType() {
|
||||
return type;
|
||||
}
|
||||
|
@@ -2,11 +2,9 @@ package storagecraft.tile;
|
||||
|
||||
import cofh.api.energy.EnergyStorage;
|
||||
import cofh.api.energy.IEnergyReceiver;
|
||||
import cpw.mods.fml.common.network.ByteBufUtils;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
@@ -293,7 +291,6 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
|
||||
return zCoord;
|
||||
}
|
||||
|
||||
// @TODO: add helpers for sending redstone control + item storages over net
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
energy.setEnergyStored(buf.readInt());
|
||||
@@ -306,13 +303,7 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
|
||||
int size = buf.readInt();
|
||||
|
||||
for (int i = 0; i < size; ++i) {
|
||||
int id = buf.readInt();
|
||||
Item type = Item.getItemById(buf.readInt());
|
||||
int quantity = buf.readInt();
|
||||
int damage = buf.readInt();
|
||||
NBTTagCompound tag = buf.readBoolean() ? ByteBufUtils.readTag(buf) : null;
|
||||
|
||||
items.add(new StorageItem(type, quantity, damage, tag, id));
|
||||
items.add(new StorageItem(buf));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,15 +317,7 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
|
||||
buf.writeInt(items.size());
|
||||
|
||||
for (StorageItem item : items) {
|
||||
buf.writeInt(items.indexOf(item));
|
||||
buf.writeInt(Item.getIdFromItem(item.getType()));
|
||||
buf.writeInt(item.getQuantity());
|
||||
buf.writeInt(item.getDamage());
|
||||
buf.writeBoolean(item.getTag() != null);
|
||||
|
||||
if (item.getTag() != null) {
|
||||
ByteBufUtils.writeTag(buf, item.getTag());
|
||||
}
|
||||
item.toBytes(buf, items.indexOf(item));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user