add method for sending storage item over net

This commit is contained in:
Raoul Van den Berge
2015-12-20 16:50:54 +01:00
parent 23a9b358bf
commit 9aaad17b7e
2 changed files with 24 additions and 19 deletions

View File

@@ -1,7 +1,9 @@
package storagecraft.storage; package storagecraft.storage;
import cpw.mods.fml.common.network.ByteBufUtils;
import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly; import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
@@ -15,6 +17,14 @@ public class StorageItem {
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
private int id; 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) { public StorageItem(Item type, int quantity, int damage, NBTTagCompound tag) {
this.type = type; this.type = type;
this.quantity = quantity; this.quantity = quantity;
@@ -32,6 +42,18 @@ public class StorageItem {
this(stack.getItem(), stack.stackSize, stack.getItemDamage(), stack.stackTagCompound); 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() { public Item getType() {
return type; return type;
} }

View File

@@ -2,11 +2,9 @@ package storagecraft.tile;
import cofh.api.energy.EnergyStorage; import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyReceiver; import cofh.api.energy.IEnergyReceiver;
import cpw.mods.fml.common.network.ByteBufUtils;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
@@ -293,7 +291,6 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
return zCoord; return zCoord;
} }
// @TODO: add helpers for sending redstone control + item storages over net
@Override @Override
public void fromBytes(ByteBuf buf) { public void fromBytes(ByteBuf buf) {
energy.setEnergyStored(buf.readInt()); energy.setEnergyStored(buf.readInt());
@@ -306,13 +303,7 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
int size = buf.readInt(); int size = buf.readInt();
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
int id = buf.readInt(); items.add(new StorageItem(buf));
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));
} }
} }
@@ -326,15 +317,7 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
buf.writeInt(items.size()); buf.writeInt(items.size());
for (StorageItem item : items) { for (StorageItem item : items) {
buf.writeInt(items.indexOf(item)); item.toBytes(buf, 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());
}
} }
} }
} }