mirror of
https://github.com/GTNewHorizons/StorageDrawers.git
synced 2026-11-18 11:29:31 +00:00
Save drawer contents using vanilla ItemStack methods to be compatible with Postea transforms (#65)
This commit is contained in:
1 parent
5d1a753a7e
commit
0d313c8a3e
4 files changed
+70
-47
No files matched your search
+7
-18
@@ -6,7 +6,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.CraftingManager;
|
||||
import net.minecraft.item.crafting.IRecipe;
|
||||
@@ -26,6 +25,7 @@ import com.jaquadro.minecraft.storagedrawers.storage.BaseDrawerData;
|
||||
import com.jaquadro.minecraft.storagedrawers.storage.CompDrawerData;
|
||||
import com.jaquadro.minecraft.storagedrawers.storage.DrawerData;
|
||||
import com.jaquadro.minecraft.storagedrawers.storage.ICentralInventory;
|
||||
import com.jaquadro.minecraft.storagedrawers.util.ItemStackConversion;
|
||||
|
||||
import cpw.mods.fml.common.FMLLog;
|
||||
import cpw.mods.fml.common.network.NetworkRegistry;
|
||||
@@ -667,27 +667,16 @@ public class TileEntityDrawersComp extends TileEntityDrawers {
|
||||
|
||||
@Override
|
||||
public void writeToNBT(int slot, NBTTagCompound tag) {
|
||||
ItemStack protoStack = getStoredItemPrototype(slot);
|
||||
if (protoStack != null && protoStack.getItem() != null) {
|
||||
tag.setShort("Item", (short) Item.getIdFromItem(protoStack.getItem()));
|
||||
tag.setShort("Meta", (short) protoStack.getItemDamage());
|
||||
tag.setInteger("Count", 0); // TODO: Remove when ready to break 1.1.7 compat
|
||||
|
||||
if (protoStack.getTagCompound() != null) tag.setTag("Tags", protoStack.getTagCompound());
|
||||
}
|
||||
final ItemStack protoStack = getStoredItemPrototype(slot);
|
||||
ItemStackConversion.writeToNBT(tag, protoStack, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(int slot, NBTTagCompound tag) {
|
||||
if (tag.hasKey("Item")) {
|
||||
Item item = Item.getItemById(tag.getShort("Item"));
|
||||
if (item != null) {
|
||||
ItemStack stack = new ItemStack(item);
|
||||
stack.setItemDamage(tag.getShort("Meta"));
|
||||
if (tag.hasKey("Tags")) stack.setTagCompound(tag.getCompoundTag("Tags"));
|
||||
|
||||
protoStack[slot] = stack;
|
||||
}
|
||||
final ItemStack stack = ItemStackConversion.readFromNBT(tag);
|
||||
if (stack != null) {
|
||||
stack.stackSize = 1;
|
||||
protoStack[slot] = stack;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ import java.util.UUID;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemBlock;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
@@ -25,6 +24,7 @@ import com.jaquadro.minecraft.storagedrawers.block.tile.TileEntityDrawers;
|
||||
import com.jaquadro.minecraft.storagedrawers.block.tile.TileEntityDrawersStandard;
|
||||
import com.jaquadro.minecraft.storagedrawers.config.ConfigManager;
|
||||
import com.jaquadro.minecraft.storagedrawers.core.ModItems;
|
||||
import com.jaquadro.minecraft.storagedrawers.util.ItemStackConversion;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
@@ -253,15 +253,8 @@ public class ItemDrawers extends ItemBlock {
|
||||
|
||||
/** Read ItemStack from NBT when representing drawer slot. */
|
||||
protected ItemStack getItemStackFromDrawer(NBTTagCompound slot) {
|
||||
// Logic copied from readNBT method of DrawerData class.
|
||||
ItemStack stack = null;
|
||||
if (slot.hasKey("Item") && slot.hasKey("Count")) {
|
||||
Item item = Item.getItemById(slot.getShort("Item"));
|
||||
if (item != null) { // p_i1881_2_ - stackSize
|
||||
stack = new ItemStack(item, 1, slot.getShort("Meta"));
|
||||
if (slot.hasKey("Tags")) stack.setTagCompound(slot.getCompoundTag("Tags"));
|
||||
}
|
||||
}
|
||||
final ItemStack stack = ItemStackConversion.readFromNBT(slot);
|
||||
if (stack != null) stack.stackSize = 1;
|
||||
return stack;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IQuantifiable
|
||||
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IShroudable;
|
||||
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IVoidable;
|
||||
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute;
|
||||
import com.jaquadro.minecraft.storagedrawers.util.ItemStackConversion;
|
||||
|
||||
public class DrawerData extends BaseDrawerData implements IVoidable, IShroudable, ILockable, IQuantifiable {
|
||||
|
||||
@@ -163,29 +164,17 @@ public class DrawerData extends BaseDrawerData implements IVoidable, IShroudable
|
||||
}
|
||||
|
||||
public void writeToNBT(NBTTagCompound tag) {
|
||||
if (protoStack.getItem() != null) {
|
||||
tag.setShort("Item", (short) Item.getIdFromItem(protoStack.getItem()));
|
||||
tag.setShort("Meta", (short) protoStack.getItemDamage());
|
||||
tag.setInteger("Count", count);
|
||||
|
||||
if (protoStack.getTagCompound() != null) tag.setTag("Tags", protoStack.getTagCompound());
|
||||
}
|
||||
ItemStackConversion.writeToNBT(tag, protoStack, count);
|
||||
}
|
||||
|
||||
public void readFromNBT(NBTTagCompound tag) {
|
||||
if (tag.hasKey("Item") && tag.hasKey("Count")) {
|
||||
Item item = Item.getItemById(tag.getShort("Item"));
|
||||
if (item != null) {
|
||||
ItemStack stack = new ItemStack(item);
|
||||
stack.setItemDamage(tag.getShort("Meta"));
|
||||
if (tag.hasKey("Tags")) stack.setTagCompound(tag.getCompoundTag("Tags"));
|
||||
|
||||
setStoredItem(stack, tag.getInteger("Count"), false);
|
||||
} else {
|
||||
reset();
|
||||
}
|
||||
} else {
|
||||
final ItemStack stack = ItemStackConversion.readFromNBT(tag);
|
||||
if (stack == null) {
|
||||
reset();
|
||||
} else {
|
||||
final int count = stack.stackSize;
|
||||
stack.stackSize = 1;
|
||||
setStoredItem(stack, count, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.jaquadro.minecraft.storagedrawers.util;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.common.util.Constants.NBT;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Utility to convert from the nbt storage format for ItemStacks to the new
|
||||
*/
|
||||
public final class ItemStackConversion {
|
||||
|
||||
private ItemStackConversion() {}
|
||||
|
||||
public static void writeToNBT(@NotNull NBTTagCompound tag, @Nullable ItemStack stack, int stacksize) {
|
||||
if (stack != null && stack.getItem() != null) {
|
||||
tag.setTag("ItemStack", stack.writeToNBT(new NBTTagCompound()));
|
||||
tag.setInteger("Count", stacksize);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static ItemStack readFromNBT(@NotNull NBTTagCompound tag) {
|
||||
if (tag.hasKey("ItemStack", NBT.TAG_COMPOUND)) {
|
||||
final ItemStack stack = ItemStack.loadItemStackFromNBT(tag.getCompoundTag("ItemStack"));
|
||||
if (stack != null && tag.hasKey("Count", NBT.TAG_INT)) {
|
||||
stack.stackSize = tag.getInteger("Count");
|
||||
}
|
||||
return stack;
|
||||
}
|
||||
return readFromLegacyNBT(tag);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static ItemStack readFromLegacyNBT(@NotNull NBTTagCompound tag) {
|
||||
if (tag.hasKey("Item") && tag.hasKey("Count")) {
|
||||
final NBTTagCompound stackNBT = new NBTTagCompound();
|
||||
stackNBT.setShort("id", tag.getShort("Item"));
|
||||
stackNBT.setByte("Count", (byte) 1);
|
||||
stackNBT.setShort("Damage", tag.getShort("Meta"));
|
||||
if (tag.hasKey("Tags", NBT.TAG_COMPOUND)) {
|
||||
stackNBT.setTag("tag", tag.getCompoundTag("Tags"));
|
||||
}
|
||||
final ItemStack stack = ItemStack.loadItemStackFromNBT(stackNBT);
|
||||
if (stack != null) stack.stackSize = tag.getInteger("Count");
|
||||
return stack;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user