-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from Xalcon/feature/bauble_capability
Feature/bauble capability
- Loading branch information
Showing
9 changed files
with
152 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); | ||
} | ||
} |