diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/IStorageDrawersApi.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/IStorageDrawersApi.java new file mode 100644 index 00000000..5c73621e --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/IStorageDrawersApi.java @@ -0,0 +1,20 @@ +package com.jaquadro.minecraft.storagedrawers.api; + +import com.jaquadro.minecraft.storagedrawers.api.pack.IPackBlockFactory; +import com.jaquadro.minecraft.storagedrawers.api.registry.IRecipeHandlerRegistry; +import com.jaquadro.minecraft.storagedrawers.api.registry.IRenderRegistry; +import com.jaquadro.minecraft.storagedrawers.api.registry.IWailaRegistry; + +public interface IStorageDrawersApi +{ + /** + * Recipe handlers are used to make custom recipes compatible with compacting drawers. + */ + IRecipeHandlerRegistry recipeHandlerRegistry (); + + IRenderRegistry renderRegistry (); + + IWailaRegistry wailaRegistry (); + + IPackBlockFactory packFactory (); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/StorageDrawersApi.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/StorageDrawersApi.java new file mode 100644 index 00000000..70a15174 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/StorageDrawersApi.java @@ -0,0 +1,28 @@ +package com.jaquadro.minecraft.storagedrawers.api; + +/** + * Entry point for the public API. + */ +public class StorageDrawersApi +{ + private static IStorageDrawersApi instance; + + /** + * API entry point. + * + * @return The {@link IStorageDrawersApi} instance or null if the API or Storage Drawers is unavailable. + */ + public static IStorageDrawersApi instance () { + if (instance == null) { + try { + Class classApi = Class.forName( "com.jaquadro.minecraft.storagedrawers.core.Api" ); + instance = (IStorageDrawersApi) classApi.getField("instance").get(null); + } + catch (Throwable t) { + return null; + } + } + + return instance; + } +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/event/DrawerPopulatedEvent.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/event/DrawerPopulatedEvent.java new file mode 100644 index 00000000..f5dca193 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/event/DrawerPopulatedEvent.java @@ -0,0 +1,19 @@ +package com.jaquadro.minecraft.storagedrawers.api.event; + +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; +import cpw.mods.fml.common.eventhandler.Event; + +/** + * This event is called when a drawer has been bound to a new item. This is + * and opportunity for mods to cache extended data with the drawer. + * + * This event is also called when the drawer is changed to empty. + */ +public class DrawerPopulatedEvent extends Event +{ + public final IDrawer drawer; + + public DrawerPopulatedEvent (IDrawer drawer) { + this.drawer = drawer; + } +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/inventory/IDrawerInventory.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/inventory/IDrawerInventory.java new file mode 100644 index 00000000..47396c79 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/inventory/IDrawerInventory.java @@ -0,0 +1,35 @@ +package com.jaquadro.minecraft.storagedrawers.api.inventory; + +import net.minecraft.inventory.ISidedInventory; +import net.minecraft.item.ItemStack; + +public interface IDrawerInventory extends ISidedInventory +{ + /** + * Gets a drawer's group slot index from an IInventory slot index. + * + * @param inventorySlot An IInventory slot index returned from getInventorySlot. + */ + int getDrawerSlot (int inventorySlot); + + /** + * Gets an IInventory slot index suitable for operations for the given type. + * + * @param drawerSlot The index of the drawer within its group. + * @param type The type of IInventory slot to return an index for. + */ + int getInventorySlot (int drawerSlot, SlotType type); + + /** + * Gets the type associated with a given IInventory slot index. + * + * @param inventorySlot An IInventory slot index returned from getInventorySlot. + */ + SlotType getInventorySlotType (int inventorySlot); + + boolean canInsertItem (int slot, ItemStack stack); + + boolean canExtractItem (int slot, ItemStack stack); + + boolean syncInventoryIfNeeded (); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/inventory/IInventoryAdapter.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/inventory/IInventoryAdapter.java new file mode 100644 index 00000000..71f55e3d --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/inventory/IInventoryAdapter.java @@ -0,0 +1,16 @@ +package com.jaquadro.minecraft.storagedrawers.api.inventory; + +import net.minecraft.item.ItemStack; + +public interface IInventoryAdapter +{ + ItemStack getInventoryStack (SlotType slotType); + + void setInStack (ItemStack stack); + + void setOutStack (ItemStack stack); + + void syncInventory (); + + boolean syncInventoryIfNeeded (); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/inventory/SlotType.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/inventory/SlotType.java new file mode 100644 index 00000000..dbb002ed --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/inventory/SlotType.java @@ -0,0 +1,15 @@ +package com.jaquadro.minecraft.storagedrawers.api.inventory; + +/** + * Classifies different IInventory slots according to how they should be used. + */ +public enum SlotType +{ + /** An inventory slot for input-only operations; stack sizes artificially held low. */ + INPUT, + + /** An inventory slot for output-only operations; stack sizes artificially held high. */ + OUTPUT; + + public static final SlotType[] values = values(); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/BlockConfiguration.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/BlockConfiguration.java new file mode 100644 index 00000000..502e46f3 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/BlockConfiguration.java @@ -0,0 +1,49 @@ +package com.jaquadro.minecraft.storagedrawers.api.pack; + +public enum BlockConfiguration +{ + BasicFull1(BlockType.Drawers, 1, false), + BasicFull2(BlockType.Drawers, 2, false), + BasicFull4(BlockType.Drawers, 4, false), + BasicHalf2(BlockType.Drawers, 2, true), + BasicHalf4(BlockType.Drawers, 4, true), + + SortingFull1(BlockType.DrawersSorting, 1, false), + SortingFull2(BlockType.DrawersSorting, 2, false), + SortingFull4(BlockType.DrawersSorting, 4, false), + SortingHalf2(BlockType.DrawersSorting, 2, true), + SortingHalf4(BlockType.DrawersSorting, 4, true), + + Trim(BlockType.Trim, 0, false); + + private final BlockType type; + private final int drawerCount; + private final boolean halfDepth; + + BlockConfiguration (BlockType type, int count, boolean half) { + this.type = type; + this.drawerCount = count; + this.halfDepth = half; + } + + public BlockType getBlockType () { + return type; + } + + public int getDrawerCount () { + return drawerCount; + } + + public boolean isHalfDepth () { + return halfDepth; + } + + public static BlockConfiguration by (BlockType type, int drawerCount, boolean halfDepth) { + for (BlockConfiguration config : values()) { + if (config.type == type && config.drawerCount == drawerCount && config.halfDepth == halfDepth) + return config; + } + + return null; + } +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/BlockType.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/BlockType.java new file mode 100644 index 00000000..9487f6cd --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/BlockType.java @@ -0,0 +1,8 @@ +package com.jaquadro.minecraft.storagedrawers.api.pack; + +public enum BlockType +{ + Drawers, + DrawersSorting, + Trim +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/IPackBlockFactory.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/IPackBlockFactory.java new file mode 100644 index 00000000..d6ffe1f5 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/IPackBlockFactory.java @@ -0,0 +1,11 @@ +package com.jaquadro.minecraft.storagedrawers.api.pack; + +import net.minecraft.block.Block; +import net.minecraft.item.ItemBlock; + +public interface IPackBlockFactory +{ + Block createBlock (BlockConfiguration blockConfig, IPackDataResolver dataResolver); + + void registerBlock (Block block, String name); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/IPackDataResolver.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/IPackDataResolver.java new file mode 100644 index 00000000..bc9627bc --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/IPackDataResolver.java @@ -0,0 +1,18 @@ +package com.jaquadro.minecraft.storagedrawers.api.pack; + +import net.minecraft.creativetab.CreativeTabs; + +public interface IPackDataResolver +{ + String getPackModID (); + + String getBlockName (BlockConfiguration blockConfig); + + CreativeTabs getCreativeTabs (BlockType type); + + boolean isValidMetaValue (int meta); + + String getUnlocalizedName (int meta); + + String getTexturePath (TextureType type, int meta); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/StandardDataResolver.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/StandardDataResolver.java new file mode 100644 index 00000000..212f2471 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/StandardDataResolver.java @@ -0,0 +1,106 @@ +package com.jaquadro.minecraft.storagedrawers.api.pack; + +import net.minecraft.creativetab.CreativeTabs; + +public class StandardDataResolver implements IPackDataResolver +{ + private String modID; + private String[] unlocalizedNames; + private CreativeTabs creativeTab; + + public StandardDataResolver (String modID, String[] unlocalizedNames) { + this.modID = modID; + this.unlocalizedNames = unlocalizedNames; + } + + public StandardDataResolver (String modID, String[] unlocalizedNames, CreativeTabs creativeTab) { + this(modID, unlocalizedNames); + this.creativeTab = creativeTab; + } + + @Override + public String getPackModID () { + return modID; + } + + protected String makeBlockName (String name) { + return getPackModID().toLowerCase() + "." + name; + } + + @Override + public String getBlockName (BlockConfiguration blockConfig) { + switch (blockConfig.getBlockType()) { + case Drawers: + case DrawersSorting: + if (blockConfig.getDrawerCount() == 1) + return makeBlockName("fullDrawers1"); + if (blockConfig.getDrawerCount() == 2 && !blockConfig.isHalfDepth()) + return makeBlockName("fullDrawers2"); + if (blockConfig.getDrawerCount() == 4 && !blockConfig.isHalfDepth()) + return makeBlockName("fullDrawers4"); + if (blockConfig.getDrawerCount() == 2 && blockConfig.isHalfDepth()) + return makeBlockName("halfDrawers2"); + if (blockConfig.getDrawerCount() == 4 && blockConfig.isHalfDepth()) + return makeBlockName("halfDrawers4"); + break; + case Trim: + return makeBlockName("trim"); + } + return null; + } + + @Override + public CreativeTabs getCreativeTabs (BlockType type) { + return creativeTab; + } + + @Override + public boolean isValidMetaValue (int meta) { + if (meta < 0 || meta >= unlocalizedNames.length) + return false; + + return unlocalizedNames != null && unlocalizedNames[meta] != null; + } + + @Override + public String getUnlocalizedName (int meta) { + if (!isValidMetaValue(meta)) + return null; + + return unlocalizedNames[meta]; + } + + protected String getBaseTexturePath () { + return getPackModID() + ":"; + } + + protected String getTextureMetaName (int meta) { + return getUnlocalizedName(meta); + } + + @Override + public String getTexturePath (TextureType type, int meta) { + switch (type) { + case Front1: + return getBaseTexturePath() + "drawers_" + getTextureMetaName(meta) + "_front_1"; + case Front2: + return getBaseTexturePath() + "drawers_" + getTextureMetaName(meta) + "_front_2"; + case Front4: + return getBaseTexturePath() + "drawers_" + getTextureMetaName(meta) + "_front_4"; + case Side: + return getBaseTexturePath() + "drawers_" + getTextureMetaName(meta) + "_side"; + case SideSort: + return getBaseTexturePath() + "drawers_" + getTextureMetaName(meta) + "_sort"; + case SideVSplit: + return getBaseTexturePath() + "drawers_" + getTextureMetaName(meta) + "_side_v"; + case SideHSplit: + return getBaseTexturePath() + "drawers_" + getTextureMetaName(meta) + "_side_h"; + case TrimBorder: + return getBaseTexturePath() + "drawers_" + getTextureMetaName(meta) + "_trim"; + case TrimBlock: + return getBaseTexturePath() + "drawers_" + getTextureMetaName(meta) + "_side"; + default: + return ""; + } + } +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/TextureType.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/TextureType.java new file mode 100644 index 00000000..e1c9eb08 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/pack/TextureType.java @@ -0,0 +1,14 @@ +package com.jaquadro.minecraft.storagedrawers.api.pack; + +public enum TextureType +{ + Front1, + Front2, + Front4, + Side, + SideSort, + SideVSplit, + SideHSplit, + TrimBorder, + TrimBlock +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IIngredientHandler.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IIngredientHandler.java new file mode 100644 index 00000000..a7432b2b --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IIngredientHandler.java @@ -0,0 +1,19 @@ +package com.jaquadro.minecraft.storagedrawers.api.registry; + +import net.minecraft.item.ItemStack; + +/** + * Ingredient handlers are used to get ItemStacks from ingredients in custom IRecipe implementations. If you have + * registered an IRecipeHandler that returns lists of objects that aren't ItemStacks, then you will need to + * implement an ingredient handler for those objects. + */ +public interface IIngredientHandler +{ + /** + * Gets an ItemStack from an object that represents an ingredient in an IRecipe. + * + * @param object An ingredient object. + * @return An ItemStack for the given ingredient. + */ + ItemStack getItemStack (Object object); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IRecipeHandler.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IRecipeHandler.java new file mode 100644 index 00000000..d4311ea8 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IRecipeHandler.java @@ -0,0 +1,36 @@ +package com.jaquadro.minecraft.storagedrawers.api.registry; + +import net.minecraft.item.crafting.IRecipe; + +import java.util.List; + +/** + * Recipe handlers are used by compacting drawers to find more-compressed forms of blocks and items. If your recipe + * to craft compressed items is a custom IRecipe implementation, you will need to register a handler for it. + */ +public interface IRecipeHandler +{ + /** + * Get the recipe ingredient list as an array of objects (usually used for shaped recipes). + * If your array does not contain ItemStack objects, you will need to register an {@link IIngredientHandler} to + * get an ItemStack from them. + * + * If you would prefer to return a List, return null in this method and implement {@link #getInputAsList}. + * + * @param recipe An instance of a custom {@link IRecipe}. + * @return An array of ItemStacks or objects with a registered {@link IIngredientHandler}. + */ + Object[] getInputAsArray (IRecipe recipe); + + /** + * Get the recipe ingredient list as a list of objects (usually used for shapeless recipes). + * If your list does not contain ItemStack objects, you will need to register an {@link IIngredientHandler} to + * get an ItemStack from them. + * + * If you would prefer to return an array, return null in this method and implement {@link #getInputAsArray}. + * + * @param recipe An instance of a custom {@link IRecipe}. + * @return A list of ItemStacks or objects with a registered {@link IIngredientHandler}. + */ + List getInputAsList (IRecipe recipe); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IRecipeHandlerRegistry.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IRecipeHandlerRegistry.java new file mode 100644 index 00000000..b83d5347 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IRecipeHandlerRegistry.java @@ -0,0 +1,12 @@ +package com.jaquadro.minecraft.storagedrawers.api.registry; + +public interface IRecipeHandlerRegistry +{ + void registerRecipeHandler (Class clazz, IRecipeHandler handler); + + void registerIngredientHandler (Class clazz, IIngredientHandler handler); + + IRecipeHandler getRecipeHandler (Class clazz); + + IIngredientHandler getIngredientHandler (Class clazz); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IRenderRegistry.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IRenderRegistry.java new file mode 100644 index 00000000..cfa4543a --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IRenderRegistry.java @@ -0,0 +1,8 @@ +package com.jaquadro.minecraft.storagedrawers.api.registry; + +import com.jaquadro.minecraft.storagedrawers.api.render.IRenderLabel; + +public interface IRenderRegistry +{ + void registerPreLabelRenderHandler (IRenderLabel renderHandler); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IWailaRegistry.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IWailaRegistry.java new file mode 100644 index 00000000..a4ee9874 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IWailaRegistry.java @@ -0,0 +1,6 @@ +package com.jaquadro.minecraft.storagedrawers.api.registry; + +public interface IWailaRegistry +{ + void registerTooltipHandler (IWailaTooltipHandler handler); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IWailaTooltipHandler.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IWailaTooltipHandler.java new file mode 100644 index 00000000..d0f3328c --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/registry/IWailaTooltipHandler.java @@ -0,0 +1,8 @@ +package com.jaquadro.minecraft.storagedrawers.api.registry; + +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; + +public interface IWailaTooltipHandler +{ + String transformItemName (IDrawer drawer, String defaultName); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/render/IRenderLabel.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/render/IRenderLabel.java new file mode 100644 index 00000000..500030aa --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/render/IRenderLabel.java @@ -0,0 +1,9 @@ +package com.jaquadro.minecraft.storagedrawers.api.render; + +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; +import net.minecraft.tileentity.TileEntity; + +public interface IRenderLabel +{ + void render (TileEntity tileEntity, IDrawerGroup drawerGroup, int slot, float brightness, float partialTickTime); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java new file mode 100644 index 00000000..c88556c0 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java @@ -0,0 +1,109 @@ +package com.jaquadro.minecraft.storagedrawers.api.storage; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + +public interface IDrawer +{ + /** + * Gets an ItemStack of size 1 representing the type, metadata, and tags of the stored items. + * The returned ItemStack should not be modified for any reason. Make a copy if you need to store or modify it. + */ + ItemStack getStoredItemPrototype (); + + /** + * Gets an ItemStack initialized to the number of items stored in this drawer. + * The returned ItemStack is guaranteed to be a new copy and can be used for any purpose. Does not affect drawer contents. + */ + ItemStack getStoredItemCopy (); + + /** + * Sets the type of the stored item and initializes it to the given amount. Any existing item will be replaced. + * + * @param itemPrototype An ItemStack representing the type, metadata, and tags of the item to store. + * @param amount The amount to initialize the stored item count to. + */ + void setStoredItem (ItemStack itemPrototype, int amount); + + /** + * Gets the number of items stored in this drawer. + */ + int getStoredItemCount (); + + /** + * Sets the number of items stored in this drawer. Triggers syncing of inventories and client data. + * Setting a drawer's count to 0 may also result in the item type being cleared, depending in implementation. + * + * @param amount The new amount of items stored in this drawer. + */ + void setStoredItemCount (int amount); + + /** + * Gets the maximum number of items that can be stored in this drawer. + * This value will vary depending on the max stack size of the stored item type. + */ + int getMaxCapacity (); + + /** + * Gets the maximum number of items that could be stored in this drawer if it held the given item. + * + * @param itemPrototype The item type to query. + */ + int getMaxCapacity (ItemStack itemPrototype); + + /** + * Gets the number of items that could still be added to this drawer before it is full. + */ + int getRemainingCapacity (); + + /** + * Gets the max stack size of the item type stored in this drawer. Convenience method. + */ + int getStoredItemStackSize (); + + /** + * Gets whether or not an item of the given type and data can be stored in this drawer. + * + * Stack size and available capacity are not considered. For drawers that are not empty, this + * method can allow ore-dictionary compatible items to be accepted into the drawer, as defined by what + * the drawer considers to be an equivalent item. + * + * @param itemPrototype An ItemStack representing the type, metadata, and tags of an item. + */ + boolean canItemBeStored (ItemStack itemPrototype); + + /** + * Gets whether or not an item of the given type and data can be extracted from this drawer. + * + * This is intended to allow outbound ore-dictionary conversions of compatible items, as defined by what + * the drawer considers to be an equivalent item. + * + * @param itemPrototype An ItemStack representing the type, metadata, and tags of an item. + */ + boolean canItemBeExtracted (ItemStack itemPrototype); + + /** + * Gets whether or not the drawer has items. + * A drawer set with an item type and 0 count is not considered empty. + */ + boolean isEmpty (); + + /** + * Gets auxiliary data that has been associated with this drawer. + * + * @param key The key used to identify the data. + * @return An opaque object that was previously stored. + */ + Object getExtendedData (String key); + + /** + * Stores auxiliary data with this drawer, mainly for use in integration. + * @param key The key to identify the data with. + * @param data The data to store. + */ + void setExtendedData (String key, Object data); + + void writeToNBT (NBTTagCompound tag); + + void readFromNBT (NBTTagCompound tag); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java new file mode 100644 index 00000000..1f1eefef --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java @@ -0,0 +1,25 @@ +package com.jaquadro.minecraft.storagedrawers.api.storage; + +import com.jaquadro.minecraft.storagedrawers.api.inventory.IDrawerInventory; + +public interface IDrawerGroup +{ + /** + * Gets the number of drawers contained within this group. + */ + int getDrawerCount (); + + /** + * Gets the drawer at the given slot within this group. + */ + IDrawer getDrawer (int slot); + + /** + * Gets whether the drawer in the given slot is usable. + */ + boolean isDrawerEnabled (int slot); + + IDrawerInventory getDrawerInventory (); + + boolean markDirtyIfNeeded (); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroupInteractive.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroupInteractive.java new file mode 100644 index 00000000..bd8a0ea7 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroupInteractive.java @@ -0,0 +1,15 @@ +package com.jaquadro.minecraft.storagedrawers.api.storage; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; + +public interface IDrawerGroupInteractive extends IDrawerGroup +{ + ItemStack takeItemsFromSlot (int slot, int count); + + int putItemsIntoSlot (int slot, ItemStack stack, int count); + + int interactPutCurrentItemIntoSlot (int slot, EntityPlayer player); + + int interactPutCurrentInventoryIntoSlot (int slot, EntityPlayer player); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/IFractionalDrawer.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/IFractionalDrawer.java new file mode 100644 index 00000000..bd87a808 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/IFractionalDrawer.java @@ -0,0 +1,28 @@ +package com.jaquadro.minecraft.storagedrawers.api.storage; + +/** + * Represents a drawer with items that are a fractional component of another item within the drawer group. Compacting + * Drawers are a primary example of this drawer type. + */ +public interface IFractionalDrawer extends IDrawer +{ + /** + * Gets the storage ratio between the held item and the most compressed item within the drawer group. + * + * For example, most ingots have a conversion rate of 9 compared to metal blocks, and nuggets a rate of 81. + * Actual conversion rates are implementation-defined. + */ + int getConversionRate (); + + /** + * Gets the number of items left in the drawer if the maximum number of equivalent compressed items had been removed. + * The equivalency is determined by the next compression tier, and not necessarily the conversion rate. + */ + int getStoredItemRemainder (); + + /** + * Gets whether or not the stored item represents the smallest granularity of material that can be stored within + * the drawer group. + */ + boolean isSmallestUnit (); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/INetworked.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/INetworked.java new file mode 100644 index 00000000..fba20d40 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/INetworked.java @@ -0,0 +1,5 @@ +package com.jaquadro.minecraft.storagedrawers.api.storage; + +public interface INetworked +{ +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/ILockable.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/ILockable.java new file mode 100644 index 00000000..7645c83a --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/ILockable.java @@ -0,0 +1,21 @@ +package com.jaquadro.minecraft.storagedrawers.api.storage.attribute; + +public interface ILockable +{ + /** + * Gets whether or not a drawer or group is locked for the given lock attribute. + */ + boolean isLocked (LockAttribute attr); + + /** + * Gets whether or not the lock state can be changed for the given lock attribute. + * If this method returns false, isLocked may still return true. + */ + boolean canLock (LockAttribute attr); + + /** + * Sets the lock state of a drawer or group for the given lock attribute. + * If canLock returns false, this is a no-op. + */ + void setLocked (LockAttribute attr, boolean isLocked); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IShroudable.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IShroudable.java new file mode 100644 index 00000000..b7f0bf03 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IShroudable.java @@ -0,0 +1,16 @@ +package com.jaquadro.minecraft.storagedrawers.api.storage.attribute; + +public interface IShroudable +{ + /** + * Gets whether or not the drawer has the shrouded attribute. + * The shrouded attribute instructs the drawer to not render its item label. + */ + boolean isShrouded (); + + /** + * Sets whether or not the drawer is currently shrouded. + * @return false if the operation is not supported, true otherwise. + */ + boolean setIsShrouded (boolean state); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IVoidable.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IVoidable.java new file mode 100644 index 00000000..9fc58063 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IVoidable.java @@ -0,0 +1,9 @@ +package com.jaquadro.minecraft.storagedrawers.api.storage.attribute; + +public interface IVoidable +{ + /** + * Gets whether or not the drawer has a voiding attribute. + */ + boolean isVoid (); +} diff --git a/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/LockAttribute.java b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/LockAttribute.java new file mode 100644 index 00000000..cb726b41 --- /dev/null +++ b/src/api/java/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/LockAttribute.java @@ -0,0 +1,37 @@ +package com.jaquadro.minecraft.storagedrawers.api.storage.attribute; + +import java.util.EnumSet; + +public enum LockAttribute +{ + LOCK_POPULATED, + LOCK_EMPTY; + + public int getFlagValue () { + return 1 << ordinal(); + } + + public static int getBitfield (EnumSet attributes) { + int value = 0; + if (attributes == null) + return value; + + for (LockAttribute attr : attributes) + value |= attr.getFlagValue(); + + return value; + } + + public static EnumSet getEnumSet (int bitfield) { + if (bitfield == 0) + return null; + + EnumSet set = EnumSet.noneOf(LockAttribute.class); + for (LockAttribute attr : values()) { + if ((bitfield & attr.getFlagValue()) != 0) + set.add(attr); + } + + return set; + } +} diff --git a/src/main/java/crazypants/enderio/machine/invpanel/server/AbstractInventory.java b/src/main/java/crazypants/enderio/machine/invpanel/server/AbstractInventory.java index f38d03c1..e279f14b 100644 --- a/src/main/java/crazypants/enderio/machine/invpanel/server/AbstractInventory.java +++ b/src/main/java/crazypants/enderio/machine/invpanel/server/AbstractInventory.java @@ -22,24 +22,40 @@ abstract class AbstractInventory { } protected void updateSlot(InventoryDatabaseServer db, int slot, ItemStack stack) { + if (stack == null) { + emptySlot(db, slot); + } else { + updateSlot(db, slot, stack, stack.stackSize); + } + } + + protected void updateSlot(InventoryDatabaseServer db, int slot, ItemStack stack, int count) { SlotKey slotKey = slotKeys[slot]; ItemEntry current = slotKey != null ? slotKey.item : null; ItemEntry key = db.lookupItem(stack, current, true); if (key != current) { - updateSlotKey(db, slot, slotKey, key, stack); - } else if (slotKey != null && slotKey.count != stack.stackSize) { - slotKey.count = stack.stackSize; + updateSlotKey(db, slot, slotKey, key, count); + } else if (slotKey != null && slotKey.count != count) { + slotKey.count = count; db.entryChanged(current); } } - private void updateSlotKey(InventoryDatabaseServer db, int slot, SlotKey slotKey, ItemEntry key, ItemStack stack) { + protected void emptySlot(InventoryDatabaseServer db, int slot) { + SlotKey slotKey = slotKeys[slot]; + if (slotKey != null) { + slotKey.remove(db); + slotKeys[slot] = null; + } + } + + private void updateSlotKey(InventoryDatabaseServer db, int slot, SlotKey slotKey, ItemEntry key, int count) { if (slotKey != null) { slotKey.remove(db); slotKey = null; } if (key != null) { - slotKey = new SlotKey(this, slot, key, stack.stackSize); + slotKey = new SlotKey(this, slot, key, count); key.addSlot(slotKey); db.entryChanged(key); } diff --git a/src/main/java/crazypants/enderio/machine/invpanel/server/DrawerGroupInventory.java b/src/main/java/crazypants/enderio/machine/invpanel/server/DrawerGroupInventory.java new file mode 100644 index 00000000..be93d634 --- /dev/null +++ b/src/main/java/crazypants/enderio/machine/invpanel/server/DrawerGroupInventory.java @@ -0,0 +1,66 @@ +package crazypants.enderio.machine.invpanel.server; + +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; +import net.minecraft.item.ItemStack; + +public class DrawerGroupInventory extends AbstractInventory { + final IDrawerGroup dg; + + public DrawerGroupInventory(IDrawerGroup dg) { + this.dg = dg; + } + + @Override + int scanInventory(InventoryDatabaseServer db) { + int count = dg.getDrawerCount(); + if (count == 0) { + setEmpty(db); + return 0; + } + if (count != slotKeys.length) { + reset(db, count); + } + for (int i = 0; i < count; i++) { + IDrawer drawer = dg.getDrawer(i); + ItemStack stack; + int quantity; + if (drawer != null && + (quantity = drawer.getStoredItemCount()) > 0 && + (stack = drawer.getStoredItemPrototype()) != null && + drawer.canItemBeExtracted(stack)) { + updateSlot(db, i, stack, quantity); + } else { + emptySlot(db, i); + } + } + return count; + } + + @Override + int extractItem(InventoryDatabaseServer db, ItemEntry entry, int slot, int count) { + if (slot >= dg.getDrawerCount()) { + return 0; + } + IDrawer drawer = dg.getDrawer(slot); + if (drawer == null) { + return 0; + } + int remaining = drawer.getStoredItemCount(); + if (remaining <= 0) { + return 0; + } + ItemStack stack = drawer.getStoredItemPrototype(); + if (db.lookupItem(stack, entry, false) != entry) { + return 0; + } + if (count > remaining) { + count = remaining; + } + remaining -= count; + drawer.setStoredItemCount(remaining); + updateCount(db, slot, entry, remaining); + return count; + } + +} diff --git a/src/main/java/crazypants/enderio/machine/invpanel/server/InventoryDatabaseServer.java b/src/main/java/crazypants/enderio/machine/invpanel/server/InventoryDatabaseServer.java index a55e0d22..84b798ea 100644 --- a/src/main/java/crazypants/enderio/machine/invpanel/server/InventoryDatabaseServer.java +++ b/src/main/java/crazypants/enderio/machine/invpanel/server/InventoryDatabaseServer.java @@ -18,7 +18,6 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.NBTTagCompound; -import powercrystals.minefactoryreloaded.api.IDeepStorageUnit; public class InventoryDatabaseServer extends InventoryDatabase { @@ -179,11 +178,7 @@ public class InventoryDatabaseServer extends InventoryDatabase { this.inventories = new AbstractInventory[sources.size()]; for(int i=0; i factories; + + static { + factories = new ArrayList(); + factories.add(new DSUFactory()); + if (Loader.isModLoaded("StorageDrawers")) { + factories.add(new DrawerFactory()); + } + } + + static AbstractInventory createInventory(NetworkedInventory ni) { + for(InventoryFactory f : factories) { + AbstractInventory ai = f.create(ni); + if (ai != null) { + return ai; + } + } + return new NormalInventory(ni); + } + + abstract AbstractInventory create(NetworkedInventory ni); + + static class DSUFactory extends InventoryFactory { + @Override + AbstractInventory create(NetworkedInventory ni) { + ISidedInventory inv = ni.getInventory(); + if (inv instanceof IDeepStorageUnit) { + return new DSUInventory((IDeepStorageUnit) inv); + } + return null; + } + } + + static class DrawerFactory extends InventoryFactory { + @Override + AbstractInventory create(NetworkedInventory ni) { + ISidedInventory inv = ni.getInventory(); + if (inv instanceof IDrawerGroup) { + return new DrawerGroupInventory((IDrawerGroup) inv); + } + return null; + } + } +}