From 31c1c41c1b2116a4d885da908328443eca441510 Mon Sep 17 00:00:00 2001 From: HenryLoenwind Date: Thu, 19 Mar 2015 01:29:04 +0100 Subject: [PATCH] Added Soulbound support for Baubles * Includes Baubles API 1.0.1.10 (will not be included in ender io jar) --- src/api/java/baubles/api/BaubleType.java | 7 +++ src/api/java/baubles/api/BaublesApi.java | 41 +++++++++++++++++ src/api/java/baubles/api/IBauble.java | 45 +++++++++++++++++++ src/api/java/baubles/api/package-info.java | 5 +++ .../enchantment/EnchantmentSoulBound.java | 17 +++++++ .../crazypants/enderio/tool/BaublesTool.java | 34 ++++++++++++++ 6 files changed, 149 insertions(+) create mode 100644 src/api/java/baubles/api/BaubleType.java create mode 100644 src/api/java/baubles/api/BaublesApi.java create mode 100644 src/api/java/baubles/api/IBauble.java create mode 100644 src/api/java/baubles/api/package-info.java create mode 100644 src/main/java/crazypants/enderio/tool/BaublesTool.java diff --git a/src/api/java/baubles/api/BaubleType.java b/src/api/java/baubles/api/BaubleType.java new file mode 100644 index 00000000..22349ff7 --- /dev/null +++ b/src/api/java/baubles/api/BaubleType.java @@ -0,0 +1,7 @@ +package baubles.api; + +public enum BaubleType { + RING, + AMULET, + BELT +} diff --git a/src/api/java/baubles/api/BaublesApi.java b/src/api/java/baubles/api/BaublesApi.java new file mode 100644 index 00000000..f122e909 --- /dev/null +++ b/src/api/java/baubles/api/BaublesApi.java @@ -0,0 +1,41 @@ +package baubles.api; + +import java.lang.reflect.Method; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import cpw.mods.fml.common.FMLLog; + +/** + * @author Azanor + */ +public class BaublesApi +{ + static Method getBaubles; + + /** + * Retrieves the baubles inventory for the supplied player + */ + public static IInventory getBaubles(EntityPlayer player) + { + IInventory ot = null; + + try + { + if(getBaubles == null) + { + Class fake = Class.forName("baubles.common.lib.PlayerHandler"); + getBaubles = fake.getMethod("getPlayerBaubles", EntityPlayer.class); + } + + ot = (IInventory) getBaubles.invoke(null, player); + } + catch(Exception ex) + { + FMLLog.warning("[Baubles API] Could not invoke baubles.common.lib.PlayerHandler method getPlayerBaubles"); + } + + return ot; + } + +} diff --git a/src/api/java/baubles/api/IBauble.java b/src/api/java/baubles/api/IBauble.java new file mode 100644 index 00000000..ccc3683f --- /dev/null +++ b/src/api/java/baubles/api/IBauble.java @@ -0,0 +1,45 @@ +package baubles.api; + +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.item.ItemStack; + +/** + * + * This interface should be extended by items that can be worn in bauble slots + * + * @author Azanor + */ + +public interface IBauble { + + /** + * This method return the type of bauble this is. + * Type is used to determine the slots it can go into. + */ + public BaubleType getBaubleType(ItemStack itemstack); + + /** + * This method is called once per tick if the bauble is being worn by a player + */ + public void onWornTick(ItemStack itemstack, EntityLivingBase player); + + /** + * This method is called when the bauble is equipped by a player + */ + public void onEquipped(ItemStack itemstack, EntityLivingBase player); + + /** + * This method is called when the bauble is unequipped by a player + */ + public void onUnequipped(ItemStack itemstack, EntityLivingBase player); + + /** + * can this bauble be placed in a bauble slot + */ + public boolean canEquip(ItemStack itemstack, EntityLivingBase player); + + /** + * Can this bauble be removed from a bauble slot + */ + public boolean canUnequip(ItemStack itemstack, EntityLivingBase player); +} diff --git a/src/api/java/baubles/api/package-info.java b/src/api/java/baubles/api/package-info.java new file mode 100644 index 00000000..497a7dd5 --- /dev/null +++ b/src/api/java/baubles/api/package-info.java @@ -0,0 +1,5 @@ +@API(owner = "Baubles", apiVersion = "1.0.1.10", provides = "Baubles|API") +package baubles.api; + +import cpw.mods.fml.common.API; + diff --git a/src/main/java/crazypants/enderio/enchantment/EnchantmentSoulBound.java b/src/main/java/crazypants/enderio/enchantment/EnchantmentSoulBound.java index 0fe3ebd8..9c7b1880 100644 --- a/src/main/java/crazypants/enderio/enchantment/EnchantmentSoulBound.java +++ b/src/main/java/crazypants/enderio/enchantment/EnchantmentSoulBound.java @@ -10,6 +10,7 @@ import net.minecraft.enchantment.EnumEnchantmentType; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemArmor; import net.minecraft.item.ItemStack; import net.minecraftforge.common.MinecraftForge; @@ -20,6 +21,7 @@ import cpw.mods.fml.common.Optional.Interface; import cpw.mods.fml.common.eventhandler.EventPriority; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import crazypants.enderio.config.Config; +import crazypants.enderio.tool.BaublesTool; import crazypants.util.Lang; @Interface(iface = "tterrag.core.api.common.enchant.IAdvancedEnchant", modid = "ttCore") @@ -73,6 +75,21 @@ public class EnchantmentSoulBound extends Enchantment implements IAdvancedEnchan } } + // Note: Baubles will also add its items to evt.drops, but later. We cannot + // wait for that because gravestone mods also listen to this event. So we have + // to fetch Baubles items ourselves here. + // For the same reason we cannot put the items into Baubles slots. + IInventory baubles = BaublesTool.getInstance().getBaubles(evt.entityPlayer); + if (baubles != null) { + for (int i = 0; i < baubles.getSizeInventory(); i++) { + ItemStack item = baubles.getStackInSlot(i); + if(isSoulBound(item)) { + addToPlayerInventory(evt.entityPlayer, item); + baubles.setInventorySlotContents(i, null); + } + } + } + } @SubscribeEvent diff --git a/src/main/java/crazypants/enderio/tool/BaublesTool.java b/src/main/java/crazypants/enderio/tool/BaublesTool.java new file mode 100644 index 00000000..9e5b30df --- /dev/null +++ b/src/main/java/crazypants/enderio/tool/BaublesTool.java @@ -0,0 +1,34 @@ +package crazypants.enderio.tool; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import cpw.mods.fml.common.Optional.Method; + +public class BaublesTool { + + private static final BaublesTool instance = new withBaubles(); + + public static BaublesTool getInstance() { + return instance; + } + + public boolean hasBaubles() { + return false; + } + + public IInventory getBaubles(EntityPlayer player) { + return null; + } + + private static class withBaubles extends BaublesTool { + @Method(modid = "Baubles") + public boolean hasBaubles() { + return true; + } + @Method(modid = "Baubles") + public IInventory getBaubles(EntityPlayer player) { + return baubles.api.BaublesApi.getBaubles(player); + } + } + +}