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..47285f94 --- /dev/null +++ b/src/main/java/baubles/api/cap/BaubleItem.java @@ -0,0 +1,19 @@ +package baubles.api.cap; + +import baubles.api.BaubleType; +import baubles.api.IBauble; +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; + } +} diff --git a/src/main/java/baubles/api/cap/BaublesCapabilities.java b/src/main/java/baubles/api/cap/BaublesCapabilities.java index de7d4f00..9a5a4bef 100644 --- a/src/main/java/baubles/api/cap/BaublesCapabilities.java +++ b/src/main/java/baubles/api/cap/BaublesCapabilities.java @@ -1,5 +1,6 @@ package baubles.api.cap; +import baubles.api.IBauble; import net.minecraft.nbt.NBTBase; import net.minecraft.util.EnumFacing; import net.minecraftforge.common.capabilities.Capability; @@ -13,6 +14,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 { @Override @@ -23,4 +27,17 @@ public NBTBase writeNBT (Capability capability, IBaublesIte @Override public void readNBT (Capability capability, IBaublesItemHandler instance, EnumFacing side, NBTBase nbt){ } } + + 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 d5984da2..6867057c 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)) + if (stack==null || stack.isEmpty() || !stack.hasCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null)) return false; - return ((IBauble) stack.getItem()).getBaubleType(stack).hasSlot(slot); + 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/client/ClientEventHandler.java b/src/main/java/baubles/client/ClientEventHandler.java index 92dc5156..ec5fff92 100644 --- a/src/main/java/baubles/client/ClientEventHandler.java +++ b/src/main/java/baubles/client/ClientEventHandler.java @@ -1,6 +1,8 @@ package baubles.client; +import baubles.api.cap.BaublesCapabilities; import net.minecraft.client.renderer.block.model.ModelResourceLocation; +import net.minecraft.client.resources.I18n; import net.minecraft.util.text.TextFormatting; import net.minecraftforge.client.event.ModelRegistryEvent; import net.minecraftforge.client.model.ModelLoader; @@ -34,9 +36,10 @@ public void playerTick(PlayerTickEvent event) { @SubscribeEvent public void tooltipEvent(ItemTooltipEvent event) { - if (!event.getItemStack().isEmpty() && event.getItemStack().getItem() instanceof IBauble) { - BaubleType bt = ((IBauble)event.getItemStack().getItem()).getBaubleType(event.getItemStack()); - event.getToolTip().add(TextFormatting.GOLD + net.minecraft.client.resources.I18n.format("name." + bt)); + if (!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.format("name."+bt)); } } } diff --git a/src/main/java/baubles/common/Baubles.java b/src/main/java/baubles/common/Baubles.java index 64e351ae..a1469403 100644 --- a/src/main/java/baubles/common/Baubles.java +++ b/src/main/java/baubles/common/Baubles.java @@ -1,6 +1,11 @@ package baubles.common; import java.io.File; + +import baubles.api.BaubleType; +import baubles.api.IBauble; +import baubles.api.cap.BaubleItem; +import baubles.api.cap.BaublesCapabilities; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import net.minecraftforge.common.capabilities.CapabilityManager; @@ -56,6 +61,9 @@ 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)); + proxy.registerEventHandlers(); PacketHandler.init(); diff --git a/src/main/java/baubles/common/CommonProxy.java b/src/main/java/baubles/common/CommonProxy.java index 64d74d5c..e7725f7a 100644 --- a/src/main/java/baubles/common/CommonProxy.java +++ b/src/main/java/baubles/common/CommonProxy.java @@ -1,6 +1,7 @@ package baubles.common; +import baubles.common.event.EventHandlerItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.world.World; import net.minecraftforge.common.MinecraftForge; @@ -29,6 +30,7 @@ public World getClientWorld() { public void registerEventHandlers() { MinecraftForge.EVENT_BUS.register(new EventHandlerEntity()); + MinecraftForge.EVENT_BUS.register(new EventHandlerItem()); } public void init() { } diff --git a/src/main/java/baubles/common/container/ContainerPlayerExpanded.java b/src/main/java/baubles/common/container/ContainerPlayerExpanded.java index af9b0abe..a04e8d36 100644 --- a/src/main/java/baubles/common/container/ContainerPlayerExpanded.java +++ b/src/main/java/baubles/common/container/ContainerPlayerExpanded.java @@ -216,9 +216,9 @@ 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(); + 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)) @@ -261,6 +261,11 @@ else if (!this.mergeItemStack(itemstack1, 9+ slotShift, 45+ slotShift, false)) return ItemStack.EMPTY; } + if (itemstack1.isEmpty() && !baubles.isEventBlocked() && slot instanceof SlotBauble && + itemstack.hasCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null)) { + itemstack.getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null).onUnequipped(itemstack, playerIn); + } + ItemStack itemstack2 = slot.onTake(playerIn, itemstack1); if (index == 0) diff --git a/src/main/java/baubles/common/container/SlotBauble.java b/src/main/java/baubles/common/container/SlotBauble.java index 919a2b6a..fa44ec43 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; @@ -29,15 +30,19 @@ 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.isEmpty()) + return false; + + IBauble bauble = stack.getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null); + return 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; @@ -45,14 +50,19 @@ 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() && !ItemStack.areItemStacksEqual(stack,getStack()) && + !((IBaublesItemHandler)getItemHandler()).isEventBlocked() && + getStack().hasCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null)) { + getStack().getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null).onUnequipped(getStack(), player); } + ItemStack oldstack = getStack().copy(); super.putStack(stack); - if (this.getHasStack() && !((IBaublesItemHandler)getItemHandler()).isEventBlocked()) { - ((IBauble)getStack().getItem()).onEquipped(getStack(), player); + if (getHasStack() && !ItemStack.areItemStacksEqual(oldstack,getStack()) + && !((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 53a58d8f..a8cb5507 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 @@ else if (args.length >= 2) { sender.sendMessage(new TextComponentTranslation("\u00a73Showing baubles for "+entityplayermp.getName())); for (int a = 0; a event) + { + ItemStack stack = event.getObject(); + 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 + ? BaublesCapabilities.CAPABILITY_ITEM_BAUBLE.cast((IBauble) stack.getItem()) + : null; + } + }); + } +}