add meta object to storage items

This commit is contained in:
Raoul Van den Berge
2015-12-16 00:04:44 +01:00
parent e43ceefd4e
commit 16c66d2be1
10 changed files with 172 additions and 156 deletions

View File

@@ -9,16 +9,8 @@ import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import java.util.Random;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
import storagecraft.proxy.CommonProxy;
@Mod(modid = SC.ID, version = SC.VERSION)
@@ -57,75 +49,4 @@ public class SC {
public void postInit(FMLPostInitializationEvent e) {
PROXY.postInit(e);
}
public static void saveInventory(IInventory inventory, NBTTagCompound nbt) {
NBTTagList tagList = new NBTTagList();
for (int i = 0; i < inventory.getSizeInventory(); i++) {
if (inventory.getStackInSlot(i) != null) {
NBTTagCompound compoundTag = new NBTTagCompound();
compoundTag.setInteger("Slot", i);
inventory.getStackInSlot(i).writeToNBT(compoundTag);
tagList.appendTag(compoundTag);
}
}
nbt.setTag("Inventory", tagList);
}
public static void restoreInventory(IInventory inventory, NBTTagCompound nbt) {
if (nbt.hasKey("Inventory")) {
NBTTagList tagList = nbt.getTagList("Inventory", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < tagList.tagCount(); i++) {
int slot = tagList.getCompoundTagAt(i).getInteger("Slot");
ItemStack stack = ItemStack.loadItemStackFromNBT(tagList.getCompoundTagAt(i));
inventory.setInventorySlotContents(slot, stack);
}
}
}
// https://github.com/cpw/ironchest/blob/master/src/main/java/cpw/mods/ironchest/BlockIronChest.java#L200
public static void dropInventory(World world, IInventory inventory, int x, int y, int z, int newSize) {
Random random = world.rand;
for (int i = newSize; i < inventory.getSizeInventory(); ++i) {
ItemStack stack = inventory.getStackInSlot(i);
if (stack == null) {
continue;
}
float xo = random.nextFloat() * 0.8F + 0.1F;
float yo = random.nextFloat() * 0.8F + 0.1F;
float zo = random.nextFloat() * 0.8F + 0.1F;
while (stack.stackSize > 0) {
int amount = random.nextInt(21) + 10;
if (amount > stack.stackSize) {
amount = stack.stackSize;
}
stack.stackSize -= amount;
EntityItem entity = new EntityItem(world, (float) x + xo, (float) y + (newSize > 0 ? 1 : 0) + yo, (float) z + zo, new ItemStack(stack.getItem(), amount, stack.getItemDamage()));
entity.motionX = (float) random.nextGaussian() * 0.05F;
entity.motionY = (float) random.nextGaussian() * 0.05F + 0.2F;
entity.motionZ = (float) random.nextGaussian() * 0.05F;
if (stack.hasTagCompound()) {
entity.getEntityItem().setTagCompound((NBTTagCompound) stack.getTagCompound().copy());
}
world.spawnEntityInWorld(entity);
}
}
}
}

View File

@@ -52,7 +52,7 @@ public class BlockGrid extends BlockSC implements ITileEntityProvider {
}
@Override
public IIcon getIcon(int side, int meta) {
public IIcon getIcon(int side, int damage) {
if (side == 3) {
return disconnectedIcon;
}

View File

@@ -11,6 +11,7 @@ import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import storagecraft.SC;
import storagecraft.tile.TileSC;
import storagecraft.util.InventoryUtil;
public class BlockSC extends Block {
private String name;
@@ -64,7 +65,7 @@ public class BlockSC extends Block {
TileEntity tile = world.getTileEntity(x, y, z);
if (tile instanceof IInventory) {
SC.dropInventory(world, (IInventory) tile, x, y, z, 0);
InventoryUtil.dropInventory(world, (IInventory) tile, x, y, z, 0);
}
super.onBlockPreDestroy(world, x, y, z, meta);

View File

@@ -12,7 +12,7 @@ public class CellStorage implements IStorage {
public static final String NBT_ITEM_TYPE = "Type";
public static final String NBT_ITEM_QUANTITY = "Quantity";
public static final String NBT_ITEM_META = "Meta";
public static final String NBT_ITEM_DAMAGE = "Damage";
public static final String NBT_ITEM_NBT = "NBT";
private ItemStack cell;
@@ -22,7 +22,7 @@ public class CellStorage implements IStorage {
}
private StorageItem createItemFromNBT(NBTTagCompound tag) {
return new StorageItem(Item.getItemById(tag.getInteger(NBT_ITEM_TYPE)), tag.getInteger(NBT_ITEM_QUANTITY), tag.getInteger(NBT_ITEM_META), tag.hasKey(NBT_ITEM_NBT) ? ((NBTTagCompound) tag.getTag(NBT_ITEM_NBT)) : null);
return new StorageItem(Item.getItemById(tag.getInteger(NBT_ITEM_TYPE)), tag.getInteger(NBT_ITEM_QUANTITY), tag.getInteger(NBT_ITEM_DAMAGE), tag.hasKey(NBT_ITEM_NBT) ? ((NBTTagCompound) tag.getTag(NBT_ITEM_NBT)) : null);
}
@Override
@@ -45,7 +45,7 @@ public class CellStorage implements IStorage {
StorageItem item = createItemFromNBT(tag);
if (item.equalsIgnoreQuantity(stack)) {
if (item.getMeta().equals(stack)) {
tag.setInteger(NBT_ITEM_QUANTITY, item.getQuantity() + stack.stackSize);
return;
@@ -56,7 +56,7 @@ public class CellStorage implements IStorage {
tag.setInteger(NBT_ITEM_TYPE, Item.getIdFromItem(stack.getItem()));
tag.setInteger(NBT_ITEM_QUANTITY, stack.stackSize);
tag.setInteger(NBT_ITEM_META, stack.getItemDamage());
tag.setInteger(NBT_ITEM_DAMAGE, stack.getItemDamage());
if (stack.stackTagCompound != null) {
tag.setTag(NBT_ITEM_NBT, stack.stackTagCompound);
@@ -76,7 +76,7 @@ public class CellStorage implements IStorage {
StorageItem item = createItemFromNBT(tag);
if (item.equalsIgnoreQuantity(stack)) {
if (item.getMeta().equals(stack)) {
if (quantity > item.getQuantity()) {
quantity = item.getQuantity();
}

View File

@@ -5,28 +5,25 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
public class StorageItem {
private Item type;
private StorageItemMeta meta;
private int quantity;
private int meta;
private NBTTagCompound tag;
public StorageItem(Item type, int quantity, int meta, NBTTagCompound tag) {
this.type = type;
public StorageItem(StorageItemMeta meta, int quantity) {
this.meta = meta;
this.quantity = quantity;
this.tag = tag;
}
public StorageItem(Item type, int quantity, int damage, NBTTagCompound tag) {
this.meta = new StorageItemMeta(type, damage, tag);
this.quantity = quantity;
}
public StorageItem(ItemStack stack) {
this(stack.getItem(), stack.stackSize, stack.getItemDamage(), stack.stackTagCompound);
}
public Item getType() {
return type;
}
public void setType(Item type) {
this.type = type;
public StorageItemMeta getMeta() {
return meta;
}
public int getQuantity() {
@@ -37,63 +34,23 @@ public class StorageItem {
this.quantity = quantity;
}
public int getMeta() {
return meta;
}
public void setMeta(int meta) {
this.meta = meta;
}
public NBTTagCompound getTag() {
return tag;
}
public void setTag(NBTTagCompound tag) {
this.tag = tag;
}
public StorageItem copy() {
return copy(quantity);
}
public StorageItem copy(int newQuantity) {
return new StorageItem(type, newQuantity, meta, tag);
return new StorageItem(meta, newQuantity);
}
public ItemStack toItemStack() {
ItemStack stack = new ItemStack(type, quantity, meta);
ItemStack stack = new ItemStack(meta.getType(), quantity, meta.getDamage());
stack.stackTagCompound = tag;
stack.stackTagCompound = meta.getTag();
return stack;
}
public boolean equalsIgnoreQuantity(ItemStack other) {
if (tag != null && !tag.equals(other.stackTagCompound)) {
return false;
}
return type == other.getItem() && meta == other.getItemDamage();
}
public boolean equalsIgnoreQuantity(StorageItem other) {
if (tag != null && !tag.equals(other.getTag())) {
return false;
}
return type == other.getType() && meta == other.getMeta();
}
public static boolean equalsIgnoreQuantity(ItemStack first, ItemStack second) {
if (first.stackTagCompound != null && !first.stackTagCompound.equals(second.stackTagCompound)) {
return false;
}
return first.getItem() == second.getItem() && first.getItemDamage() == second.getItemDamage();
}
public boolean equals(StorageItem other) {
return other.getQuantity() == quantity && equalsIgnoreQuantity(other);
return other.getQuantity() == quantity && other.getMeta().equals(meta);
}
}

View File

@@ -0,0 +1,45 @@
package storagecraft.storage;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
public class StorageItemMeta {
private Item type;
private int damage;
private NBTTagCompound tag;
public StorageItemMeta(Item type, int damage, NBTTagCompound tag) {
this.type = type;
this.damage = damage;
this.tag = tag;
}
public Item getType() {
return type;
}
public int getDamage() {
return damage;
}
public NBTTagCompound getTag() {
return tag;
}
public boolean equals(StorageItemMeta meta) {
if (tag != null && !tag.equals(meta.getTag())) {
return false;
}
return type == meta.getType() && damage == meta.getDamage();
}
public boolean equals(ItemStack stack) {
if (tag != null && !tag.equals(stack.stackTagCompound)) {
return false;
}
return type == stack.getItem() && damage == stack.getItemDamage();
}
}

View File

@@ -141,7 +141,7 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
StorageItem other = items.get(j);
if (item.equalsIgnoreQuantity(other)) {
if (item.getMeta().equals(other.getMeta())) {
item.setQuantity(item.getQuantity() + other.getQuantity());
markedIndexes.add(j);
@@ -255,10 +255,10 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
for (int i = 0; i < size; ++i) {
Item type = Item.getItemById(buf.readInt());
int quantity = buf.readInt();
int meta = buf.readInt();
int damage = buf.readInt();
NBTTagCompound tag = buf.readBoolean() ? ByteBufUtils.readTag(buf) : null;
items.add(new StorageItem(type, quantity, meta, tag));
items.add(new StorageItem(type, quantity, damage, tag));
}
}
@@ -270,13 +270,13 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
buf.writeInt(items.size());
for (StorageItem item : items) {
buf.writeInt(Item.getIdFromItem(item.getType()));
buf.writeInt(Item.getIdFromItem(item.getMeta().getType()));
buf.writeInt(item.getQuantity());
buf.writeInt(item.getMeta());
buf.writeBoolean(item.getTag() != null);
buf.writeInt(item.getMeta().getDamage());
buf.writeBoolean(item.getMeta().getTag() != null);
if (item.getTag() != null) {
ByteBufUtils.writeTag(buf, item.getTag());
if (item.getMeta().getTag() != null) {
ByteBufUtils.writeTag(buf, item.getMeta().getTag());
}
}
}

View File

@@ -5,11 +5,11 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import storagecraft.SC;
import storagecraft.inventory.InventorySC;
import storagecraft.storage.CellStorage;
import storagecraft.storage.IStorage;
import storagecraft.storage.IStorageProvider;
import storagecraft.util.InventoryUtil;
public class TileDrive extends TileMachine implements IInventory, IStorageProvider {
private InventorySC inventory = new InventorySC("drive", 8);
@@ -91,14 +91,14 @@ public class TileDrive extends TileMachine implements IInventory, IStorageProvid
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
SC.restoreInventory(this, nbt);
InventoryUtil.restoreInventory(this, nbt);
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
SC.saveInventory(this, nbt);
InventoryUtil.saveInventory(this, nbt);
}
@Override

View File

@@ -7,6 +7,7 @@ import net.minecraft.tileentity.TileEntity;
import storagecraft.storage.IStorage;
import storagecraft.storage.IStorageProvider;
import storagecraft.storage.StorageItem;
import storagecraft.util.InventoryUtil;
public class TileStorageProxy extends TileMachine implements IStorageProvider, IStorage {
private IInventory inventory;
@@ -57,7 +58,7 @@ public class TileStorageProxy extends TileMachine implements IStorageProvider, I
inventory.setInventorySlotContents(i, stack);
return;
} else if (StorageItem.equalsIgnoreQuantity(slot, stack)) {
} else if (InventoryUtil.equalsIgnoreQuantity(slot, stack)) {
int toAdd = toGo;
if (slot.stackSize + toAdd > slot.getMaxStackSize()) {
@@ -86,7 +87,7 @@ public class TileStorageProxy extends TileMachine implements IStorageProvider, I
for (int i = 0; i < inventory.getSizeInventory(); ++i) {
ItemStack slot = inventory.getStackInSlot(i);
if (slot != null && StorageItem.equalsIgnoreQuantity(slot, stack)) {
if (slot != null && InventoryUtil.equalsIgnoreQuantity(slot, stack)) {
if (quantity > slot.stackSize) {
quantity = slot.stackSize;
}
@@ -117,7 +118,7 @@ public class TileStorageProxy extends TileMachine implements IStorageProvider, I
if (slot == null) {
return true;
} else if (StorageItem.equalsIgnoreQuantity(slot, stack)) {
} else if (InventoryUtil.equalsIgnoreQuantity(slot, stack)) {
int toAdd = toGo;
if (slot.stackSize + toAdd > slot.getMaxStackSize()) {

View File

@@ -0,0 +1,91 @@
package storagecraft.util;
import java.util.Random;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
public class InventoryUtil {
public static void saveInventory(IInventory inventory, NBTTagCompound nbt) {
NBTTagList tagList = new NBTTagList();
for (int i = 0; i < inventory.getSizeInventory(); i++) {
if (inventory.getStackInSlot(i) != null) {
NBTTagCompound compoundTag = new NBTTagCompound();
compoundTag.setInteger("Slot", i);
inventory.getStackInSlot(i).writeToNBT(compoundTag);
tagList.appendTag(compoundTag);
}
}
nbt.setTag("Inventory", tagList);
}
public static void restoreInventory(IInventory inventory, NBTTagCompound nbt) {
if (nbt.hasKey("Inventory")) {
NBTTagList tagList = nbt.getTagList("Inventory", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < tagList.tagCount(); i++) {
int slot = tagList.getCompoundTagAt(i).getInteger("Slot");
ItemStack stack = ItemStack.loadItemStackFromNBT(tagList.getCompoundTagAt(i));
inventory.setInventorySlotContents(slot, stack);
}
}
}
// https://github.com/cpw/ironchest/blob/master/src/main/java/cpw/mods/ironchest/BlockIronChest.java#L200
public static void dropInventory(World world, IInventory inventory, int x, int y, int z, int newSize) {
Random random = world.rand;
for (int i = newSize; i < inventory.getSizeInventory(); ++i) {
ItemStack stack = inventory.getStackInSlot(i);
if (stack == null) {
continue;
}
float xo = random.nextFloat() * 0.8F + 0.1F;
float yo = random.nextFloat() * 0.8F + 0.1F;
float zo = random.nextFloat() * 0.8F + 0.1F;
while (stack.stackSize > 0) {
int amount = random.nextInt(21) + 10;
if (amount > stack.stackSize) {
amount = stack.stackSize;
}
stack.stackSize -= amount;
EntityItem entity = new EntityItem(world, (float) x + xo, (float) y + (newSize > 0 ? 1 : 0) + yo, (float) z + zo, new ItemStack(stack.getItem(), amount, stack.getItemDamage()));
entity.motionX = (float) random.nextGaussian() * 0.05F;
entity.motionY = (float) random.nextGaussian() * 0.05F + 0.2F;
entity.motionZ = (float) random.nextGaussian() * 0.05F;
if (stack.hasTagCompound()) {
entity.getEntityItem().setTagCompound((NBTTagCompound) stack.getTagCompound().copy());
}
world.spawnEntityInWorld(entity);
}
}
}
public static boolean equalsIgnoreQuantity(ItemStack first, ItemStack second) {
if (first.stackTagCompound != null && !first.stackTagCompound.equals(second.stackTagCompound)) {
return false;
}
return first.getItem() == second.getItem() && first.getItemDamage() == second.getItemDamage();
}
}