diff options
author | Alkalus <draknyte1@hotmail.com> | 2017-09-24 14:06:28 +1000 |
---|---|---|
committer | Alkalus <draknyte1@hotmail.com> | 2017-09-24 14:06:28 +1000 |
commit | 8391992b43365e261b986da893cf659e6aa178ee (patch) | |
tree | e7104e9a57b86282ec7d8b790165dc78f7d6a472 /src/Java/gtPlusPlus/core/util/nbt | |
parent | 665ecfab7aac36acde1e8bd2b0216f5144b82bd7 (diff) | |
download | GT5-Unofficial-8391992b43365e261b986da893cf659e6aa178ee.tar.gz GT5-Unofficial-8391992b43365e261b986da893cf659e6aa178ee.tar.bz2 GT5-Unofficial-8391992b43365e261b986da893cf659e6aa178ee.zip |
$ Temporary Trade Table crash fix.
Diffstat (limited to 'src/Java/gtPlusPlus/core/util/nbt')
-rw-r--r-- | src/Java/gtPlusPlus/core/util/nbt/NBTUtils.java | 141 |
1 files changed, 70 insertions, 71 deletions
diff --git a/src/Java/gtPlusPlus/core/util/nbt/NBTUtils.java b/src/Java/gtPlusPlus/core/util/nbt/NBTUtils.java index a82a4f1ba4..636fb60e6b 100644 --- a/src/Java/gtPlusPlus/core/util/nbt/NBTUtils.java +++ b/src/Java/gtPlusPlus/core/util/nbt/NBTUtils.java @@ -2,13 +2,11 @@ package gtPlusPlus.core.util.nbt; import static gtPlusPlus.core.item.ModItems.ZZZ_Empty; -import java.util.Iterator; import java.util.Map; import gregtech.api.items.GT_MetaGenerated_Tool; import gregtech.api.util.GT_ModHandler; import gregtech.api.util.GT_Utility; -import gtPlusPlus.core.item.ModItems; import gtPlusPlus.core.util.Utils; import gtPlusPlus.core.util.item.ItemUtils; import gtPlusPlus.core.util.reflect.ReflectionUtils; @@ -17,7 +15,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; public class NBTUtils { - + public static NBTTagCompound getNBT(ItemStack aStack) { NBTTagCompound rNBT = aStack.getTagCompound(); return ((rNBT == null) ? new NBTTagCompound() : rNBT); @@ -34,18 +32,17 @@ public class NBTUtils { return tNBT.getString("title"); } - public static ItemStack[] readItemsFromNBT(ItemStack itemstack){ + public static ItemStack[] readItemsFromNBT(ItemStack itemstack) { NBTTagCompound tNBT = getNBT(itemstack); final NBTTagList list = tNBT.getTagList("Items", 10); ItemStack inventory[] = new ItemStack[list.tagCount()]; - for(int i = 0;i<list.tagCount();i++){ + for (int i = 0; i < list.tagCount(); i++) { final NBTTagCompound data = list.getCompoundTagAt(i); final int slot = data.getInteger("Slot"); - if((slot >= 0) && (slot < list.tagCount())){ - if (ItemStack.loadItemStackFromNBT(data) == ItemUtils.getSimpleStack(ZZZ_Empty)){ + if ((slot >= 0) && (slot < list.tagCount())) { + if (ItemStack.loadItemStackFromNBT(data) == ItemUtils.getSimpleStack(ZZZ_Empty)) { inventory[slot] = null; - } - else { + } else { inventory[slot] = ItemStack.loadItemStackFromNBT(data); } @@ -53,19 +50,18 @@ public class NBTUtils { } return inventory; } - - public static ItemStack[] readItemsFromNBT(ItemStack itemstack, String customkey){ + + public static ItemStack[] readItemsFromNBT(ItemStack itemstack, String customkey) { NBTTagCompound tNBT = getNBT(itemstack); final NBTTagList list = tNBT.getTagList(customkey, 10); ItemStack inventory[] = new ItemStack[list.tagCount()]; - for(int i = 0;i<list.tagCount();i++){ + for (int i = 0; i < list.tagCount(); i++) { final NBTTagCompound data = list.getCompoundTagAt(i); final int slot = data.getInteger("Slot"); - if((slot >= 0) && (slot < list.tagCount())){ - if (ItemStack.loadItemStackFromNBT(data) == ItemUtils.getSimpleStack(ZZZ_Empty)){ + if ((slot >= 0) && (slot < list.tagCount())) { + if (ItemStack.loadItemStackFromNBT(data) == ItemUtils.getSimpleStack(ZZZ_Empty)) { inventory[slot] = null; - } - else { + } else { inventory[slot] = ItemStack.loadItemStackFromNBT(data); } @@ -74,18 +70,17 @@ public class NBTUtils { return inventory; } - public static ItemStack writeItemsToNBT(ItemStack itemstack, ItemStack[] stored){ + public static ItemStack writeItemsToNBT(ItemStack itemstack, ItemStack[] stored) { NBTTagCompound tNBT = getNBT(itemstack); final NBTTagList list = new NBTTagList(); - for(int i = 0;i<stored.length;i++){ + for (int i = 0; i < stored.length; i++) { final ItemStack stack = stored[i]; - if(stack != null){ + if (stack != null) { final NBTTagCompound data = new NBTTagCompound(); stack.writeToNBT(data); data.setInteger("Slot", i); list.appendTag(data); - } - else { + } else { final NBTTagCompound data = new NBTTagCompound(); ItemStack nullstack = ItemUtils.getSimpleStack(ZZZ_Empty); nullstack.writeToNBT(data); @@ -97,13 +92,13 @@ public class NBTUtils { itemstack.setTagCompound(tNBT); return itemstack; } - - public static ItemStack writeItemsToNBT(ItemStack itemstack, ItemStack[] stored, String customkey){ + + public static ItemStack writeItemsToNBT(ItemStack itemstack, ItemStack[] stored, String customkey) { NBTTagCompound tNBT = getNBT(itemstack); final NBTTagList list = new NBTTagList(); - for(int i = 0;i<stored.length;i++){ + for (int i = 0; i < stored.length; i++) { final ItemStack stack = stored[i]; - if(stack != null){ + if (stack != null) { final NBTTagCompound data = new NBTTagCompound(); stack.writeToNBT(data); data.setInteger("Slot", i); @@ -114,31 +109,35 @@ public class NBTUtils { itemstack.setTagCompound(tNBT); return itemstack; } - - public static ItemStack writeItemsToGtCraftingComponents(ItemStack rStack, ItemStack[] stored, boolean copyTags){ - - if (copyTags){ + + public static ItemStack writeItemsToGtCraftingComponents(ItemStack rStack, ItemStack[] stored, boolean copyTags) { + + if (copyTags) { for (int i = 0; i < stored.length; i++) { - if (stored[i] != null && stored[i].hasTagCompound()) { - rStack.setTagCompound((NBTTagCompound) stored[i].getTagCompound().copy()); - break; - } - } - } - - NBTTagCompound rNBT = rStack.getTagCompound(), tNBT = new NBTTagCompound(); - if (rNBT == null) rNBT = new NBTTagCompound(); - for (int i = 0; i < 9; i++) { - ItemStack tStack = stored[i]; - if (tStack != null && GT_Utility.getContainerItem(tStack, true) == null && !(tStack.getItem() instanceof GT_MetaGenerated_Tool)) { - tStack = GT_Utility.copyAmount(1, tStack); - if(GT_Utility.isStackValid(tStack)){ - GT_ModHandler.dischargeElectricItem(tStack, Integer.MAX_VALUE, Integer.MAX_VALUE, true, false, true); - tNBT.setTag("Ingredient." + i, tStack.writeToNBT(new NBTTagCompound()));} - } - } - rNBT.setTag("GT.CraftingComponents", tNBT); - rStack.setTagCompound(rNBT); + if (stored[i] != null && stored[i].hasTagCompound()) { + rStack.setTagCompound((NBTTagCompound) stored[i].getTagCompound().copy()); + break; + } + } + } + + NBTTagCompound rNBT = rStack.getTagCompound(), tNBT = new NBTTagCompound(); + if (rNBT == null) + rNBT = new NBTTagCompound(); + for (int i = 0; i < 9; i++) { + ItemStack tStack = stored[i]; + if (tStack != null && GT_Utility.getContainerItem(tStack, true) == null + && !(tStack.getItem() instanceof GT_MetaGenerated_Tool)) { + tStack = GT_Utility.copyAmount(1, tStack); + if (GT_Utility.isStackValid(tStack)) { + GT_ModHandler.dischargeElectricItem(tStack, Integer.MAX_VALUE, Integer.MAX_VALUE, true, false, + true); + tNBT.setTag("Ingredient." + i, tStack.writeToNBT(new NBTTagCompound())); + } + } + } + rNBT.setTag("GT.CraftingComponents", tNBT); + rStack.setTagCompound(rNBT); return rStack; } @@ -152,7 +151,7 @@ public class NBTUtils { NBTTagCompound tNBT = getNBT(aStack); return tNBT.getBoolean(aTag); } - + public static void setInteger(ItemStack aStack, String aTag, int aInt) { NBTTagCompound tNBT = getNBT(aStack); tNBT.setInteger(aTag, aInt); @@ -163,7 +162,7 @@ public class NBTUtils { NBTTagCompound tNBT = getNBT(aStack); return tNBT.getInteger(aTag); } - + public static void setString(ItemStack aStack, String aTag, String aString) { NBTTagCompound tNBT = getNBT(aStack); tNBT.setString(aTag, aString); @@ -174,31 +173,31 @@ public class NBTUtils { NBTTagCompound tNBT = getNBT(aStack); return tNBT.getString(aTag); } - - public static boolean tryIterateNBTData(ItemStack aStack){ - NBTTagCompound aNBT = NBTUtils.getNBT(aStack); - if (aNBT != null){ - if (!aNBT.hasNoTags()){ - int tagCount = 0; - Map<?, ?> mInternalMap = ReflectionUtils.getField(aNBT, "tagMap"); - - if (mInternalMap != null){ - mInternalMap.forEach( (k,v) -> System.out.println("Key: " + k + ": Value: " + v)); - } - else { - Utils.LOG_INFO("Data map reflected from NBTTagCompound was not valid."); + + public static boolean tryIterateNBTData(ItemStack aStack) { + try { + NBTTagCompound aNBT = NBTUtils.getNBT(aStack); + if (aNBT != null) { + if (!aNBT.hasNoTags()) { + int tagCount = 0; + Map<?, ?> mInternalMap = ReflectionUtils.getField(aNBT, "tagMap"); + + if (mInternalMap != null) { + mInternalMap.forEach((k, v) -> System.out.println("Key: " + k + ": Value: " + v)); + } else { + Utils.LOG_INFO("Data map reflected from NBTTagCompound was not valid."); + return false; + } + } else { return false; - } - } - else { + } + } else { return false; } - } - else { + return true; + } catch (Throwable t) { return false; } - return true; } - } |