From 733ba15be500d17ae3766e290acc2f0e1f584369 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 5 Jul 2017 13:24:26 +0200 Subject: [PATCH] added baubles api and change build.gradle for build --- build.gradle | 6 +- src/api/java/baubles/api/BaubleType.java | 30 +++++ src/api/java/baubles/api/BaublesApi.java | 36 ++++++ src/api/java/baubles/api/IBauble.java | 62 +++++++++ .../baubles/api/cap/BaublesCapabilities.java | 31 +++++ .../baubles/api/cap/BaublesContainer.java | 95 ++++++++++++++ .../api/cap/BaublesContainerProvider.java | 39 ++++++ .../baubles/api/cap/IBaublesItemHandler.java | 25 ++++ .../api/inv/BaublesInventoryWrapper.java | 121 ++++++++++++++++++ src/api/java/baubles/api/package-info.java | 5 + .../baubles/api/render/IRenderBauble.java | 114 +++++++++++++++++ .../models/item/itemdivingamulet.json | 2 +- 12 files changed, 562 insertions(+), 4 deletions(-) create mode 100644 src/api/java/baubles/api/BaubleType.java create mode 100644 src/api/java/baubles/api/BaublesApi.java create mode 100644 src/api/java/baubles/api/IBauble.java create mode 100644 src/api/java/baubles/api/cap/BaublesCapabilities.java create mode 100644 src/api/java/baubles/api/cap/BaublesContainer.java create mode 100644 src/api/java/baubles/api/cap/BaublesContainerProvider.java create mode 100644 src/api/java/baubles/api/cap/IBaublesItemHandler.java create mode 100644 src/api/java/baubles/api/inv/BaublesInventoryWrapper.java create mode 100644 src/api/java/baubles/api/package-info.java create mode 100644 src/api/java/baubles/api/render/IRenderBauble.java diff --git a/build.gradle b/build.gradle index 1b331aa..c1c176b 100644 --- a/build.gradle +++ b/build.gradle @@ -11,13 +11,13 @@ apply plugin: 'net.minecraftforge.gradle.forge' //Only edit below this line, the above code adds and enables the necessary things for Forge to be setup. -version = "1.11-1.0" +version = "1.11-1.3BETA" group= "com.baublelicious" archivesBaseName = "baublelicious" -sourceCompatibility = targetCompatibility = "1.6" // Need this here so eclipse task generates correctly. +sourceCompatibility = targetCompatibility = "1.8" // Need this here so eclipse task generates correctly. compileJava { - sourceCompatibility = targetCompatibility = "1.6" + sourceCompatibility = targetCompatibility = "1.8" } minecraft { diff --git a/src/api/java/baubles/api/BaubleType.java b/src/api/java/baubles/api/BaubleType.java new file mode 100644 index 0000000..07697c2 --- /dev/null +++ b/src/api/java/baubles/api/BaubleType.java @@ -0,0 +1,30 @@ +package baubles.api; + +public enum BaubleType { + AMULET(0), + RING(1,2), + BELT(3), + TRINKET(0,1,2,3,4,5,6), + HEAD(4), + BODY(5), + CHARM(6); + + int[] validSlots; + + private BaubleType(int ... validSlots) { + this.validSlots = validSlots; + } + + public boolean hasSlot(int slot) { + for (int s:validSlots) { + if (s == slot) return true; + } + return false; + } + + public int[] getValidSlots() { + return validSlots; + } + + +} diff --git a/src/api/java/baubles/api/BaublesApi.java b/src/api/java/baubles/api/BaublesApi.java new file mode 100644 index 0000000..6f6329c --- /dev/null +++ b/src/api/java/baubles/api/BaublesApi.java @@ -0,0 +1,36 @@ +package baubles.api; + +import baubles.api.cap.BaublesCapabilities; +import baubles.api.cap.IBaublesItemHandler; +import baubles.api.inv.BaublesInventoryWrapper; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; + +/** + * @author Azanor + */ +public class BaublesApi +{ + + /** + * Retrieves the baubles inventory capability handler for the supplied player + */ + public static IBaublesItemHandler getBaublesHandler(EntityPlayer player) + { + IBaublesItemHandler handler = player.getCapability(BaublesCapabilities.CAPABILITY_BAUBLES, null); + handler.setPlayer(player); + return handler; + } + + /** + * Retrieves the baubles capability handler wrapped as a IInventory for the supplied player + */ + @Deprecated + public static IInventory getBaubles(EntityPlayer player) + { + IBaublesItemHandler handler = player.getCapability(BaublesCapabilities.CAPABILITY_BAUBLES, null); + handler.setPlayer(player); + return new BaublesInventoryWrapper(handler, player); + } + +} diff --git a/src/api/java/baubles/api/IBauble.java b/src/api/java/baubles/api/IBauble.java new file mode 100644 index 0000000..b704098 --- /dev/null +++ b/src/api/java/baubles/api/IBauble.java @@ -0,0 +1,62 @@ +package baubles.api; + +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.item.ItemStack; + +/** + * + * This interface should be extended by items that can be worn in bauble slots + * + * @author Azanor + */ + +public interface IBauble { + + /** + * This method return the type of bauble this is. + * Type is used to determine the slots it can go into. + */ + public BaubleType getBaubleType(ItemStack itemstack); + + /** + * This method is called once per tick if the bauble is being worn by a player + */ + public default void onWornTick(ItemStack itemstack, EntityLivingBase player) { + } + + /** + * This method is called when the bauble is equipped by a player + */ + public default void onEquipped(ItemStack itemstack, EntityLivingBase player) { + } + + /** + * This method is called when the bauble is unequipped by a player + */ + public default void onUnequipped(ItemStack itemstack, EntityLivingBase player) { + } + + /** + * can this bauble be placed in a bauble slot + */ + public default boolean canEquip(ItemStack itemstack, EntityLivingBase player) { + return true; + } + + /** + * Can this bauble be removed from a bauble slot + */ + public default boolean canUnequip(ItemStack itemstack, EntityLivingBase player) { + return true; + } + + /** + * Will bauble automatically sync to client if a change is detected in its NBT or damage values? + * Default is off, so override and set to true if you want to auto sync. + * This sync is not instant, but occurs every 10 ticks (.5 seconds). + */ + public default boolean willAutoSync(ItemStack itemstack, EntityLivingBase player) { + return false; + } + +} diff --git a/src/api/java/baubles/api/cap/BaublesCapabilities.java b/src/api/java/baubles/api/cap/BaublesCapabilities.java new file mode 100644 index 0000000..fcfd437 --- /dev/null +++ b/src/api/java/baubles/api/cap/BaublesCapabilities.java @@ -0,0 +1,31 @@ +package baubles.api.cap; + +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; + +public class BaublesCapabilities { + + /** + * Access to the baubles capability. + */ + @CapabilityInject(IBaublesItemHandler.class) + public static final Capability CAPABILITY_BAUBLES = null; + + public static class CapabilityBaubles implements IStorage { + + @Override + public NBTBase writeNBT (Capability capability, IBaublesItemHandler instance, EnumFacing side) { + + return null; + } + + @Override + public void readNBT (Capability capability, IBaublesItemHandler instance, EnumFacing side, NBTBase nbt) { + + } + } + +} diff --git a/src/api/java/baubles/api/cap/BaublesContainer.java b/src/api/java/baubles/api/cap/BaublesContainer.java new file mode 100644 index 0000000..948e210 --- /dev/null +++ b/src/api/java/baubles/api/cap/BaublesContainer.java @@ -0,0 +1,95 @@ +package baubles.api.cap; + +import baubles.api.IBauble; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.item.ItemStack; +import net.minecraftforge.items.ItemStackHandler; + +public class BaublesContainer extends ItemStackHandler implements IBaublesItemHandler { + + private final static int BAUBLE_SLOTS = 7; + private boolean[] changed = new boolean[BAUBLE_SLOTS]; + private boolean blockEvents=false; + private EntityLivingBase player; + + public BaublesContainer() + { + super(BAUBLE_SLOTS); + } + + @Override + public void setSize(int size) + { + if (size, ICapabilityProvider { + + private final BaublesContainer container; + + public BaublesContainerProvider(BaublesContainer container) { + this.container = container; + } + + @Override + public boolean hasCapability (Capability capability, EnumFacing facing) { + return capability == BaublesCapabilities.CAPABILITY_BAUBLES; + } + + @Override + @SuppressWarnings("unchecked") + public T getCapability (Capability capability, EnumFacing facing) { + if (capability == BaublesCapabilities.CAPABILITY_BAUBLES) return (T) this.container; + return null; + } + + @Override + public NBTTagCompound serializeNBT () { + return this.container.serializeNBT(); + } + + @Override + public void deserializeNBT (NBTTagCompound nbt) { + this.container.deserializeNBT(nbt); + } + +} diff --git a/src/api/java/baubles/api/cap/IBaublesItemHandler.java b/src/api/java/baubles/api/cap/IBaublesItemHandler.java new file mode 100644 index 0000000..8397641 --- /dev/null +++ b/src/api/java/baubles/api/cap/IBaublesItemHandler.java @@ -0,0 +1,25 @@ +package baubles.api.cap; + +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraftforge.items.IItemHandlerModifiable; + +public interface IBaublesItemHandler extends IItemHandlerModifiable { + + public boolean isItemValidForSlot(int slot, ItemStack stack, EntityLivingBase player); + + /** + * Used internally to prevent equip/unequip events from triggering when they shouldn't + */ + public boolean isEventBlocked(); + public void setEventBlock(boolean blockEvents); + + /** + * Used internally for syncing. Indicates if the inventory has changed since last sync + */ + boolean isChanged(int slot); + void setChanged(int slot, boolean changed); + + public void setPlayer(EntityLivingBase player); +} diff --git a/src/api/java/baubles/api/inv/BaublesInventoryWrapper.java b/src/api/java/baubles/api/inv/BaublesInventoryWrapper.java new file mode 100644 index 0000000..c966cde --- /dev/null +++ b/src/api/java/baubles/api/inv/BaublesInventoryWrapper.java @@ -0,0 +1,121 @@ +package baubles.api.inv; + +import baubles.api.cap.IBaublesItemHandler; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.TextComponentString; + +public class BaublesInventoryWrapper implements IInventory { + + final IBaublesItemHandler handler; + final EntityPlayer player; + + public BaublesInventoryWrapper(IBaublesItemHandler handler) { + super(); + this.handler = handler; + this.player = null; + } + + public BaublesInventoryWrapper(IBaublesItemHandler handler, EntityPlayer player) { + super(); + this.handler = handler; + this.player = player; + } + + @Override + public String getName() { + return "BaublesInventory"; + } + + @Override + public boolean hasCustomName() { + return false; + } + + @Override + public ITextComponent getDisplayName() { + return new TextComponentString(this.getName()); + } + + @Override + public int getSizeInventory() { + return handler.getSlots(); + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public ItemStack getStackInSlot(int index) { + return handler.getStackInSlot(index); + } + + @Override + public ItemStack decrStackSize(int index, int count) { + return handler.extractItem(index, count, false); + } + + @Override + public ItemStack removeStackFromSlot(int index) { + ItemStack out = this.getStackInSlot(index); + handler.setStackInSlot(index, ItemStack.EMPTY); + return out; + } + + @Override + public void setInventorySlotContents(int index, ItemStack stack) { + handler.setStackInSlot(index, stack); + } + + @Override + public int getInventoryStackLimit() { + return 64; + } + + @Override + public void markDirty() { } + + @Override + public boolean isUsableByPlayer(EntityPlayer player) { + return true; + } + + @Override + public void openInventory(EntityPlayer player) { } + + @Override + public void closeInventory(EntityPlayer player) { } + + @Override + public boolean isItemValidForSlot(int index, ItemStack stack) { + return handler.isItemValidForSlot(index, stack, player); + } + + @Override + public int getField(int id) { + return 0; + } + + @Override + public void setField(int id, int value) {} + + @Override + public int getFieldCount() { + return 0; + } + + @Override + public void clear() { + for (int i = 0; i < this.getSizeInventory(); ++i) + { + this.setInventorySlotContents(i, ItemStack.EMPTY); + } + } + + + +} diff --git a/src/api/java/baubles/api/package-info.java b/src/api/java/baubles/api/package-info.java new file mode 100644 index 0000000..0052d2a --- /dev/null +++ b/src/api/java/baubles/api/package-info.java @@ -0,0 +1,5 @@ +@API(owner = "Baubles", apiVersion = "1.4.0.2", provides = "Baubles|API") +package baubles.api; + +import net.minecraftforge.fml.common.API; + diff --git a/src/api/java/baubles/api/render/IRenderBauble.java b/src/api/java/baubles/api/render/IRenderBauble.java new file mode 100644 index 0000000..4b26753 --- /dev/null +++ b/src/api/java/baubles/api/render/IRenderBauble.java @@ -0,0 +1,114 @@ +/** + * This class was created by . It's distributed as + * part of the Botania Mod. Get the Source Code in github: + * https://github.com/Vazkii/Botania + * + * Botania is Open Source and distributed under the + * Botania License: http://botaniamod.net/license.php + * + * File Created @ [Aug 27, 2014, 8:55:00 PM (GMT)] + */ + +package baubles.api.render; + +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.util.math.MathHelper; + +/** + * A Bauble Item that implements this will be have hooks to render something on + * the player while its equipped. + * This class doesn't extend IBauble to make the API not depend on the Baubles + * API, but the item in question still needs to implement IBauble. + */ +public interface IRenderBauble { + + /** + * Called for the rendering of the bauble on the player. The player instance can be + * acquired through the event parameter. Transformations are already applied for + * the RenderType passed in. Make sure to check against the type parameter for + * rendering. + */ + public void onPlayerBaubleRender(ItemStack stack, EntityPlayer player, RenderType type, float partialTicks); + + /** + * A few helper methods for the render. + */ + final class Helper { + + /** + * Rotates the render for a bauble correctly if the player is sneaking. + * Use for renders under {@link RenderType#BODY}. + */ + public static void rotateIfSneaking(EntityPlayer player) { + if(player.isSneaking()) + applySneakingRotation(); + } + + /** + * Rotates the render for a bauble correctly for a sneaking player. + * Use for renders under {@link RenderType#BODY}. + */ + public static void applySneakingRotation() { + GlStateManager.translate(0F, 0.2F, 0F); + GlStateManager.rotate(90F / (float) Math.PI, 1.0F, 0.0F, 0.0F); + } + + /** + * Shifts the render for a bauble correctly to the head, including sneaking rotation. + * Use for renders under {@link RenderType#HEAD}. + */ + public static void translateToHeadLevel(EntityPlayer player) { + GlStateManager.translate(0, -player.getDefaultEyeHeight(), 0); + if (player.isSneaking()) + GlStateManager.translate(0.25F * MathHelper.sin(player.rotationPitch * (float) Math.PI / 180), 0.25F * MathHelper.cos(player.rotationPitch * (float) Math.PI / 180), 0F); + } + + /** + * Shifts the render for a bauble correctly to the face. + * Use for renders under {@link RenderType#HEAD}, and usually after calling {@link Helper#translateToHeadLevel(EntityPlayer)}. + */ + public static void translateToFace() { + GlStateManager.rotate(90F, 0F, 1F, 0F); + GlStateManager.rotate(180F, 1F, 0F, 0F); + GlStateManager.translate(0f, -4.35f, -1.27f); + } + + /** + * Scales down the render to a correct size. + * Use for any render. + */ + public static void defaultTransforms() { + GlStateManager.translate(0.0, 3.0, 1.0); + GlStateManager.scale(0.55, 0.55, 0.55); + } + + /** + * Shifts the render for a bauble correctly to the chest. + * Use for renders under {@link RenderType#BODY}, and usually after calling {@link Helper#rotateIfSneaking(EntityPlayer)}. + */ + public static void translateToChest() { + GlStateManager.rotate(180F, 1F, 0F, 0F); + GlStateManager.translate(0F, -3.2F, -0.85F); + } + + } + + public enum RenderType { + /** + * Render Type for the player's body, translations apply on the player's rotation. + * Sneaking is not handled and should be done during the render. + * @see IRenderBauble.Helper + */ + BODY, + + /** + * Render Type for the player's body, translations apply on the player's head rotations. + * Sneaking is not handled and should be done during the render. + * @see IRenderBauble.Helper + */ + HEAD; + } + +} diff --git a/src/main/resources/assets/baublelicious/models/item/itemdivingamulet.json b/src/main/resources/assets/baublelicious/models/item/itemdivingamulet.json index 7724d25..62c5f02 100644 --- a/src/main/resources/assets/baublelicious/models/item/itemdivingamulet.json +++ b/src/main/resources/assets/baublelicious/models/item/itemdivingamulet.json @@ -1,5 +1,5 @@ { - "parent": "builtin/generated", + "parent": "item/generated", "textures": { "layer0": "baublelicious:items/itemdivingamulet" },