Skip to content

Commit

Permalink
Merge pull request #217 from Xalcon/feature/1.12_bauble_capability
Browse files Browse the repository at this point in the history
IBauble capability for 1.12

Thanks man, you are a lifesaver :)
  • Loading branch information
Azanor authored Aug 2, 2017
2 parents 9f35fd1 + 0361524 commit 5fcbafe
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 20 deletions.
19 changes: 19 additions & 0 deletions src/main/java/baubles/api/cap/BaubleItem.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
17 changes: 17 additions & 0 deletions src/main/java/baubles/api/cap/BaublesCapabilities.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,6 +14,9 @@ public class BaublesCapabilities {
@CapabilityInject(IBaublesItemHandler.class)
public static final Capability<IBaublesItemHandler> CAPABILITY_BAUBLES = null;

@CapabilityInject(IBauble.class)
public static final Capability<IBauble> CAPABILITY_ITEM_BAUBLE = null;

public static class CapabilityBaubles<T extends IBaublesItemHandler> implements IStorage<IBaublesItemHandler> {

@Override
Expand All @@ -23,4 +27,17 @@ public NBTBase writeNBT (Capability<IBaublesItemHandler> capability, IBaublesIte
@Override
public void readNBT (Capability<IBaublesItemHandler> capability, IBaublesItemHandler instance, EnumFacing side, NBTBase nbt){ }
}

public static class CapabilityItemBaubleStorage implements IStorage<IBauble> {

@Override
public NBTBase writeNBT (Capability<IBauble> capability, IBauble instance, EnumFacing side) {
return null;
}

@Override
public void readNBT (Capability<IBauble> capability, IBauble instance, EnumFacing side, NBTBase nbt) {

}
}
}
6 changes: 3 additions & 3 deletions src/main/java/baubles/api/cap/BaublesContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/baubles/client/ClientEventHandler.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/baubles/common/Baubles.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -56,6 +61,9 @@ public void preInit(FMLPreInitializationEvent event) {
CapabilityManager.INSTANCE.register(IBaublesItemHandler.class,
new CapabilityBaubles<IBaublesItemHandler>(), BaublesContainer.class);

CapabilityManager.INSTANCE
.register(IBauble.class, new BaublesCapabilities.CapabilityItemBaubleStorage(), () -> new BaubleItem(BaubleType.TRINKET));

proxy.registerEventHandlers();
PacketHandler.init();

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/baubles/common/CommonProxy.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 18 additions & 8 deletions src/main/java/baubles/common/container/SlotBauble.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,30 +30,39 @@ 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;
}

@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);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/baubles/common/event/CommandBaubles.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,8 +73,8 @@ else if (args.length >= 2) {
sender.sendMessage(new TextComponentTranslation("\u00a73Showing baubles for "+entityplayermp.getName()));
for (int a = 0; a<baubles.getSlots();a++) {
ItemStack st = baubles.getStackInSlot(a);
if (st!=null && !st.isEmpty() && st.getItem() instanceof IBauble) {
IBauble bauble=(IBauble)st.getItem();
if (!st.isEmpty() && st.hasCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null)) {
IBauble bauble = st.getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null);
BaubleType bt = bauble.getBaubleType(st);
sender.sendMessage(new TextComponentTranslation("\u00a73 [Slot "+a+"] "+bt+" "+st.getDisplayName()));
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/baubles/common/event/EventHandlerEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import baubles.api.cap.BaublesCapabilities;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
Expand Down Expand Up @@ -102,8 +104,8 @@ public void playerTick(PlayerEvent.LivingUpdateEvent event) {
for (int a = 0; a < count; a++) {
ItemStack baubleStack = baubles.getStackInSlot(a);
IBauble bauble = null;
if (baubleStack != null && !baubleStack.isEmpty() && baubleStack.getItem() instanceof IBauble) {
bauble = (IBauble) baubleStack.getItem();
if (!baubleStack.isEmpty() && baubleStack.hasCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null)) {
bauble = baubleStack.getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null);
//Worn Tick
bauble.onWornTick(baubleStack, player);
}
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/baubles/common/event/EventHandlerItem.java
Original file line number Diff line number Diff line change
@@ -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<ItemStack> 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> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
return capability == BaublesCapabilities.CAPABILITY_ITEM_BAUBLE
? BaublesCapabilities.CAPABILITY_ITEM_BAUBLE.cast((IBauble) stack.getItem())
: null;
}
});
}
}

0 comments on commit 5fcbafe

Please sign in to comment.