From 8595ba717adb0eac1fda45b87b0c1c501a07d9ea Mon Sep 17 00:00:00 2001 From: Xalcon Date: Sun, 4 Jun 2017 12:55:09 +0200 Subject: [PATCH 1/3] - Registered IBauble as a capability - Replaced all IBauble item checks with has/getCapability - Added backwards compatibility event handler which adds the capability to all items that implement IBauble but dont have the capability yet --- src/main/java/baubles/api/cap/BaubleItem.java | 32 +++++++++++ .../baubles/api/cap/BaublesCapabilities.java | 22 +++++++- .../baubles/api/cap/BaublesContainer.java | 8 +-- src/main/java/baubles/common/Baubles.java | 16 +++++- .../container/ContainerPlayerExpanded.java | 21 ++++---- .../baubles/common/container/SlotBauble.java | 25 ++++++--- .../baubles/common/event/CommandBaubles.java | 5 +- .../common/event/EventHandlerEntity.java | 11 ++-- .../common/event/EventHandlerItem.java | 54 +++++++++++++++++++ 9 files changed, 163 insertions(+), 31 deletions(-) create mode 100644 src/main/java/baubles/api/cap/BaubleItem.java create mode 100644 src/main/java/baubles/common/event/EventHandlerItem.java diff --git a/src/main/java/baubles/api/cap/BaubleItem.java b/src/main/java/baubles/api/cap/BaubleItem.java new file mode 100644 index 00000000..3815414c --- /dev/null +++ b/src/main/java/baubles/api/cap/BaubleItem.java @@ -0,0 +1,32 @@ +package baubles.api.cap; + +import baubles.api.BaubleType; +import baubles.api.IBauble; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.item.ItemStack; + +public class BaubleItem implements IBauble +{ + private BaubleType baubleType; + + public BaubleItem(BaubleType type) { + baubleType = type; + } + + @Override + public BaubleType getBaubleType(ItemStack itemstack) { + return baubleType; + } + + @Override + public void onWornTick(ItemStack itemstack, EntityLivingBase player) { + } + + @Override + public void onEquipped(ItemStack itemstack, EntityLivingBase player) { + } + + @Override + public void onUnequipped(ItemStack itemstack, EntityLivingBase player) { + } +} diff --git a/src/main/java/baubles/api/cap/BaublesCapabilities.java b/src/main/java/baubles/api/cap/BaublesCapabilities.java index fcfd4375..cf84c6c2 100644 --- a/src/main/java/baubles/api/cap/BaublesCapabilities.java +++ b/src/main/java/baubles/api/cap/BaublesCapabilities.java @@ -1,11 +1,14 @@ package baubles.api.cap; +import baubles.api.IBauble; import net.minecraft.nbt.NBTBase; import net.minecraft.util.EnumFacing; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability.IStorage; import net.minecraftforge.common.capabilities.CapabilityInject; +import javax.annotation.Nonnull; + public class BaublesCapabilities { /** @@ -13,6 +16,9 @@ public class BaublesCapabilities { */ @CapabilityInject(IBaublesItemHandler.class) public static final Capability CAPABILITY_BAUBLES = null; + + @CapabilityInject(IBauble.class) + public static final Capability CAPABILITY_ITEM_BAUBLE = null; public static class CapabilityBaubles implements IStorage { @@ -27,5 +33,19 @@ public void readNBT (Capability capability, IBaublesItemHan } } - + + public static class CapabilityItemBaubleStorage implements IStorage { + + @Override + public NBTBase writeNBT (Capability capability, IBauble instance, EnumFacing side) { + + return null; + } + + @Override + public void readNBT (Capability capability, IBauble instance, EnumFacing side, NBTBase nbt) { + + } + } + } diff --git a/src/main/java/baubles/api/cap/BaublesContainer.java b/src/main/java/baubles/api/cap/BaublesContainer.java index 948e210e..c7e164d8 100644 --- a/src/main/java/baubles/api/cap/BaublesContainer.java +++ b/src/main/java/baubles/api/cap/BaublesContainer.java @@ -36,10 +36,10 @@ public void setSize(int size) */ @Override public boolean isItemValidForSlot(int slot, ItemStack stack, EntityLivingBase player) { - if (stack==null || stack.isEmpty() || !(stack.getItem() instanceof IBauble) || - !((IBauble) stack.getItem()).canEquip(stack, player)) - return false; - return ((IBauble) stack.getItem()).getBaubleType(stack).hasSlot(slot); + if (stack==null || stack.isEmpty() || !stack.hasCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null)) + return false; + IBauble bauble = stack.getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null); + return bauble.canEquip(stack, player) && bauble.getBaubleType(stack).hasSlot(slot); } @Override diff --git a/src/main/java/baubles/common/Baubles.java b/src/main/java/baubles/common/Baubles.java index eaf339d9..4d03a6b0 100644 --- a/src/main/java/baubles/common/Baubles.java +++ b/src/main/java/baubles/common/Baubles.java @@ -2,6 +2,13 @@ import java.io.File; +import baubles.api.BaubleType; +import baubles.api.IBauble; +import baubles.api.cap.BaubleItem; +import baubles.api.cap.BaublesCapabilities; +import baubles.common.event.EventHandlerItem; +import net.minecraft.client.Minecraft; +import net.minecraftforge.client.MinecraftForgeClient; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -41,6 +48,7 @@ public class Baubles { public static Baubles instance; public EventHandlerEntity entityEventHandler; + public EventHandlerItem itemEventHandler; public File modDir; public static final Logger log = LogManager.getLogger(MODID.toUpperCase()); @@ -60,12 +68,18 @@ public void preInit(FMLPreInitializationEvent event) { CapabilityManager.INSTANCE.register(IBaublesItemHandler.class, new CapabilityBaubles(), BaublesContainer.class); + + CapabilityManager.INSTANCE.register(IBauble.class, + new BaublesCapabilities.CapabilityItemBaubleStorage(), () -> new BaubleItem(BaubleType.TRINKET)); PacketHandler.init(); entityEventHandler = new EventHandlerEntity(); - + itemEventHandler = new EventHandlerItem(); + MinecraftForge.EVENT_BUS.register(entityEventHandler); + MinecraftForge.EVENT_BUS.register(itemEventHandler); + ///////////////////// proxy.registerItemModels(); diff --git a/src/main/java/baubles/common/container/ContainerPlayerExpanded.java b/src/main/java/baubles/common/container/ContainerPlayerExpanded.java index 98f87d8b..dc4e551d 100644 --- a/src/main/java/baubles/common/container/ContainerPlayerExpanded.java +++ b/src/main/java/baubles/common/container/ContainerPlayerExpanded.java @@ -231,17 +231,17 @@ else if (entityequipmentslot == EntityEquipmentSlot.OFFHAND && !((Slot)this.inve } // inv -> bauble - else if (itemstack.getItem() instanceof IBauble) + else if (itemstack.hasCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null)) { - IBauble bauble = (IBauble) itemstack1.getItem(); - for (int baubleSlot : bauble.getBaubleType(itemstack).getValidSlots()) { - if ( bauble.canEquip(itemstack1, thePlayer) && !((Slot)this.inventorySlots.get(baubleSlot+9)).getHasStack() && - !this.mergeItemStack(itemstack1, baubleSlot+9, baubleSlot + 10, false)) - { - return ItemStack.EMPTY; - } - if (itemstack1.getCount() == 0) break; - } + IBauble bauble = itemstack.getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null); + for (int baubleSlot : bauble.getBaubleType(itemstack).getValidSlots()) { + if ( bauble.canEquip(itemstack1, thePlayer) && !((Slot)this.inventorySlots.get(baubleSlot+9)).getHasStack() && + !this.mergeItemStack(itemstack1, baubleSlot+9, baubleSlot + 10, false)) + { + return ItemStack.EMPTY; + } + if (itemstack1.getCount() == 0) break; + } } else if (index >= 9+ slotShift && index < 36+ slotShift) @@ -293,7 +293,6 @@ else if (!this.mergeItemStack(itemstack1, 9+ slotShift, 45+ slotShift, false)) private void unequipBauble(ItemStack stack) { } - @Override public boolean canMergeSlot(ItemStack stack, Slot slot) diff --git a/src/main/java/baubles/common/container/SlotBauble.java b/src/main/java/baubles/common/container/SlotBauble.java index 3315ca2b..a873f08f 100644 --- a/src/main/java/baubles/common/container/SlotBauble.java +++ b/src/main/java/baubles/common/container/SlotBauble.java @@ -1,6 +1,7 @@ package baubles.common.container; import baubles.api.IBauble; +import baubles.api.cap.BaublesCapabilities; import baubles.api.cap.IBaublesItemHandler; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; @@ -30,15 +31,21 @@ public boolean isItemValid(ItemStack stack) @Override public boolean canTakeStack(EntityPlayer player) { - return getStack()!=null && !getStack().isEmpty() && - ((IBauble)getStack().getItem()).canUnequip(getStack(), player); + ItemStack stack = getStack(); + if(stack==null || stack.isEmpty()) + return false; + + IBauble bauble = stack.getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null); + // xalcon: the player should be able to unequip non-bauble items. Non-baubles shouldnt show up here, but we never know + return bauble == null || bauble.canUnequip(stack, player); } @Override public ItemStack onTake(EntityPlayer playerIn, ItemStack stack) { + if (!getHasStack() && !((IBaublesItemHandler)getItemHandler()).isEventBlocked() && - stack.getItem() instanceof IBauble) { - ((IBauble)stack.getItem()).onUnequipped(stack, playerIn); + stack.hasCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null)) { + stack.getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null).onUnequipped(stack, playerIn); } super.onTake(playerIn, stack); return stack; @@ -46,14 +53,16 @@ public ItemStack onTake(EntityPlayer playerIn, ItemStack stack) { @Override public void putStack(ItemStack stack) { - if (getHasStack() && !((IBaublesItemHandler)getItemHandler()).isEventBlocked()) { - ((IBauble)getStack().getItem()).onUnequipped(getStack(), player); + if (getHasStack() && !((IBaublesItemHandler)getItemHandler()).isEventBlocked() && + getStack().hasCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null)) { + getStack().getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null).onUnequipped(getStack(), player); } super.putStack(stack); - if (this.getHasStack() && !((IBaublesItemHandler)getItemHandler()).isEventBlocked()) { - ((IBauble)getStack().getItem()).onEquipped(getStack(), player); + if (this.getHasStack() && !((IBaublesItemHandler)getItemHandler()).isEventBlocked() && + getStack().hasCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null)) { + getStack().getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null).onEquipped(getStack(), player); } } diff --git a/src/main/java/baubles/common/event/CommandBaubles.java b/src/main/java/baubles/common/event/CommandBaubles.java index c0b57056..3b006c18 100644 --- a/src/main/java/baubles/common/event/CommandBaubles.java +++ b/src/main/java/baubles/common/event/CommandBaubles.java @@ -6,6 +6,7 @@ import baubles.api.BaubleType; import baubles.api.BaublesApi; import baubles.api.IBauble; +import baubles.api.cap.BaublesCapabilities; import baubles.api.cap.IBaublesItemHandler; import net.minecraft.command.CommandBase; import net.minecraft.command.CommandException; @@ -72,8 +73,8 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args sender.sendMessage(new TextComponentTranslation("\u00a73Showing baubles for "+entityplayermp.getName())); for (int a = 0; a drops, Entity e) { @SubscribeEvent public void tooltipEvent(ItemTooltipEvent event) { - if (event.getItemStack()!=null && !event.getItemStack().isEmpty() && event.getItemStack().getItem() instanceof IBauble) { - BaubleType bt = ((IBauble)event.getItemStack().getItem()).getBaubleType(event.getItemStack()); + if (event.getItemStack()!=null && !event.getItemStack().isEmpty() && event.getItemStack().hasCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null)) { + IBauble bauble = event.getItemStack().getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null); + BaubleType bt = bauble.getBaubleType(event.getItemStack()); event.getToolTip().add(TextFormatting.GOLD+I18n.translateToLocal("name."+bt)); } } diff --git a/src/main/java/baubles/common/event/EventHandlerItem.java b/src/main/java/baubles/common/event/EventHandlerItem.java new file mode 100644 index 00000000..2f0fae97 --- /dev/null +++ b/src/main/java/baubles/common/event/EventHandlerItem.java @@ -0,0 +1,54 @@ +package baubles.common.event; + +import baubles.api.IBauble; +import baubles.api.cap.BaublesCapabilities; +import baubles.common.Baubles; +import net.minecraft.item.ItemStack; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.ICapabilityProvider; +import net.minecraftforge.event.AttachCapabilitiesEvent; +import net.minecraftforge.fml.common.eventhandler.EventPriority; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class EventHandlerItem +{ + private static ResourceLocation capabilityResourceLocation = new ResourceLocation(Baubles.MODID, "bauble_cap"); + + /** + * Handles backwards compatibility with items that implement IBauble instead of exposing it as a capability. + * This adds a IBauble capability wrapper for all items, if the item: + * - does implement the IBauble interface + * - does not already have the capability + * - did not get the capability by another event handler earlier in the chain + * @param event + */ + @SubscribeEvent(priority = EventPriority.LOWEST) + public void itemCapabilityAttach(AttachCapabilitiesEvent.Item event) + { + ItemStack stack = event.getItemStack(); + if(stack.isEmpty() || !(stack.getItem() instanceof IBauble) || stack.hasCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null) + || event.getCapabilities().values().stream().anyMatch(c -> c.hasCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null))) + return; + + event.addCapability(capabilityResourceLocation, new ICapabilityProvider() { + + @Override + public boolean hasCapability(@Nonnull Capability capability, @Nullable EnumFacing facing) + { + return capability == BaublesCapabilities.CAPABILITY_ITEM_BAUBLE; + } + + @Nullable + @Override + public T getCapability(@Nonnull Capability capability, @Nullable EnumFacing facing) + { + return capability == BaublesCapabilities.CAPABILITY_ITEM_BAUBLE ? (T)stack.getItem() : null; + } + }); + } +} From a918ba317a77c12c2a2f26b16e881a8b6406c205 Mon Sep 17 00:00:00 2001 From: Xalcon Date: Sun, 4 Jun 2017 14:59:46 +0200 Subject: [PATCH 2/3] Minor Cleanup --- src/main/java/baubles/api/cap/BaubleItem.java | 13 ------------- .../java/baubles/common/event/EventHandlerItem.java | 4 +++- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/main/java/baubles/api/cap/BaubleItem.java b/src/main/java/baubles/api/cap/BaubleItem.java index 3815414c..47285f94 100644 --- a/src/main/java/baubles/api/cap/BaubleItem.java +++ b/src/main/java/baubles/api/cap/BaubleItem.java @@ -2,7 +2,6 @@ import baubles.api.BaubleType; import baubles.api.IBauble; -import net.minecraft.entity.EntityLivingBase; import net.minecraft.item.ItemStack; public class BaubleItem implements IBauble @@ -17,16 +16,4 @@ public BaubleItem(BaubleType type) { public BaubleType getBaubleType(ItemStack itemstack) { return baubleType; } - - @Override - public void onWornTick(ItemStack itemstack, EntityLivingBase player) { - } - - @Override - public void onEquipped(ItemStack itemstack, EntityLivingBase player) { - } - - @Override - public void onUnequipped(ItemStack itemstack, EntityLivingBase player) { - } } diff --git a/src/main/java/baubles/common/event/EventHandlerItem.java b/src/main/java/baubles/common/event/EventHandlerItem.java index 2f0fae97..d9af0f14 100644 --- a/src/main/java/baubles/common/event/EventHandlerItem.java +++ b/src/main/java/baubles/common/event/EventHandlerItem.java @@ -47,7 +47,9 @@ public boolean hasCapability(@Nonnull Capability capability, @Nullable EnumFa @Override public T getCapability(@Nonnull Capability capability, @Nullable EnumFacing facing) { - return capability == BaublesCapabilities.CAPABILITY_ITEM_BAUBLE ? (T)stack.getItem() : null; + return capability == BaublesCapabilities.CAPABILITY_ITEM_BAUBLE + ? BaublesCapabilities.CAPABILITY_ITEM_BAUBLE.cast((IBauble)stack.getItem()) + : null; } }); } From 8188e013a0746a2d5a1876edf8487b658980c3b7 Mon Sep 17 00:00:00 2001 From: Xalcon Date: Sun, 4 Jun 2017 15:07:16 +0200 Subject: [PATCH 3/3] Identation fixes (Github, wtf?) --- src/main/java/baubles/common/container/SlotBauble.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/baubles/common/container/SlotBauble.java b/src/main/java/baubles/common/container/SlotBauble.java index a873f08f..56cfcdc7 100644 --- a/src/main/java/baubles/common/container/SlotBauble.java +++ b/src/main/java/baubles/common/container/SlotBauble.java @@ -31,12 +31,12 @@ public boolean isItemValid(ItemStack stack) @Override public boolean canTakeStack(EntityPlayer player) { - ItemStack stack = getStack(); - if(stack==null || stack.isEmpty()) - return false; + ItemStack stack = getStack(); + if(stack==null || stack.isEmpty()) + return false; - IBauble bauble = stack.getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null); - // xalcon: the player should be able to unequip non-bauble items. Non-baubles shouldnt show up here, but we never know + IBauble bauble = stack.getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null); + // xalcon: the player should be able to unequip non-bauble items. Non-baubles shouldnt show up here, but we never know return bauble == null || bauble.canUnequip(stack, player); }