From ac522723a12060770fdeae8a8de3ef51516e009c Mon Sep 17 00:00:00 2001 From: HenryLoenwind Date: Thu, 17 Dec 2015 20:20:24 +0100 Subject: [PATCH] Add support for soulbound items in a Galacticraft inventory --- .../enchantment/EnchantmentSoulBound.java | 22 +++++++++- .../crazypants/util/GalacticraftUtil.java | 44 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/main/java/crazypants/util/GalacticraftUtil.java diff --git a/src/main/java/crazypants/enderio/enchantment/EnchantmentSoulBound.java b/src/main/java/crazypants/enderio/enchantment/EnchantmentSoulBound.java index a931d8a5..c1b0258a 100644 --- a/src/main/java/crazypants/enderio/enchantment/EnchantmentSoulBound.java +++ b/src/main/java/crazypants/enderio/enchantment/EnchantmentSoulBound.java @@ -8,6 +8,7 @@ import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.enchantment.EnumEnchantmentType; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemArmor; @@ -25,6 +26,7 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent; import crazypants.enderio.EnderIO; import crazypants.enderio.config.Config; import crazypants.util.BaublesUtil; +import crazypants.util.GalacticraftUtil; @Interface(iface = "tterrag.core.api.common.enchant.IAdvancedEnchant", modid = "ttCore") public class EnchantmentSoulBound extends Enchantment implements IAdvancedEnchant { @@ -59,7 +61,7 @@ public class EnchantmentSoulBound extends Enchantment implements IAdvancedEnchan } /* - * This is called the moment the player dies and drops is stuff. + * This is called the moment the player dies and drops his stuff. * * We go early, so we can get our items before other mods put them into some * grave. Also remove them from the list so they won't get duped. If the @@ -101,7 +103,23 @@ public class EnchantmentSoulBound extends Enchantment implements IAdvancedEnchan } } } - + + // Galacticraft. Again we are too early for those items. We just dump the + // stuff into the normal inventory to not have to keep a separate list. + if (evt.entityPlayer instanceof EntityPlayerMP) { + IInventory galacticraft = GalacticraftUtil.getGCInventoryForPlayer((EntityPlayerMP) evt.entityPlayer); + if (galacticraft != null) { + for (int i = 0; i < galacticraft.getSizeInventory(); i++) { + ItemStack item = galacticraft.getStackInSlot(i); + if (isSoulBound(item)) { + if (addToPlayerInventory(evt.entityPlayer, item)) { + galacticraft.setInventorySlotContents(i, null); + } + } + } + } + } + } /* diff --git a/src/main/java/crazypants/util/GalacticraftUtil.java b/src/main/java/crazypants/util/GalacticraftUtil.java new file mode 100644 index 00000000..7a2d1d48 --- /dev/null +++ b/src/main/java/crazypants/util/GalacticraftUtil.java @@ -0,0 +1,44 @@ +package crazypants.util; + +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.inventory.IInventory; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import crazypants.enderio.Log; + +/** + * This is a near-verbatim copy of Galacticraft's API class 'AccessInventoryGC'. + * + * We don't need the extended functionality and already have more APIs included + * than is good for our sanity. + * + */ +public class GalacticraftUtil { + + private static Class playerStatsClass; + private static Method getMethod; + private static Field extendedInventoryField; + + private static boolean accessFailed = false; + + public static IInventory getGCInventoryForPlayer(EntityPlayerMP player) { + if (!accessFailed) { + try { + if (playerStatsClass == null || getMethod == null || extendedInventoryField == null) { + playerStatsClass = Class.forName("micdoodle8.mods.galacticraft.core.entities.player.GCPlayerStats"); + getMethod = playerStatsClass.getMethod("get", EntityPlayerMP.class); + extendedInventoryField = playerStatsClass.getField("extendedInventory"); + } + Object stats = getMethod.invoke(null, player); + return stats == null ? null : (IInventory) extendedInventoryField.get(stats); + } catch (Exception e) { + Log.info("Galacticraft inventory inaccessable. Most likely because it is not installed."); + accessFailed = true; + } + } + + return null; + } +} \ No newline at end of file