Skip to content

Commit

Permalink
Merge pull request #208 from Xalcon/feature/bauble_capability
Browse files Browse the repository at this point in the history
Feature/bauble capability
  • Loading branch information
Azanor authored Aug 1, 2017
2 parents 0476cd6 + 8188e01 commit 8f99a00
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 31 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;
}
}
22 changes: 21 additions & 1 deletion src/main/java/baubles/api/cap/BaublesCapabilities.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
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 {

/**
* Access to the baubles capability.
*/
@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> {

Expand All @@ -27,5 +33,19 @@ public void readNBT (Capability<IBaublesItemHandler> capability, IBaublesItemHan

}
}


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) {

}
}

}
8 changes: 4 additions & 4 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))
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
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/baubles/common/Baubles.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
Expand All @@ -60,12 +68,18 @@ 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));

PacketHandler.init();

entityEventHandler = new EventHandlerEntity();

itemEventHandler = new EventHandlerItem();

MinecraftForge.EVENT_BUS.register(entityEventHandler);
MinecraftForge.EVENT_BUS.register(itemEventHandler);


/////////////////////
proxy.registerItemModels();
Expand Down
21 changes: 10 additions & 11 deletions src/main/java/baubles/common/container/ContainerPlayerExpanded.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 17 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 @@ -30,30 +31,38 @@ 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;
}

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

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 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args
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!=null && !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
11 changes: 7 additions & 4 deletions src/main/java/baubles/common/event/EventHandlerEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import baubles.api.BaubleType;
import baubles.api.BaublesApi;
import baubles.api.IBauble;
import baubles.api.cap.BaublesCapabilities;
import baubles.api.cap.BaublesContainer;
import baubles.api.cap.BaublesContainerProvider;
import baubles.api.cap.IBaublesItemHandler;
Expand Down Expand Up @@ -108,8 +109,9 @@ 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 != null && !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 Expand Up @@ -157,8 +159,9 @@ public void dropItemsAt(EntityPlayer player, List<EntityItem> 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));
}
}
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/baubles/common/event/EventHandlerItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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> 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 8f99a00

Please sign in to comment.