From 08db9c3c6a7146dda2c7de17b329c8e50d0c496e Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Thu, 22 Jun 2017 00:54:43 -0400 Subject: [PATCH 01/22] Bunch of revisions working towards attributes on a capability. --- .../api/storage/EmptyDrawerAttributes.java | 6 + .../api/storage/IDrawerAttributes.java | 52 ++++ .../storage/IDrawerAttributesModifiable.java | 43 +++ .../storagedrawers/block/BlockDrawers.java | 8 +- .../block/tile/TileEntityController.java | 121 ++++---- .../block/tile/TileEntityDrawers.java | 272 ++++++++++++++---- .../block/tile/TileEntityDrawersComp.java | 6 +- .../block/tile/TileEntityDrawersStandard.java | 6 +- .../capabilities/BasicDrawerAttributes.java | 108 +++++++ .../CapabilityDrawerAttributes.java | 64 +++++ .../inventory/DrawerItemHandler.java | 20 +- .../storagedrawers/item/ItemDrawerKey.java | 41 +-- .../storagedrawers/item/ItemKey.java | 73 +++++ .../storagedrawers/item/ItemQuantifyKey.java | 40 +-- .../storagedrawers/item/ItemShroudKey.java | 40 +-- .../storage/CompDrawerData.java | 24 +- .../storage/DefaultStorageProvider.java | 4 +- .../storagedrawers/storage/DrawerData.java | 65 ++--- .../storage/ICentralInventory.java | 12 +- .../storage/IStorageProvider.java | 12 +- 20 files changed, 718 insertions(+), 299 deletions(-) create mode 100644 src/com/jaquadro/minecraft/storagedrawers/api/storage/EmptyDrawerAttributes.java create mode 100644 src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerAttributes.java create mode 100644 src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerAttributesModifiable.java create mode 100644 src/com/jaquadro/minecraft/storagedrawers/core/capabilities/BasicDrawerAttributes.java create mode 100644 src/com/jaquadro/minecraft/storagedrawers/core/capabilities/CapabilityDrawerAttributes.java create mode 100644 src/com/jaquadro/minecraft/storagedrawers/item/ItemKey.java diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/EmptyDrawerAttributes.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/EmptyDrawerAttributes.java new file mode 100644 index 000000000..96b09d097 --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/EmptyDrawerAttributes.java @@ -0,0 +1,6 @@ +package com.jaquadro.minecraft.storagedrawers.api.storage; + +public class EmptyDrawerAttributes implements IDrawerAttributes +{ + public EmptyDrawerAttributes () { } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerAttributes.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerAttributes.java new file mode 100644 index 000000000..6ce00f71b --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerAttributes.java @@ -0,0 +1,52 @@ +package com.jaquadro.minecraft.storagedrawers.api.storage; + +import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; + +public interface IDrawerAttributes +{ + /** + * Gets whether or not the lock state can be changed for the given lock attribute. + * If this method returns false, isItemLocked may still return true. + */ + default boolean canItemLock (LockAttribute attr) { + return false; + } + + /** + * Gets whether or not a drawer or group is locked for the given lock attribute. + */ + default boolean isItemLocked (LockAttribute attr) { + return false; + } + + /** + * Gets whether or not the drawer has the concealed attribute. + * The shrouded attribute instructs the drawer to not render its item label. + */ + default boolean isConcealed () { + return false; + } + + /** + * Gets whether or not the drawer has the sealed attribute. + * A sealed drawer cannot be interacted with, and when broken will retain all of its items and upgrades. + */ + default boolean isSealed () { + return false; + } + + /** + * Gets whether or not the drawer has the quantified attribute. + * The quantified attribute instructs the drawer to render its numerical quantity. + */ + default boolean isShowingQuantity () { + return false; + } + + /** + * Gets whether or not the drawer has a voiding attribute. + */ + default boolean isVoid () { + return false; + } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerAttributesModifiable.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerAttributesModifiable.java new file mode 100644 index 000000000..918388624 --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerAttributesModifiable.java @@ -0,0 +1,43 @@ +package com.jaquadro.minecraft.storagedrawers.api.storage; + +import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; + +public interface IDrawerAttributesModifiable extends IDrawerAttributes +{ + /** + * Sets whether or not the drawer is currently concealed. + * @return false if the operation is not supported, true otherwise. + */ + default boolean setIsConcealed (boolean state) { + return false; + } + + /** + * Sets the lock state of a drawer or group for the given lock attribute. + * If canItemLock returns false, this is a no-op. + * @return false if the operation is not supported, true otherwise. + */ + default boolean setItemLocked (LockAttribute attr, boolean isLocked) { + return false; + } + + /** + * Sets whether or not the drawer is currently quantified. + * @return false if the operation is not supported, true otherwise. + */ + default boolean setIsShowingQuantity (boolean state) { + return false; + } + + /** + * Sets whether or not the drawer is currently sealed. + * @return false if the operation is not supported, true otherwise. + */ + default boolean setIsSealed (boolean state) { + return false; + } + + default boolean setIsVoid (boolean state) { + return false; + } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java index 872d3a88d..360d7dd6c 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java @@ -7,7 +7,6 @@ import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; import com.jaquadro.minecraft.storagedrawers.api.storage.INetworked; import com.jaquadro.minecraft.storagedrawers.block.dynamic.StatusModelData; -import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; import com.jaquadro.minecraft.storagedrawers.block.modeldata.DrawerStateModelData; import com.jaquadro.minecraft.storagedrawers.block.tile.TileEntityDrawers; import com.jaquadro.minecraft.storagedrawers.config.ConfigManager; @@ -237,6 +236,9 @@ public boolean onBlockActivated (World world, BlockPos pos, IBlockState state, E } if (!item.isEmpty()) { + if (item.getItem() instanceof ItemKey) + return false; + if (item.getItem() instanceof ItemTrim && player.isSneaking()) { if (!retrimBlock(world, pos, item)) return false; @@ -277,7 +279,7 @@ else if (item.getItem() instanceof ItemUpgrade) { return true; } - else if (item.getItem() == ModItems.drawerKey) { + /*else if (item.getItem() == ModItems.drawerKey) { boolean locked = tileDrawers.isItemLocked(LockAttribute.LOCK_POPULATED); tileDrawers.setItemLocked(LockAttribute.LOCK_POPULATED, !locked); tileDrawers.setItemLocked(LockAttribute.LOCK_EMPTY, !locked); @@ -291,7 +293,7 @@ else if (item.getItem() == ModItems.shroudKey) { else if (item.getItem() == ModItems.quantifyKey) { tileDrawers.setIsShowingQuantity(!tileDrawers.isShowingQuantity()); return true; - } + }*/ else if (item.getItem() instanceof ItemPersonalKey) { String securityKey = ((ItemPersonalKey) item.getItem()).getSecurityProviderKey(item.getItemDamage()); ISecurityProvider provider = StorageDrawers.securityRegistry.getProvider(securityKey); diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java index 298c37475..5a11cb1db 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java @@ -3,7 +3,9 @@ import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.api.security.ISecurityProvider; import com.jaquadro.minecraft.storagedrawers.api.storage.*; -import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*; + +import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IProtectable; +import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; import com.jaquadro.minecraft.storagedrawers.block.BlockSlave; import com.jaquadro.minecraft.storagedrawers.inventory.DrawerItemHandler; import com.jaquadro.minecraft.storagedrawers.security.SecurityManager; @@ -21,6 +23,8 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.CapabilityInject; +import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.fml.common.FMLLog; import net.minecraftforge.items.CapabilityItemHandler; import org.apache.logging.log4j.Level; @@ -30,6 +34,11 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IPriorityGroup, ISmartGroup { + @CapabilityInject(IDrawerAttributes.class) + public static Capability DRAWER_ATTRIBUTES_CAPABILITY = null; + + private static final IDrawerAttributes EMPTY_ATTRIBUTES = new EmptyDrawerAttributes(); + private static final int PRI_LOCKED = 0; private static final int PRI_LOCKED_VOID = 1; private static final int PRI_NORMAL = 2; @@ -95,6 +104,17 @@ public int compare (SlotRecord o1, SlotRecord o2) { } }; + private IDrawerAttributes getAttributes (Object obj) { + IDrawerAttributes attrs = null; + if (obj instanceof ICapabilityProvider) + attrs = ((ICapabilityProvider) obj).getCapability(DRAWER_ATTRIBUTES_CAPABILITY, null); + + if (attrs == null) + attrs = EMPTY_ATTRIBUTES; + + return attrs; + } + private int getSlotPriority (SlotRecord record) { IDrawerGroup group = getGroupForSlotRecord(record); if (group == null) { @@ -107,28 +127,23 @@ private int getSlotPriority (SlotRecord record) { return PRI_DISABLED; } + IDrawerAttributes attrs = getAttributes(group); + if (drawer.isEmpty()) { - if ((drawer instanceof IItemLockable && ((IItemLockable) drawer).isItemLocked(LockAttribute.LOCK_EMPTY)) || - (group instanceof IItemLockable && ((IItemLockable) group).isItemLocked(LockAttribute.LOCK_EMPTY))) { + if (attrs.isItemLocked(LockAttribute.LOCK_EMPTY)) return PRI_LOCKED_EMPTY; - } else return PRI_EMPTY; } - if ((drawer instanceof IVoidable && ((IVoidable) drawer).isVoid()) || - (group instanceof IVoidable && ((IVoidable) group).isVoid())) { - if ((drawer instanceof IItemLockable && ((IItemLockable) drawer).isItemLocked(LockAttribute.LOCK_POPULATED)) || - (group instanceof IItemLockable && ((IItemLockable) group).isItemLocked(LockAttribute.LOCK_POPULATED))) { + if (attrs.isVoid()) { + if (attrs.isItemLocked(LockAttribute.LOCK_POPULATED)) return PRI_LOCKED_VOID; - } return PRI_VOID; } - if ((drawer instanceof IItemLockable && ((IItemLockable) drawer).isItemLocked(LockAttribute.LOCK_POPULATED)) || - (group instanceof IItemLockable && ((IItemLockable) group).isItemLocked(LockAttribute.LOCK_POPULATED))) { + if (attrs.isItemLocked(LockAttribute.LOCK_POPULATED)) return PRI_LOCKED; - } return PRI_NORMAL; } @@ -209,7 +224,8 @@ protected int insertItems (@Nonnull ItemStack stack, GameProfile profile) { itemsLeft = insertItemsIntoDrawer(drawer, itemsLeft); - if (drawer instanceof IVoidable && ((IVoidable) drawer).isVoid()) + IDrawerAttributes attrs = getAttributes(group); + if (attrs.isVoid()) itemsLeft = 0; if (itemsLeft == 0) break; @@ -285,19 +301,16 @@ public void toggleShroud (GameProfile profile) { continue; } - for (int i = 0, n = record.storage.getDrawerCount(); i < n; i++) { - IDrawer drawer = record.storage.getDrawerIfEnabled(i); - if (!(drawer instanceof IShroudable)) - continue; - - IShroudable shroudableStorage = (IShroudable)drawer; - if (template == null) { - template = shroudableStorage.isShrouded(); - state = !template; - } + IDrawerAttributes attrs = getAttributes(record.storage); + if (!(attrs instanceof IDrawerAttributesModifiable)) + continue; - shroudableStorage.setIsShrouded(state); + IDrawerAttributesModifiable mattrs = (IDrawerAttributesModifiable)attrs; + if (template == null) { + template = mattrs.isConcealed(); + state = !template; } + mattrs.setIsConcealed(state); } } @@ -314,19 +327,16 @@ public void toggleQuantified (GameProfile profile) { continue; } - for (int i = 0, n = record.storage.getDrawerCount(); i < n; i++) { - IDrawer drawer = record.storage.getDrawerIfEnabled(i); - if (!(drawer instanceof IQuantifiable)) - continue; - - IQuantifiable quantifiableStorage = (IQuantifiable)drawer; - if (template == null) { - template = quantifiableStorage.isShowingQuantity(); - state = !template; - } + IDrawerAttributes attrs = getAttributes(record.storage); + if (!(attrs instanceof IDrawerAttributesModifiable)) + continue; - quantifiableStorage.setIsShowingQuantity(state); + IDrawerAttributesModifiable mattrs = (IDrawerAttributesModifiable)attrs; + if (template == null) { + template = mattrs.isShowingQuantity(); + state = !template; } + mattrs.setIsShowingQuantity(state); } } @@ -343,32 +353,18 @@ public void toggleLock (EnumSet attributes, LockAttribute key, Ga continue; } - if (record.storage instanceof IItemLockable) { - IItemLockable lockableStorage = (IItemLockable)record.storage; - if (template == null) { - template = lockableStorage.isItemLocked(key); - state = !template; - } + IDrawerAttributes attrs = getAttributes(record.storage); + if (!(attrs instanceof IDrawerAttributesModifiable)) + continue; - for (LockAttribute attr : attributes) - lockableStorage.setItemLocked(attr, state); + IDrawerAttributesModifiable mattrs = (IDrawerAttributesModifiable)attrs; + if (template == null) { + template = mattrs.isItemLocked(key); + state = !template; } - else { - for (int i = 0, n = record.storage.getDrawerCount(); i < n; i++) { - IDrawer drawer = record.storage.getDrawerIfEnabled(i); - if (!(drawer instanceof IShroudable)) - continue; - - IItemLockable lockableStorage = (IItemLockable)drawer; - if (template == null) { - template = lockableStorage.isItemLocked(key); - state = !template; - } - for (LockAttribute attr : attributes) - lockableStorage.setItemLocked(attr, state); - } - } + for (LockAttribute attr : attributes) + mattrs.setItemLocked(attr, state); } } @@ -788,8 +784,8 @@ private void advance () { IDrawer drawer = candidateGroup.getDrawer(candidate.slot); if (insert) { - boolean voiding = (drawer instanceof IVoidable) ? ((IVoidable) drawer).isVoid() : false; - if (!(drawer.canItemBeStored(stack) && (drawer.isEmpty() || drawer.getRemainingCapacity() > 0 || voiding))) + IDrawerAttributes attrs = getAttributes(candidateGroup); + if (!(drawer.canItemBeStored(stack) && (drawer.isEmpty() || drawer.getRemainingCapacity() > 0 || attrs.isVoid()))) continue; } else { @@ -818,8 +814,9 @@ private void advance () { } if (insert) { - boolean voiding = (drawer instanceof IVoidable) ? ((IVoidable) drawer).isVoid() : false; - if (!(drawer.canItemBeStored(stack) && (drawer.isEmpty() || drawer.getRemainingCapacity() > 0 || voiding))) + IDrawerGroup group = getGroupForDrawerSlot(slot); + IDrawerAttributes attrs = getAttributes(group); + if (!(drawer.canItemBeStored(stack) && (drawer.isEmpty() || drawer.getRemainingCapacity() > 0 || attrs.isVoid()))) continue; } else { diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index 185f7dd2d..5a7f458ef 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -6,12 +6,16 @@ import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.api.security.ISecurityProvider; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributesModifiable; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroupInteractive; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*; import com.jaquadro.minecraft.storagedrawers.block.BlockDrawersCustom; import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.ControllerData; import com.jaquadro.minecraft.storagedrawers.config.ConfigManager; import com.jaquadro.minecraft.storagedrawers.core.ModItems; +import com.jaquadro.minecraft.storagedrawers.core.capabilities.BasicDrawerAttributes; +import com.jaquadro.minecraft.storagedrawers.core.capabilities.CapabilityDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.inventory.DrawerItemHandler; import com.jaquadro.minecraft.storagedrawers.item.EnumUpgradeStatus; import com.jaquadro.minecraft.storagedrawers.item.EnumUpgradeStorage; @@ -48,14 +52,14 @@ public abstract class TileEntityDrawers extends ChamLockableTileEntity implement private int direction; private String material; private int drawerCapacity = 1; - private boolean shrouded = false; - private boolean quantified = false; + //private boolean shrouded = false; + //private boolean quantified = false; private boolean taped = false; private boolean hideUpgrade = false; private UUID owner; private String securityKey; - private EnumSet lockAttributes = null; + //private EnumSet lockAttributes = null; private ItemStack[] upgrades = new ItemStack[5]; @@ -69,7 +73,22 @@ public abstract class TileEntityDrawers extends ChamLockableTileEntity implement @Nonnull private ItemStack materialTrim; + private class DrawerAttributes extends BasicDrawerAttributes + { + @Override + protected void onAttributeChanged () { + attributeChanged(); + + if (getWorld() != null && !getWorld().isRemote) { + markDirty(); + markBlockForUpdate(); + } + } + } + protected TileEntityDrawers (int drawerCount) { + drawerAttributes = new DrawerAttributes(); + for (int i = 0; i < upgrades.length; i++) upgrades[i] = ItemStack.EMPTY; @@ -288,10 +307,10 @@ protected void attributeChanged () { @Override public boolean isItemLocked (LockAttribute attr) { - if (!StorageDrawers.config.cache.enableLockUpgrades || lockAttributes == null) + if (!StorageDrawers.config.cache.enableLockUpgrades) return false; - return lockAttributes.contains(attr); + return drawerAttributes.isItemLocked(attr); } @Override @@ -307,75 +326,32 @@ public void setItemLocked (LockAttribute attr, boolean isLocked) { if (!StorageDrawers.config.cache.enableLockUpgrades) return; - if (isLocked && (lockAttributes == null || !lockAttributes.contains(attr))) { - if (lockAttributes == null) - lockAttributes = EnumSet.of(attr); - else - lockAttributes.add(attr); - - attributeChanged(); - - if (getWorld() != null && !getWorld().isRemote) { - markDirty(); - markBlockForUpdate(); - } - } - else if (!isLocked && lockAttributes != null && lockAttributes.contains(attr)) { - lockAttributes.remove(attr); - - attributeChanged(); - - if (getWorld() != null && !getWorld().isRemote) { - markDirty(); - markBlockForUpdate(); - } - } + drawerAttributes.setItemLocked(attr, isLocked); } public boolean isShrouded () { if (!StorageDrawers.config.cache.enableShroudUpgrades) return false; - return shrouded; + return drawerAttributes.isConcealed(); } public void setIsShrouded (boolean shrouded) { - if (this.shrouded != shrouded) { - this.shrouded = shrouded; - - attributeChanged(); - - if (getWorld() != null && !getWorld().isRemote) { - markDirty(); - markBlockForUpdate(); - } - } + drawerAttributes.setIsConcealed(shrouded); } public boolean isShowingQuantity () { if (!StorageDrawers.config.cache.enableQuantifiableUpgrades) return false; - return quantified; + return drawerAttributes.isShowingQuantity(); } public boolean setIsShowingQuantity (boolean quantified) { if (!StorageDrawers.config.cache.enableQuantifiableUpgrades) return false; - if (this.quantified != quantified) { - this.quantified = quantified; - - attributeChanged(); - - if (getWorld() != null && !getWorld().isRemote) { - markDirty(); - - IBlockState state = getWorld().getBlockState(getPos()); - getWorld().notifyBlockUpdate(getPos(), state, state, 3); - } - } - + drawerAttributes.setIsShowingQuantity(quantified); return true; } @@ -781,6 +757,185 @@ protected NBTTagCompound writeToFixedNBT (NBTTagCompound tag) { return tag; } + @Override + public void readFromPortableNBT (NBTTagCompound tag) { + super.readFromPortableNBT(tag); + + upgrades = new ItemStack[upgrades.length]; + for (int i = 0; i < upgrades.length; i++) + upgrades[i] = ItemStack.EMPTY; + + material = null; + if (tag.hasKey("Mat")) + material = tag.getString("Mat"); + + drawerCapacity = tag.getInteger("Cap"); + + NBTTagList upgradeList = tag.getTagList("Upgrades", Constants.NBT.TAG_COMPOUND); + for (int i = 0; i < upgradeList.tagCount(); i++) { + NBTTagCompound upgradeTag = upgradeList.getCompoundTagAt(i); + + int slot = upgradeTag.getByte("Slot"); + setUpgrade(slot, new ItemStack(upgradeTag)); + } + + drawerAttributes.setIsVoid(isVoid()); + + drawerAttributes.setItemLocked(LockAttribute.LOCK_EMPTY, false); + drawerAttributes.setItemLocked(LockAttribute.LOCK_POPULATED, false); + if (tag.hasKey("Lock")) { + EnumSet attrs = LockAttribute.getEnumSet(tag.getByte("Lock")); + if (attrs != null) { + drawerAttributes.setItemLocked(LockAttribute.LOCK_EMPTY, attrs.contains(LockAttribute.LOCK_EMPTY)); + drawerAttributes.setItemLocked(LockAttribute.LOCK_POPULATED, attrs.contains(LockAttribute.LOCK_POPULATED)); + } + } + + drawerAttributes.setIsConcealed(false); + if (tag.hasKey("Shr")) + drawerAttributes.setIsConcealed(tag.getBoolean("Shr")); + + drawerAttributes.setIsShowingQuantity(false); + if (tag.hasKey("Qua")) { + drawerAttributes.setIsShowingQuantity(tag.getBoolean("Qua")); + } + + owner = null; + if (tag.hasKey("Own")) + owner = UUID.fromString(tag.getString("Own")); + + securityKey = null; + if (tag.hasKey("Sec")) + securityKey = tag.getString("Sec"); + + hideUpgrade = false; + if (tag.hasKey("HideUp")) + hideUpgrade = tag.getBoolean("HideUp"); + + NBTTagList slots = tag.getTagList("Slots", Constants.NBT.TAG_COMPOUND); + + drawers = new IDrawer[slots.tagCount()]; + for (int i = 0, n = drawers.length; i < n; i++) { + NBTTagCompound slot = slots.getCompoundTagAt(i); + drawers[i] = createDrawer(i); + drawers[i].readFromNBT(slot); + } + + materialSide = ItemStack.EMPTY; + if (tag.hasKey("MatS")) + materialSide = new ItemStack(tag.getCompoundTag("MatS")); + + materialFront = ItemStack.EMPTY; + if (tag.hasKey("MatF")) + materialFront = new ItemStack(tag.getCompoundTag("MatF")); + + materialTrim = ItemStack.EMPTY; + if (tag.hasKey("MatT")) + materialTrim = new ItemStack(tag.getCompoundTag("MatT")); + + attributeChanged(); + } + + @Override + public NBTTagCompound writeToPortableNBT (NBTTagCompound tag) { + tag = super.writeToPortableNBT(tag); + + tag.setInteger("Cap", getDrawerCapacity()); + + if (material != null) + tag.setString("Mat", material); + + NBTTagList upgradeList = new NBTTagList(); + for (int i = 0; i < upgrades.length; i++) { + if (!upgrades[i].isEmpty()) { + NBTTagCompound upgradeTag = upgrades[i].writeToNBT(new NBTTagCompound()); + upgradeTag.setByte("Slot", (byte)i); + + upgradeList.appendTag(upgradeTag); + } + } + + if (upgradeList.tagCount() > 0) + tag.setTag("Upgrades", upgradeList); + + EnumSet attrs = EnumSet.noneOf(LockAttribute.class); + if (drawerAttributes.isItemLocked(LockAttribute.LOCK_EMPTY)) + attrs.add(LockAttribute.LOCK_EMPTY); + if (drawerAttributes.isItemLocked(LockAttribute.LOCK_POPULATED)) + attrs.add(LockAttribute.LOCK_POPULATED); + + if (!attrs.isEmpty()) { + tag.setByte("Lock", (byte)LockAttribute.getBitfield(attrs)); + } + + if (drawerAttributes.isConcealed()) + tag.setBoolean("Shr", true); + + if (drawerAttributes.isShowingQuantity()) + tag.setBoolean("Qua", true); + + if (owner != null) + tag.setString("Own", owner.toString()); + + if (securityKey != null) + tag.setString("Sec", securityKey); + + if (hideUpgrade) + tag.setBoolean("HideUp", true); + + NBTTagList slots = new NBTTagList(); + for (IDrawer drawer : drawers) { + NBTTagCompound slot = new NBTTagCompound(); + drawer.writeToNBT(slot); + slots.appendTag(slot); + } + + tag.setTag("Slots", slots); + + if (!materialSide.isEmpty()) { + NBTTagCompound itag = new NBTTagCompound(); + materialSide.writeToNBT(itag); + tag.setTag("MatS", itag); + } + + if (!materialFront.isEmpty()) { + NBTTagCompound itag = new NBTTagCompound(); + materialFront.writeToNBT(itag); + tag.setTag("MatF", itag); + } + + if (!materialTrim.isEmpty()) { + NBTTagCompound itag = new NBTTagCompound(); + materialTrim.writeToNBT(itag); + tag.setTag("MatT", itag); + } + + return tag; + } + + /*@Override + protected void readFromFixedNBT (NBTTagCompound tag) { + super.readFromFixedNBT(tag); + + setDirection(tag.getByte("Dir")); + + taped = false; + if (tag.hasKey("Tape")) + taped = tag.getBoolean("Tape"); + } + + @Override + protected NBTTagCompound writeToFixedNBT (NBTTagCompound tag) { + tag = super.writeToFixedNBT(tag); + + tag.setByte("Dir", (byte) direction); + + if (taped) + tag.setBoolean("Tape", true); + + return tag; + } + @Override public void readFromPortableNBT (NBTTagCompound tag) { super.readFromPortableNBT(tag); @@ -919,7 +1074,7 @@ public NBTTagCompound writeToPortableNBT (NBTTagCompound tag) { } return tag; - } + }*/ @Override public void markDirty () { @@ -1006,18 +1161,25 @@ protected IItemHandler createUnSidedHandler () { return new DrawerItemHandler(this); } + private IDrawerAttributesModifiable drawerAttributes; + @SuppressWarnings("unchecked") @Override public T getCapability(net.minecraftforge.common.capabilities.Capability capability, net.minecraft.util.EnumFacing facing) { if (capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return (T) (itemHandler == null ? (itemHandler = createUnSidedHandler()) : itemHandler); + if (capability == CapabilityDrawerAttributes.DRAWER_ATTRIBUTES_CAPABILITY) + return (T) drawerAttributes; + return super.getCapability(capability, facing); } @Override public boolean hasCapability(net.minecraftforge.common.capabilities.Capability capability, net.minecraft.util.EnumFacing facing) { - return capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing); + return capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY + || capability == CapabilityDrawerAttributes.DRAWER_ATTRIBUTES_CAPABILITY + || super.hasCapability(capability, facing); } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java index f4bad89ec..d2853c9c3 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java @@ -73,7 +73,7 @@ public int getStoredItemRemainder (int slot) { @Override protected IDrawer createDrawer (int slot) { - return new CompDrawerData(getCentralInventory(), slot); + return new CompDrawerData(getCentralInventory(), this, slot); } @Override @@ -642,7 +642,7 @@ public boolean isSmallestUnit (int slot) { return convRate[slot] == 1; } - @Override + /*@Override public boolean isVoidSlot (int slot) { return isVoid(); } @@ -671,7 +671,7 @@ public boolean setIsSlotShowingQuantity (int slot, boolean state) { @Override public boolean isLocked (int slot, LockAttribute attr) { return TileEntityDrawersComp.this.isItemLocked(attr); - } + }*/ @Override public void writeToNBT (int slot, NBTTagCompound tag) { diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java index d08839325..cd7800b00 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java @@ -46,7 +46,7 @@ protected IStorageProvider getStorageProvider () { @Override protected IDrawer createDrawer (int slot) { - return new DrawerData(getStorageProvider(), slot); + return new DrawerData(getStorageProvider(), this, slot); } @Override @@ -121,7 +121,7 @@ public int getSlotStackCapacity (int slot) { return getEffectiveStorageMultiplier() * getEffectiveDrawerCapacity(); } - @Override + /*@Override public boolean isLocked (int slot, LockAttribute attr) { return TileEntityDrawersStandard.this.isItemLocked(attr); } @@ -150,7 +150,7 @@ public boolean isShowingQuantity (int slot) { @Override public boolean setIsShowingQuantity (int slot, boolean state) { return TileEntityDrawersStandard.this.setIsShowingQuantity(state); - } + }*/ @Override public boolean isStorageUnlimited (int slot) { diff --git a/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/BasicDrawerAttributes.java b/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/BasicDrawerAttributes.java new file mode 100644 index 000000000..4fa26bc56 --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/BasicDrawerAttributes.java @@ -0,0 +1,108 @@ +package com.jaquadro.minecraft.storagedrawers.core.capabilities; + +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributesModifiable; +import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.common.util.INBTSerializable; + +import java.util.EnumSet; + +public class BasicDrawerAttributes implements IDrawerAttributes, IDrawerAttributesModifiable, INBTSerializable +{ + private EnumSet itemLock = EnumSet.noneOf(LockAttribute.class); + private boolean isConcealed; + private boolean isShowingQuantity; + private boolean isVoid; + + @Override + public boolean canItemLock (LockAttribute attr) { + return true; + } + + @Override + public boolean isItemLocked (LockAttribute attr) { + return itemLock.contains(attr); + } + + @Override + public boolean setItemLocked (LockAttribute attr, boolean isLocked) { + if (isItemLocked(attr) != isLocked) { + if (isLocked) + itemLock.add(attr); + else + itemLock.remove(attr); + + onAttributeChanged(); + } + + return true; + } + + @Override + public boolean isConcealed () { + return isConcealed; + } + + @Override + public boolean setIsConcealed (boolean state) { + if (isConcealed != state) { + isConcealed = state; + onAttributeChanged(); + } + + return true; + } + + @Override + public boolean isVoid () { + return isVoid; + } + + @Override + public boolean setIsVoid (boolean state) { + if (isVoid != state) { + isVoid = state; + onAttributeChanged(); + } + + return true; + } + + @Override + public boolean isShowingQuantity () { + return isShowingQuantity; + } + + @Override + public boolean setIsShowingQuantity (boolean state) { + if (isShowingQuantity != state) { + isShowingQuantity = state; + onAttributeChanged(); + } + + return true; + } + + @Override + public NBTTagCompound serializeNBT () { + NBTTagCompound tag = new NBTTagCompound(); + + tag.setInteger("itemLock", LockAttribute.getBitfield(itemLock)); + tag.setBoolean("concealed", isConcealed); + tag.setBoolean("void", isVoid); + tag.setBoolean("quant", isShowingQuantity); + + return tag; + } + + @Override + public void deserializeNBT (NBTTagCompound nbt) { + itemLock = LockAttribute.getEnumSet(nbt.getInteger("itemLock")); + isConcealed = nbt.getBoolean("concealed"); + isVoid = nbt.getBoolean("void"); + isShowingQuantity = nbt.getBoolean("quant"); + } + + protected void onAttributeChanged () { } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/CapabilityDrawerAttributes.java b/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/CapabilityDrawerAttributes.java new file mode 100644 index 000000000..de3406da7 --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/CapabilityDrawerAttributes.java @@ -0,0 +1,64 @@ +package com.jaquadro.minecraft.storagedrawers.core.capabilities; + +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributesModifiable; +import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.EnumFacing; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.CapabilityInject; +import net.minecraftforge.common.capabilities.CapabilityManager; +import net.minecraftforge.common.util.INBTSerializable; + +import javax.annotation.Nullable; + +public class CapabilityDrawerAttributes +{ + @CapabilityInject(IDrawerAttributes.class) + public static Capability DRAWER_ATTRIBUTES_CAPABILITY = null; + + public static void register () { + CapabilityManager.INSTANCE.register(IDrawerAttributes.class, new DefaultStorage(), BasicDrawerAttributes::new); + } + + private static class DefaultStorage implements Capability.IStorage + { + @Nullable + @Override + public NBTBase writeNBT (Capability capability, IDrawerAttributes instance, EnumFacing side) { + if (instance instanceof INBTSerializable) + return ((INBTSerializable) instance).serializeNBT(); + + NBTTagCompound tag = new NBTTagCompound(); + tag.setBoolean("lockedPop", instance.isItemLocked(LockAttribute.LOCK_POPULATED)); + tag.setBoolean("lockedEmpty", instance.isItemLocked(LockAttribute.LOCK_EMPTY)); + tag.setBoolean("concealed", instance.isConcealed()); + tag.setBoolean("void", instance.isVoid()); + tag.setBoolean("quant", instance.isShowingQuantity()); + + return tag; + } + + @Override + public void readNBT (Capability capability, IDrawerAttributes instance, EnumFacing side, NBTBase nbt) { + if (instance instanceof INBTSerializable) { + @SuppressWarnings("unchecked") + INBTSerializable serializer = (INBTSerializable)instance; + serializer.deserializeNBT(nbt); + return; + } + + if (!(instance instanceof IDrawerAttributesModifiable)) + throw new RuntimeException("IDrawerAttributes instance does not implement IDrawerAttributesModifiable"); + IDrawerAttributesModifiable modifiable = (IDrawerAttributesModifiable) instance; + + NBTTagCompound tag = (NBTTagCompound)nbt; + modifiable.setItemLocked(LockAttribute.LOCK_POPULATED, tag.getBoolean("lockedPop")); + modifiable.setItemLocked(LockAttribute.LOCK_EMPTY, tag.getBoolean("lockedEmpty")); + modifiable.setIsConcealed(tag.getBoolean("concealed")); + modifiable.setIsVoid(tag.getBoolean("void")); + modifiable.setIsShowingQuantity(tag.getBoolean("quant")); + } + } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java index 099501a52..54d8769ef 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java @@ -1,23 +1,30 @@ package com.jaquadro.minecraft.storagedrawers.inventory; import com.jaquadro.minecraft.storagedrawers.StorageDrawers; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; -import com.jaquadro.minecraft.storagedrawers.api.storage.IPriorityGroup; -import com.jaquadro.minecraft.storagedrawers.api.storage.ISmartGroup; +import com.jaquadro.minecraft.storagedrawers.api.storage.*; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IVoidable; import net.minecraft.init.Items; import net.minecraft.item.ItemStack; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.CapabilityInject; +import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.items.IItemHandler; import javax.annotation.Nonnull; public class DrawerItemHandler implements IItemHandler { + @CapabilityInject(IDrawerAttributes.class) + public static Capability DRAWER_ATTRIBUTES_CAPABILITY = null; + private IDrawerGroup group; + private ICapabilityProvider capProvider; public DrawerItemHandler (IDrawerGroup group) { this.group = group; + + if (group instanceof ICapabilityProvider) + capProvider = (ICapabilityProvider)group; } @Override @@ -118,7 +125,10 @@ private ItemStack insertItemInternal (int slot, @Nonnull ItemStack stack, boolea return stack; int availableCount = drawer.isEmpty() ? drawer.getMaxCapacity(stack) : drawer.getRemainingCapacity(); - if (drawer instanceof IVoidable && ((IVoidable) drawer).isVoid()) + + // TODO: May not need void check here with updated storage implementation + IDrawerAttributes attrs = capProvider.getCapability(DRAWER_ATTRIBUTES_CAPABILITY, null); + if (attrs != null && attrs.isVoid()) availableCount = Integer.MAX_VALUE; int stackSize = stack.getCount(); diff --git a/src/com/jaquadro/minecraft/storagedrawers/item/ItemDrawerKey.java b/src/com/jaquadro/minecraft/storagedrawers/item/ItemDrawerKey.java index bf05b3290..16c259ef9 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/item/ItemDrawerKey.java +++ b/src/com/jaquadro/minecraft/storagedrawers/item/ItemDrawerKey.java @@ -1,23 +1,11 @@ package com.jaquadro.minecraft.storagedrawers.item; -import com.google.common.collect.Multimap; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributesModifiable; +import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; import com.jaquadro.minecraft.storagedrawers.core.ModCreativeTabs; -import net.minecraft.client.resources.I18n; -import net.minecraft.client.util.ITooltipFlag; -import net.minecraft.entity.SharedMonsterAttributes; -import net.minecraft.entity.ai.attributes.AttributeModifier; -import net.minecraft.inventory.EntityEquipmentSlot; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.world.World; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.List; -public class ItemDrawerKey extends Item +public class ItemDrawerKey extends ItemKey { public ItemDrawerKey (String registryName, String unlocalizedName) { setRegistryName(registryName); @@ -27,24 +15,9 @@ public ItemDrawerKey (String registryName, String unlocalizedName) { } @Override - @SideOnly(Side.CLIENT) - public void addInformation (@Nonnull ItemStack itemStack, @Nullable World world, List list, ITooltipFlag advanced) { - String name = getUnlocalizedName(itemStack); - list.add(I18n.format(name + ".description")); - } - - @SideOnly(Side.CLIENT) - public boolean isFull3D () { - return true; - } - - @Override - public Multimap getAttributeModifiers (EntityEquipmentSlot slot, @Nonnull ItemStack stack) { - Multimap multimap = super.getAttributeModifiers(slot, stack); - - if (slot == EntityEquipmentSlot.MAINHAND) - multimap.put(SharedMonsterAttributes.ATTACK_DAMAGE.getName(), new AttributeModifier(ATTACK_DAMAGE_MODIFIER, "Weapon modifier", (double)2, 0)); - - return multimap; + protected void handleDrawerAttributes (IDrawerAttributesModifiable attrs) { + boolean locked = attrs.isItemLocked(LockAttribute.LOCK_POPULATED); + attrs.setItemLocked(LockAttribute.LOCK_EMPTY, !locked); + attrs.setItemLocked(LockAttribute.LOCK_POPULATED, !locked); } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/item/ItemKey.java b/src/com/jaquadro/minecraft/storagedrawers/item/ItemKey.java new file mode 100644 index 000000000..28044694b --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/item/ItemKey.java @@ -0,0 +1,73 @@ +package com.jaquadro.minecraft.storagedrawers.item; + +import com.google.common.collect.Multimap; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributesModifiable; +import net.minecraft.client.resources.I18n; +import net.minecraft.client.util.ITooltipFlag; +import net.minecraft.entity.SharedMonsterAttributes; +import net.minecraft.entity.ai.attributes.AttributeModifier; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.EntityEquipmentSlot; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.EnumActionResult; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.EnumHand; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.CapabilityInject; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.List; + +public class ItemKey extends Item +{ + @CapabilityInject(IDrawerAttributes.class) + public static Capability DRAWER_ATTRIBUTES_CAPABILITY = null; + + @Override + @SideOnly(Side.CLIENT) + public void addInformation (@Nonnull ItemStack itemStack, @Nullable World world, List list, ITooltipFlag advanced) { + String name = getUnlocalizedName(itemStack); + list.add(I18n.format(name + ".description")); + } + + @SideOnly(Side.CLIENT) + public boolean isFull3D () { + return true; + } + + @Override + @Nonnull + public Multimap getAttributeModifiers (EntityEquipmentSlot slot, @Nonnull ItemStack stack) { + Multimap multimap = super.getAttributeModifiers(slot, stack); + + if (slot == EntityEquipmentSlot.MAINHAND) + multimap.put(SharedMonsterAttributes.ATTACK_DAMAGE.getName(), new AttributeModifier(ATTACK_DAMAGE_MODIFIER, "Weapon modifier", (double)2, 0)); + + return multimap; + } + + @Override + public EnumActionResult onItemUse (EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { + TileEntity tile = world.getTileEntity(pos); + if (tile == null) + return EnumActionResult.PASS; + + IDrawerAttributes attrs = tile.getCapability(DRAWER_ATTRIBUTES_CAPABILITY, null); + if (!(attrs instanceof IDrawerAttributesModifiable)) + return EnumActionResult.PASS; + + handleDrawerAttributes((IDrawerAttributesModifiable)attrs); + + return EnumActionResult.SUCCESS; + } + + protected void handleDrawerAttributes (IDrawerAttributesModifiable attrs) { } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/item/ItemQuantifyKey.java b/src/com/jaquadro/minecraft/storagedrawers/item/ItemQuantifyKey.java index 2331d2399..bfa1f787e 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/item/ItemQuantifyKey.java +++ b/src/com/jaquadro/minecraft/storagedrawers/item/ItemQuantifyKey.java @@ -1,23 +1,9 @@ package com.jaquadro.minecraft.storagedrawers.item; -import com.google.common.collect.Multimap; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributesModifiable; import com.jaquadro.minecraft.storagedrawers.core.ModCreativeTabs; -import net.minecraft.client.resources.I18n; -import net.minecraft.client.util.ITooltipFlag; -import net.minecraft.entity.SharedMonsterAttributes; -import net.minecraft.entity.ai.attributes.AttributeModifier; -import net.minecraft.inventory.EntityEquipmentSlot; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.world.World; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.List; - -public class ItemQuantifyKey extends Item +public class ItemQuantifyKey extends ItemKey { public ItemQuantifyKey (String registryName, String unlocalizedName) { setRegistryName(registryName); @@ -27,25 +13,7 @@ public ItemQuantifyKey (String registryName, String unlocalizedName) { } @Override - @SideOnly(Side.CLIENT) - public void addInformation (@Nonnull ItemStack itemStack, @Nullable World world, List list, ITooltipFlag advanced) { - String name = getUnlocalizedName(itemStack); - list.add(I18n.format(name + ".description")); - } - - @SideOnly(Side.CLIENT) - public boolean isFull3D() - { - return true; - } - - @Override - public Multimap getAttributeModifiers (EntityEquipmentSlot slot, ItemStack stack) { - Multimap multimap = super.getAttributeModifiers(slot, stack); - - if (slot == EntityEquipmentSlot.MAINHAND) - multimap.put(SharedMonsterAttributes.ATTACK_DAMAGE.getName(), new AttributeModifier(ATTACK_DAMAGE_MODIFIER, "Weapon modifier", (double)2, 0)); - - return multimap; + protected void handleDrawerAttributes (IDrawerAttributesModifiable attrs) { + attrs.setIsShowingQuantity(!attrs.isShowingQuantity()); } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/item/ItemShroudKey.java b/src/com/jaquadro/minecraft/storagedrawers/item/ItemShroudKey.java index 89b8265c6..852c39fde 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/item/ItemShroudKey.java +++ b/src/com/jaquadro/minecraft/storagedrawers/item/ItemShroudKey.java @@ -1,23 +1,9 @@ package com.jaquadro.minecraft.storagedrawers.item; -import com.google.common.collect.Multimap; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributesModifiable; import com.jaquadro.minecraft.storagedrawers.core.ModCreativeTabs; -import net.minecraft.client.resources.I18n; -import net.minecraft.client.util.ITooltipFlag; -import net.minecraft.entity.SharedMonsterAttributes; -import net.minecraft.entity.ai.attributes.AttributeModifier; -import net.minecraft.inventory.EntityEquipmentSlot; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.world.World; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.List; - -public class ItemShroudKey extends Item +public class ItemShroudKey extends ItemKey { public ItemShroudKey (String registryName, String unlocalizedName) { setRegistryName(registryName); @@ -27,25 +13,7 @@ public ItemShroudKey (String registryName, String unlocalizedName) { } @Override - @SideOnly(Side.CLIENT) - public void addInformation (@Nonnull ItemStack itemStack, @Nullable World world, List list, ITooltipFlag advanced) { - String name = getUnlocalizedName(itemStack); - list.add(I18n.format(name + ".description")); - } - - @SideOnly(Side.CLIENT) - public boolean isFull3D() - { - return true; - } - - @Override - public Multimap getAttributeModifiers (EntityEquipmentSlot slot, @Nonnull ItemStack stack) { - Multimap multimap = super.getAttributeModifiers(slot, stack); - - if (slot == EntityEquipmentSlot.MAINHAND) - multimap.put(SharedMonsterAttributes.ATTACK_DAMAGE.getName(), new AttributeModifier(ATTACK_DAMAGE_MODIFIER, "Weapon modifier", (double)2, 0)); - - return multimap; + protected void handleDrawerAttributes (IDrawerAttributesModifiable attrs) { + attrs.setIsConcealed(!attrs.isConcealed()); } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/CompDrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/CompDrawerData.java index 2f7c29244..e2bfe0174 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/CompDrawerData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/CompDrawerData.java @@ -1,21 +1,35 @@ package com.jaquadro.minecraft.storagedrawers.storage; +import com.jaquadro.minecraft.storagedrawers.api.storage.EmptyDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.api.storage.IFractionalDrawer; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.CapabilityInject; +import net.minecraftforge.common.capabilities.ICapabilityProvider; import javax.annotation.Nonnull; -public class CompDrawerData extends BaseDrawerData implements IFractionalDrawer, IVoidable, IShroudable, IQuantifiable, IItemLockable +public class CompDrawerData extends BaseDrawerData implements IFractionalDrawer { + @CapabilityInject(IDrawerAttributes.class) + static Capability ATTR_CAPABILITY = null; + private ICentralInventory central; private int slot; - public CompDrawerData (ICentralInventory centralInventory, int slot) { + IDrawerAttributes attrs; + + public CompDrawerData (ICentralInventory centralInventory, ICapabilityProvider capProvider, int slot) { this.slot = slot; this.central = centralInventory; + + attrs = capProvider.getCapability(ATTR_CAPABILITY, null); + if (attrs == null) + attrs = new EmptyDrawerAttributes(); } @Override @@ -74,7 +88,7 @@ protected int getItemCapacityForInventoryStack () { @Override public boolean canItemBeStored (@Nonnull ItemStack itemPrototype) { - if (getStoredItemPrototype().isEmpty() && !isItemLocked(LockAttribute.LOCK_EMPTY)) + if (getStoredItemPrototype().isEmpty() && !attrs.isItemLocked(LockAttribute.LOCK_EMPTY)) return true; return areItemsEqual(itemPrototype); @@ -121,7 +135,7 @@ public void refresh () { refreshOreDictMatches(); } - @Override + /*@Override public boolean isVoid () { return central.isVoidSlot(slot); } @@ -157,5 +171,5 @@ public boolean canItemLock (LockAttribute attr) { } @Override - public void setItemLocked (LockAttribute attr, boolean isLocked) { } + public void setItemLocked (LockAttribute attr, boolean isLocked) { }*/ } diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/DefaultStorageProvider.java b/src/com/jaquadro/minecraft/storagedrawers/storage/DefaultStorageProvider.java index 637d4d78a..c0383ad96 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/DefaultStorageProvider.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/DefaultStorageProvider.java @@ -36,7 +36,7 @@ public int getSlotStackCapacity (int slot) { return 0; } - @Override + /*@Override public boolean isLocked ( int slot, LockAttribute attr) { return false; } @@ -64,7 +64,7 @@ public boolean isShowingQuantity (int slot) { @Override public boolean setIsShowingQuantity (int slot, boolean state) { return false; - } + }*/ @Override public boolean isStorageUnlimited (int slot) { diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java index 870d27f86..d1f8708e5 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java @@ -1,21 +1,32 @@ package com.jaquadro.minecraft.storagedrawers.storage; import com.jaquadro.minecraft.storagedrawers.api.event.DrawerPopulatedEvent; +import com.jaquadro.minecraft.storagedrawers.api.storage.EmptyDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*; import com.jaquadro.minecraft.storagedrawers.inventory.ItemStackHelper; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.CapabilityInject; +import net.minecraftforge.common.capabilities.ICapabilityProvider; import javax.annotation.Nonnull; -public class DrawerData extends BaseDrawerData implements IVoidable, IShroudable, IQuantifiable, IItemLockable +public class DrawerData extends BaseDrawerData { + @CapabilityInject(IDrawerAttributes.class) + static Capability ATTR_CAPABILITY = null; + private IStorageProvider storageProvider; + private ICapabilityProvider capProvider; private int slot; + IDrawerAttributes attrs; + @Nonnull private ItemStack protoStack; private int count; @@ -23,8 +34,14 @@ public class DrawerData extends BaseDrawerData implements IVoidable, IShroudable private boolean isUnlimited; private int stackCapacity; - public DrawerData (IStorageProvider provider, int slot) { + public DrawerData (IStorageProvider provider, ICapabilityProvider capProvider, int slot) { storageProvider = provider; + this.capProvider = capProvider; + + attrs = capProvider.getCapability(ATTR_CAPABILITY, null); + if (attrs == null) + attrs = new EmptyDrawerAttributes(); + protoStack = ItemStack.EMPTY; this.slot = slot; @@ -95,7 +112,7 @@ public void setStoredItemCount (int amount, boolean mark, boolean clearOnEmpty) if (amount == 0) { if (clearOnEmpty) { - if (!storageProvider.isLocked(slot, LockAttribute.LOCK_POPULATED)) + if (!attrs.isItemLocked(LockAttribute.LOCK_POPULATED)) reset(); if (mark) storageProvider.markDirty(slot); @@ -150,7 +167,7 @@ public int getStoredItemStackSize () { @Override protected int getItemCapacityForInventoryStack () { - if (storageProvider.isVoid(slot)) + if (attrs.isVoid()) return Integer.MAX_VALUE; else return getMaxCapacity(); @@ -158,7 +175,7 @@ protected int getItemCapacityForInventoryStack () { @Override public boolean canItemBeStored (@Nonnull ItemStack itemPrototype) { - if (protoStack.isEmpty() && !isItemLocked(LockAttribute.LOCK_EMPTY)) + if (protoStack.isEmpty() && !attrs.isItemLocked(LockAttribute.LOCK_EMPTY)) return true; return areItemsEqual(itemPrototype); @@ -226,43 +243,5 @@ protected void reset () { DrawerPopulatedEvent event = new DrawerPopulatedEvent(this); MinecraftForge.EVENT_BUS.post(event); } - - @Override - public boolean isVoid () { - return storageProvider.isVoid(slot); - } - - @Override - public boolean isShrouded () { - return storageProvider.isShrouded(slot); - } - - @Override - public boolean setIsShrouded (boolean state) { - return storageProvider.setIsShrouded(slot, state); - } - - @Override - public boolean isShowingQuantity () { - return storageProvider.isShowingQuantity(slot); - } - - @Override - public boolean setIsShowingQuantity (boolean state) { - return storageProvider.setIsShowingQuantity(slot, state); - } - - @Override - public boolean isItemLocked (LockAttribute attr) { - return storageProvider.isLocked(slot, attr); - } - - @Override - public boolean canItemLock (LockAttribute attr) { - return false; - } - - @Override - public void setItemLocked (LockAttribute attr, boolean isLocked) { } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/ICentralInventory.java b/src/com/jaquadro/minecraft/storagedrawers/storage/ICentralInventory.java index 911687735..92d431595 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/ICentralInventory.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/ICentralInventory.java @@ -36,17 +36,17 @@ public interface ICentralInventory boolean isSmallestUnit (int slot); - boolean isVoidSlot (int slot); + //boolean isVoidSlot (int slot); - boolean isShroudedSlot (int slot); + //boolean isShroudedSlot (int slot); - boolean setIsSlotShrouded (int slot, boolean state); + //boolean setIsSlotShrouded (int slot, boolean state); - boolean isSlotShowingQuantity (int slot); + //boolean isSlotShowingQuantity (int slot); - boolean setIsSlotShowingQuantity (int slot, boolean state); + //boolean setIsSlotShowingQuantity (int slot, boolean state); - boolean isLocked (int slot, LockAttribute attr); + //boolean isLocked (int slot, LockAttribute attr); void readFromNBT (int slot, NBTTagCompound tag); diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/IStorageProvider.java b/src/com/jaquadro/minecraft/storagedrawers/storage/IStorageProvider.java index 272664bab..a7f85e7e8 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/IStorageProvider.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/IStorageProvider.java @@ -12,17 +12,17 @@ public interface IStorageProvider int getSlotStackCapacity (int slot); - boolean isLocked (int slot, LockAttribute attr); + //boolean isLocked (int slot, LockAttribute attr); - boolean isVoid (int slot); + //boolean isVoid (int slot); - boolean isShrouded (int slot); + //boolean isShrouded (int slot); - boolean setIsShrouded (int slot, boolean state); + //boolean setIsShrouded (int slot, boolean state); - boolean isShowingQuantity (int slot); + //boolean isShowingQuantity (int slot); - boolean setIsShowingQuantity (int slot, boolean state); + //boolean setIsShowingQuantity (int slot, boolean state); boolean isStorageUnlimited (int slot); From e0896b8ad796915f926fa6afb1635fc84aea42fb Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sat, 24 Jun 2017 01:01:57 -0400 Subject: [PATCH 02/22] Restructure upgrades --- .../storagedrawers/block/BlockDrawers.java | 10 +- .../block/modeldata/DrawerStateModelData.java | 19 +- .../block/tile/TileEntityDrawers.java | 296 ++---------------- .../block/tile/TileEntityDrawersComp.java | 6 +- .../block/tile/TileEntityDrawersStandard.java | 3 +- .../block/tile/tiledata/UpgradeData.java | 231 ++++++++++++++ .../renderer/TileEntityDrawersRenderer.java | 21 +- .../storagedrawers/integration/Waila.java | 21 +- .../inventory/DrawerItemHandler.java | 1 - .../inventory/InventoryUpgrade.java | 14 +- .../storage/IUpgradeProvider.java | 4 - 11 files changed, 317 insertions(+), 309 deletions(-) create mode 100644 src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java index 360d7dd6c..2555fe227 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java @@ -259,10 +259,10 @@ else if (item.getItem() instanceof ItemUpgrade) { return false; } - if (!tileDrawers.canAddUpgrade(item)) + if (!tileDrawers.upgrades().canAddUpgrade(item)) return false; - if (!tileDrawers.addUpgrade(item)) { + if (!tileDrawers.upgrades().addUpgrade(item)) { if (!world.isRemote) player.sendStatusMessage(new TextComponentTranslation("storagedrawers.msg.maxUpgrades"), true); @@ -495,8 +495,8 @@ public void breakBlock (World world, BlockPos pos, IBlockState state) { TileEntityDrawers tile = getTileEntity(world, pos); if (tile != null && !tile.isSealed()) { - for (int i = 0; i < tile.getUpgradeSlotCount(); i++) { - ItemStack stack = tile.getUpgrade(i); + for (int i = 0; i < tile.upgrades().getSlotCount(); i++) { + ItemStack stack = tile.upgrades().getUpgrade(i); if (!stack.isEmpty()) { if (stack.getItem() instanceof ItemUpgradeCreative) continue; @@ -552,7 +552,7 @@ public float getExplosionResistance (World world, BlockPos pos, Entity exploder, TileEntityDrawers tile = getTileEntity(world, pos); if (tile != null) { for (int slot = 0; slot < 5; slot++) { - ItemStack stack = tile.getUpgrade(slot); + ItemStack stack = tile.upgrades().getUpgrade(slot); if (stack.isEmpty() || !(stack.getItem() instanceof ItemUpgradeStorage)) continue; diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/modeldata/DrawerStateModelData.java b/src/com/jaquadro/minecraft/storagedrawers/block/modeldata/DrawerStateModelData.java index 385cb1187..0357c4c39 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/modeldata/DrawerStateModelData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/modeldata/DrawerStateModelData.java @@ -2,14 +2,20 @@ import com.jaquadro.minecraft.chameleon.model.ModelData; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; import com.jaquadro.minecraft.storagedrawers.block.tile.TileEntityDrawers; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.CapabilityInject; import java.util.Arrays; import java.util.UUID; public final class DrawerStateModelData extends ModelData { + @CapabilityInject(IDrawerAttributes.class) + public static Capability DRAWER_ATTRIBUTES_CAPABILITY = null; + private final boolean shroudedFlag; private final boolean lockedFlag; private final boolean voidFlag; @@ -18,10 +24,15 @@ public final class DrawerStateModelData extends ModelData private final boolean[] emptyFlags; public DrawerStateModelData (TileEntityDrawers tile) { - if (tile != null) { - shroudedFlag = tile.isShrouded(); - lockedFlag = tile.isItemLocked(LockAttribute.LOCK_POPULATED); - voidFlag = tile.isVoid(); + IDrawerAttributes attr = null; + if (tile != null) + attr = tile.getCapability(DRAWER_ATTRIBUTES_CAPABILITY, null); + + if (tile != null && attr != null) { + shroudedFlag = attr.isConcealed(); + lockedFlag = attr.isItemLocked(LockAttribute.LOCK_POPULATED); + voidFlag = attr.isVoid(); + owner = tile.getOwner(); emptyFlags = new boolean[tile.getDrawerCount()]; diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index 5a7f458ef..b8a6836f5 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -12,15 +12,11 @@ import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*; import com.jaquadro.minecraft.storagedrawers.block.BlockDrawersCustom; import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.ControllerData; -import com.jaquadro.minecraft.storagedrawers.config.ConfigManager; -import com.jaquadro.minecraft.storagedrawers.core.ModItems; +import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.UpgradeData; import com.jaquadro.minecraft.storagedrawers.core.capabilities.BasicDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.core.capabilities.CapabilityDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.inventory.DrawerItemHandler; -import com.jaquadro.minecraft.storagedrawers.item.EnumUpgradeStatus; -import com.jaquadro.minecraft.storagedrawers.item.EnumUpgradeStorage; -import com.jaquadro.minecraft.storagedrawers.item.ItemUpgrade; -import com.jaquadro.minecraft.storagedrawers.storage.IUpgradeProvider; +import com.jaquadro.minecraft.storagedrawers.item.EnumUpgradeRedstone; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; @@ -28,7 +24,6 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.MathHelper; import net.minecraft.world.ILockableContainer; import net.minecraft.world.World; @@ -41,7 +36,7 @@ import java.util.EnumSet; import java.util.UUID; -public abstract class TileEntityDrawers extends ChamLockableTileEntity implements IDrawerGroupInteractive, IUpgradeProvider, IItemLockable, ISealable, IProtectable, IQuantifiable +public abstract class TileEntityDrawers extends ChamLockableTileEntity implements IDrawerGroupInteractive, ISealable, IProtectable { private LockableData lockData = new LockableData(); private CustomNameData customNameData = new CustomNameData("storagedrawers.container.drawers"); @@ -52,16 +47,12 @@ public abstract class TileEntityDrawers extends ChamLockableTileEntity implement private int direction; private String material; private int drawerCapacity = 1; - //private boolean shrouded = false; - //private boolean quantified = false; private boolean taped = false; private boolean hideUpgrade = false; private UUID owner; private String securityKey; - //private EnumSet lockAttributes = null; - - private ItemStack[] upgrades = new ItemStack[5]; + private UpgradeData upgradeData; private long lastClickTime; private UUID lastClickUUID; @@ -89,8 +80,8 @@ protected void onAttributeChanged () { protected TileEntityDrawers (int drawerCount) { drawerAttributes = new DrawerAttributes(); - for (int i = 0; i < upgrades.length; i++) - upgrades[i] = ItemStack.EMPTY; + upgradeData = new UpgradeData(7); + upgradeData.setDrawerAttributes(drawerAttributes); materialSide = ItemStack.EMPTY; materialFront = ItemStack.EMPTY; @@ -110,6 +101,14 @@ protected void initWithDrawerCount (int drawerCount) { drawers[i] = createDrawer(i); } + public IDrawerAttributes getDrawerAttributes () { + return drawerAttributes; + } + + public UpgradeData upgrades () { + return upgradeData; + } + public int getDirection () { return direction; } @@ -131,146 +130,15 @@ public void setMaterial (String material) { this.material = material; } - public int getMaxStorageLevel () { - int maxLevel = 1; - for (ItemStack upgrade : upgrades) { - if (upgrade != null && upgrade.getItem() == ModItems.upgradeStorage) - maxLevel = Math.max(maxLevel, upgrade.getItemDamage()); - } - - return maxLevel; - } - - public int getEffectiveStorageLevel () { - int level = 0; - for (ItemStack upgrade : upgrades) { - if (upgrade.getItem() == ModItems.upgradeStorage) - level += upgrade.getItemDamage(); - } - - return Math.max(level, 1); - } - - public int getEffectiveStorageMultiplier () { - ConfigManager config = StorageDrawers.config; - - int multiplier = 0; - for (ItemStack stack : upgrades) { - if (stack.getItem() == ModItems.upgradeStorage) { - int level = EnumUpgradeStorage.byMetadata(stack.getItemDamage()).getLevel(); - multiplier += config.getStorageUpgradeMultiplier(level); - } - } - - if (multiplier == 0) - multiplier = config.getStorageUpgradeMultiplier(1); - - return multiplier; - } - - public int getEffectiveStatusLevel () { - int maxLevel = -1; - for (ItemStack upgrade : upgrades) { - if (upgrade.getItem() == ModItems.upgradeStatus) - maxLevel = upgrade.getItemDamage(); - } - - if (maxLevel == -1) - return 0; - - return EnumUpgradeStatus.byMetadata(maxLevel).getLevel(); - } - - public int getUpgradeSlotCount () { - return 5; - } - - @Nonnull - public ItemStack getUpgrade (int slot) { - slot = MathHelper.clamp(slot, 0, 4); - return upgrades[slot]; - } - - public boolean addUpgrade (@Nonnull ItemStack upgrade) { - int slot = getNextUpgradeSlot(); - if (slot == -1) - return false; - - setUpgrade(slot, upgrade); - return true; - } - - public void setUpgrade (int slot, @Nonnull ItemStack upgrade) { - slot = MathHelper.clamp(slot, 0, 4); - - if (!upgrade.isEmpty()) { - upgrade = upgrade.copy(); - upgrade.setCount(1); - } - - upgrades[slot] = upgrade; - - if (getWorld() != null) { - if (!getWorld().isRemote) { - markDirty(); - markBlockForUpdate(); - } - getWorld().notifyNeighborsOfStateChange(getPos(), getBlockType(), false); - getWorld().notifyNeighborsOfStateChange(getPos().down(), getBlockType(), false); - } - - attributeChanged(); - } - - public boolean canAddUpgrade (@Nonnull ItemStack upgrade) { - if (upgrade.isEmpty()) - return false; - if (!(upgrade.getItem() instanceof ItemUpgrade)) - return false; - - ItemUpgrade candidate = (ItemUpgrade)upgrade.getItem(); - if (candidate.getAllowMultiple()) - return true; - - for (ItemStack stack : upgrades) { - if (stack.isEmpty()) - continue; - - if (!(stack.getItem() instanceof ItemUpgrade)) - continue; - - ItemUpgrade reference = (ItemUpgrade)stack.getItem(); - if (candidate == reference) - return false; - } - - return true; - } - - public int getNextUpgradeSlot () { - for (int i = 0; i < upgrades.length; i++) { - if (upgrades[i].isEmpty()) - return i; - } - - return -1; - } - public int getDrawerCapacity () { return drawerCapacity; } - @Deprecated - public void setDrawerCapacity (int stackCount) { - drawerCapacity = stackCount; - attributeChanged(); - } - public boolean canAddOneStackUpgrade () { if (getEffectiveDrawerCapacity() == 1) return false; - int storageMult = getEffectiveStorageMultiplier(); + int storageMult = upgradeData.getStorageMultiplier(); int lostStackCapacity = storageMult * (getEffectiveDrawerCapacity() - 1); for (int i = 0; i < getDrawerCount(); i++) { @@ -287,10 +155,8 @@ public boolean canAddOneStackUpgrade () { } public int getEffectiveDrawerCapacity () { - for (ItemStack upgrade : upgrades) { - if (!upgrade.isEmpty() && upgrade.getItem() == ModItems.upgradeOneStack) - return 1; - } + if (upgradeData.hasOneStackUpgrade()) + return 1; return getDrawerCapacity(); } @@ -305,30 +171,6 @@ protected void attributeChanged () { } } - @Override - public boolean isItemLocked (LockAttribute attr) { - if (!StorageDrawers.config.cache.enableLockUpgrades) - return false; - - return drawerAttributes.isItemLocked(attr); - } - - @Override - public boolean canItemLock (LockAttribute attr) { - if (!StorageDrawers.config.cache.enableLockUpgrades) - return false; - - return true; - } - - @Override - public void setItemLocked (LockAttribute attr, boolean isLocked) { - if (!StorageDrawers.config.cache.enableLockUpgrades) - return; - - drawerAttributes.setItemLocked(attr, isLocked); - } - public boolean isShrouded () { if (!StorageDrawers.config.cache.enableShroudUpgrades) return false; @@ -336,10 +178,6 @@ public boolean isShrouded () { return drawerAttributes.isConcealed(); } - public void setIsShrouded (boolean shrouded) { - drawerAttributes.setIsConcealed(shrouded); - } - public boolean isShowingQuantity () { if (!StorageDrawers.config.cache.enableQuantifiableUpgrades) return false; @@ -347,14 +185,6 @@ public boolean isShowingQuantity () { return drawerAttributes.isShowingQuantity(); } - public boolean setIsShowingQuantity (boolean quantified) { - if (!StorageDrawers.config.cache.enableQuantifiableUpgrades) - return false; - - drawerAttributes.setIsShowingQuantity(quantified); - return true; - } - @Override public UUID getOwner () { if (!StorageDrawers.config.cache.enablePersonalUpgrades) @@ -412,19 +242,6 @@ public boolean setSecurityProvider (ISecurityProvider provider) { return true; } - public boolean shouldHideUpgrades () { - return hideUpgrade; - } - - public void setShouldHideUpgrades (boolean hide) { - hideUpgrade = hide; - - if (getWorld() != null && !getWorld().isRemote) { - markDirty(); - markBlockForUpdate(); - } - } - public boolean isSealed () { if (!StorageDrawers.config.cache.enableTape) return false; @@ -450,69 +267,38 @@ public boolean setIsSealed (boolean sealed) { return true; } - public boolean isVoid () { - if (!StorageDrawers.config.cache.enableVoidUpgrades) - return false; - - for (ItemStack stack : upgrades) { - if (stack.getItem() == ModItems.upgradeVoid) - return true; - } - - return false; - } - public boolean isUnlimited () { if (!StorageDrawers.config.cache.enableCreativeUpgrades) return false; - for (ItemStack stack : upgrades) { - if (stack.getItem() == ModItems.upgradeCreative) - return true; - } - - return false; + return upgradeData.hasUnlimitedUpgrade(); } public boolean isVending () { if (!StorageDrawers.config.cache.enableCreativeUpgrades) return false; - for (ItemStack stack : upgrades) { - if (stack.getItem() == ModItems.upgradeCreative && stack.getItemDamage() == 1) - return true; - } - - return false; + return upgradeData.hasVendingUpgrade(); } public boolean isRedstone () { if (!StorageDrawers.config.cache.enableRedstoneUpgrades) return false; - for (ItemStack stack : upgrades) { - if (stack.getItem() == ModItems.upgradeRedstone) - return true; - } - - return false; + return upgradeData.getRedstoneType() != null; } public int getRedstoneLevel () { - int redstoneType = -1; - for (ItemStack stack : upgrades) { - if (stack.getItem() == ModItems.upgradeRedstone) { - redstoneType = stack.getItemDamage(); - break; - } - } + EnumUpgradeRedstone type = upgradeData.getRedstoneType(); + if (type == null) + return 0; - switch (redstoneType) { - case 0: + switch (type) { + case COMBINED: return getCombinedRedstoneLevel(); - case 1: + case MAX: return getMaxRedstoneLevel(); - case 2: + case MIN: return getMinRedstoneLevel(); default: return 0; @@ -676,7 +462,7 @@ public int putItemsIntoSlot (int slot, @Nonnull ItemStack stack, int count) { return 0; int countAdded = Math.min(count, stack.getCount()); - if (!isVoid()) + if (!drawerAttributes.isVoid()) countAdded = Math.min(countAdded, drawer.getRemainingCapacity()); drawer.setStoredItemCount(drawer.getStoredItemCount() + countAdded); @@ -761,25 +547,14 @@ protected NBTTagCompound writeToFixedNBT (NBTTagCompound tag) { public void readFromPortableNBT (NBTTagCompound tag) { super.readFromPortableNBT(tag); - upgrades = new ItemStack[upgrades.length]; - for (int i = 0; i < upgrades.length; i++) - upgrades[i] = ItemStack.EMPTY; - material = null; if (tag.hasKey("Mat")) material = tag.getString("Mat"); drawerCapacity = tag.getInteger("Cap"); - NBTTagList upgradeList = tag.getTagList("Upgrades", Constants.NBT.TAG_COMPOUND); - for (int i = 0; i < upgradeList.tagCount(); i++) { - NBTTagCompound upgradeTag = upgradeList.getCompoundTagAt(i); - - int slot = upgradeTag.getByte("Slot"); - setUpgrade(slot, new ItemStack(upgradeTag)); - } - - drawerAttributes.setIsVoid(isVoid()); + if (tag.hasKey("Upgrades")) + upgradeData.deserializeNBT(tag.getTagList("Upgrades", Constants.NBT.TAG_COMPOUND)); drawerAttributes.setItemLocked(LockAttribute.LOCK_EMPTY, false); drawerAttributes.setItemLocked(LockAttribute.LOCK_POPULATED, false); @@ -845,16 +620,7 @@ public NBTTagCompound writeToPortableNBT (NBTTagCompound tag) { if (material != null) tag.setString("Mat", material); - NBTTagList upgradeList = new NBTTagList(); - for (int i = 0; i < upgrades.length; i++) { - if (!upgrades[i].isEmpty()) { - NBTTagCompound upgradeTag = upgrades[i].writeToNBT(new NBTTagCompound()); - upgradeTag.setByte("Slot", (byte)i); - - upgradeList.appendTag(upgradeTag); - } - } - + NBTTagList upgradeList = upgradeData.serializeNBT(); if (upgradeList.tagCount() > 0) tag.setTag("Upgrades", upgradeList); diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java index d2853c9c3..e97ecfe36 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java @@ -532,7 +532,7 @@ public void setStoredItemCount (int slot, int amount) { pooledCount = poolMax; if (pooledCount != oldCount) { - if (pooledCount != 0 || TileEntityDrawersComp.this.isItemLocked(LockAttribute.LOCK_POPULATED)) + if (pooledCount != 0 || TileEntityDrawersComp.this.getDrawerAttributes().isItemLocked(LockAttribute.LOCK_POPULATED)) markAmountDirty(); else { clear(); @@ -615,7 +615,7 @@ public int getStoredItemStackSize (int slot) { @Override public int getItemCapacityForInventoryStack (int slot) { - if (isVoid()) + if (getDrawerAttributes().isVoid()) return Integer.MAX_VALUE; else return getMaxCapacity(slot); @@ -729,7 +729,7 @@ private int getStackCapacity (int slot) { } private int getBaseStackCapacity () { - return TileEntityDrawersComp.this.getEffectiveStorageMultiplier() * TileEntityDrawersComp.this.getEffectiveDrawerCapacity(); + return TileEntityDrawersComp.this.upgrades().getStorageMultiplier() * TileEntityDrawersComp.this.getEffectiveDrawerCapacity(); } public void markAmountDirty () { diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java index cd7800b00..65fc446e1 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java @@ -3,7 +3,6 @@ import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.api.storage.EnumBasicDrawer; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; -import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; import com.jaquadro.minecraft.storagedrawers.block.BlockStandardDrawers; import com.jaquadro.minecraft.storagedrawers.config.ConfigManager; import com.jaquadro.minecraft.storagedrawers.inventory.ContainerDrawers1; @@ -118,7 +117,7 @@ public StandardStorageProvider () { @Override public int getSlotStackCapacity (int slot) { - return getEffectiveStorageMultiplier() * getEffectiveDrawerCapacity(); + return upgrades().getStorageMultiplier() * getEffectiveDrawerCapacity(); } /*@Override diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java new file mode 100644 index 000000000..e8f2be332 --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java @@ -0,0 +1,231 @@ +package com.jaquadro.minecraft.storagedrawers.block.tile.tiledata; + +import com.jaquadro.minecraft.storagedrawers.StorageDrawers; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributesModifiable; +import com.jaquadro.minecraft.storagedrawers.config.ConfigManager; +import com.jaquadro.minecraft.storagedrawers.core.ModItems; +import com.jaquadro.minecraft.storagedrawers.item.*; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.util.math.MathHelper; +import net.minecraftforge.common.util.INBTSerializable; + +import javax.annotation.Nonnull; + +public class UpgradeData implements INBTSerializable +{ + private final ItemStack[] upgrades; + private int storageMultiplier; + private EnumUpgradeStatus statusType; + private EnumUpgradeRedstone redstoneType; + + // TODO: Do we need to provide these? + private boolean hasOneStack; + private boolean hasVoid; + private boolean hasUnlimited; + private boolean hasVending; + + private IDrawerAttributesModifiable attrs; + + public UpgradeData (int slotCount) { + upgrades = new ItemStack[slotCount]; + for (int i = 0; i < upgrades.length; i++) + upgrades[i] = ItemStack.EMPTY; + + syncStorageMultiplier(); + } + + public void setDrawerAttributes (IDrawerAttributesModifiable attrs) { + this.attrs = attrs; + syncUpgrades(); + } + + public int getSlotCount () { + return upgrades.length; + } + + @Nonnull + public ItemStack getUpgrade (int slot) { + slot = MathHelper.clamp(slot, 0, upgrades.length - 1); + return upgrades[slot]; + } + + public boolean addUpgrade (@Nonnull ItemStack upgrade) { + int slot = getNextUpgradeSlot(); + if (slot == -1) + return false; + + setUpgrade(slot, upgrade); + return true; + } + + public void setUpgrade (int slot, @Nonnull ItemStack upgrade) { + slot = MathHelper.clamp(slot, 0, upgrades.length - 1); + + if (!upgrade.isEmpty()) { + upgrade = upgrade.copy(); + upgrade.setCount(1); + } + + ItemStack prevUpgrade = upgrades[slot]; + upgrades[slot] = upgrade; + + syncUpgrades(); + onUpgradeChanged(prevUpgrade, upgrade); + } + + public boolean canAddUpgrade (@Nonnull ItemStack upgrade) { + if (upgrade.isEmpty()) + return false; + if (!(upgrade.getItem() instanceof ItemUpgrade)) + return false; + + ItemUpgrade candidate = (ItemUpgrade)upgrade.getItem(); + if (candidate.getAllowMultiple()) + return true; + + for (ItemStack stack : upgrades) { + if (stack.isEmpty()) + continue; + + if (!(stack.getItem() instanceof ItemUpgrade)) + continue; + + ItemUpgrade reference = (ItemUpgrade)stack.getItem(); + if (candidate == reference) + return false; + } + + return true; + } + + public int getStorageMultiplier () { + return storageMultiplier; + } + + public EnumUpgradeStatus getStatusType () { + return statusType; + } + + public EnumUpgradeRedstone getRedstoneType () { + return redstoneType; + } + + public boolean hasOneStackUpgrade () { + return hasOneStack; + } + + public boolean hasUnlimitedUpgrade () { + return hasUnlimited; + } + + public boolean hasVendingUpgrade () { + return hasVending; + } + + private int getNextUpgradeSlot () { + for (int i = 0; i < upgrades.length; i++) { + if (upgrades[i].isEmpty()) + return i; + } + + return -1; + } + + private void syncUpgrades () { + if (this.attrs == null) + return; + + syncStorageMultiplier(); + syncStatusLevel(); + syncRedstoneLevel(); + + hasOneStack = false; + hasVoid = false; + hasUnlimited = false; + hasVending = false; + + for (ItemStack stack : upgrades) { + Item item = stack.getItem(); + if (item == ModItems.upgradeOneStack) + hasOneStack = true; + else if (item == ModItems.upgradeVoid) + hasVoid = true; + else if (item == ModItems.upgradeCreative) { + EnumUpgradeCreative type = EnumUpgradeCreative.byMetadata(stack.getMetadata()); + if (type == EnumUpgradeCreative.STORAGE) + hasUnlimited = true; + else if (type == EnumUpgradeCreative.VENDING) + hasVending = true; + } + } + + attrs.setIsVoid(hasVoid); + } + + private void syncStorageMultiplier () { + ConfigManager config = StorageDrawers.config; + storageMultiplier = 0; + + for (ItemStack stack : upgrades) { + if (stack.getItem() == ModItems.upgradeStorage) { + int level = EnumUpgradeStorage.byMetadata(stack.getMetadata()).getLevel(); + storageMultiplier += config.getStorageUpgradeMultiplier(level); + } + } + + if (storageMultiplier == 0) + storageMultiplier = config.getStorageUpgradeMultiplier(1); + } + + private void syncStatusLevel () { + statusType = null; + + for (ItemStack stack : upgrades) { + if (stack.getItem() == ModItems.upgradeStatus) { + statusType = EnumUpgradeStatus.byMetadata(stack.getMetadata()); + break; + } + } + } + + private void syncRedstoneLevel () { + redstoneType = null; + + for (ItemStack stack : upgrades) { + if (stack.getItem() == ModItems.upgradeRedstone) { + redstoneType = EnumUpgradeRedstone.byMetadata(stack.getMetadata()); + break; + } + } + } + + @Override + public NBTTagList serializeNBT () { + NBTTagList tagList = new NBTTagList(); + for (int i = 0; i < upgrades.length; i++) { + if (!upgrades[i].isEmpty()) { + NBTTagCompound upgradeTag = upgrades[i].writeToNBT(new NBTTagCompound()); + upgradeTag.setByte("Slot", (byte)i); + + tagList.appendTag(upgradeTag); + } + } + + return tagList; + } + + @Override + public void deserializeNBT (NBTTagList tagList) { + for (int i = 0; i < tagList.tagCount(); i++) { + NBTTagCompound upgradeTag = tagList.getCompoundTagAt(i); + + int slot = upgradeTag.getByte("Slot"); + setUpgrade(slot, new ItemStack(upgradeTag)); + } + } + + protected void onUpgradeChanged (ItemStack oldUpgrade, ItemStack newUpgrade) { } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java b/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java index 63fe7ffab..640dedbdd 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java +++ b/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java @@ -21,10 +21,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.renderer.*; -import net.minecraft.client.renderer.block.model.ModelManager; -import net.minecraft.client.renderer.color.ItemColors; import net.minecraft.client.renderer.texture.TextureAtlasSprite; -import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.client.renderer.texture.TextureMap; import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; import net.minecraft.item.ItemBlock; @@ -83,7 +80,7 @@ public void render (TileEntityDrawers tile, double x, double y, double z, float boolean cache = mc.gameSettings.fancyGraphics; mc.gameSettings.fancyGraphics = true; renderUpgrades(renderer, tile, state); - if (!tile.isShrouded() && !tile.isSealed()) + if (!tile.getDrawerAttributes().isConcealed() && !tile.isSealed()) renderFastItemSet(renderer, tile, state, side, depth, partialTickTime); mc.gameSettings.fancyGraphics = cache; @@ -297,12 +294,12 @@ private void renderUpgrades (ChamRender renderer, TileEntityDrawers tile, IBlock GlStateManager.enableAlpha(); - renderIndicator(renderer, tile, state, tile.getDirection(), tile.getEffectiveStatusLevel()); + renderIndicator(renderer, tile, state, tile.getDirection(), tile.upgrades().getStatusType()); renderTape(renderer, tile, state, tile.getDirection(), tile.isSealed()); } - private void renderIndicator (ChamRender renderer, TileEntityDrawers tile, IBlockState blockState, int side, int level) { - if (level <= 0 || side < 2 || side > 5) + private void renderIndicator (ChamRender renderer, TileEntityDrawers tile, IBlockState blockState, int side, EnumUpgradeStatus level) { + if (level == null || side < 2 || side > 5) return; BlockDrawers block = (BlockDrawers)blockState.getBlock(); @@ -318,11 +315,11 @@ private void renderIndicator (ChamRender renderer, TileEntityDrawers tile, IBloc for (int i = 0; i < count; i++) { IDrawer drawer = tile.getDrawer(i); - if (drawer == null || tile.isShrouded()) + if (drawer == null || tile.getDrawerAttributes().isConcealed()) continue; - TextureAtlasSprite iconOff = Chameleon.instance.iconRegistry.getIcon(statusInfo.getSlot(i).getOffResource(EnumUpgradeStatus.byLevel(level))); - TextureAtlasSprite iconOn = Chameleon.instance.iconRegistry.getIcon(statusInfo.getSlot(i).getOnResource(EnumUpgradeStatus.byLevel(level))); + TextureAtlasSprite iconOff = Chameleon.instance.iconRegistry.getIcon(statusInfo.getSlot(i).getOffResource(level)); + TextureAtlasSprite iconOn = Chameleon.instance.iconRegistry.getIcon(statusInfo.getSlot(i).getOnResource(level)); Area2D statusArea = statusInfo.getSlot(i).getStatusArea(); Area2D activeArea = statusInfo.getSlot(i).getStatusActiveArea(); @@ -338,14 +335,14 @@ private void renderIndicator (ChamRender renderer, TileEntityDrawers tile, IBloc GlStateManager.doPolygonOffset(-1, -10); - if (level == 1 && drawer.getMaxCapacity() > 0 && drawer.getRemainingCapacity() == 0) { + if (level == EnumUpgradeStatus.LEVEL1 && drawer.getMaxCapacity() > 0 && drawer.getRemainingCapacity() == 0) { renderer.setRenderBounds(statusArea.getX() * unit, statusArea.getY() * unit, 0, (statusArea.getX() + statusArea.getWidth()) * unit, (statusArea.getY() + statusArea.getHeight()) * unit, depth - frontDepth); renderer.state.setRotateTransform(ChamRender.ZPOS, side); renderer.renderFace(ChamRender.FACE_ZPOS, null, blockState, BlockPos.ORIGIN, iconOn, 1, 1, 1); renderer.state.clearRotateTransform(); } - else if (level >= 2) { + else if (level == EnumUpgradeStatus.LEVEL2) { int stepX = statusInfo.getSlot(i).getActiveStepsX(); int stepY = statusInfo.getSlot(i).getActiveStepsY(); diff --git a/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java b/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java index 8bc4394bf..ea26e5acf 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java +++ b/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java @@ -3,7 +3,9 @@ import com.jaquadro.minecraft.chameleon.integration.IntegrationModule; import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.api.registry.IWailaTooltipHandler; +import com.jaquadro.minecraft.storagedrawers.api.storage.EmptyDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.api.storage.IFractionalDrawer; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; import com.jaquadro.minecraft.storagedrawers.block.BlockDrawers; @@ -21,6 +23,8 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.fml.common.event.FMLInterModComms; import javax.annotation.Nonnull; @@ -74,6 +78,9 @@ public static void registerProvider(IWailaRegistrar registrar) { public static class WailaDrawer implements IWailaDataProvider { + @CapabilityInject(IDrawerAttributes.class) + public static Capability DRAWER_ATTRIBUTES_CAPABILITY = null; + @Override @Nonnull public ItemStack getWailaStack (IWailaDataAccessor accessor, IWailaConfigHandler config) { @@ -92,6 +99,9 @@ public List getWailaHead (@Nonnull ItemStack itemStack, List cur @Override public List getWailaBody (@Nonnull ItemStack itemStack, List currenttip, IWailaDataAccessor accessor, IWailaConfigHandler config) { TileEntityDrawers tile = (TileEntityDrawers) accessor.getTileEntity(); + IDrawerAttributes attr = tile.getCapability(DRAWER_ATTRIBUTES_CAPABILITY, null); + if (attr == null) + attr = new EmptyDrawerAttributes(); if (SecurityManager.hasAccess(Minecraft.getMinecraft().player.getGameProfile(), tile)) { if (config.getConfig("display.content")) { @@ -133,20 +143,19 @@ else if (stacks > 0) if (tile.isUnlimited() || tile.isVending()) currenttip.add(I18n.format("storagedrawers.waila.nolimit")); else { - int limit = tile.getEffectiveDrawerCapacity() * tile.getEffectiveStorageMultiplier(); - currenttip.add(I18n.format("storagedrawers.waila.limit", limit, tile.getEffectiveStorageMultiplier())); + int multiplier = tile.upgrades().getStorageMultiplier(); + int limit = tile.getEffectiveDrawerCapacity() * multiplier; + currenttip.add(I18n.format("storagedrawers.waila.limit", limit, multiplier)); } } } if (config.getConfig("display.status")) { String attrib = ""; - if (tile.isItemLocked(LockAttribute.LOCK_POPULATED)) + if (attr.isItemLocked(LockAttribute.LOCK_POPULATED)) attrib += (attrib.isEmpty() ? "" : ", ") + I18n.format("storagedrawers.waila.locked"); - if (tile.isVoid()) + if (attr.isVoid()) attrib += (attrib.isEmpty() ? "" : ", ") + I18n.format("storagedrawers.waila.void"); - if (tile.isSorting()) - attrib += (attrib.isEmpty() ? "" : ", ") + I18n.format("storagedrawers.waila.sorting"); if (tile.getOwner() != null) attrib += (attrib.isEmpty() ? "" : ", ") + I18n.format("storagedrawers.waila.protected"); diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java index 54d8769ef..4357df4c4 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java @@ -2,7 +2,6 @@ import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.api.storage.*; -import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IVoidable; import net.minecraft.init.Items; import net.minecraft.item.ItemStack; import net.minecraftforge.common.capabilities.Capability; diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryUpgrade.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryUpgrade.java index ea496f8d8..9c7fb412c 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryUpgrade.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryUpgrade.java @@ -30,7 +30,7 @@ public int getSizeInventory () { @Override public boolean isEmpty () { for (int i = 0; i < upgradeCapacity; i++) { - if (!tile.getUpgrade(i).isEmpty()) + if (!tile.upgrades().getUpgrade(i).isEmpty()) return false; } @@ -40,15 +40,15 @@ public boolean isEmpty () { @Override @Nonnull public ItemStack getStackInSlot (int slot) { - return tile.getUpgrade(slot); + return tile.upgrades().getUpgrade(slot); } @Override @Nonnull public ItemStack decrStackSize (int slot, int count) { - ItemStack stack = tile.getUpgrade(slot); + ItemStack stack = tile.upgrades().getUpgrade(slot); if (count > 0) - tile.setUpgrade(slot, ItemStack.EMPTY); + tile.upgrades().setUpgrade(slot, ItemStack.EMPTY); return stack; } @@ -64,7 +64,7 @@ public void setInventorySlotContents (int slot, @Nonnull ItemStack item) { //if (item != null && item.stackSize > getInventoryStackLimit()) // item.stackSize = getInventoryStackLimit(); - tile.setUpgrade(slot, item); + tile.upgrades().setUpgrade(slot, item); } @Override @@ -136,7 +136,7 @@ public boolean canAddOneStackUpgrade () { } public boolean canAddUpgrade (@Nonnull ItemStack item) { - return tile.canAddUpgrade(item); + return tile.upgrades().canAddUpgrade(item); } public boolean canRemoveStorageUpgrade (int storageLevel) { @@ -145,7 +145,7 @@ public boolean canRemoveStorageUpgrade (int storageLevel) { private boolean canRemoveStorageUpgrade (TileEntityDrawers tile, int storageLevel) { int storageMult = StorageDrawers.config.getStorageUpgradeMultiplier(storageLevel); - int effectiveStorageMult = tile.getEffectiveStorageMultiplier(); + int effectiveStorageMult = tile.upgrades().getStorageMultiplier(); if (effectiveStorageMult == storageMult) storageMult--; diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/IUpgradeProvider.java b/src/com/jaquadro/minecraft/storagedrawers/storage/IUpgradeProvider.java index 94c73ff84..3bcbf1c4b 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/IUpgradeProvider.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/IUpgradeProvider.java @@ -2,9 +2,5 @@ public interface IUpgradeProvider { - boolean isVoid (); - - boolean isSorting (); - boolean isShrouded (); } From b7bc7ce9f0ca2b2b9dcbe97fb1d8c6463373a87e Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sat, 24 Jun 2017 01:04:20 -0400 Subject: [PATCH 03/22] Remove dead interfaces --- .../api/storage/attribute/IItemLockable.java | 21 ------------------- .../api/storage/attribute/IQuantifiable.java | 16 -------------- .../api/storage/attribute/IShroudable.java | 16 -------------- .../api/storage/attribute/IVoidable.java | 9 -------- .../storage/IUpgradeProvider.java | 6 ------ 5 files changed, 68 deletions(-) delete mode 100644 src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IItemLockable.java delete mode 100644 src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IQuantifiable.java delete mode 100644 src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IShroudable.java delete mode 100644 src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IVoidable.java delete mode 100644 src/com/jaquadro/minecraft/storagedrawers/storage/IUpgradeProvider.java diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IItemLockable.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IItemLockable.java deleted file mode 100644 index 684822e65..000000000 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IItemLockable.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.jaquadro.minecraft.storagedrawers.api.storage.attribute; - -public interface IItemLockable -{ - /** - * Gets whether or not a drawer or group is locked for the given lock attribute. - */ - boolean isItemLocked (LockAttribute attr); - - /** - * Gets whether or not the lock state can be changed for the given lock attribute. - * If this method returns false, isItemLocked may still return true. - */ - boolean canItemLock (LockAttribute attr); - - /** - * Sets the lock state of a drawer or group for the given lock attribute. - * If canItemLock returns false, this is a no-op. - */ - void setItemLocked (LockAttribute attr, boolean isLocked); -} diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IQuantifiable.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IQuantifiable.java deleted file mode 100644 index 1e37e82cb..000000000 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IQuantifiable.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.jaquadro.minecraft.storagedrawers.api.storage.attribute; - -public interface IQuantifiable -{ - /** - * Gets whether or not the drawer has the quantified attribute. - * The quantified attribute instructs the drawer to render its numerical quantity. - */ - boolean isShowingQuantity (); - - /** - * Sets whether or not the drawer is currently quantified. - * @return false if the operation is not supported, true otherwise. - */ - boolean setIsShowingQuantity (boolean state); -} diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IShroudable.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IShroudable.java deleted file mode 100644 index b7f0bf035..000000000 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IShroudable.java +++ /dev/null @@ -1,16 +0,0 @@ -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/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IVoidable.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IVoidable.java deleted file mode 100644 index 9fc580635..000000000 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IVoidable.java +++ /dev/null @@ -1,9 +0,0 @@ -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/com/jaquadro/minecraft/storagedrawers/storage/IUpgradeProvider.java b/src/com/jaquadro/minecraft/storagedrawers/storage/IUpgradeProvider.java deleted file mode 100644 index 3bcbf1c4b..000000000 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/IUpgradeProvider.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.jaquadro.minecraft.storagedrawers.storage; - -public interface IUpgradeProvider -{ - boolean isShrouded (); -} From 96d572bfcaaa6e5c291ee6090cbe011be221531e Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sat, 24 Jun 2017 01:33:48 -0400 Subject: [PATCH 04/22] Consolidate render material storage --- .../block/BlockDrawersCustom.java | 6 +- .../storagedrawers/block/BlockTrimCustom.java | 2 +- .../block/modeldata/MaterialModelData.java | 20 ++-- .../block/tile/TileEntityDrawers.java | 99 ++--------------- .../block/tile/TileEntityTrim.java | 73 +------------ .../block/tile/tiledata/MaterialData.java | 103 ++++++++++++++++++ .../item/ItemCustomDrawers.java | 6 +- .../storagedrawers/item/ItemCustomTrim.java | 4 +- 8 files changed, 137 insertions(+), 176 deletions(-) create mode 100644 src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/MaterialData.java diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawersCustom.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawersCustom.java index 27d9558d4..860f37083 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawersCustom.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawersCustom.java @@ -47,7 +47,7 @@ public boolean canRenderInLayer (IBlockState state, BlockRenderLayer layer) { @Override public boolean doesSideBlockRendering (IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing face) { TileEntityDrawers tile = getTileEntity(world, pos); - if (tile != null && tile.getEffectiveMaterialSide().isEmpty()) + if (tile != null && tile.material().getEffectiveSide().isEmpty()) return false; return super.doesSideBlockRendering(state, world, pos, face); @@ -65,7 +65,7 @@ protected ItemStack getMainDrop (IBlockAccess world, BlockPos pos, IBlockState s if (tile == null) return ItemCustomDrawers.makeItemStack(state, 1, ItemStack.EMPTY, ItemStack.EMPTY, ItemStack.EMPTY); - ItemStack drop = ItemCustomDrawers.makeItemStack(state, 1, tile.getMaterialSide(), tile.getMaterialTrim(), tile.getMaterialFront()); + ItemStack drop = ItemCustomDrawers.makeItemStack(state, 1, tile.material().getSide(), tile.material().getTrim(), tile.material().getFront()); if (drop.isEmpty()) return ItemStack.EMPTY; @@ -92,7 +92,7 @@ public void getSubBlocks (CreativeTabs creativeTabs, NonNullList list @Override public boolean onBlockActivated (World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) { TileEntityDrawers tile = getTileEntity(world, pos); - if (tile != null && tile.getMaterialSide().isEmpty()) + if (tile != null && tile.material().getSide().isEmpty()) return false; return super.onBlockActivated(world, pos, state, player, hand, side, hitX, hitY, hitZ); diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockTrimCustom.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockTrimCustom.java index 62bbb3e9d..fb0947a44 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockTrimCustom.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockTrimCustom.java @@ -57,7 +57,7 @@ protected ItemStack getMainDrop (IBlockAccess world, BlockPos pos, IBlockState s if (tile == null) return ItemCustomTrim.makeItemStack(this, 1, ItemStack.EMPTY, ItemStack.EMPTY); - return ItemCustomTrim.makeItemStack(this, 1, tile.getMaterialSide(), tile.getMaterialTrim()); + return ItemCustomTrim.makeItemStack(this, 1, tile.material().getSide(), tile.material().getTrim()); } @Override diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/modeldata/MaterialModelData.java b/src/com/jaquadro/minecraft/storagedrawers/block/modeldata/MaterialModelData.java index 431ba1d2c..fe638cf9b 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/modeldata/MaterialModelData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/modeldata/MaterialModelData.java @@ -32,12 +32,12 @@ public MaterialModelData (TileEntityDrawers tile) { effectiveMatTrim = ItemStack.EMPTY; } else { - matFront = tile.getMaterialFront(); - matSide = tile.getMaterialSide(); - matTrim = tile.getMaterialTrim(); - effectiveMatFront = tile.getEffectiveMaterialFront(); - effectiveMatSide = tile.getEffectiveMaterialSide(); - effectiveMatTrim = tile.getEffectiveMaterialTrim(); + matFront = tile.material().getFront(); + matSide = tile.material().getSide(); + matTrim = tile.material().getTrim(); + effectiveMatFront = tile.material().getEffectiveFront(); + effectiveMatSide = tile.material().getEffectiveSide(); + effectiveMatTrim = tile.material().getEffectiveTrim(); } } @@ -52,10 +52,10 @@ public MaterialModelData (TileEntityTrim tile) { effectiveMatTrim = ItemStack.EMPTY; } else { - matSide = tile.getMaterialSide(); - matTrim = tile.getMaterialTrim(); - effectiveMatSide = tile.getEffectiveMaterialSide(); - effectiveMatTrim = tile.getEffectiveMaterialTrim(); + matSide = tile.material().getSide(); + matTrim = tile.material().getTrim(); + effectiveMatSide = tile.material().getEffectiveSide(); + effectiveMatTrim = tile.material().getEffectiveTrim(); } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index b8a6836f5..b10fe1ab9 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -12,6 +12,7 @@ import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*; import com.jaquadro.minecraft.storagedrawers.block.BlockDrawersCustom; import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.ControllerData; +import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.MaterialData; import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.UpgradeData; import com.jaquadro.minecraft.storagedrawers.core.capabilities.BasicDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.core.capabilities.CapabilityDrawerAttributes; @@ -40,6 +41,7 @@ public abstract class TileEntityDrawers extends ChamLockableTileEntity implement { private LockableData lockData = new LockableData(); private CustomNameData customNameData = new CustomNameData("storagedrawers.container.drawers"); + private MaterialData materialData = new MaterialData(); public final ControllerData controllerData = new ControllerData(); private IDrawer[] drawers; @@ -57,13 +59,6 @@ public abstract class TileEntityDrawers extends ChamLockableTileEntity implement private long lastClickTime; private UUID lastClickUUID; - @Nonnull - private ItemStack materialSide; - @Nonnull - private ItemStack materialFront; - @Nonnull - private ItemStack materialTrim; - private class DrawerAttributes extends BasicDrawerAttributes { @Override @@ -83,13 +78,11 @@ protected TileEntityDrawers (int drawerCount) { upgradeData = new UpgradeData(7); upgradeData.setDrawerAttributes(drawerAttributes); - materialSide = ItemStack.EMPTY; - materialFront = ItemStack.EMPTY; - materialTrim = ItemStack.EMPTY; - injectData(lockData); - injectData(customNameData); + injectPortableData(customNameData); + injectPortableData(materialData); injectData(controllerData); + initWithDrawerCount(drawerCount); } @@ -109,6 +102,10 @@ public UpgradeData upgrades () { return upgradeData; } + public MaterialData material () { + return materialData; + } + public int getDirection () { return direction; } @@ -369,52 +366,6 @@ protected int getMaxRedstoneLevel () { return (int)Math.ceil(maxRatio * 14); } - public boolean isSorting () { - return false; - } - - @Nonnull - public ItemStack getMaterialSide () { - return materialSide; - } - - @Nonnull - public ItemStack getMaterialFront () { - return materialFront; - } - - @Nonnull - public ItemStack getMaterialTrim () { - return materialTrim; - } - - @Nonnull - public ItemStack getEffectiveMaterialSide () { - return materialSide; - } - - @Nonnull - public ItemStack getEffectiveMaterialFront () { - return !materialFront.isEmpty() ? materialFront : materialSide; - } - - @Nonnull - public ItemStack getEffectiveMaterialTrim () { - return !materialTrim.isEmpty() ? materialTrim : materialSide; - } - - public void setMaterialSide (@Nonnull ItemStack material) { - materialSide = material; - } - - public void setMaterialFront (@Nonnull ItemStack material) { - materialFront = material; - } - - public void setMaterialTrim (@Nonnull ItemStack material) { - materialTrim = material; - } - @Nonnull public ItemStack takeItemsFromSlot (int slot, int count) { if (slot < 0 || slot >= getDrawerCount()) @@ -596,18 +547,6 @@ public void readFromPortableNBT (NBTTagCompound tag) { drawers[i].readFromNBT(slot); } - materialSide = ItemStack.EMPTY; - if (tag.hasKey("MatS")) - materialSide = new ItemStack(tag.getCompoundTag("MatS")); - - materialFront = ItemStack.EMPTY; - if (tag.hasKey("MatF")) - materialFront = new ItemStack(tag.getCompoundTag("MatF")); - - materialTrim = ItemStack.EMPTY; - if (tag.hasKey("MatT")) - materialTrim = new ItemStack(tag.getCompoundTag("MatT")); - attributeChanged(); } @@ -658,24 +597,6 @@ public NBTTagCompound writeToPortableNBT (NBTTagCompound tag) { tag.setTag("Slots", slots); - if (!materialSide.isEmpty()) { - NBTTagCompound itag = new NBTTagCompound(); - materialSide.writeToNBT(itag); - tag.setTag("MatS", itag); - } - - if (!materialFront.isEmpty()) { - NBTTagCompound itag = new NBTTagCompound(); - materialFront.writeToNBT(itag); - tag.setTag("MatF", itag); - } - - if (!materialTrim.isEmpty()) { - NBTTagCompound itag = new NBTTagCompound(); - materialTrim.writeToNBT(itag); - tag.setTag("MatT", itag); - } - return tag; } @@ -910,7 +831,7 @@ public IDrawer getDrawerIfEnabled (int slot) { if (isSealed()) return null; - if (getBlockType() instanceof BlockDrawersCustom && materialSide.isEmpty()) + if (getBlockType() instanceof BlockDrawersCustom && material().getSide().isEmpty()) return null; return drawers[slot]; diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityTrim.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityTrim.java index abc8bb9c6..17404484b 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityTrim.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityTrim.java @@ -1,81 +1,18 @@ package com.jaquadro.minecraft.storagedrawers.block.tile; import com.jaquadro.minecraft.chameleon.block.ChamTileEntity; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; - -import javax.annotation.Nonnull; +import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.MaterialData; public class TileEntityTrim extends ChamTileEntity { - @Nonnull - private ItemStack materialSide; - @Nonnull - private ItemStack materialTrim; + private MaterialData materialData = new MaterialData(); public TileEntityTrim () { - materialSide = ItemStack.EMPTY; - materialTrim = ItemStack.EMPTY; - } - - @Nonnull - public ItemStack getMaterialSide () { - return materialSide; - } - - @Nonnull - public ItemStack getMaterialTrim () { - return materialTrim; - } - - @Nonnull - public ItemStack getEffectiveMaterialSide () { - return materialSide; - } - - @Nonnull - public ItemStack getEffectiveMaterialTrim () { - return !materialTrim.isEmpty() ? materialTrim : materialSide; - } - - public void setMaterialSide (@Nonnull ItemStack material) { - materialSide = material; + injectPortableData(materialData); } - public void setMaterialTrim (@Nonnull ItemStack material) { - materialTrim = material; - } - - @Override - public void readFromPortableNBT (NBTTagCompound tag) { - super.readFromPortableNBT(tag); - - materialSide = ItemStack.EMPTY; - if (tag.hasKey("MatS")) - materialSide = new ItemStack(tag.getCompoundTag("MatS")); - - materialTrim = ItemStack.EMPTY; - if (tag.hasKey("MatT")) - materialTrim = new ItemStack(tag.getCompoundTag("MatT")); - } - - @Override - public NBTTagCompound writeToPortableNBT (NBTTagCompound tag) { - tag = super.writeToPortableNBT(tag); - - if (!materialSide.isEmpty()) { - NBTTagCompound itag = new NBTTagCompound(); - materialSide.writeToNBT(itag); - tag.setTag("MatS", itag); - } - - if (!materialTrim.isEmpty()) { - NBTTagCompound itag = new NBTTagCompound(); - materialTrim.writeToNBT(itag); - tag.setTag("MatT", itag); - } - - return tag; + public MaterialData material () { + return materialData; } @Override diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/MaterialData.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/MaterialData.java new file mode 100644 index 000000000..91c1fd7ef --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/MaterialData.java @@ -0,0 +1,103 @@ +package com.jaquadro.minecraft.storagedrawers.block.tile.tiledata; + +import com.jaquadro.minecraft.chameleon.block.tiledata.TileDataShim; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + +import javax.annotation.Nonnull; + +public class MaterialData extends TileDataShim +{ + @Nonnull + private ItemStack materialSide; + @Nonnull + private ItemStack materialFront; + @Nonnull + private ItemStack materialTrim; + + public MaterialData () { + materialSide = ItemStack.EMPTY; + materialFront = ItemStack.EMPTY; + materialTrim = ItemStack.EMPTY; + } + + @Nonnull + public ItemStack getSide () { + return materialSide; + } + + @Nonnull + public ItemStack getFront () { + return materialFront; + } + + @Nonnull + public ItemStack getTrim () { + return materialTrim; + } + + @Nonnull + public ItemStack getEffectiveSide () { + return materialSide; + } + + @Nonnull + public ItemStack getEffectiveFront () { + return !materialFront.isEmpty() ? materialFront : materialSide; + } + + @Nonnull + public ItemStack getEffectiveTrim () { + return !materialTrim.isEmpty() ? materialTrim : materialSide; + } + + public void setSide (@Nonnull ItemStack material) { + materialSide = material; + } + + public void setFront (@Nonnull ItemStack material) { + materialFront = material; + } + + public void setTrim (@Nonnull ItemStack material) { + materialTrim = material; + } + + @Override + public void readFromNBT (NBTTagCompound tag) { + materialSide = ItemStack.EMPTY; + if (tag.hasKey("MatS")) + materialSide = new ItemStack(tag.getCompoundTag("MatS")); + + materialFront = ItemStack.EMPTY; + if (tag.hasKey("MatF")) + materialFront = new ItemStack(tag.getCompoundTag("MatF")); + + materialTrim = ItemStack.EMPTY; + if (tag.hasKey("MatT")) + materialTrim = new ItemStack(tag.getCompoundTag("MatT")); + } + + @Override + public NBTTagCompound writeToNBT (NBTTagCompound tag) { + if (!materialSide.isEmpty()) { + NBTTagCompound itag = new NBTTagCompound(); + materialSide.writeToNBT(itag); + tag.setTag("MatS", itag); + } + + if (!materialFront.isEmpty()) { + NBTTagCompound itag = new NBTTagCompound(); + materialFront.writeToNBT(itag); + tag.setTag("MatF", itag); + } + + if (!materialTrim.isEmpty()) { + NBTTagCompound itag = new NBTTagCompound(); + materialTrim.writeToNBT(itag); + tag.setTag("MatT", itag); + } + + return tag; + } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/item/ItemCustomDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/item/ItemCustomDrawers.java index 003c9d27a..9b4cb9044 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/item/ItemCustomDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/item/ItemCustomDrawers.java @@ -50,11 +50,11 @@ public boolean placeBlockAt (@Nonnull ItemStack stack, EntityPlayer player, Worl TileEntityDrawers tile = (TileEntityDrawers) world.getTileEntity(pos); if (tile != null && stack.hasTagCompound() && !stack.getTagCompound().hasKey("tile")) { if (stack.getTagCompound().hasKey("MatS")) - tile.setMaterialSide(new ItemStack(stack.getTagCompound().getCompoundTag("MatS"))); + tile.material().setSide(new ItemStack(stack.getTagCompound().getCompoundTag("MatS"))); if (stack.getTagCompound().hasKey("MatT")) - tile.setMaterialTrim(new ItemStack(stack.getTagCompound().getCompoundTag("MatT"))); + tile.material().setTrim(new ItemStack(stack.getTagCompound().getCompoundTag("MatT"))); if (stack.getTagCompound().hasKey("MatF")) - tile.setMaterialFront(new ItemStack(stack.getTagCompound().getCompoundTag("MatF"))); + tile.material().setFront(new ItemStack(stack.getTagCompound().getCompoundTag("MatF"))); } return true; diff --git a/src/com/jaquadro/minecraft/storagedrawers/item/ItemCustomTrim.java b/src/com/jaquadro/minecraft/storagedrawers/item/ItemCustomTrim.java index 7a78f2fa9..60065d6e5 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/item/ItemCustomTrim.java +++ b/src/com/jaquadro/minecraft/storagedrawers/item/ItemCustomTrim.java @@ -28,9 +28,9 @@ public boolean placeBlockAt (@Nonnull ItemStack stack, EntityPlayer player, Worl TileEntityTrim tile = (TileEntityTrim) world.getTileEntity(pos); if (tile != null && stack.hasTagCompound() && !stack.getTagCompound().hasKey("tile")) { if (stack.getTagCompound().hasKey("MatS")) - tile.setMaterialSide(new ItemStack(stack.getTagCompound().getCompoundTag("MatS"))); + tile.material().setSide(new ItemStack(stack.getTagCompound().getCompoundTag("MatS"))); if (stack.getTagCompound().hasKey("MatT")) - tile.setMaterialTrim(new ItemStack(stack.getTagCompound().getCompoundTag("MatT"))); + tile.material().setTrim(new ItemStack(stack.getTagCompound().getCompoundTag("MatT"))); } return true; From 51226105c5b85b9caad123f074485b36bc557f4d Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sat, 24 Jun 2017 02:15:54 -0400 Subject: [PATCH 05/22] Some more upgrade rejiggering --- .../storagedrawers/block/BlockDrawers.java | 22 +--- .../block/tile/TileEntityDrawers.java | 115 +++++++++++------- .../block/tile/tiledata/UpgradeData.java | 21 +++- .../renderer/TileEntityDrawersRenderer.java | 2 +- .../inventory/InventoryUpgrade.java | 9 +- .../storagedrawers/inventory/SlotUpgrade.java | 11 +- 6 files changed, 96 insertions(+), 84 deletions(-) diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java index 2555fe227..cf4c59b7f 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java @@ -252,15 +252,12 @@ public boolean onBlockActivated (World world, BlockPos pos, IBlockState state, E return true; } else if (item.getItem() instanceof ItemUpgrade) { - if (item.getItem() == ModItems.upgradeOneStack && !tileDrawers.canAddOneStackUpgrade()) { + if (!tileDrawers.upgrades().canAddUpgrade(item)) { if (!world.isRemote) player.sendStatusMessage(new TextComponentTranslation("storagedrawers.msg.cannotAddUpgrade"), true); - return false; - } - - if (!tileDrawers.upgrades().canAddUpgrade(item)) return false; + } if (!tileDrawers.upgrades().addUpgrade(item)) { if (!world.isRemote) @@ -279,21 +276,6 @@ else if (item.getItem() instanceof ItemUpgrade) { return true; } - /*else if (item.getItem() == ModItems.drawerKey) { - boolean locked = tileDrawers.isItemLocked(LockAttribute.LOCK_POPULATED); - tileDrawers.setItemLocked(LockAttribute.LOCK_POPULATED, !locked); - tileDrawers.setItemLocked(LockAttribute.LOCK_EMPTY, !locked); - - return true; - } - else if (item.getItem() == ModItems.shroudKey) { - tileDrawers.setIsShrouded(!tileDrawers.isShrouded()); - return true; - } - else if (item.getItem() == ModItems.quantifyKey) { - tileDrawers.setIsShowingQuantity(!tileDrawers.isShowingQuantity()); - return true; - }*/ else if (item.getItem() instanceof ItemPersonalKey) { String securityKey = ((ItemPersonalKey) item.getItem()).getSecurityProviderKey(item.getItemDamage()); ISecurityProvider provider = StorageDrawers.securityRegistry.getProvider(securityKey); diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index b10fe1ab9..772276067 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -14,10 +14,12 @@ import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.ControllerData; import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.MaterialData; import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.UpgradeData; +import com.jaquadro.minecraft.storagedrawers.core.ModItems; import com.jaquadro.minecraft.storagedrawers.core.capabilities.BasicDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.core.capabilities.CapabilityDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.inventory.DrawerItemHandler; import com.jaquadro.minecraft.storagedrawers.item.EnumUpgradeRedstone; +import com.jaquadro.minecraft.storagedrawers.item.EnumUpgradeStorage; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; @@ -50,10 +52,10 @@ public abstract class TileEntityDrawers extends ChamLockableTileEntity implement private String material; private int drawerCapacity = 1; private boolean taped = false; - private boolean hideUpgrade = false; private UUID owner; private String securityKey; + private IDrawerAttributesModifiable drawerAttributes; private UpgradeData upgradeData; private long lastClickTime; @@ -72,10 +74,76 @@ protected void onAttributeChanged () { } } + private class DrawerUpgradeData extends UpgradeData + { + DrawerUpgradeData () { + super(7); + } + + @Override + public boolean canAddUpgrade (@Nonnull ItemStack upgrade) { + if (!super.canAddUpgrade(upgrade)) + return false; + + if (upgrade.getItem() == ModItems.upgradeOneStack) { + int lostStackCapacity = upgradeData.getStorageMultiplier() * (getEffectiveDrawerCapacity() - 1); + if (!stackCapacityCheck(lostStackCapacity)) + return false; + } + + return true; + } + + @Override + public boolean canRemoveUpgrade (int slot) { + if (!super.canRemoveUpgrade(slot)) + return false; + + ItemStack upgrade = getUpgrade(slot); + if (upgrade.getItem() == ModItems.upgradeStorage) { + int storageLevel = EnumUpgradeStorage.byMetadata(upgrade.getMetadata()).getLevel(); + int storageMult = StorageDrawers.config.getStorageUpgradeMultiplier(storageLevel); + int effectiveStorageMult = upgradeData.getStorageMultiplier(); + if (effectiveStorageMult == storageMult) + storageMult--; + + int addedStackCapacity = storageMult * getEffectiveDrawerCapacity(); + if (!stackCapacityCheck(addedStackCapacity)) + return false; + } + + return true; + } + + @Override + protected void onUpgradeChanged (ItemStack oldUpgrade, ItemStack newUpgrade) { + attributeChanged(); + + if (getWorld() != null && !getWorld().isRemote) { + markDirty(); + markBlockForUpdate(); + } + } + + private boolean stackCapacityCheck (int stackCapacity) { + for (int i = 0; i < getDrawerCount(); i++) { + IDrawer drawer = getDrawerIfEnabled(i); + if (drawer == null || drawer.isEmpty()) + continue; + + int addedItemCapacity = stackCapacity * drawer.getStoredItemStackSize(); + if (drawer.getMaxCapacity() - addedItemCapacity < drawer.getStoredItemCount()) + return false; + } + + return true; + } + } + protected TileEntityDrawers (int drawerCount) { drawerAttributes = new DrawerAttributes(); - upgradeData = new UpgradeData(7); + upgradeData = new DrawerUpgradeData(); upgradeData.setDrawerAttributes(drawerAttributes); injectData(lockData); @@ -131,26 +199,6 @@ public int getDrawerCapacity () { return drawerCapacity; } - public boolean canAddOneStackUpgrade () { - if (getEffectiveDrawerCapacity() == 1) - return false; - - int storageMult = upgradeData.getStorageMultiplier(); - int lostStackCapacity = storageMult * (getEffectiveDrawerCapacity() - 1); - - for (int i = 0; i < getDrawerCount(); i++) { - IDrawer drawer = getDrawerIfEnabled(i); - if (drawer == null || drawer.isEmpty()) - continue; - - int lostItemCapacity = lostStackCapacity * drawer.getStoredItemStackSize(); - if (drawer.getMaxCapacity() - lostItemCapacity < drawer.getStoredItemCount()) - return false; - } - - return true; - } - public int getEffectiveDrawerCapacity () { if (upgradeData.hasOneStackUpgrade()) return 1; @@ -168,20 +216,6 @@ protected void attributeChanged () { } } - public boolean isShrouded () { - if (!StorageDrawers.config.cache.enableShroudUpgrades) - return false; - - return drawerAttributes.isConcealed(); - } - - public boolean isShowingQuantity () { - if (!StorageDrawers.config.cache.enableQuantifiableUpgrades) - return false; - - return drawerAttributes.isShowingQuantity(); - } - @Override public UUID getOwner () { if (!StorageDrawers.config.cache.enablePersonalUpgrades) @@ -534,10 +568,6 @@ public void readFromPortableNBT (NBTTagCompound tag) { if (tag.hasKey("Sec")) securityKey = tag.getString("Sec"); - hideUpgrade = false; - if (tag.hasKey("HideUp")) - hideUpgrade = tag.getBoolean("HideUp"); - NBTTagList slots = tag.getTagList("Slots", Constants.NBT.TAG_COMPOUND); drawers = new IDrawer[slots.tagCount()]; @@ -585,9 +615,6 @@ public NBTTagCompound writeToPortableNBT (NBTTagCompound tag) { if (securityKey != null) tag.setString("Sec", securityKey); - if (hideUpgrade) - tag.setBoolean("HideUp", true); - NBTTagList slots = new NBTTagList(); for (IDrawer drawer : drawers) { NBTTagCompound slot = new NBTTagCompound(); @@ -848,8 +875,6 @@ protected IItemHandler createUnSidedHandler () { return new DrawerItemHandler(this); } - private IDrawerAttributesModifiable drawerAttributes; - @SuppressWarnings("unchecked") @Override public T getCapability(net.minecraftforge.common.capabilities.Capability capability, net.minecraft.util.EnumFacing facing) diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java index e8f2be332..829512a45 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java @@ -61,7 +61,7 @@ public boolean addUpgrade (@Nonnull ItemStack upgrade) { return true; } - public void setUpgrade (int slot, @Nonnull ItemStack upgrade) { + public boolean setUpgrade (int slot, @Nonnull ItemStack upgrade) { slot = MathHelper.clamp(slot, 0, upgrades.length - 1); if (!upgrade.isEmpty()) { @@ -70,10 +70,24 @@ public void setUpgrade (int slot, @Nonnull ItemStack upgrade) { } ItemStack prevUpgrade = upgrades[slot]; + if (!prevUpgrade.isEmpty() && !canRemoveUpgrade(slot)) + return false; + + upgrades[slot] = ItemStack.EMPTY; + syncStorageMultiplier(); + + if (!canAddUpgrade(upgrade)) { + upgrades[slot] = prevUpgrade; + syncStorageMultiplier(); + return false; + } + upgrades[slot] = upgrade; syncUpgrades(); onUpgradeChanged(prevUpgrade, upgrade); + + return true; } public boolean canAddUpgrade (@Nonnull ItemStack upgrade) { @@ -101,6 +115,11 @@ public boolean canAddUpgrade (@Nonnull ItemStack upgrade) { return true; } + public boolean canRemoveUpgrade (int slot) { + slot = MathHelper.clamp(slot, 0, upgrades.length - 1); + return !upgrades[slot].isEmpty(); + } + public int getStorageMultiplier () { return storageMultiplier; } diff --git a/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java b/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java index 640dedbdd..e56c94571 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java +++ b/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java @@ -123,7 +123,7 @@ private void renderFastItemSet (ChamRender renderer, TileEntityDrawers tile, IBl renderFastItem(renderer, renderStacks[i], tile, state, i, side, depth, partialTickTime); } - if (tile.isShowingQuantity()) { + if (tile.getDrawerAttributes().isShowingQuantity()) { EntityPlayerSP player = Minecraft.getMinecraft().player; BlockPos blockPos = tile.getPos().add(.5, .5, .5); double distance = Math.sqrt(blockPos.distanceSq(player.getPosition())); diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryUpgrade.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryUpgrade.java index 9c7fb412c..900daf4f6 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryUpgrade.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryUpgrade.java @@ -105,10 +105,7 @@ public void closeInventory (EntityPlayer player) { } @Override public boolean isItemValidForSlot (int slot, @Nonnull ItemStack item) { - if (item.getItem() == ModItems.upgradeOneStack) - return tile.canAddOneStackUpgrade(); - - return item.getItem() instanceof ItemUpgrade; + return tile.upgrades().canAddUpgrade(item); } @Override @@ -131,10 +128,6 @@ public void clear () { } - public boolean canAddOneStackUpgrade () { - return tile.canAddOneStackUpgrade(); - } - public boolean canAddUpgrade (@Nonnull ItemStack item) { return tile.upgrades().canAddUpgrade(item); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/SlotUpgrade.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/SlotUpgrade.java index df36c2928..57fb7ad25 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/SlotUpgrade.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/SlotUpgrade.java @@ -21,15 +21,8 @@ public boolean isItemValid(@Nonnull ItemStack stack) { if (stack.isEmpty()) return false; - if (inventory instanceof InventoryUpgrade) { - if (!((InventoryUpgrade)inventory).canAddUpgrade(stack)) - return false; - - if (stack.getItem() == ModItems.upgradeOneStack) - return ((InventoryUpgrade) inventory).canAddOneStackUpgrade(); - - return true; - } + if (inventory instanceof InventoryUpgrade) + return ((InventoryUpgrade) inventory).canAddUpgrade(stack); return false; } From 2f566dbcfefe71041c9d30cc907020a85a9f30db Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sat, 24 Jun 2017 19:27:22 -0400 Subject: [PATCH 06/22] lambdas --- .../storagedrawers/block/tile/TileEntityDrawers.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index 772276067..22cf60982 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -811,12 +811,7 @@ public void clientUpdateCount (final int slot, final int count) { if (!getWorld().isRemote) return; - Minecraft.getMinecraft().addScheduledTask(new Runnable() { - @Override - public void run () { - TileEntityDrawers.this.clientUpdateCountAsync(slot, count); - } - }); + Minecraft.getMinecraft().addScheduledTask(() -> TileEntityDrawers.this.clientUpdateCountAsync(slot, count)); } @SideOnly(Side.CLIENT) From ec744385b202ff6b703054cc64ffe446f78e61c1 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sun, 25 Jun 2017 01:43:08 -0400 Subject: [PATCH 07/22] More separation --- .../api/storage/IDrawerAttributes.java | 8 +++++ .../storage/IDrawerAttributesModifiable.java | 8 +++++ .../storagedrawers/block/BlockDrawers.java | 2 +- .../block/tile/TileEntityDrawers.java | 26 ++------------ .../block/tile/TileEntityDrawersComp.java | 16 +++++---- .../block/tile/TileEntityDrawersStandard.java | 4 +-- .../block/tile/tiledata/UpgradeData.java | 21 ++++++++++- .../capabilities/BasicDrawerAttributes.java | 36 +++++++++++++++++++ .../CapabilityDrawerAttributes.java | 4 +++ .../storagedrawers/integration/Waila.java | 2 +- .../inventory/InventoryUpgrade.java | 28 ++------------- .../storagedrawers/inventory/SlotUpgrade.java | 2 +- .../storage/DefaultStorageProvider.java | 4 +-- .../storagedrawers/storage/DrawerData.java | 12 +++---- .../storage/IStorageProvider.java | 4 +-- 15 files changed, 105 insertions(+), 72 deletions(-) diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerAttributes.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerAttributes.java index 6ce00f71b..4ee09e9f9 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerAttributes.java +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerAttributes.java @@ -49,4 +49,12 @@ default boolean isShowingQuantity () { default boolean isVoid () { return false; } + + default boolean isUnlimitedStorage () { + return false; + } + + default boolean isUnlimitedVending () { + return false; + } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerAttributesModifiable.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerAttributesModifiable.java index 918388624..0e5c0768e 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerAttributesModifiable.java +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerAttributesModifiable.java @@ -40,4 +40,12 @@ default boolean setIsSealed (boolean state) { default boolean setIsVoid (boolean state) { return false; } + + default boolean setIsUnlimitedStorage (boolean state) { + return false; + } + + default boolean setIsUnlimitedVending (boolean state) { + return false; + } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java index cf4c59b7f..5382fc8b5 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java @@ -486,7 +486,7 @@ public void breakBlock (World world, BlockPos pos, IBlockState state) { } } - if (!tile.isVending()) + if (!tile.getDrawerAttributes().isUnlimitedVending()) DrawerInventoryHelper.dropInventoryItems(world, pos, tile); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index 22cf60982..62956b607 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -44,6 +44,8 @@ public abstract class TileEntityDrawers extends ChamLockableTileEntity implement private LockableData lockData = new LockableData(); private CustomNameData customNameData = new CustomNameData("storagedrawers.container.drawers"); private MaterialData materialData = new MaterialData(); + private UpgradeData upgradeData = new DrawerUpgradeData(); + public final ControllerData controllerData = new ControllerData(); private IDrawer[] drawers; @@ -56,7 +58,6 @@ public abstract class TileEntityDrawers extends ChamLockableTileEntity implement private String securityKey; private IDrawerAttributesModifiable drawerAttributes; - private UpgradeData upgradeData; private long lastClickTime; private UUID lastClickUUID; @@ -143,11 +144,11 @@ private boolean stackCapacityCheck (int stackCapacity) { protected TileEntityDrawers (int drawerCount) { drawerAttributes = new DrawerAttributes(); - upgradeData = new DrawerUpgradeData(); upgradeData.setDrawerAttributes(drawerAttributes); injectData(lockData); injectPortableData(customNameData); + injectPortableData(upgradeData); injectPortableData(materialData); injectData(controllerData); @@ -298,20 +299,6 @@ public boolean setIsSealed (boolean sealed) { return true; } - public boolean isUnlimited () { - if (!StorageDrawers.config.cache.enableCreativeUpgrades) - return false; - - return upgradeData.hasUnlimitedUpgrade(); - } - - public boolean isVending () { - if (!StorageDrawers.config.cache.enableCreativeUpgrades) - return false; - - return upgradeData.hasVendingUpgrade(); - } - public boolean isRedstone () { if (!StorageDrawers.config.cache.enableRedstoneUpgrades) return false; @@ -538,9 +525,6 @@ public void readFromPortableNBT (NBTTagCompound tag) { drawerCapacity = tag.getInteger("Cap"); - if (tag.hasKey("Upgrades")) - upgradeData.deserializeNBT(tag.getTagList("Upgrades", Constants.NBT.TAG_COMPOUND)); - drawerAttributes.setItemLocked(LockAttribute.LOCK_EMPTY, false); drawerAttributes.setItemLocked(LockAttribute.LOCK_POPULATED, false); if (tag.hasKey("Lock")) { @@ -589,10 +573,6 @@ public NBTTagCompound writeToPortableNBT (NBTTagCompound tag) { if (material != null) tag.setString("Mat", material); - NBTTagList upgradeList = upgradeData.serializeNBT(); - if (upgradeList.tagCount() > 0) - tag.setTag("Upgrades", upgradeList); - EnumSet attrs = EnumSet.noneOf(LockAttribute.class); if (drawerAttributes.isItemLocked(LockAttribute.LOCK_EMPTY)) attrs.add(LockAttribute.LOCK_EMPTY); diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java index e97ecfe36..1cd513821 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java @@ -2,6 +2,7 @@ import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; import com.jaquadro.minecraft.storagedrawers.config.CompTierRegistry; import com.jaquadro.minecraft.storagedrawers.config.ConfigManager; @@ -510,7 +511,7 @@ public int getStoredItemCount (int slot) { if (convRate == null || convRate[slot] == 0) return 0; - if (TileEntityDrawersComp.this.isVending()) + if (TileEntityDrawersComp.this.getDrawerAttributes().isUnlimitedVending()) return Integer.MAX_VALUE; return pooledCount / convRate[slot]; @@ -521,7 +522,7 @@ public void setStoredItemCount (int slot, int amount) { if (convRate == null || convRate[slot] == 0) return; - if (TileEntityDrawersComp.this.isVending()) + if (TileEntityDrawersComp.this.getDrawerAttributes().isUnlimitedVending()) return; int oldCount = pooledCount; @@ -552,7 +553,8 @@ public int getMaxCapacity (int slot) { if (protoStack[0].isEmpty() || protoStack[slot].isEmpty() || convRate == null || convRate[slot] == 0) return 0; - if (TileEntityDrawersComp.this.isUnlimited() || TileEntityDrawersComp.this.isVending()) { + IDrawerAttributes attrs = TileEntityDrawersComp.this.getDrawerAttributes(); + if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) { if (convRate == null || protoStack[slot].isEmpty() || convRate[slot] == 0) return Integer.MAX_VALUE; return Integer.MAX_VALUE / convRate[slot]; @@ -566,7 +568,8 @@ public int getMaxCapacity (int slot, @Nonnull ItemStack itemPrototype) { if (itemPrototype.isEmpty()) return 0; - if (TileEntityDrawersComp.this.isUnlimited() || TileEntityDrawersComp.this.isVending()) { + IDrawerAttributes attrs = TileEntityDrawersComp.this.getDrawerAttributes(); + if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) { if (convRate == null || protoStack[slot].isEmpty() || convRate[slot] == 0) return Integer.MAX_VALUE; return Integer.MAX_VALUE / convRate[slot]; @@ -586,7 +589,8 @@ public int getDefaultMaxCapacity (int slot) { if (!isDrawerEnabled(slot)) return 0; - if (TileEntityDrawersComp.this.isUnlimited() || TileEntityDrawersComp.this.isVending()) + IDrawerAttributes attrs = TileEntityDrawersComp.this.getDrawerAttributes(); + if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) return Integer.MAX_VALUE; return 64 * getBaseStackCapacity(); @@ -596,7 +600,7 @@ public int getDefaultMaxCapacity (int slot) { public int getRemainingCapacity (int slot) { if (protoStack[0].isEmpty() || protoStack[slot].isEmpty() || convRate == null || convRate[slot] == 0) return 0; - if (TileEntityDrawersComp.this.isVending()) + if (TileEntityDrawersComp.this.getDrawerAttributes().isUnlimitedVending()) return Integer.MAX_VALUE; int rawMaxCapacity = protoStack[0].getItem().getItemStackLimit(protoStack[0]) * getStackCapacity(0) * convRate[0]; diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java index 65fc446e1..cf896dfe6 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java @@ -149,7 +149,7 @@ public boolean isShowingQuantity (int slot) { @Override public boolean setIsShowingQuantity (int slot, boolean state) { return TileEntityDrawersStandard.this.setIsShowingQuantity(state); - }*/ + } @Override public boolean isStorageUnlimited (int slot) { @@ -159,7 +159,7 @@ public boolean isStorageUnlimited (int slot) { @Override public boolean isVendingUnlimited (int slot) { return TileEntityDrawersStandard.this.isVending(); - } + }*/ @Override public boolean isRedstone (int slot) { diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java index 829512a45..ceae409eb 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java @@ -1,5 +1,6 @@ package com.jaquadro.minecraft.storagedrawers.block.tile.tiledata; +import com.jaquadro.minecraft.chameleon.block.tiledata.TileDataShim; import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributesModifiable; import com.jaquadro.minecraft.storagedrawers.config.ConfigManager; @@ -10,11 +11,12 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.util.math.MathHelper; +import net.minecraftforge.common.util.Constants; import net.minecraftforge.common.util.INBTSerializable; import javax.annotation.Nonnull; -public class UpgradeData implements INBTSerializable +public class UpgradeData extends TileDataShim implements INBTSerializable { private final ItemStack[] upgrades; private int storageMultiplier; @@ -182,6 +184,8 @@ else if (type == EnumUpgradeCreative.VENDING) } attrs.setIsVoid(hasVoid); + attrs.setIsUnlimitedStorage(hasUnlimited); + attrs.setIsUnlimitedVending(hasVending); } private void syncStorageMultiplier () { @@ -246,5 +250,20 @@ public void deserializeNBT (NBTTagList tagList) { } } + @Override + public void readFromNBT (NBTTagCompound tag) { + if (tag.hasKey("Upgrades")) + deserializeNBT(tag.getTagList("Upgrades", Constants.NBT.TAG_COMPOUND)); + } + + @Override + public NBTTagCompound writeToNBT (NBTTagCompound tag) { + NBTTagList upgradeList = serializeNBT(); + if (upgradeList.tagCount() > 0) + tag.setTag("Upgrades", upgradeList); + + return tag; + } + protected void onUpgradeChanged (ItemStack oldUpgrade, ItemStack newUpgrade) { } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/BasicDrawerAttributes.java b/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/BasicDrawerAttributes.java index 4fa26bc56..e440906db 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/BasicDrawerAttributes.java +++ b/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/BasicDrawerAttributes.java @@ -14,6 +14,8 @@ public class BasicDrawerAttributes implements IDrawerAttributes, IDrawerAttribut private boolean isConcealed; private boolean isShowingQuantity; private boolean isVoid; + private boolean isUnlimitedStorage; + private boolean isUnlimitedVending; @Override public boolean canItemLock (LockAttribute attr) { @@ -84,6 +86,36 @@ public boolean setIsShowingQuantity (boolean state) { return true; } + @Override + public boolean isUnlimitedStorage () { + return isUnlimitedStorage; + } + + @Override + public boolean setIsUnlimitedStorage (boolean state) { + if (isUnlimitedStorage != state) { + isUnlimitedStorage = state; + onAttributeChanged(); + } + + return true; + } + + @Override + public boolean isUnlimitedVending () { + return isUnlimitedVending; + } + + @Override + public boolean setIsUnlimitedVending (boolean state) { + if (isUnlimitedVending != state) { + isUnlimitedVending = state; + onAttributeChanged(); + } + + return true; + } + @Override public NBTTagCompound serializeNBT () { NBTTagCompound tag = new NBTTagCompound(); @@ -92,6 +124,8 @@ public NBTTagCompound serializeNBT () { tag.setBoolean("concealed", isConcealed); tag.setBoolean("void", isVoid); tag.setBoolean("quant", isShowingQuantity); + tag.setBoolean("unlimited", isUnlimitedStorage); + tag.setBoolean("vending", isUnlimitedVending); return tag; } @@ -102,6 +136,8 @@ public void deserializeNBT (NBTTagCompound nbt) { isConcealed = nbt.getBoolean("concealed"); isVoid = nbt.getBoolean("void"); isShowingQuantity = nbt.getBoolean("quant"); + isUnlimitedStorage = nbt.getBoolean("unlimited"); + isUnlimitedVending = nbt.getBoolean("vending"); } protected void onAttributeChanged () { } diff --git a/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/CapabilityDrawerAttributes.java b/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/CapabilityDrawerAttributes.java index de3406da7..a3bc3e46a 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/CapabilityDrawerAttributes.java +++ b/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/CapabilityDrawerAttributes.java @@ -36,6 +36,8 @@ public NBTBase writeNBT (Capability capability, IDrawerAttrib tag.setBoolean("concealed", instance.isConcealed()); tag.setBoolean("void", instance.isVoid()); tag.setBoolean("quant", instance.isShowingQuantity()); + tag.setBoolean("unlimited", instance.isUnlimitedStorage()); + tag.setBoolean("vending", instance.isUnlimitedVending()); return tag; } @@ -59,6 +61,8 @@ public void readNBT (Capability capability, IDrawerAttributes modifiable.setIsConcealed(tag.getBoolean("concealed")); modifiable.setIsVoid(tag.getBoolean("void")); modifiable.setIsShowingQuantity(tag.getBoolean("quant")); + modifiable.setIsUnlimitedStorage(tag.getBoolean("unlimited")); + modifiable.setIsUnlimitedVending(tag.getBoolean("vending")); } } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java b/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java index ea26e5acf..da6785889 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java +++ b/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java @@ -140,7 +140,7 @@ else if (stacks > 0) } if (config.getConfig("display.stacklimit")) { - if (tile.isUnlimited() || tile.isVending()) + if (tile.getDrawerAttributes().isUnlimitedStorage() || tile.getDrawerAttributes().isUnlimitedVending()) currenttip.add(I18n.format("storagedrawers.waila.nolimit")); else { int multiplier = tile.upgrades().getStorageMultiplier(); diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryUpgrade.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryUpgrade.java index 900daf4f6..7b51be257 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryUpgrade.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryUpgrade.java @@ -61,9 +61,6 @@ public ItemStack removeStackFromSlot (int slot) { @Override public void setInventorySlotContents (int slot, @Nonnull ItemStack item) { - //if (item != null && item.stackSize > getInventoryStackLimit()) - // item.stackSize = getInventoryStackLimit(); - tile.upgrades().setUpgrade(slot, item); } @@ -132,28 +129,7 @@ public boolean canAddUpgrade (@Nonnull ItemStack item) { return tile.upgrades().canAddUpgrade(item); } - public boolean canRemoveStorageUpgrade (int storageLevel) { - return canRemoveStorageUpgrade(tile, storageLevel); - } - - private boolean canRemoveStorageUpgrade (TileEntityDrawers tile, int storageLevel) { - int storageMult = StorageDrawers.config.getStorageUpgradeMultiplier(storageLevel); - int effectiveStorageMult = tile.upgrades().getStorageMultiplier(); - if (effectiveStorageMult == storageMult) - storageMult--; - - int addedStackCapacity = storageMult * tile.getEffectiveDrawerCapacity(); - - for (int i = 0; i < tile.getDrawerCount(); i++) { - IDrawer drawer = tile.getDrawerIfEnabled(i); - if (drawer == null || drawer.isEmpty()) - continue; - - int addedItemCapacity = addedStackCapacity * drawer.getStoredItemStackSize(); - if (drawer.getMaxCapacity() - addedItemCapacity < drawer.getStoredItemCount()) - return false; - } - - return true; + public boolean canRemoveStorageUpgrade (int slot) { + return tile.upgrades().canRemoveUpgrade(slot); } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/SlotUpgrade.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/SlotUpgrade.java index 57fb7ad25..d01810b30 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/SlotUpgrade.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/SlotUpgrade.java @@ -33,7 +33,7 @@ public boolean canTakeStack (EntityPlayer player) { ItemStack stack = getStack(); if (stack.getItem() == ModItems.upgradeStorage) { EnumUpgradeStorage upgrade = EnumUpgradeStorage.byMetadata(stack.getMetadata()); - return ((InventoryUpgrade) inventory).canRemoveStorageUpgrade(upgrade.getLevel()); + return ((InventoryUpgrade) inventory).canRemoveStorageUpgrade(getSlotIndex()); } if (player != null) { diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/DefaultStorageProvider.java b/src/com/jaquadro/minecraft/storagedrawers/storage/DefaultStorageProvider.java index c0383ad96..a33537636 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/DefaultStorageProvider.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/DefaultStorageProvider.java @@ -64,7 +64,7 @@ public boolean isShowingQuantity (int slot) { @Override public boolean setIsShowingQuantity (int slot, boolean state) { return false; - }*/ + } @Override public boolean isStorageUnlimited (int slot) { @@ -74,7 +74,7 @@ public boolean isStorageUnlimited (int slot) { @Override public boolean isVendingUnlimited (int slot) { return false; - } + }*/ @Override public boolean isRedstone (int slot) { diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java index d1f8708e5..897f68148 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java @@ -31,7 +31,6 @@ public class DrawerData extends BaseDrawerData private ItemStack protoStack; private int count; - private boolean isUnlimited; private int stackCapacity; public DrawerData (IStorageProvider provider, ICapabilityProvider capProvider, int slot) { @@ -91,7 +90,7 @@ private void setStoredItem (@Nonnull ItemStack itemPrototype, int amount, boolea @Override public int getStoredItemCount () { - if (!protoStack.isEmpty() && storageProvider.isVendingUnlimited(slot)) + if (!protoStack.isEmpty() && attrs.isUnlimitedVending()) return Integer.MAX_VALUE; return count; @@ -103,7 +102,7 @@ public void setStoredItemCount (int amount) { } public void setStoredItemCount (int amount, boolean mark, boolean clearOnEmpty) { - if (protoStack.isEmpty() || storageProvider.isVendingUnlimited(slot)) + if (protoStack.isEmpty() || attrs.isUnlimitedVending()) return; count = amount; @@ -132,7 +131,7 @@ public int getMaxCapacity (@Nonnull ItemStack itemPrototype) { if (itemPrototype.isEmpty()) return 0; - if (isUnlimited) + if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) return Integer.MAX_VALUE; return itemPrototype.getItem().getItemStackLimit(itemPrototype) * stackCapacity; @@ -140,7 +139,7 @@ public int getMaxCapacity (@Nonnull ItemStack itemPrototype) { @Override public int getDefaultMaxCapacity () { - if (isUnlimited) + if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) return Integer.MAX_VALUE; return 64 * stackCapacity; @@ -151,7 +150,7 @@ public int getRemainingCapacity () { if (protoStack.isEmpty()) return 0; - if (storageProvider.isVendingUnlimited(slot)) + if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) return Integer.MAX_VALUE; return getMaxCapacity() - getStoredItemCount(); @@ -200,7 +199,6 @@ public void attributeChanged () { } private void updateAttributeCache () { - isUnlimited = storageProvider.isStorageUnlimited(slot) || storageProvider.isVendingUnlimited(slot); stackCapacity = storageProvider.getSlotStackCapacity(slot); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/IStorageProvider.java b/src/com/jaquadro/minecraft/storagedrawers/storage/IStorageProvider.java index a7f85e7e8..2b86a22e3 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/IStorageProvider.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/IStorageProvider.java @@ -24,9 +24,9 @@ public interface IStorageProvider //boolean setIsShowingQuantity (int slot, boolean state); - boolean isStorageUnlimited (int slot); + //boolean isStorageUnlimited (int slot); - boolean isVendingUnlimited (int slot); + //boolean isVendingUnlimited (int slot); boolean isRedstone (int slot); From 6830858b4316730c986193770a930133b9026c5d Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sun, 25 Jun 2017 02:02:42 -0400 Subject: [PATCH 08/22] Deprecation shit --- .../storagedrawers/StorageDrawers.java | 3 ++ .../storagedrawers/block/BlockDrawers.java | 32 +++++++++++-------- .../block/BlockStandardDrawers.java | 1 + .../block/BlockVariantDrawers.java | 1 + .../block/tile/TileEntityDrawersComp.java | 26 +++++++-------- .../storagedrawers/integration/Waila.java | 4 ++- 6 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java index 167b5a784..bf1934b93 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java @@ -21,6 +21,7 @@ import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper; import net.minecraftforge.fml.relauncher.Side; +import org.apache.logging.log4j.Logger; import java.io.File; @@ -37,6 +38,7 @@ public class StorageDrawers public static final Api api = new Api(); + public static Logger log; public static SimpleNetworkWrapper network; public static ConfigManager config; public static CompTierRegistry compRegistry; @@ -54,6 +56,7 @@ public class StorageDrawers @Mod.EventHandler public void preInit (FMLPreInitializationEvent event) { + log = event.getModLog(); config = new ConfigManager(new File(event.getModConfigurationDirectory(), MOD_ID + ".cfg")); network = NetworkRegistry.INSTANCE.newSimpleChannel(MOD_ID); diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java index 5382fc8b5..085996bc2 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java @@ -20,6 +20,7 @@ import net.minecraft.block.*; import net.minecraft.block.material.Material; import net.minecraft.block.properties.*; +import net.minecraft.block.state.BlockFaceShape; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; import net.minecraft.client.particle.ParticleManager; @@ -231,8 +232,8 @@ public boolean onBlockActivated (World world, BlockPos pos, IBlockState state, E return false; if (StorageDrawers.config.cache.debugTrace) { - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, "BlockDrawers.onBlockActivated"); - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, (item.isEmpty()) ? " null item" : " " + item.toString()); + StorageDrawers.log.info("BlockDrawers.onBlockActivated"); + StorageDrawers.log.info((item.isEmpty()) ? " null item" : " " + item.toString()); } if (!item.isEmpty()) { @@ -351,7 +352,7 @@ public void onBlockClicked(World worldIn, BlockPos pos, EntityPlayer playerIn) { } if (StorageDrawers.config.cache.debugTrace) - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, "onBlockClicked"); + StorageDrawers.log.info("onBlockClicked"); RayTraceResult rayResult = net.minecraftforge.common.ForgeHooks.rayTraceEyes(playerIn, ((EntityPlayerMP) playerIn).interactionManager.getBlockReachDistance() + 1); if (rayResult == null) @@ -392,7 +393,7 @@ public void onBlockClicked(World worldIn, BlockPos pos, EntityPlayer playerIn) { item = tileDrawers.takeItemsFromSlot(slot, 1); if (StorageDrawers.config.cache.debugTrace) - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, (item.isEmpty()) ? " null item" : " " + item.toString()); + StorageDrawers.log.info((item.isEmpty()) ? " null item" : " " + item.toString()); IBlockState state = worldIn.getBlockState(pos); if (!item.isEmpty()) { @@ -422,21 +423,28 @@ public boolean rotateBlock (World world, BlockPos pos, EnumFacing axis) { } @Override - public boolean isSideSolid (IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos, EnumFacing side) { + @SuppressWarnings("deprecation") + public BlockFaceShape getBlockFaceShape (IBlockAccess world, IBlockState state, BlockPos pos, EnumFacing side) { TileEntityDrawers tile = getTileEntity(world, pos); if (tile == null) - return true; + return BlockFaceShape.SOLID; if (isHalfDepth(state)) - return side.getOpposite().ordinal() == tile.getDirection(); + return side.getOpposite().ordinal() == tile.getDirection() ? BlockFaceShape.SOLID : BlockFaceShape.UNDEFINED; if (side == EnumFacing.DOWN) { Block blockUnder = world.getBlockState(pos.down()).getBlock(); if (blockUnder instanceof BlockChest || blockUnder instanceof BlockEnderChest) - return false; + return BlockFaceShape.UNDEFINED; } - return side.ordinal() != tile.getDirection(); + return side.ordinal() != tile.getDirection() ? BlockFaceShape.SOLID : BlockFaceShape.BOWL; + } + + @Override + @SuppressWarnings("deprecation") + public boolean isSideSolid (IBlockState state, @Nonnull IBlockAccess world, @Nonnull BlockPos pos, EnumFacing side) { + return getBlockFaceShape(world, state, pos, side) == BlockFaceShape.SOLID; } private void dropItemStack (World world, BlockPos pos, EntityPlayer player, @Nonnull ItemStack stack) { @@ -494,12 +502,8 @@ public void breakBlock (World world, BlockPos pos, IBlockState state) { } @Override - @Nonnull - public List getDrops (IBlockAccess world, BlockPos pos, IBlockState state, int fortune) { - List drops = new ArrayList(); + public void getDrops (NonNullList drops, IBlockAccess world, BlockPos pos, IBlockState state, int fortune) { drops.add(getMainDrop(world, pos, state)); - - return drops; } protected ItemStack getMainDrop (IBlockAccess world, BlockPos pos, IBlockState state) { diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockStandardDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockStandardDrawers.java index 29dbceb4a..5165c0071 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockStandardDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockStandardDrawers.java @@ -146,6 +146,7 @@ public boolean shouldSideBeRendered (IBlockState blockState, IBlockAccess blockA } @Override + @SuppressWarnings("deprecation") public boolean causesSuffocation (IBlockState state) { return false; } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockVariantDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockVariantDrawers.java index d5523748b..bc85a9c2e 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockVariantDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockVariantDrawers.java @@ -98,6 +98,7 @@ public boolean retrimBlock (World world, BlockPos pos, ItemStack prototype) { Block protoBlock = Block.getBlockFromItem(prototype.getItem()); int protoMeta = prototype.getItemDamage(); + @SuppressWarnings("deprecation") IBlockState newState = protoBlock.getStateFromMeta(protoMeta); if (newState == null || !(newState.getBlock() instanceof BlockTrim)) return false; diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java index 1cd513821..29126d521 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java @@ -218,13 +218,13 @@ private void populateSlots (@Nonnull ItemStack stack) { ItemStack uTier1 = findHigherTier(stack); if (!uTier1.isEmpty()) { if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, "Picked candidate " + uTier1.toString() + " with conv=" + lookupSizeResult); + StorageDrawers.log.info("Picked candidate " + uTier1.toString() + " with conv=" + lookupSizeResult); int uCount1 = lookupSizeResult; ItemStack uTier2 = findHigherTier(uTier1); if (!uTier2.isEmpty()) { if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, "Picked candidate " + uTier2.toString() + " with conv=" + lookupSizeResult); + StorageDrawers.log.info("Picked candidate " + uTier2.toString() + " with conv=" + lookupSizeResult); populateSlot(index++, uTier2, lookupSizeResult * uCount1); } @@ -240,7 +240,7 @@ private void populateSlots (@Nonnull ItemStack stack) { ItemStack lTier1 = findLowerTier(stack); if (!lTier1.isEmpty()) { if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, "Picked candidate " + lTier1.toString() + " with conv=" + lookupSizeResult); + StorageDrawers.log.info("Picked candidate " + lTier1.toString() + " with conv=" + lookupSizeResult); populateSlot(index++, lTier1, 1); for (int i = 0; i < index - 1; i++) @@ -253,7 +253,7 @@ private void populateSlots (@Nonnull ItemStack stack) { ItemStack lTier2 = findLowerTier(lTier1); if (!lTier2.isEmpty()) { if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, "Picked candidate " + lTier2.toString() + " with conv=" + lookupSizeResult); + StorageDrawers.log.info("Picked candidate " + lTier2.toString() + " with conv=" + lookupSizeResult); populateSlot(index++, lTier2, 1); for (int i = 0; i < index - 1; i++) @@ -273,12 +273,12 @@ private void populateSlot (int slot, @Nonnull ItemStack stack, int conversion) { @Nonnull private ItemStack findHigherTier (@Nonnull ItemStack stack) { if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, "Finding ascending candidates for " + stack.toString()); + StorageDrawers.log.info("Finding ascending candidates for " + stack.toString()); CompTierRegistry.Record record = StorageDrawers.compRegistry.findHigherTier(stack); if (record != null) { if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, "Found " + record.upper.toString() + " in registry with conv=" + record.convRate); + StorageDrawers.log.info("Found " + record.upper.toString() + " in registry with conv=" + record.convRate); lookupSizeResult = record.convRate; return record.upper; @@ -310,7 +310,7 @@ private ItemStack findHigherTier (@Nonnull ItemStack stack) { candidates.add(match); if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, "Found ascending candidate for " + stack.toString() + ": " + match.toString() + " size=" + lookupSizeResult + ", inverse=" + comp.toString()); + StorageDrawers.log.info("Found ascending candidate for " + stack.toString() + ": " + match.toString() + " size=" + lookupSizeResult + ", inverse=" + comp.toString()); break; } @@ -327,7 +327,7 @@ private ItemStack findHigherTier (@Nonnull ItemStack stack) { return candidates.get(0); if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, "No candidates found"); + StorageDrawers.log.info("No candidates found"); return ItemStack.EMPTY; } @@ -350,12 +350,12 @@ private List findAllMatchingRecipes (InventoryCrafting crafting) { @Nonnull private ItemStack findLowerTier (@Nonnull ItemStack stack) { if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, "Finding descending candidates for " + stack.toString()); + StorageDrawers.log.info("Finding descending candidates for " + stack.toString()); CompTierRegistry.Record record = StorageDrawers.compRegistry.findLowerTier(stack); if (record != null) { if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, "Found " + record.lower.toString() + " in registry with conv=" + record.convRate); + StorageDrawers.log.info("Found " + record.lower.toString() + " in registry with conv=" + record.convRate); lookupSizeResult = record.convRate; return record.lower; @@ -381,9 +381,9 @@ private ItemStack findLowerTier (@Nonnull ItemStack stack) { candidatesRate.put(match, lookupSizeResult); if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, "Found descending candidate for " + stack.toString() + ": " + match.toString() + " size=" + lookupSizeResult + ", inverse=" + comp.toString()); + StorageDrawers.log.info("Found descending candidate for " + stack.toString() + ": " + match.toString() + " size=" + lookupSizeResult + ", inverse=" + comp.toString()); } else if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, "Back-check failed for " + match.toString() + " size=" + lookupSizeResult + ", inverse=" + comp.toString()); + StorageDrawers.log.info("Back-check failed for " + match.toString() + " size=" + lookupSizeResult + ", inverse=" + comp.toString()); } } } @@ -402,7 +402,7 @@ private ItemStack findLowerTier (@Nonnull ItemStack stack) { } if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, "No candidates found"); + StorageDrawers.log.info("No candidates found"); return ItemStack.EMPTY; } diff --git a/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java b/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java index da6785889..8e4a4b450 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java +++ b/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java @@ -21,6 +21,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.common.capabilities.Capability; @@ -84,7 +85,8 @@ public static class WailaDrawer implements IWailaDataProvider @Override @Nonnull public ItemStack getWailaStack (IWailaDataAccessor accessor, IWailaConfigHandler config) { - List drops = accessor.getBlock().getDrops(accessor.getWorld(), accessor.getPosition(), accessor.getBlockState(), 0); + NonNullList drops = NonNullList.create(); + accessor.getBlock().getDrops(drops, accessor.getWorld(), accessor.getPosition(), accessor.getBlockState(), 0); if (drops.size() == 0) return ItemStack.EMPTY; From f104de649eba34d42b64ca3c1577ff9192b97405 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sun, 25 Jun 2017 02:13:10 -0400 Subject: [PATCH 09/22] More own-damn-logging --- .../storagedrawers/block/tile/TileEntityController.java | 8 ++++---- .../storagedrawers/network/CountUpdateMessage.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java index 5a11cb1db..813320895 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java @@ -165,10 +165,10 @@ public TileEntityController () { } public void printDebugInfo () { - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, "Controller at " + pos.toString()); - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, " Range: " + range + " blocks"); - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, " Stored records: " + storage.size() + ", slot list: " + drawerSlots.length); - FMLLog.log(StorageDrawers.MOD_ID, Level.INFO, " Ticks since last update: " + (getWorld().getTotalWorldTime() - lastUpdateTime)); + StorageDrawers.log.info("Controller at " + pos.toString()); + StorageDrawers.log.info(" Range: " + range + " blocks"); + StorageDrawers.log.info(" Stored records: " + storage.size() + ", slot list: " + drawerSlots.length); + StorageDrawers.log.info(" Ticks since last update: " + (getWorld().getTotalWorldTime() - lastUpdateTime)); } @Override diff --git a/src/com/jaquadro/minecraft/storagedrawers/network/CountUpdateMessage.java b/src/com/jaquadro/minecraft/storagedrawers/network/CountUpdateMessage.java index 620b4bb8d..592f5db84 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/network/CountUpdateMessage.java +++ b/src/com/jaquadro/minecraft/storagedrawers/network/CountUpdateMessage.java @@ -47,7 +47,7 @@ public void fromBytes (ByteBuf buf) { } catch (IndexOutOfBoundsException e) { failed = true; - FMLLog.log(StorageDrawers.MOD_ID, Level.ERROR, e, "CountUpdateMessage: Unexpected end of packet.\nMessage: %s", ByteBufUtil.hexDump(buf, 0, buf.writerIndex())); + StorageDrawers.log.error("CountUpdateMessage: Unexpected end of packet.\nMessage: " + ByteBufUtil.hexDump(buf, 0, buf.writerIndex()), e); } } From 7e0901dbfe4969b3a72e72aa9577609b049a71e5 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Mon, 26 Jun 2017 01:56:28 -0400 Subject: [PATCH 10/22] Into the crux of the data model update --- .../storagedrawers/api/storage/IDrawer.java | 62 ++++-- .../block/tile/TileEntityDrawers.java | 47 ++-- .../block/tile/TileEntityDrawersComp.java | 2 - .../block/tile/TileEntityDrawersStandard.java | 78 +++---- .../storage/BaseDrawerData.java | 18 +- .../storagedrawers/storage/DrawerData.java | 203 +++++++++--------- 6 files changed, 197 insertions(+), 213 deletions(-) diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java index 851eb6f45..4296590ac 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java @@ -1,7 +1,6 @@ package com.jaquadro.minecraft.storagedrawers.api.storage; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; import javax.annotation.Nonnull; @@ -18,10 +17,9 @@ public interface IDrawer * 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. * @return The IDrawer actually set with the prototype. Some drawer groups can redirect a set operation to another member. */ - IDrawer setStoredItem (@Nonnull ItemStack itemPrototype, int amount); + IDrawer setStoredItem (@Nonnull ItemStack itemPrototype); /** * Gets the number of items stored in this drawer. @@ -36,34 +34,58 @@ public interface IDrawer */ void setStoredItemCount (int amount); + /** + * Adds or removes a given amount from the number of items stored in this drawer. + * + * @param amount The amount to add (positive) or subtract (negative). + * @return 0 if the full adjustment was committed, or a positive value representing the remainder if the full + * amount couldn't be added or subtracted. + */ + default int adjustStoredItemCount (int amount) { + if (amount > 0) { + int insert = Math.min(amount, getRemainingCapacity()); + setStoredItemCount(getStoredItemCount() + insert); + return amount - insert; + } else if (amount < 0) { + int stored = getStoredItemCount(); + int destroy = Math.min(Math.abs(amount), getStoredItemCount()); + setStoredItemCount(stored - destroy); + return amount + destroy; + } else { + return 0; + } + } + /** * 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 (); + default int getMaxCapacity () { + return getMaxCapacity(getStoredItemPrototype()); + } /** * 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. + * @param itemPrototype The item type to query. Pass the empty stack to get the max capacity for an empty slot. */ int getMaxCapacity (@Nonnull ItemStack itemPrototype); - /** - * Gets the maxmimum number of items that could be stored in this drawer for a standard item stack size - * of 64. - */ - int getDefaultMaxCapacity (); - /** * 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. + * Gets the max stack size of the item type stored in this drawer. */ - int getStoredItemStackSize (); + default int getStoredItemStackSize () { + @Nonnull ItemStack protoStack = getStoredItemPrototype(); + if (protoStack.isEmpty()) + return 0; + + return protoStack.getItem().getItemStackLimit(protoStack); + } /** * Gets whether or not an item of the given type and data can be stored in this drawer. @@ -93,27 +115,31 @@ public interface IDrawer */ boolean isEmpty (); + default boolean isEnabled () { + return true; + } + /** * 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); + // 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 setExtendedData (String key, Object data); /** * Called when a component attribute of a drawer (such as lock or void) changes state. */ - void attributeChanged (); + // void attributeChanged (); - void writeToNBT (NBTTagCompound tag); + // void writeToNBT (NBTTagCompound tag); - void readFromNBT (NBTTagCompound tag); + // void readFromNBT (NBTTagCompound tag); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index 62956b607..8be0b07dd 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -20,9 +20,11 @@ import com.jaquadro.minecraft.storagedrawers.inventory.DrawerItemHandler; import com.jaquadro.minecraft.storagedrawers.item.EnumUpgradeRedstone; import com.jaquadro.minecraft.storagedrawers.item.EnumUpgradeStorage; +import com.jaquadro.minecraft.storagedrawers.network.CountUpdateMessage; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; @@ -31,6 +33,8 @@ import net.minecraft.world.ILockableContainer; import net.minecraft.world.World; import net.minecraftforge.common.util.Constants; +import net.minecraftforge.common.util.INBTSerializable; +import net.minecraftforge.fml.common.network.NetworkRegistry.TargetPoint; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.items.IItemHandler; @@ -66,8 +70,6 @@ private class DrawerAttributes extends BasicDrawerAttributes { @Override protected void onAttributeChanged () { - attributeChanged(); - if (getWorld() != null && !getWorld().isRemote) { markDirty(); markBlockForUpdate(); @@ -118,8 +120,6 @@ public boolean canRemoveUpgrade (int slot) { @Override protected void onUpgradeChanged (ItemStack oldUpgrade, ItemStack newUpgrade) { - attributeChanged(); - if (getWorld() != null && !getWorld().isRemote) { markDirty(); markBlockForUpdate(); @@ -207,16 +207,6 @@ public int getEffectiveDrawerCapacity () { return getDrawerCapacity(); } - protected void attributeChanged () { - for (int i = 0; i < getDrawerCount(); i++) { - IDrawer drawer = getDrawer(i); - if (drawer == null) - continue; - - drawer.attributeChanged(); - } - } - @Override public UUID getOwner () { if (!StorageDrawers.config.cache.enablePersonalUpgrades) @@ -233,8 +223,6 @@ public boolean setOwner (UUID owner) { if ((this.owner != null && !this.owner.equals(owner)) || (owner != null && !owner.equals(this.owner))) { this.owner = owner; - attributeChanged(); - if (getWorld() != null && !getWorld().isRemote) { markDirty(); markBlockForUpdate(); @@ -263,8 +251,6 @@ public boolean setSecurityProvider (ISecurityProvider provider) { if ((newKey != null && !newKey.equals(securityKey)) || (securityKey != null && !securityKey.equals(newKey))) { securityKey = newKey; - attributeChanged(); - if (getWorld() != null && !getWorld().isRemote) { markDirty(); markBlockForUpdate(); @@ -288,8 +274,6 @@ public boolean setIsSealed (boolean sealed) { if (this.taped != sealed) { this.taped = sealed; - attributeChanged(); - if (getWorld() != null && !getWorld().isRemote) { markDirty(); markBlockForUpdate(); @@ -428,7 +412,7 @@ public int putItemsIntoSlot (int slot, @Nonnull ItemStack stack, int count) { IDrawer drawer = drawers[slot]; if (drawer.isEmpty()) - drawer.setStoredItem(stack, 0); + drawer.setStoredItem(stack); if (!drawer.canItemBeStored(stack)) return 0; @@ -558,10 +542,9 @@ public void readFromPortableNBT (NBTTagCompound tag) { for (int i = 0, n = drawers.length; i < n; i++) { NBTTagCompound slot = slots.getCompoundTagAt(i); drawers[i] = createDrawer(i); - drawers[i].readFromNBT(slot); + if (drawers[i] instanceof INBTSerializable) + ((INBTSerializable)drawers[i]).deserializeNBT(slot); } - - attributeChanged(); } @Override @@ -597,9 +580,10 @@ public NBTTagCompound writeToPortableNBT (NBTTagCompound tag) { NBTTagList slots = new NBTTagList(); for (IDrawer drawer : drawers) { - NBTTagCompound slot = new NBTTagCompound(); - drawer.writeToNBT(slot); - slots.appendTag(slot); + if (drawer instanceof INBTSerializable) + slots.appendTag(((INBTSerializable)drawer).serializeNBT()); + else + slots.appendTag(new NBTTagCompound()); } tag.setTag("Slots", slots); @@ -786,6 +770,15 @@ public boolean markDirtyIfNeeded () { return true; } + protected void syncClientCount (int slot, int count) { + if (getWorld() != null && getWorld().isRemote) + return; + + TargetPoint point = new TargetPoint(getWorld().provider.getDimension(), + getPos().getX(), getPos().getY(), getPos().getZ(), 500); + StorageDrawers.network.sendToAllAround(new CountUpdateMessage(getPos(), slot, count), point); + } + @SideOnly(Side.CLIENT) public void clientUpdateCount (final int slot, final int count) { if (!getWorld().isRemote) diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java index 29126d521..c5a7b6fa7 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java @@ -98,8 +98,6 @@ public int getDrawerCapacity () { if (capacity <= 0) capacity = 1; - - attributeChanged(); } return capacity; diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java index cf896dfe6..461fbb525 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java @@ -1,6 +1,7 @@ package com.jaquadro.minecraft.storagedrawers.block.tile; import com.jaquadro.minecraft.storagedrawers.StorageDrawers; +import com.jaquadro.minecraft.storagedrawers.api.event.DrawerPopulatedEvent; import com.jaquadro.minecraft.storagedrawers.api.storage.EnumBasicDrawer; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; import com.jaquadro.minecraft.storagedrawers.block.BlockStandardDrawers; @@ -9,11 +10,12 @@ import com.jaquadro.minecraft.storagedrawers.inventory.ContainerDrawers2; import com.jaquadro.minecraft.storagedrawers.inventory.ContainerDrawers4; import com.jaquadro.minecraft.storagedrawers.storage.*; -import com.jaquadro.minecraft.storagedrawers.storage.IStorageProvider; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.common.capabilities.ICapabilityProvider; public class TileEntityDrawersStandard extends TileEntityDrawers { @@ -21,8 +23,6 @@ public class TileEntityDrawersStandard extends TileEntityDrawers null, StorageDrawers.MOD_ID + ":basicDrawers1", StorageDrawers.MOD_ID + ":basicDrawers2", null, StorageDrawers.MOD_ID + ":basicDrawers4" }; - private IStorageProvider storageProvider = new StandardStorageProvider(); - private int capacity = 0; public TileEntityDrawersStandard () { @@ -37,15 +37,9 @@ public void setDrawerCount (int count) { initWithDrawerCount(count); } - protected IStorageProvider getStorageProvider () { - if (storageProvider == null) - storageProvider = new StandardStorageProvider(); - return storageProvider; - } - @Override protected IDrawer createDrawer (int slot) { - return new DrawerData(getStorageProvider(), this, slot); + return new StandardDrawerData(this, slot); } @Override @@ -102,68 +96,44 @@ public int getDrawerCapacity () { if (capacity <= 0) capacity = 1; - - attributeChanged(); } return capacity; } - private class StandardStorageProvider extends DefaultStorageProvider + private class StandardDrawerData extends DrawerData { - public StandardStorageProvider () { - super(TileEntityDrawersStandard.this, TileEntityDrawersStandard.this); - } - - @Override - public int getSlotStackCapacity (int slot) { - return upgrades().getStorageMultiplier() * getEffectiveDrawerCapacity(); - } - - /*@Override - public boolean isLocked (int slot, LockAttribute attr) { - return TileEntityDrawersStandard.this.isItemLocked(attr); - } - - @Override - public boolean isVoid (int slot) { - return TileEntityDrawersStandard.this.isVoid(); - } - - @Override - public boolean isShrouded (int slot) { - return TileEntityDrawersStandard.this.isShrouded(); - } + private int slot; - @Override - public boolean setIsShrouded (int slot, boolean state) { - TileEntityDrawersStandard.this.setIsShrouded(state); - return true; + public StandardDrawerData (ICapabilityProvider capProvider, int slot) { + super(capProvider); + this.slot = slot; } @Override - public boolean isShowingQuantity (int slot) { - return TileEntityDrawersStandard.this.isShowingQuantity(); + protected int getStackCapacity () { + return upgrades().getStorageMultiplier() * getEffectiveDrawerCapacity(); } @Override - public boolean setIsShowingQuantity (int slot, boolean state) { - return TileEntityDrawersStandard.this.setIsShowingQuantity(state); - } + protected void onItemChanged () { + DrawerPopulatedEvent event = new DrawerPopulatedEvent(this); + MinecraftForge.EVENT_BUS.post(event); - @Override - public boolean isStorageUnlimited (int slot) { - return TileEntityDrawersStandard.this.isUnlimited(); + if (getWorld() != null && !getWorld().isRemote) { + markDirty(); + markBlockForUpdate(); + } } @Override - public boolean isVendingUnlimited (int slot) { - return TileEntityDrawersStandard.this.isVending(); - }*/ + protected void onAmountChanged () { + syncClientCount(slot, getStoredItemCount()); - @Override - public boolean isRedstone (int slot) { - return TileEntityDrawersStandard.this.isRedstone(); + if (getWorld() != null && !getWorld().isRemote) { + markDirty(); + markBlockForUpdate(); + } } } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java index 5cfb4700f..8e3502919 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java @@ -3,6 +3,8 @@ import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.oredict.OreDictionary; import javax.annotation.Nonnull; @@ -11,19 +13,15 @@ import java.util.List; import java.util.Map; -public abstract class BaseDrawerData implements IDrawer +public abstract class BaseDrawerData implements IDrawer, INBTSerializable { private List oreDictMatches; - private Map auxData; + //private Map auxData; protected BaseDrawerData () { } - protected void postInit () { - - } - protected void reset () { oreDictMatches = null; } @@ -58,7 +56,7 @@ protected void refreshOreDictMatches () { } } - @Override + /*@Override public Object getExtendedData (String key) { if (auxData == null || !auxData.containsKey(key)) return null; @@ -75,11 +73,7 @@ public void setExtendedData (String key, Object data) { } @Override - public void attributeChanged () { } - - protected int getItemCapacityForInventoryStack () { - return getMaxCapacity(); - } + public void attributeChanged () { }*/ public boolean areItemsEqual (@Nonnull ItemStack item) { ItemStack protoStack = getStoredItemPrototype(); diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java index 897f68148..c2c3485cc 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java @@ -21,9 +21,9 @@ public class DrawerData extends BaseDrawerData @CapabilityInject(IDrawerAttributes.class) static Capability ATTR_CAPABILITY = null; - private IStorageProvider storageProvider; - private ICapabilityProvider capProvider; - private int slot; + //private IStorageProvider storageProvider; + //private ICapabilityProvider capProvider; + //private int slot; IDrawerAttributes attrs; @@ -33,20 +33,18 @@ public class DrawerData extends BaseDrawerData private int stackCapacity; - public DrawerData (IStorageProvider provider, ICapabilityProvider capProvider, int slot) { - storageProvider = provider; - this.capProvider = capProvider; + public DrawerData (ICapabilityProvider capProvider) { + //this.capProvider = capProvider; attrs = capProvider.getCapability(ATTR_CAPABILITY, null); if (attrs == null) attrs = new EmptyDrawerAttributes(); protoStack = ItemStack.EMPTY; - this.slot = slot; - updateAttributeCache(); + //updateAttributeCache(); - postInit(); + //postInit(); } @Override @@ -56,41 +54,34 @@ public ItemStack getStoredItemPrototype () { } @Override - public IDrawer setStoredItem (@Nonnull ItemStack itemPrototype, int amount) { - setStoredItem(itemPrototype, amount, true); - return this; - } + public IDrawer setStoredItem (@Nonnull ItemStack itemPrototype) { + if (areItemsEqual(itemPrototype)) { + return this; + } - private void setStoredItem (@Nonnull ItemStack itemPrototype, int amount, boolean mark) { itemPrototype = ItemStackHelper.getItemPrototype(itemPrototype); if (itemPrototype.isEmpty()) { - setStoredItemCount(0, false, true); - protoStack = ItemStack.EMPTY; - - DrawerPopulatedEvent event = new DrawerPopulatedEvent(this); - MinecraftForge.EVENT_BUS.post(event); - - if (mark) - storageProvider.markDirty(slot); - return; + reset(); + return this; } protoStack = itemPrototype; protoStack.setCount(1); + count = 0; - refreshOreDictMatches(); - setStoredItemCount(amount, mark, false); + // TODO: Oredict blah blah + // refreshOreDictMatches(); - DrawerPopulatedEvent event = new DrawerPopulatedEvent(this); - MinecraftForge.EVENT_BUS.post(event); - - if (mark) - storageProvider.markDirty(slot); + onItemChanged(); + return this; } @Override public int getStoredItemCount () { - if (!protoStack.isEmpty() && attrs.isUnlimitedVending()) + if (protoStack.isEmpty()) + return 0; + + if (attrs.isUnlimitedVending()) return Integer.MAX_VALUE; return count; @@ -98,52 +89,65 @@ public int getStoredItemCount () { @Override public void setStoredItemCount (int amount) { - setStoredItemCount(amount, true, true); - } + if (protoStack.isEmpty() || count == amount) + return; - public void setStoredItemCount (int amount, boolean mark, boolean clearOnEmpty) { - if (protoStack.isEmpty() || attrs.isUnlimitedVending()) + if (attrs.isUnlimitedVending()) return; - count = amount; - if (count > getMaxCapacity()) - count = getMaxCapacity(); + count = Math.min(amount, getMaxCapacity()); + count = Math.max(count, 0); if (amount == 0) { - if (clearOnEmpty) { - if (!attrs.isItemLocked(LockAttribute.LOCK_POPULATED)) - reset(); - if (mark) - storageProvider.markDirty(slot); - } - } - else if (mark) - storageProvider.markAmountDirty(slot); + if (!attrs.isItemLocked(LockAttribute.LOCK_POPULATED)) + reset(); + } else + onAmountChanged(); } @Override - public int getMaxCapacity () { - return getMaxCapacity(protoStack); + public int adjustStoredItemCount (int amount) { + if (protoStack.isEmpty() || amount == 0) + return amount; + + if (amount > 0) { + if (attrs.isUnlimitedVending()) + return 0; + + int originalCount = count; + count = Math.min(amount, getMaxCapacity()); + onAmountChanged(); + + if (attrs.isVoid()) + return 0; + + return amount - (count - originalCount); + } else { + int originalCount = count; + setStoredItemCount(originalCount + amount); + + return amount - (count - originalCount); + } } @Override public int getMaxCapacity (@Nonnull ItemStack itemPrototype) { - if (itemPrototype.isEmpty()) - return 0; - if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) return Integer.MAX_VALUE; - return itemPrototype.getItem().getItemStackLimit(itemPrototype) * stackCapacity; + if (itemPrototype.isEmpty()) + return 64 * getStackCapacity(); + + return itemPrototype.getItem().getItemStackLimit(itemPrototype) * getStackCapacity(); } - @Override + /*@Override public int getDefaultMaxCapacity () { if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) return Integer.MAX_VALUE; return 64 * stackCapacity; - } + }*/ @Override public int getRemainingCapacity () { @@ -156,21 +160,13 @@ public int getRemainingCapacity () { return getMaxCapacity() - getStoredItemCount(); } - @Override - public int getStoredItemStackSize () { - if (protoStack.isEmpty()) - return 0; - - return protoStack.getItem().getItemStackLimit(protoStack); - } - - @Override + /*@Override protected int getItemCapacityForInventoryStack () { if (attrs.isVoid()) return Integer.MAX_VALUE; else return getMaxCapacity(); - } + }*/ @Override public boolean canItemBeStored (@Nonnull ItemStack itemPrototype) { @@ -193,53 +189,60 @@ public boolean isEmpty () { return protoStack.isEmpty(); } - @Override + /*@Override public void attributeChanged () { updateAttributeCache(); - } + }*/ - private void updateAttributeCache () { + /*private void updateAttributeCache () { stackCapacity = storageProvider.getSlotStackCapacity(slot); - } + }*/ + + @Override + protected void reset () { + protoStack = ItemStack.EMPTY; + count = 0; - public void writeToNBT (NBTTagCompound tag) { - if (!protoStack.isEmpty()) { - tag.setShort("Item", (short) Item.getIdFromItem(protoStack.getItem())); - tag.setShort("Meta", (short) protoStack.getMetadata()); - tag.setInteger("Count", count); + super.reset(); - if (protoStack.getTagCompound() != null) - tag.setTag("Tags", protoStack.getTagCompound()); - } + onItemChanged(); } - public void readFromNBT (NBTTagCompound tag) { - if (tag.hasKey("Item") && tag.hasKey("Count")) { - Item item = Item.getItemById(tag.getShort("Item")); - if (item != null) { - ItemStack stack = new ItemStack(item); - stack.setItemDamage(tag.getShort("Meta")); - if (tag.hasKey("Tags")) - stack.setTagCompound(tag.getCompoundTag("Tags")); - - setStoredItem(stack, tag.getInteger("Count"), false); - } - else { - reset(); - } - } - else { - reset(); - } + @Override + public NBTTagCompound serializeNBT () { + NBTTagCompound tag = new NBTTagCompound(); + NBTTagCompound item = new NBTTagCompound(); + protoStack.writeToNBT(item); + + tag.setTag("Item", item); + tag.setInteger("Count", count); + + return tag; } @Override - protected void reset () { - protoStack = ItemStack.EMPTY; - super.reset(); + public void deserializeNBT (NBTTagCompound nbt) { + ItemStack tagItem = ItemStack.EMPTY; + int tagCount = 0; - DrawerPopulatedEvent event = new DrawerPopulatedEvent(this); - MinecraftForge.EVENT_BUS.post(event); + if (nbt.hasKey("Item")) + tagItem = new ItemStack(nbt.getCompoundTag("Item")); + if (nbt.hasKey("Count")) + tagCount = nbt.getInteger("Count"); + + setStoredItem(tagItem); + setStoredItemCount(tagCount); } + + protected int getStackCapacity() { + return stackCapacity; + } + + // TODO: Handler should also take care of DrawerPopulatedEvent + // DrawerPopulatedEvent event = new DrawerPopulatedEvent(this); + // MinecraftForge.EVENT_BUS.post(event); + protected void onItemChanged() { } + + protected void onAmountChanged() { } } From 6c7a7425323babe6d1e235d18d8f6b9395ecd574 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Thu, 29 Jun 2017 21:09:33 -0400 Subject: [PATCH 11/22] drawer data work --- .../storage/CompDrawerData.java | 250 ++++++++++++++++++ .../storagedrawers/storage/DrawerData.java | 19 +- 2 files changed, 259 insertions(+), 10 deletions(-) diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/CompDrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/CompDrawerData.java index e2bfe0174..e43e78506 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/CompDrawerData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/CompDrawerData.java @@ -5,6 +5,7 @@ import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.api.storage.IFractionalDrawer; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*; +import com.jaquadro.minecraft.storagedrawers.inventory.ItemStackHelper; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.capabilities.Capability; @@ -13,6 +14,255 @@ import javax.annotation.Nonnull; +class FractionalStorage +{ + @CapabilityInject(IDrawerAttributes.class) + static Capability ATTR_CAPABILITY = null; + + private int slotCount; + private ItemStack[] protoStack; + private int[] convRate; + private int pooledCount; + + IDrawerAttributes attrs; + + public FractionalStorage (ICapabilityProvider capProvider, int slotCount) { + this.slotCount = slotCount; + + protoStack = new ItemStack[slotCount]; + for (int i = 0; i < slotCount; i++) + protoStack[i] = ItemStack.EMPTY; + + convRate = new int[slotCount]; + + attrs = capProvider.getCapability(ATTR_CAPABILITY, null); + if (attrs == null) + attrs = new EmptyDrawerAttributes(); + } + + @Nonnull + public ItemStack getStack (int slot) { + return protoStack[slot]; + } + + public int getConv (int slot) { + return convRate[slot]; + } + + @Nonnull + public ItemStack baseStack () { + return protoStack[0]; + } + + public int baseRate () { + return convRate[0]; + } + + public int setStoredItem (int slot, @Nonnull ItemStack itemPrototype) { + itemPrototype = ItemStackHelper.getItemPrototype(itemPrototype); + if (itemPrototype.isEmpty()) { + reset(); + return slot; + } + + if (baseRate() == 0) { + populateSlots(itemPrototype); + for (int i = 0; i < slotCount; i++) { + if (BaseDrawerData.areItemsEqual(protoStack[i], itemPrototype)) { + slot = i; + pooledCount = 0; + } + } + + // TODO: Refresh all slots + onItemChanged(); + } + + return slot; + } + + public int getStoredCount (int slot) { + if (convRate[slot] == 0) + return 0; + + if (attrs.isUnlimitedVending()) + return Integer.MAX_VALUE; + + return pooledCount / convRate[slot]; + } + + public void setStoredItemCount (int slot, int amount) { + if (convRate[slot] == 0) + return; + + if (attrs.isUnlimitedVending()) + return; + + int oldCount = pooledCount; + + pooledCount = (pooledCount % convRate[slot]) + convRate[slot] * amount; + pooledCount = Math.min(pooledCount, getMaxCapacity(0) * convRate[0]); + pooledCount = Math.max(pooledCount, 0); + + if (pooledCount == oldCount) + return; + + if (pooledCount == 0 && !attrs.isItemLocked(LockAttribute.LOCK_POPULATED)) + reset(); + else + onAmountChanged(); + } + + public int adjustStoredItemCount (int slot, int amount) { + if (convRate[slot] == 0 || amount == 0) + return amount; + + if (amount > 0) { + if (attrs.isUnlimitedVending()) + return 0; + + int poolMax = getMaxCapacity(0) * convRate[0]; + if (poolMax < 0) + poolMax = Integer.MAX_VALUE; + + int canAdd = (poolMax - pooledCount) / convRate[slot]; + int willAdd = Math.min(amount, canAdd); + if (willAdd == 0) + return amount; + + pooledCount += convRate[slot] * willAdd; + + onAmountChanged(); + + if (attrs.isVoid()) + return 0; + + return amount - willAdd; + } + else { + amount = -amount; + + int canRemove = pooledCount / convRate[slot]; + int willRemove = Math.min(amount, canRemove); + if (willRemove == 0) + return amount; + + pooledCount -= willRemove; + + if (pooledCount == 0 && !attrs.isItemLocked(LockAttribute.LOCK_POPULATED)) + reset(); + else + onAmountChanged(); + + return amount - willRemove; + } + } + + public int getMaxCapacity (int slot) { + if (baseStack().isEmpty() || convRate[slot] == 0) + return 0; + + if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) + return Integer.MAX_VALUE / convRate[slot]; + + return baseStack().getItem().getItemStackLimit(baseStack()) * getStackCapacity() * (baseRate() / convRate[slot]); + } + + public int getMaxCapacity (int slot, @Nonnull ItemStack itemPrototype) { + if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) { + if (convRate[slot] == 0) + return Integer.MAX_VALUE; + return Integer.MAX_VALUE / convRate[slot]; + } + + if (baseStack().isEmpty()) { + int itemStackLimit = 64; + if (!itemPrototype.isEmpty()) + itemStackLimit = itemPrototype.getItem().getItemStackLimit(itemPrototype); + return itemStackLimit * getStackCapacity(); + } + + if (BaseDrawerData.areItemsEqual(protoStack[slot], itemPrototype)) + return getMaxCapacity(slot); + + return 0; + } + + public int getRemainingCapacity (int slot) { + if (baseStack().isEmpty() || convRate[slot] == 0) + return 0; + + if (attrs.isUnlimitedVending()) + return Integer.MAX_VALUE; + + int rawMaxCapacity = baseStack().getItem().getItemStackLimit(baseStack()) * getStackCapacity() * baseRate(); + int rawRemaining = rawMaxCapacity - pooledCount; + + return rawRemaining / convRate[slot]; + } + + public boolean isEmpty (int slot) { + return protoStack[slot].isEmpty(); + } + + private void reset () { + pooledCount = 0; + + for (int i = 0; i < slotCount; i++) { + protoStack[i] = ItemStack.EMPTY; + convRate[i] = 0; + } + + // TODO: Refresh all slots + + onItemChanged(); + } + + private void populateSlots(@Nonnull ItemStack stack) { + + } + + protected int getStackCapacity() { + return 0; + } + + protected void onItemChanged() { } + + protected void onAmountChanged() { } +} + +class CDD extends BaseDrawerData +{ + @CapabilityInject(IDrawerAttributes.class) + static Capability ATTR_CAPABILITY = null; + + private FractionalStorage storage; + private int slot; + + IDrawerAttributes attrs; + + public CDD (ICapabilityProvider capProvider, FractionalStorage storage, int slot) { + this.storage = storage; + this.slot = slot; + } + + @Nonnull + @Override + public ItemStack getStoredItemPrototype () { + return storage.getStack(slot); + } + + @Override + public IDrawer setStoredItem (@Nonnull ItemStack itemPrototype) { + return null; + } + + @Override + public int getStoredItemCount () { + return storage.getStoredCount(slot); + } +} + public class CompDrawerData extends BaseDrawerData implements IFractionalDrawer { @CapabilityInject(IDrawerAttributes.class) diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java index c2c3485cc..caa90aaa1 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java @@ -98,10 +98,9 @@ public void setStoredItemCount (int amount) { count = Math.min(amount, getMaxCapacity()); count = Math.max(count, 0); - if (amount == 0) { - if (!attrs.isItemLocked(LockAttribute.LOCK_POPULATED)) - reset(); - } else + if (amount == 0 && !attrs.isItemLocked(LockAttribute.LOCK_POPULATED)) + reset(); + else onAmountChanged(); } @@ -116,13 +115,16 @@ public int adjustStoredItemCount (int amount) { int originalCount = count; count = Math.min(amount, getMaxCapacity()); - onAmountChanged(); + + if (count != originalCount) + onAmountChanged(); if (attrs.isVoid()) return 0; return amount - (count - originalCount); - } else { + } + else { int originalCount = count; setStoredItemCount(originalCount + amount); @@ -154,7 +156,7 @@ public int getRemainingCapacity () { if (protoStack.isEmpty()) return 0; - if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) + if (attrs.isUnlimitedVending()) return Integer.MAX_VALUE; return getMaxCapacity() - getStoredItemCount(); @@ -238,9 +240,6 @@ protected int getStackCapacity() { return stackCapacity; } - // TODO: Handler should also take care of DrawerPopulatedEvent - // DrawerPopulatedEvent event = new DrawerPopulatedEvent(this); - // MinecraftForge.EVENT_BUS.post(event); protected void onItemChanged() { } protected void onAmountChanged() { } From abec7f679ca2b1ee7454cc9f77efc43cd55400a3 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Fri, 30 Jun 2017 02:37:21 -0400 Subject: [PATCH 12/22] More work on comp drawer data rewrite --- .../storagedrawers/api/storage/IDrawer.java | 1 + .../api/storage/IDrawerGroup.java | 6 +- .../tile/tiledata/FractionalDrawerGroup.java | 426 ++++++++++++++++++ .../storage/CompDrawerData.java | 264 +---------- 4 files changed, 438 insertions(+), 259 deletions(-) create mode 100644 src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java index 4296590ac..727c9943b 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java @@ -19,6 +19,7 @@ public interface IDrawer * @param itemPrototype An ItemStack representing the type, metadata, and tags of the item to store. * @return The IDrawer actually set with the prototype. Some drawer groups can redirect a set operation to another member. */ + @Nonnull IDrawer setStoredItem (@Nonnull ItemStack itemPrototype); /** diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java index bc54eb6bf..a1ea24f22 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java @@ -15,12 +15,12 @@ public interface IDrawerGroup /** * Gets the drawer at the given slot within this group only if it is enabled. */ - IDrawer getDrawerIfEnabled (int slot); + //IDrawer getDrawerIfEnabled (int slot); /** * Gets whether the drawer in the given slot is usable. */ - boolean isDrawerEnabled (int slot); + //boolean isDrawerEnabled (int slot); - boolean markDirtyIfNeeded (); + //boolean markDirtyIfNeeded (); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java new file mode 100644 index 000000000..f494a45bd --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java @@ -0,0 +1,426 @@ +package com.jaquadro.minecraft.storagedrawers.block.tile.tiledata; + +import com.jaquadro.minecraft.chameleon.block.tiledata.TileDataShim; +import com.jaquadro.minecraft.storagedrawers.api.storage.EmptyDrawerAttributes; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; +import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; +import com.jaquadro.minecraft.storagedrawers.inventory.ItemStackHelper; +import com.jaquadro.minecraft.storagedrawers.storage.BaseDrawerData; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.CapabilityInject; +import net.minecraftforge.common.capabilities.ICapabilityProvider; +import net.minecraftforge.common.util.Constants; +import net.minecraftforge.common.util.INBTSerializable; + +import javax.annotation.Nonnull; + +public class FractionalDrawerGroup extends TileDataShim implements IDrawerGroup +{ + private FractionalStorage storage; + private FractionalDrawer[] slots; + + public FractionalDrawerGroup (ICapabilityProvider capProvider, int slotCount) { + storage = new FractionalStorage(capProvider, this, slotCount); + + slots = new FractionalDrawer[slotCount]; + for (int i = 0; i < slotCount; i++) { + slots[i] = new FractionalDrawer(storage, i); + } + } + + @Override + public int getDrawerCount () { + return slots.length; + } + + @Override + public FractionalDrawer getDrawer (int slot) { + return slots[slot]; + } + + @Override + public void readFromNBT (NBTTagCompound tag) { + if (tag.hasKey("Drawers")) + storage.deserializeNBT(tag.getCompoundTag("Drawers")); + } + + @Override + public NBTTagCompound writeToNBT (NBTTagCompound tag) { + tag.setTag("Drawers", storage.serializeNBT()); + return tag; + } + + private static class FractionalStorage implements INBTSerializable + { + @CapabilityInject(IDrawerAttributes.class) + static Capability ATTR_CAPABILITY = null; + + private FractionalDrawerGroup group; + private int slotCount; + private ItemStack[] protoStack; + private int[] convRate; + private int pooledCount; + + IDrawerAttributes attrs; + + public FractionalStorage (ICapabilityProvider capProvider, FractionalDrawerGroup group, int slotCount) { + this.group = group; + this.slotCount = slotCount; + + protoStack = new ItemStack[slotCount]; + for (int i = 0; i < slotCount; i++) + protoStack[i] = ItemStack.EMPTY; + + convRate = new int[slotCount]; + + attrs = capProvider.getCapability(ATTR_CAPABILITY, null); + if (attrs == null) + attrs = new EmptyDrawerAttributes(); + } + + @Nonnull + public ItemStack getStack (int slot) { + return protoStack[slot]; + } + + @Nonnull + public ItemStack baseStack () { + return protoStack[0]; + } + + public int baseRate () { + return convRate[0]; + } + + public FractionalDrawer setStoredItem (int slot, @Nonnull ItemStack itemPrototype) { + itemPrototype = ItemStackHelper.getItemPrototype(itemPrototype); + if (itemPrototype.isEmpty()) { + reset(); + return group.getDrawer(slot); + } + + if (baseRate() == 0) { + populateSlots(itemPrototype); + for (int i = 0; i < slotCount; i++) { + if (BaseDrawerData.areItemsEqual(protoStack[i], itemPrototype)) { + slot = i; + pooledCount = 0; + } + } + + for (int i = 0; i < slotCount; i++) + group.getDrawer(i).reset(); + + onItemChanged(); + } + + return group.getDrawer(slot); + } + + public int getStoredCount (int slot) { + if (convRate[slot] == 0) + return 0; + + if (attrs.isUnlimitedVending()) + return Integer.MAX_VALUE; + + return pooledCount / convRate[slot]; + } + + public void setStoredItemCount (int slot, int amount) { + if (convRate[slot] == 0) + return; + + if (attrs.isUnlimitedVending()) + return; + + int oldCount = pooledCount; + + pooledCount = (pooledCount % convRate[slot]) + convRate[slot] * amount; + pooledCount = Math.min(pooledCount, getMaxCapacity(0) * convRate[0]); + pooledCount = Math.max(pooledCount, 0); + + if (pooledCount == oldCount) + return; + + if (pooledCount == 0 && !attrs.isItemLocked(LockAttribute.LOCK_POPULATED)) + reset(); + else + onAmountChanged(); + } + + public int adjustStoredItemCount (int slot, int amount) { + if (convRate[slot] == 0 || amount == 0) + return amount; + + if (amount > 0) { + if (attrs.isUnlimitedVending()) + return 0; + + int poolMax = getMaxCapacity(0) * convRate[0]; + if (poolMax < 0) + poolMax = Integer.MAX_VALUE; + + int canAdd = (poolMax - pooledCount) / convRate[slot]; + int willAdd = Math.min(amount, canAdd); + if (willAdd == 0) + return amount; + + pooledCount += convRate[slot] * willAdd; + + onAmountChanged(); + + if (attrs.isVoid()) + return 0; + + return amount - willAdd; + } + else { + amount = -amount; + + int canRemove = pooledCount / convRate[slot]; + int willRemove = Math.min(amount, canRemove); + if (willRemove == 0) + return amount; + + pooledCount -= willRemove; + + if (pooledCount == 0 && !attrs.isItemLocked(LockAttribute.LOCK_POPULATED)) + reset(); + else + onAmountChanged(); + + return amount - willRemove; + } + } + + public int getMaxCapacity (int slot) { + if (baseStack().isEmpty() || convRate[slot] == 0) + return 0; + + if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) + return Integer.MAX_VALUE / convRate[slot]; + + return baseStack().getItem().getItemStackLimit(baseStack()) * getStackCapacity() * (baseRate() / convRate[slot]); + } + + public int getMaxCapacity (int slot, @Nonnull ItemStack itemPrototype) { + if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) { + if (convRate[slot] == 0) + return Integer.MAX_VALUE; + return Integer.MAX_VALUE / convRate[slot]; + } + + if (baseStack().isEmpty()) { + int itemStackLimit = 64; + if (!itemPrototype.isEmpty()) + itemStackLimit = itemPrototype.getItem().getItemStackLimit(itemPrototype); + return itemStackLimit * getStackCapacity(); + } + + if (BaseDrawerData.areItemsEqual(protoStack[slot], itemPrototype)) + return getMaxCapacity(slot); + + return 0; + } + + public int getRemainingCapacity (int slot) { + if (baseStack().isEmpty() || convRate[slot] == 0) + return 0; + + if (attrs.isUnlimitedVending()) + return Integer.MAX_VALUE; + + int rawMaxCapacity = baseStack().getItem().getItemStackLimit(baseStack()) * getStackCapacity() * baseRate(); + int rawRemaining = rawMaxCapacity - pooledCount; + + return rawRemaining / convRate[slot]; + } + + public boolean isEmpty (int slot) { + return protoStack[slot].isEmpty(); + } + + public boolean isEnabled (int slot) { + if (baseStack().isEmpty()) + return true; + + return !protoStack[slot].isEmpty(); + } + + private void reset () { + pooledCount = 0; + + for (int i = 0; i < slotCount; i++) { + protoStack[i] = ItemStack.EMPTY; + convRate[i] = 0; + } + + for (int i = 0; i < slotCount; i++) + group.getDrawer(i).reset(); + + onItemChanged(); + } + + @Override + public NBTTagCompound serializeNBT () { + NBTTagList itemList = new NBTTagList(); + for (int i = 0; i < slotCount; i++) { + if (protoStack[i].isEmpty()) + continue; + + NBTTagCompound itemTag = new NBTTagCompound(); + protoStack[i].writeToNBT(itemTag); + + NBTTagCompound slotTag = new NBTTagCompound(); + slotTag.setByte("Slot", (byte)i); + slotTag.setInteger("Conv", convRate[i]); + slotTag.setTag("Item", itemTag); + + itemList.appendTag(slotTag); + } + + NBTTagCompound tag = new NBTTagCompound(); + tag.setInteger("Count", pooledCount); + tag.setTag("Items", itemList); + + return tag; + } + + @Override + public void deserializeNBT (NBTTagCompound tag) { + for (int i = 0; i < slotCount; i++) { + protoStack[i] = ItemStack.EMPTY; + convRate[i] = 0; + } + + pooledCount = tag.getInteger("Count"); + + NBTTagList itemList = tag.getTagList("Items", Constants.NBT.TAG_COMPOUND); + for (int i = 0; i < itemList.tagCount(); i++) { + NBTTagCompound slotTag = itemList.getCompoundTagAt(i); + int slot = slotTag.getByte("Slot"); + + protoStack[slot] = new ItemStack(slotTag.getCompoundTag("Item")); + convRate[slot] = slotTag.getByte("Conv"); + } + + for (int i = 0; i < slotCount; i++) + group.getDrawer(i).reset(); + + onItemChanged(); + } + + private void populateSlots(@Nonnull ItemStack stack) { + + } + + protected int getStackCapacity() { + return 0; + } + + protected void onItemChanged() { } + + protected void onAmountChanged() { } + } + + private static class FractionalDrawer extends BaseDrawerData + { + private FractionalStorage storage; + private int slot; + + IDrawerAttributes attrs; + + public FractionalDrawer (FractionalStorage storage, int slot) { + this.storage = storage; + this.slot = slot; + } + + @Nonnull + @Override + public ItemStack getStoredItemPrototype () { + return storage.getStack(slot); + } + + @Nonnull + @Override + public IDrawer setStoredItem (@Nonnull ItemStack itemPrototype) { + return storage.setStoredItem(slot, itemPrototype); + } + + @Override + public int getStoredItemCount () { + return storage.getStoredCount(slot); + } + + @Override + public void setStoredItemCount (int amount) { + storage.setStoredItemCount(slot, amount); + } + + @Override + public int adjustStoredItemCount (int amount) { + return storage.adjustStoredItemCount(slot, amount); + } + + @Override + public int getMaxCapacity () { + return storage.getMaxCapacity(slot); + } + + @Override + public int getMaxCapacity (@Nonnull ItemStack itemPrototype) { + return storage.getMaxCapacity(slot, itemPrototype); + } + + @Override + public int getRemainingCapacity () { + return storage.getRemainingCapacity(slot); + } + + @Override + public boolean canItemBeStored (@Nonnull ItemStack itemPrototype) { + if (getStoredItemPrototype().isEmpty() && !attrs.isItemLocked(LockAttribute.LOCK_EMPTY)) + return true; + + return areItemsEqual(itemPrototype); + } + + @Override + public boolean canItemBeExtracted (@Nonnull ItemStack itemPrototype) { + return areItemsEqual(itemPrototype); + } + + @Override + public boolean isEmpty () { + return storage.isEmpty(slot); + } + + @Override + public boolean isEnabled () { + return storage.isEnabled(slot); + } + + @Override + protected void reset () { + super.reset(); + refreshOreDictMatches(); + } + + @Override + public NBTTagCompound serializeNBT () { + // Handled by group + return new NBTTagCompound(); + } + + @Override + public void deserializeNBT (NBTTagCompound nbt) { + // Handled by group + } + } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/CompDrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/CompDrawerData.java index e43e78506..778df3cd9 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/CompDrawerData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/CompDrawerData.java @@ -1,269 +1,20 @@ package com.jaquadro.minecraft.storagedrawers.storage; -import com.jaquadro.minecraft.storagedrawers.api.storage.EmptyDrawerAttributes; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; -import com.jaquadro.minecraft.storagedrawers.api.storage.IFractionalDrawer; +import com.jaquadro.minecraft.storagedrawers.api.storage.*; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*; import com.jaquadro.minecraft.storagedrawers.inventory.ItemStackHelper; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.common.capabilities.ICapabilityProvider; +import net.minecraftforge.common.util.Constants; +import net.minecraftforge.common.util.INBTSerializable; import javax.annotation.Nonnull; -class FractionalStorage -{ - @CapabilityInject(IDrawerAttributes.class) - static Capability ATTR_CAPABILITY = null; - - private int slotCount; - private ItemStack[] protoStack; - private int[] convRate; - private int pooledCount; - - IDrawerAttributes attrs; - - public FractionalStorage (ICapabilityProvider capProvider, int slotCount) { - this.slotCount = slotCount; - - protoStack = new ItemStack[slotCount]; - for (int i = 0; i < slotCount; i++) - protoStack[i] = ItemStack.EMPTY; - - convRate = new int[slotCount]; - - attrs = capProvider.getCapability(ATTR_CAPABILITY, null); - if (attrs == null) - attrs = new EmptyDrawerAttributes(); - } - - @Nonnull - public ItemStack getStack (int slot) { - return protoStack[slot]; - } - - public int getConv (int slot) { - return convRate[slot]; - } - - @Nonnull - public ItemStack baseStack () { - return protoStack[0]; - } - - public int baseRate () { - return convRate[0]; - } - - public int setStoredItem (int slot, @Nonnull ItemStack itemPrototype) { - itemPrototype = ItemStackHelper.getItemPrototype(itemPrototype); - if (itemPrototype.isEmpty()) { - reset(); - return slot; - } - - if (baseRate() == 0) { - populateSlots(itemPrototype); - for (int i = 0; i < slotCount; i++) { - if (BaseDrawerData.areItemsEqual(protoStack[i], itemPrototype)) { - slot = i; - pooledCount = 0; - } - } - - // TODO: Refresh all slots - onItemChanged(); - } - - return slot; - } - - public int getStoredCount (int slot) { - if (convRate[slot] == 0) - return 0; - - if (attrs.isUnlimitedVending()) - return Integer.MAX_VALUE; - - return pooledCount / convRate[slot]; - } - - public void setStoredItemCount (int slot, int amount) { - if (convRate[slot] == 0) - return; - - if (attrs.isUnlimitedVending()) - return; - - int oldCount = pooledCount; - - pooledCount = (pooledCount % convRate[slot]) + convRate[slot] * amount; - pooledCount = Math.min(pooledCount, getMaxCapacity(0) * convRate[0]); - pooledCount = Math.max(pooledCount, 0); - - if (pooledCount == oldCount) - return; - - if (pooledCount == 0 && !attrs.isItemLocked(LockAttribute.LOCK_POPULATED)) - reset(); - else - onAmountChanged(); - } - - public int adjustStoredItemCount (int slot, int amount) { - if (convRate[slot] == 0 || amount == 0) - return amount; - - if (amount > 0) { - if (attrs.isUnlimitedVending()) - return 0; - - int poolMax = getMaxCapacity(0) * convRate[0]; - if (poolMax < 0) - poolMax = Integer.MAX_VALUE; - - int canAdd = (poolMax - pooledCount) / convRate[slot]; - int willAdd = Math.min(amount, canAdd); - if (willAdd == 0) - return amount; - - pooledCount += convRate[slot] * willAdd; - - onAmountChanged(); - - if (attrs.isVoid()) - return 0; - - return amount - willAdd; - } - else { - amount = -amount; - - int canRemove = pooledCount / convRate[slot]; - int willRemove = Math.min(amount, canRemove); - if (willRemove == 0) - return amount; - - pooledCount -= willRemove; - - if (pooledCount == 0 && !attrs.isItemLocked(LockAttribute.LOCK_POPULATED)) - reset(); - else - onAmountChanged(); - - return amount - willRemove; - } - } - - public int getMaxCapacity (int slot) { - if (baseStack().isEmpty() || convRate[slot] == 0) - return 0; - - if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) - return Integer.MAX_VALUE / convRate[slot]; - - return baseStack().getItem().getItemStackLimit(baseStack()) * getStackCapacity() * (baseRate() / convRate[slot]); - } - - public int getMaxCapacity (int slot, @Nonnull ItemStack itemPrototype) { - if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) { - if (convRate[slot] == 0) - return Integer.MAX_VALUE; - return Integer.MAX_VALUE / convRate[slot]; - } - - if (baseStack().isEmpty()) { - int itemStackLimit = 64; - if (!itemPrototype.isEmpty()) - itemStackLimit = itemPrototype.getItem().getItemStackLimit(itemPrototype); - return itemStackLimit * getStackCapacity(); - } - - if (BaseDrawerData.areItemsEqual(protoStack[slot], itemPrototype)) - return getMaxCapacity(slot); - - return 0; - } - - public int getRemainingCapacity (int slot) { - if (baseStack().isEmpty() || convRate[slot] == 0) - return 0; - - if (attrs.isUnlimitedVending()) - return Integer.MAX_VALUE; - - int rawMaxCapacity = baseStack().getItem().getItemStackLimit(baseStack()) * getStackCapacity() * baseRate(); - int rawRemaining = rawMaxCapacity - pooledCount; - - return rawRemaining / convRate[slot]; - } - - public boolean isEmpty (int slot) { - return protoStack[slot].isEmpty(); - } - - private void reset () { - pooledCount = 0; - - for (int i = 0; i < slotCount; i++) { - protoStack[i] = ItemStack.EMPTY; - convRate[i] = 0; - } - - // TODO: Refresh all slots - - onItemChanged(); - } - - private void populateSlots(@Nonnull ItemStack stack) { - - } - - protected int getStackCapacity() { - return 0; - } - - protected void onItemChanged() { } - - protected void onAmountChanged() { } -} - -class CDD extends BaseDrawerData -{ - @CapabilityInject(IDrawerAttributes.class) - static Capability ATTR_CAPABILITY = null; - - private FractionalStorage storage; - private int slot; - - IDrawerAttributes attrs; - - public CDD (ICapabilityProvider capProvider, FractionalStorage storage, int slot) { - this.storage = storage; - this.slot = slot; - } - - @Nonnull - @Override - public ItemStack getStoredItemPrototype () { - return storage.getStack(slot); - } - - @Override - public IDrawer setStoredItem (@Nonnull ItemStack itemPrototype) { - return null; - } - - @Override - public int getStoredItemCount () { - return storage.getStoredCount(slot); - } -} - -public class CompDrawerData extends BaseDrawerData implements IFractionalDrawer +/*public class CompDrawerData extends BaseDrawerData implements IFractionalDrawer { @CapabilityInject(IDrawerAttributes.class) static Capability ATTR_CAPABILITY = null; @@ -385,7 +136,7 @@ public void refresh () { refreshOreDictMatches(); } - /*@Override + @Override public boolean isVoid () { return central.isVoidSlot(slot); } @@ -421,5 +172,6 @@ public boolean canItemLock (LockAttribute attr) { } @Override - public void setItemLocked (LockAttribute attr, boolean isLocked) { }*/ + public void setItemLocked (LockAttribute attr, boolean isLocked) { } } +*/ \ No newline at end of file From d8e23b11e15ea038a249e2a2f12023af19d676ec Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sat, 1 Jul 2017 01:26:06 -0400 Subject: [PATCH 13/22] It compiles, but the damage is probably incalculable --- .../storagedrawers/api/storage/Drawers.java | 84 +++ .../api/storage/IDrawerGroup.java | 3 + .../block/BlockCompDrawers.java | 4 +- .../block/tile/TileEntityController.java | 65 +- .../block/tile/TileEntityDrawers.java | 115 +-- .../block/tile/TileEntityDrawersComp.java | 712 ++---------------- .../block/tile/TileEntityDrawersStandard.java | 40 +- .../block/tile/TileEntitySlave.java | 29 +- .../tile/tiledata/FractionalDrawerGroup.java | 215 ++++-- .../tile/tiledata/StandardDrawerGroup.java | 69 ++ .../block/tile/tiledata/UpgradeData.java | 30 +- .../renderer/TileEntityDrawersRenderer.java | 4 +- .../storagedrawers/integration/Waila.java | 4 +- .../inventory/DrawerInventoryHelper.java | 4 +- .../inventory/DrawerItemHandler.java | 44 +- .../storagedrawers/inventory/SlotDrawer.java | 7 +- .../storagedrawers/item/ItemDrawers.java | 3 - .../storage/BaseDrawerData.java | 2 +- .../storagedrawers/storage/DrawerData.java | 65 +- .../storage/ICentralInventory.java | 54 -- .../storagedrawers/util/CompactingHelper.java | 274 +++++++ 21 files changed, 825 insertions(+), 1002 deletions(-) create mode 100644 src/com/jaquadro/minecraft/storagedrawers/api/storage/Drawers.java create mode 100644 src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java delete mode 100644 src/com/jaquadro/minecraft/storagedrawers/storage/ICentralInventory.java create mode 100644 src/com/jaquadro/minecraft/storagedrawers/util/CompactingHelper.java diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/Drawers.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/Drawers.java new file mode 100644 index 000000000..b9237ad8e --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/Drawers.java @@ -0,0 +1,84 @@ +package com.jaquadro.minecraft.storagedrawers.api.storage; + +import net.minecraft.item.ItemStack; + +import javax.annotation.Nonnull; + +public class Drawers +{ + public static final IDrawer DISABLED = new DisabledDrawer(); + public static final IFractionalDrawer DISABLED_FRACTIONAL = new DisabledFractionalDrawer(); + + private static class DisabledDrawer implements IDrawer + { + @Nonnull + @Override + public ItemStack getStoredItemPrototype () { + return ItemStack.EMPTY; + } + + @Nonnull + @Override + public IDrawer setStoredItem (@Nonnull ItemStack itemPrototype) { + return this; + } + + @Override + public int getStoredItemCount () { + return 0; + } + + @Override + public void setStoredItemCount (int amount) { + + } + + @Override + public int getMaxCapacity (@Nonnull ItemStack itemPrototype) { + return 0; + } + + @Override + public int getRemainingCapacity () { + return 0; + } + + @Override + public boolean canItemBeStored (@Nonnull ItemStack itemPrototype) { + return false; + } + + @Override + public boolean canItemBeExtracted (@Nonnull ItemStack itemPrototype) { + return false; + } + + @Override + public boolean isEmpty () { + return true; + } + + @Override + public boolean isEnabled () { + return false; + } + } + + private static class DisabledFractionalDrawer extends DisabledDrawer implements IFractionalDrawer + { + @Override + public int getConversionRate () { + return 0; + } + + @Override + public int getStoredItemRemainder () { + return 0; + } + + @Override + public boolean isSmallestUnit () { + return false; + } + } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java index a1ea24f22..30438b5fd 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java @@ -1,5 +1,7 @@ package com.jaquadro.minecraft.storagedrawers.api.storage; +import javax.annotation.Nonnull; + public interface IDrawerGroup { /** @@ -10,6 +12,7 @@ public interface IDrawerGroup /** * Gets the drawer at the given slot within this group. */ + @Nonnull IDrawer getDrawer (int slot); /** diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockCompDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockCompDrawers.java index f76a67f07..f2f296c57 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockCompDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockCompDrawers.java @@ -115,9 +115,9 @@ public IBlockState getActualState (IBlockState state, IBlockAccess world, BlockP return state; EnumCompDrawer slots = EnumCompDrawer.OPEN1; - if (tile.isDrawerEnabled(1)) + if (tile.getDrawer(1).isEnabled()) slots = EnumCompDrawer.OPEN2; - if (tile.isDrawerEnabled(2)) + if (tile.getDrawer(2).isEnabled()) slots = EnumCompDrawer.OPEN3; return super.getActualState(state, world, pos).withProperty(SLOTS, slots); diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java index 813320895..bf606cb7a 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java @@ -25,9 +25,7 @@ import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.common.capabilities.ICapabilityProvider; -import net.minecraftforge.fml.common.FMLLog; import net.minecraftforge.items.CapabilityItemHandler; -import org.apache.logging.log4j.Level; import javax.annotation.Nonnull; import java.util.*; @@ -122,8 +120,8 @@ private int getSlotPriority (SlotRecord record) { } int drawerSlot = record.slot; - IDrawer drawer = group.getDrawerIfEnabled(drawerSlot); - if (drawer == null) { + IDrawer drawer = group.getDrawer(drawerSlot); + if (!drawer.isEnabled()) { return PRI_DISABLED; } @@ -432,11 +430,8 @@ private void rebuildPrimaryLookup (ItemMetaCollectionRegistry lookup continue; int drawerSlot = record.slot; - IDrawer drawer = group.getDrawerIfEnabled(drawerSlot); - if (drawer == null) - continue; - - if (drawer.isEmpty()) + IDrawer drawer = group.getDrawer(drawerSlot); + if (!drawer.isEnabled() || drawer.isEmpty()) continue; ItemStack item = drawer.getStoredItemPrototype(); @@ -661,58 +656,12 @@ public IDrawer getDrawer (int slot) { return group.getDrawer(getLocalDrawerSlot(slot)); } - @Override - public boolean isDrawerEnabled (int slot) { - IDrawerGroup group = getGroupForDrawerSlot(slot); - if (group == null) - return false; - - return group.isDrawerEnabled(getLocalDrawerSlot(slot)); - } - - @Override - public IDrawer getDrawerIfEnabled (int slot) { - IDrawerGroup group = getGroupForDrawerSlot(slot); - if (group == null) - return null; - - int localSlot = getLocalDrawerSlot(slot); - return group.getDrawerIfEnabled(localSlot); - } - @Override public int[] getAccessibleDrawerSlots () { return drawerSlots; } - @Override - public void markDirty () { - for (StorageRecord record : storage.values()) { - IDrawerGroup group = record.storage; - if (group != null) - group.markDirtyIfNeeded(); - } - - super.markDirty(); - } - - @Override - public boolean markDirtyIfNeeded () { - boolean synced = false; - - for (StorageRecord record : storage.values()) { - IDrawerGroup group = record.storage; - if (group != null) - synced |= group.markDirtyIfNeeded(); - } - - if (synced) - super.markDirty(); - - return synced; - } - - private DrawerItemHandler itemHandler = new DrawerItemHandler(this); + private DrawerItemHandler itemHandler = new DrawerItemHandler(this, this); @Override public boolean hasCapability (Capability capability, EnumFacing facing) { @@ -803,8 +752,8 @@ private void advance () { for (; index2 < drawerSlots.length; index2++) { int slot = drawerSlots[index2]; - IDrawer drawer = getDrawerIfEnabled(slot); - if (drawer == null) + IDrawer drawer = getDrawer(slot); + if (!drawer.isEnabled()) continue; if (strict) { diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index 8be0b07dd..efc4226e7 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -5,12 +5,8 @@ import com.jaquadro.minecraft.chameleon.block.tiledata.LockableData; import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.api.security.ISecurityProvider; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributesModifiable; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroupInteractive; +import com.jaquadro.minecraft.storagedrawers.api.storage.*; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*; -import com.jaquadro.minecraft.storagedrawers.block.BlockDrawersCustom; import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.ControllerData; import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.MaterialData; import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.UpgradeData; @@ -24,7 +20,6 @@ import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; @@ -52,8 +47,6 @@ public abstract class TileEntityDrawers extends ChamLockableTileEntity implement public final ControllerData controllerData = new ControllerData(); - private IDrawer[] drawers; - private int direction; private String material; private int drawerCapacity = 1; @@ -128,8 +121,8 @@ protected void onUpgradeChanged (ItemStack oldUpgrade, ItemStack newUpgrade) { private boolean stackCapacityCheck (int stackCapacity) { for (int i = 0; i < getDrawerCount(); i++) { - IDrawer drawer = getDrawerIfEnabled(i); - if (drawer == null || drawer.isEmpty()) + IDrawer drawer = getDrawer(i); + if (!drawer.isEnabled() || drawer.isEmpty()) continue; int addedItemCapacity = stackCapacity * drawer.getStoredItemStackSize(); @@ -141,7 +134,7 @@ private boolean stackCapacityCheck (int stackCapacity) { } } - protected TileEntityDrawers (int drawerCount) { + protected TileEntityDrawers () { drawerAttributes = new DrawerAttributes(); upgradeData.setDrawerAttributes(drawerAttributes); @@ -152,16 +145,18 @@ protected TileEntityDrawers (int drawerCount) { injectPortableData(materialData); injectData(controllerData); - initWithDrawerCount(drawerCount); + //initWithDrawerCount(drawerCount); } - protected abstract IDrawer createDrawer (int slot); + //protected abstract IDrawer createDrawer (int slot); - protected void initWithDrawerCount (int drawerCount) { + /*protected void initWithDrawerCount (int drawerCount) { drawers = new IDrawer[drawerCount]; for (int i = 0; i < drawerCount; i++) drawers[i] = createDrawer(i); - } + }*/ + + protected abstract IDrawerGroup getGroup (); public IDrawerAttributes getDrawerAttributes () { return drawerAttributes; @@ -312,8 +307,8 @@ protected int getCombinedRedstoneLevel () { float fillRatio = 0; for (int i = 0; i < getDrawerCount(); i++) { - IDrawer drawer = getDrawerIfEnabled(i); - if (drawer == null) + IDrawer drawer = getDrawer(i); + if (!drawer.isEnabled()) continue; if (drawer.getMaxCapacity() > 0) @@ -335,8 +330,8 @@ protected int getMinRedstoneLevel () { float minRatio = 2; for (int i = 0; i < getDrawerCount(); i++) { - IDrawer drawer = getDrawerIfEnabled(i); - if (drawer == null) + IDrawer drawer = getDrawer(i); + if (!drawer.isEnabled()) continue; if (drawer.getMaxCapacity() > 0) @@ -357,8 +352,8 @@ protected int getMaxRedstoneLevel () { float maxRatio = 0; for (int i = 0; i < getDrawerCount(); i++) { - IDrawer drawer = getDrawerIfEnabled(i); - if (drawer == null) + IDrawer drawer = getDrawer(i); + if (!drawer.isEnabled()) continue; if (drawer.getMaxCapacity() > 0) @@ -373,15 +368,12 @@ protected int getMaxRedstoneLevel () { @Nonnull public ItemStack takeItemsFromSlot (int slot, int count) { - if (slot < 0 || slot >= getDrawerCount()) - return ItemStack.EMPTY; - - IDrawer drawer = drawers[slot]; - if (drawer.isEmpty()) + IDrawer drawer = getGroup().getDrawer(slot); + if (!drawer.isEnabled() || drawer.isEmpty()) return ItemStack.EMPTY; ItemStack stack = drawer.getStoredItemPrototype().copy(); - stack.setCount(Math.min(count, drawers[slot].getStoredItemCount())); + stack.setCount(Math.min(count, drawer.getStoredItemCount())); drawer.setStoredItemCount(drawer.getStoredItemCount() - stack.getCount()); @@ -395,22 +387,11 @@ public ItemStack takeItemsFromSlot (int slot, int count) { return stack; } - @Nonnull - protected ItemStack getItemsFromSlot (int slot, int count) { - if (drawers[slot].isEmpty()) - return ItemStack.EMPTY; - - ItemStack stack = drawers[slot].getStoredItemPrototype().copy(); - stack.setCount(Math.min(count, drawers[slot].getStoredItemCount())); - - return stack; - } - public int putItemsIntoSlot (int slot, @Nonnull ItemStack stack, int count) { - if (slot < 0 || slot >= getDrawerCount()) + IDrawer drawer = getGroup().getDrawer(slot); + if (!drawer.isEnabled()) return 0; - IDrawer drawer = drawers[slot]; if (drawer.isEmpty()) drawer.setStoredItem(stack); @@ -428,7 +409,8 @@ public int putItemsIntoSlot (int slot, @Nonnull ItemStack stack, int count) { } public int interactPutCurrentItemIntoSlot (int slot, EntityPlayer player) { - if (slot < 0 || slot >= getDrawerCount()) + IDrawer drawer = getDrawer(slot); + if (!drawer.isEnabled()) return 0; int count = 0; @@ -440,11 +422,12 @@ public int interactPutCurrentItemIntoSlot (int slot, EntityPlayer player) { } public int interactPutCurrentInventoryIntoSlot (int slot, EntityPlayer player) { - if (slot < 0 || slot >= getDrawerCount()) + IDrawer drawer = getGroup().getDrawer(slot); + if (!drawer.isEnabled()) return 0; int count = 0; - if (!drawers[slot].isEmpty()) { + if (!drawer.isEmpty()) { for (int i = 0, n = player.inventory.getSizeInventory(); i < n; i++) { ItemStack subStack = player.inventory.getStackInSlot(i); if (!subStack.isEmpty()) { @@ -536,6 +519,7 @@ public void readFromPortableNBT (NBTTagCompound tag) { if (tag.hasKey("Sec")) securityKey = tag.getString("Sec"); + /* NBTTagList slots = tag.getTagList("Slots", Constants.NBT.TAG_COMPOUND); drawers = new IDrawer[slots.tagCount()]; @@ -544,7 +528,7 @@ public void readFromPortableNBT (NBTTagCompound tag) { drawers[i] = createDrawer(i); if (drawers[i] instanceof INBTSerializable) ((INBTSerializable)drawers[i]).deserializeNBT(slot); - } + }*/ } @Override @@ -578,7 +562,7 @@ public NBTTagCompound writeToPortableNBT (NBTTagCompound tag) { if (securityKey != null) tag.setString("Sec", securityKey); - NBTTagList slots = new NBTTagList(); + /*NBTTagList slots = new NBTTagList(); for (IDrawer drawer : drawers) { if (drawer instanceof INBTSerializable) slots.appendTag(((INBTSerializable)drawer).serializeNBT()); @@ -586,7 +570,7 @@ public NBTTagCompound writeToPortableNBT (NBTTagCompound tag) { slots.appendTag(new NBTTagCompound()); } - tag.setTag("Slots", slots); + tag.setTag("Slots", slots);*/ return tag; } @@ -764,12 +748,6 @@ public void markDirty () { super.markDirty(); } - @Override - public boolean markDirtyIfNeeded () { - super.markDirty(); - return true; - } - protected void syncClientCount (int slot, int count) { if (getWorld() != null && getWorld().isRemote) return; @@ -789,8 +767,8 @@ public void clientUpdateCount (final int slot, final int count) { @SideOnly(Side.CLIENT) private void clientUpdateCountAsync (int slot, int count) { - IDrawer drawer = getDrawerIfEnabled(slot); - if (drawer != null && drawer.getStoredItemCount() != count) + IDrawer drawer = getDrawer(slot); + if (!drawer.isEnabled() && drawer.getStoredItemCount() != count) drawer.setStoredItemCount(count); } @@ -807,40 +785,19 @@ public boolean shouldRefresh (World world, BlockPos pos, IBlockState oldState, I @Override public int getDrawerCount () { - return drawers.length; + return getGroup().getDrawerCount(); } @Override + @Nonnull public IDrawer getDrawer (int slot) { - if (slot < 0 || slot >= drawers.length) - return null; - - return drawers[slot]; - } - - @Override - public IDrawer getDrawerIfEnabled (int slot) { - if (slot < 0 || slot >= drawers.length) - return null; - - if (isSealed()) - return null; - - if (getBlockType() instanceof BlockDrawersCustom && material().getSide().isEmpty()) - return null; - - return drawers[slot]; - } - - @Override - public boolean isDrawerEnabled (int slot) { - return getDrawerIfEnabled(slot) != null; + return getGroup().getDrawer(slot); } private net.minecraftforge.items.IItemHandler itemHandler; protected IItemHandler createUnSidedHandler () { - return new DrawerItemHandler(this); + return new DrawerItemHandler(getGroup(), this); } @SuppressWarnings("unchecked") diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java index c5a7b6fa7..a5f855b3a 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java @@ -2,80 +2,92 @@ import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; -import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; -import com.jaquadro.minecraft.storagedrawers.config.CompTierRegistry; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; +import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.FractionalDrawerGroup; import com.jaquadro.minecraft.storagedrawers.config.ConfigManager; import com.jaquadro.minecraft.storagedrawers.inventory.ContainerDrawersComp; import com.jaquadro.minecraft.storagedrawers.network.CountUpdateMessage; -import com.jaquadro.minecraft.storagedrawers.storage.*; -import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; -import net.minecraft.inventory.InventoryCrafting; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.item.crafting.CraftingManager; -import net.minecraft.item.crafting.IRecipe; -import net.minecraft.item.crafting.Ingredient; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.util.NonNullList; -import net.minecraft.util.ResourceLocation; -import net.minecraftforge.fml.common.FMLLog; +import net.minecraft.world.World; import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -import org.apache.logging.log4j.Level; - -import javax.annotation.Nonnull; -import java.util.*; public class TileEntityDrawersComp extends TileEntityDrawers { - private static InventoryLookup lookup1 = new InventoryLookup(1, 1); - private static InventoryLookup lookup2 = new InventoryLookup(2, 2); - private static InventoryLookup lookup3 = new InventoryLookup(3, 3); - - private ICentralInventory centralInventory; + private GroupData groupData; private int capacity = 0; - private int pooledCount; - private int lookupSizeResult; - - private ItemStack[] protoStack; - private int[] convRate; public TileEntityDrawersComp () { - super(3); + groupData = new GroupData(3); + groupData.setCapabilityProvider(this); - protoStack = new ItemStack[getDrawerCount()]; - for (int i = 0; i < protoStack.length; i++) - protoStack[i] = ItemStack.EMPTY; + injectPortableData(groupData); + } - convRate = new int[getDrawerCount()]; + @Override + protected IDrawerGroup getGroup () { + return groupData; } - protected ICentralInventory getCentralInventory () { - if (centralInventory == null) - centralInventory = new CompCentralInventory(); - return centralInventory; + private class GroupData extends FractionalDrawerGroup + { + public GroupData (int slotCount) { + super(slotCount); + } + + @Override + protected World getWorld () { + return TileEntityDrawersComp.this.getWorld(); + } + + @Override + protected void log (String message) { + if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) + StorageDrawers.log.info(message); + } + + @Override + protected int getStackCapacity () { + return upgrades().getStorageMultiplier() * getEffectiveDrawerCapacity(); + } + + @Override + protected void onItemChanged () { + if (getWorld() != null && !getWorld().isRemote) { + markDirty(); + markBlockForUpdate(); + } + } + + @Override + protected void onAmountChanged () { + if (getWorld() != null && !getWorld().isRemote) { + IMessage message = new CountUpdateMessage(getPos(), 0, getPooledCount()); + NetworkRegistry.TargetPoint targetPoint = new NetworkRegistry.TargetPoint(getWorld().provider.getDimension(), getPos().getX(), getPos().getY(), getPos().getZ(), 500); + + StorageDrawers.network.sendToAllAround(message, targetPoint); + + markDirty(); + } + } } + // TODO + /* public int getStoredItemRemainder (int slot) { + groupData.getDrawer(slot) int count = centralInventory.getStoredItemCount(slot); if (slot > 0 && convRate[slot] > 0) count -= centralInventory.getStoredItemCount(slot - 1) * (convRate[slot - 1] / convRate[slot]); return count; - } - - @Override - protected IDrawer createDrawer (int slot) { - return new CompDrawerData(getCentralInventory(), this, slot); - } + }*/ @Override public Container createContainer (InventoryPlayer playerInventory, EntityPlayer playerIn) { @@ -103,14 +115,7 @@ public int getDrawerCapacity () { return capacity; } - @Override - public IDrawer getDrawerIfEnabled (int slot) { - if (slot > 0 && slot < convRate.length && convRate[slot] == 0) - return null; - - return super.getDrawerIfEnabled(slot); - } - + /* @Override public int putItemsIntoSlot (int slot, @Nonnull ItemStack stack, int count) { int added = 0; @@ -132,14 +137,14 @@ public int putItemsIntoSlot (int slot, @Nonnull ItemStack stack, int count) { } return added + super.putItemsIntoSlot(slot, stack, count); - } + }*/ @Override public boolean dataPacketRequiresRenderUpdate () { return true; } - @Override + /*@Override public void readFromPortableNBT (NBTTagCompound tag) { super.readFromPortableNBT(tag); @@ -182,7 +187,7 @@ public NBTTagCompound writeToPortableNBT (NBTTagCompound tag) { tag.setByte("Conv2", (byte)convRate[2]); return tag; - } + }*/ @Override @SideOnly(Side.CLIENT) @@ -190,607 +195,16 @@ public void clientUpdateCount (final int slot, final int count) { if (!getWorld().isRemote) return; - Minecraft.getMinecraft().addScheduledTask(new Runnable() { - @Override - public void run () { - TileEntityDrawersComp.this.clientUpdateCountAsync(slot, count); - } - }); + Minecraft.getMinecraft().addScheduledTask(() -> TileEntityDrawersComp.this.clientUpdateCountAsync(count)); } @SideOnly(Side.CLIENT) - private void clientUpdateCountAsync (int slot, int count) { - if (count != pooledCount) { - pooledCount = count; - } + private void clientUpdateCountAsync (int count) { + groupData.setPooledCount(count); } @Override public String getName () { return hasCustomName() ? super.getName() : "storagedrawers.container.compDrawers"; } - - private void populateSlots (@Nonnull ItemStack stack) { - int index = 0; - - ItemStack uTier1 = findHigherTier(stack); - if (!uTier1.isEmpty()) { - if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - StorageDrawers.log.info("Picked candidate " + uTier1.toString() + " with conv=" + lookupSizeResult); - - int uCount1 = lookupSizeResult; - ItemStack uTier2 = findHigherTier(uTier1); - if (!uTier2.isEmpty()) { - if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - StorageDrawers.log.info("Picked candidate " + uTier2.toString() + " with conv=" + lookupSizeResult); - - populateSlot(index++, uTier2, lookupSizeResult * uCount1); - } - - populateSlot(index++, uTier1, uCount1); - } - - populateSlot(index++, stack, 1); - - if (index == 3) - return; - - ItemStack lTier1 = findLowerTier(stack); - if (!lTier1.isEmpty()) { - if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - StorageDrawers.log.info("Picked candidate " + lTier1.toString() + " with conv=" + lookupSizeResult); - - populateSlot(index++, lTier1, 1); - for (int i = 0; i < index - 1; i++) - convRate[i] *= lookupSizeResult; - } - - if (index == 3 || lTier1.isEmpty()) - return; - - ItemStack lTier2 = findLowerTier(lTier1); - if (!lTier2.isEmpty()) { - if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - StorageDrawers.log.info("Picked candidate " + lTier2.toString() + " with conv=" + lookupSizeResult); - - populateSlot(index++, lTier2, 1); - for (int i = 0; i < index - 1; i++) - convRate[i] *= lookupSizeResult; - } - } - - private void populateSlot (int slot, @Nonnull ItemStack stack, int conversion) { - convRate[slot] = conversion; - protoStack[slot] = stack.copy(); - //centralInventory.setStoredItem(slot, stack, 0); - //getDrawer(slot).setStoredItem(stack, 0); - - markBlockForUpdate(); - } - - @Nonnull - private ItemStack findHigherTier (@Nonnull ItemStack stack) { - if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - StorageDrawers.log.info("Finding ascending candidates for " + stack.toString()); - - CompTierRegistry.Record record = StorageDrawers.compRegistry.findHigherTier(stack); - if (record != null) { - if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - StorageDrawers.log.info("Found " + record.upper.toString() + " in registry with conv=" + record.convRate); - - lookupSizeResult = record.convRate; - return record.upper; - } - - List candidates = new ArrayList<>(); - - setupLookup(lookup3, stack); - List fwdCandidates = findAllMatchingRecipes(lookup3); - - if (fwdCandidates.size() == 0) { - setupLookup(lookup2, stack); - fwdCandidates = findAllMatchingRecipes(lookup2); - } - - if (fwdCandidates.size() > 0) { - int size = lookupSizeResult; - - for (ItemStack match : fwdCandidates) { - setupLookup(lookup1, match); - List backCandidates = findAllMatchingRecipes(lookup1); - - for (ItemStack comp : backCandidates) { - if (comp.getCount() != size) - continue; - - if (!DrawerData.areItemsEqual(comp, stack, false)) - continue; - - candidates.add(match); - if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - StorageDrawers.log.info("Found ascending candidate for " + stack.toString() + ": " + match.toString() + " size=" + lookupSizeResult + ", inverse=" + comp.toString()); - - break; - } - } - - lookupSizeResult = size; - } - - ItemStack modMatch = findMatchingModCandidate(stack, candidates); - if (!modMatch.isEmpty()) - return modMatch; - - if (candidates.size() > 0) - return candidates.get(0); - - if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - StorageDrawers.log.info("No candidates found"); - - return ItemStack.EMPTY; - } - - private List findAllMatchingRecipes (InventoryCrafting crafting) { - List candidates = new ArrayList<>(); - - for (Object aRecipeList : CraftingManager.REGISTRY) { - IRecipe recipe = (IRecipe) aRecipeList; - if (recipe.matches(crafting, getWorld())) { - ItemStack result = recipe.getCraftingResult(crafting); - if (!result.isEmpty()) - candidates.add(result); - } - } - - return candidates; - } - - @Nonnull - private ItemStack findLowerTier (@Nonnull ItemStack stack) { - if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - StorageDrawers.log.info("Finding descending candidates for " + stack.toString()); - - CompTierRegistry.Record record = StorageDrawers.compRegistry.findLowerTier(stack); - if (record != null) { - if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - StorageDrawers.log.info("Found " + record.lower.toString() + " in registry with conv=" + record.convRate); - - lookupSizeResult = record.convRate; - return record.lower; - } - - List candidates = new ArrayList<>(); - Map candidatesRate = new HashMap<>(); - - for (IRecipe recipe : CraftingManager.REGISTRY) { - ItemStack output = recipe.getRecipeOutput(); - if (!DrawerData.areItemsEqual(stack, output, true)) - continue; - - @Nonnull ItemStack match = tryMatch(stack, recipe.getIngredients()); - if (!match.isEmpty()) { - setupLookup(lookup1, output); - List compMatches = findAllMatchingRecipes(lookup1); - for (ItemStack comp : compMatches) { - int recipeSize = recipe.getIngredients().size(); - if (DrawerData.areItemsEqual(match, comp, true) && comp.getCount() == recipeSize) { - lookupSizeResult = recipeSize; - candidates.add(match); - candidatesRate.put(match, lookupSizeResult); - - if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - StorageDrawers.log.info("Found descending candidate for " + stack.toString() + ": " + match.toString() + " size=" + lookupSizeResult + ", inverse=" + comp.toString()); - } else if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - StorageDrawers.log.info("Back-check failed for " + match.toString() + " size=" + lookupSizeResult + ", inverse=" + comp.toString()); - } - } - } - - ItemStack modMatch = findMatchingModCandidate(stack, candidates); - if (!modMatch.isEmpty()) { - lookupSizeResult = candidatesRate.get(modMatch); - return modMatch; - } - - if (candidates.size() > 0) { - ItemStack match = candidates.get(0); - lookupSizeResult = candidatesRate.get(match); - - return match; - } - - if (!getWorld().isRemote && StorageDrawers.config.cache.debugTrace) - StorageDrawers.log.info("No candidates found"); - - return ItemStack.EMPTY; - } - - @Nonnull - private ItemStack findMatchingModCandidate (@Nonnull ItemStack reference, List candidates) { - ResourceLocation referenceName = reference.getItem().getRegistryName(); - if (referenceName != null) { - for (ItemStack candidate : candidates) { - ResourceLocation matchName = candidate.getItem().getRegistryName(); - if (matchName != null) { - if (referenceName.getResourceDomain().equals(matchName.getResourceDomain())) - return candidate; - } - } - } - - return ItemStack.EMPTY; - } - - @Nonnull - private ItemStack tryMatch (@Nonnull ItemStack stack, NonNullList ingredients) { - if (ingredients.size() != 9 && ingredients.size() != 4) - return ItemStack.EMPTY; - - Ingredient refIngredient = ingredients.get(0); - ItemStack[] refMatchingStacks = refIngredient.getMatchingStacks(); - if (refMatchingStacks.length == 0) - return ItemStack.EMPTY; - - for (int i = 1, n = ingredients.size(); i < n; i++) { - Ingredient ingredient = ingredients.get(i); - @Nonnull ItemStack match = ItemStack.EMPTY; - - for (ItemStack ingItemMatch : refMatchingStacks) { - if (ingredient.apply(ingItemMatch)) { - match = ingItemMatch; - break; - } - } - - if (match.isEmpty()) - return ItemStack.EMPTY; - } - - ItemStack match = findMatchingModCandidate(stack, Arrays.asList(refMatchingStacks)); - if (match.isEmpty()) - match = refMatchingStacks[0]; - - return match; - } - - private void setupLookup (InventoryLookup inv, @Nonnull ItemStack stack) { - for (int i = 0, n = inv.getSizeInventory(); i < n; i++) - inv.setInventorySlotContents(i, stack); - - lookupSizeResult = inv.getSizeInventory(); - } - - private class CompCentralInventory implements ICentralInventory - { - // TODO: More consistent handling of index 0/slot and some once-only logging - - @Override - @Nonnull - public ItemStack getStoredItemPrototype (int slot) { - return protoStack[slot]; - } - - @Override - public IDrawer setStoredItem (int slot, @Nonnull ItemStack itemPrototype, int amount) { - boolean itemValid = !itemPrototype.isEmpty(); - if (itemValid && convRate != null && convRate[0] == 0) { - IDrawer target = null; - populateSlots(itemPrototype); - for (int i = 0; i < getDrawerCount(); i++) { - if (BaseDrawerData.areItemsEqual(protoStack[i], itemPrototype)) { - target = getDrawer(i); - pooledCount = (pooledCount % convRate[i]) + convRate[i] * amount; - } - } - - for (int i = 0; i < getDrawerCount(); i++) { - if (i == slot) - continue; - - IDrawer drawer = getDrawer(i); - if (drawer instanceof CompDrawerData) - ((CompDrawerData) drawer).refresh(); - } - - markBlockForUpdate(); - return target; - } - else if (!itemValid && isDrawerEnabled(slot)) { - pooledCount = 0; - clear(); - markBlockForUpdate(); - } - - return getDrawer(slot); - } - - @Override - public int getStoredItemCount (int slot) { - if (convRate == null || convRate[slot] == 0) - return 0; - - if (TileEntityDrawersComp.this.getDrawerAttributes().isUnlimitedVending()) - return Integer.MAX_VALUE; - - return pooledCount / convRate[slot]; - } - - @Override - public void setStoredItemCount (int slot, int amount) { - if (convRate == null || convRate[slot] == 0) - return; - - if (TileEntityDrawersComp.this.getDrawerAttributes().isUnlimitedVending()) - return; - - int oldCount = pooledCount; - pooledCount = (pooledCount % convRate[slot]) + convRate[slot] * amount; - - int poolMax = getMaxCapacity(0) * convRate[0]; - if (pooledCount > poolMax) - pooledCount = poolMax; - - if (pooledCount != oldCount) { - if (pooledCount != 0 || TileEntityDrawersComp.this.getDrawerAttributes().isItemLocked(LockAttribute.LOCK_POPULATED)) - markAmountDirty(); - else { - clear(); - markBlockForUpdate(); - } - - if (!getWorld().isRemote && isRedstone()) { - IBlockState state = getWorld().getBlockState(getPos()); - getWorld().notifyNeighborsOfStateChange(getPos(), state.getBlock(), false); - getWorld().notifyNeighborsOfStateChange(getPos().down(), state.getBlock(), false); - } - } - } - - @Override - public int getMaxCapacity (int slot) { - if (protoStack[0].isEmpty() || protoStack[slot].isEmpty() || convRate == null || convRate[slot] == 0) - return 0; - - IDrawerAttributes attrs = TileEntityDrawersComp.this.getDrawerAttributes(); - if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) { - if (convRate == null || protoStack[slot].isEmpty() || convRate[slot] == 0) - return Integer.MAX_VALUE; - return Integer.MAX_VALUE / convRate[slot]; - } - - return protoStack[0].getItem().getItemStackLimit(protoStack[0]) * getStackCapacity(0) * getConversionRate(slot); - } - - @Override - public int getMaxCapacity (int slot, @Nonnull ItemStack itemPrototype) { - if (itemPrototype.isEmpty()) - return 0; - - IDrawerAttributes attrs = TileEntityDrawersComp.this.getDrawerAttributes(); - if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) { - if (convRate == null || protoStack[slot].isEmpty() || convRate[slot] == 0) - return Integer.MAX_VALUE; - return Integer.MAX_VALUE / convRate[slot]; - } - - if (convRate == null || protoStack[0].isEmpty() || convRate[0] == 0) - return itemPrototype.getItem().getItemStackLimit(itemPrototype) * getBaseStackCapacity(); - - if (BaseDrawerData.areItemsEqual(protoStack[slot], itemPrototype)) - return getMaxCapacity(slot); - - return 0; - } - - @Override - public int getDefaultMaxCapacity (int slot) { - if (!isDrawerEnabled(slot)) - return 0; - - IDrawerAttributes attrs = TileEntityDrawersComp.this.getDrawerAttributes(); - if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) - return Integer.MAX_VALUE; - - return 64 * getBaseStackCapacity(); - } - - @Override - public int getRemainingCapacity (int slot) { - if (protoStack[0].isEmpty() || protoStack[slot].isEmpty() || convRate == null || convRate[slot] == 0) - return 0; - if (TileEntityDrawersComp.this.getDrawerAttributes().isUnlimitedVending()) - return Integer.MAX_VALUE; - - int rawMaxCapacity = protoStack[0].getItem().getItemStackLimit(protoStack[0]) * getStackCapacity(0) * convRate[0]; - int rawRemaining = rawMaxCapacity - pooledCount; - - return rawRemaining / convRate[slot]; - } - - @Override - public int getStoredItemStackSize (int slot) { - if (protoStack[slot].isEmpty() || convRate == null || convRate[slot] == 0) - return 0; - - return protoStack[slot].getItem().getItemStackLimit(protoStack[slot]); - } - - @Override - public int getItemCapacityForInventoryStack (int slot) { - if (getDrawerAttributes().isVoid()) - return Integer.MAX_VALUE; - else - return getMaxCapacity(slot); - } - - @Override - public int getConversionRate (int slot) { - if (protoStack[0].isEmpty() || protoStack[slot].isEmpty() || convRate == null || convRate[slot] == 0) - return 0; - - return convRate[0] / convRate[slot]; - } - - @Override - public int getStoredItemRemainder (int slot) { - return TileEntityDrawersComp.this.getStoredItemRemainder(slot); - } - - @Override - public boolean isSmallestUnit (int slot) { - if (protoStack[slot].isEmpty() || convRate == null || convRate[slot] == 0) - return false; - - return convRate[slot] == 1; - } - - /*@Override - public boolean isVoidSlot (int slot) { - return isVoid(); - } - - @Override - public boolean isShroudedSlot (int slot) { - return isShrouded(); - } - - @Override - public boolean setIsSlotShrouded (int slot, boolean state) { - setIsShrouded(state); - return true; - } - - @Override - public boolean isSlotShowingQuantity (int slot) { - return isShowingQuantity(); - } - - @Override - public boolean setIsSlotShowingQuantity (int slot, boolean state) { - return setIsShowingQuantity(state); - } - - @Override - public boolean isLocked (int slot, LockAttribute attr) { - return TileEntityDrawersComp.this.isItemLocked(attr); - }*/ - - @Override - public void writeToNBT (int slot, NBTTagCompound tag) { - ItemStack protoStack = getStoredItemPrototype(slot); - if (!protoStack.isEmpty()) { - tag.setShort("Item", (short) Item.getIdFromItem(protoStack.getItem())); - tag.setShort("Meta", (short) protoStack.getItemDamage()); - - if (protoStack.getTagCompound() != null) - tag.setTag("Tags", protoStack.getTagCompound()); - } - } - - @Override - public void readFromNBT (int slot, NBTTagCompound tag) { - if (tag.hasKey("Item")) { - Item item = Item.getItemById(tag.getShort("Item")); - if (item != null) { - ItemStack stack = new ItemStack(item); - stack.setItemDamage(tag.getShort("Meta")); - if (tag.hasKey("Tags")) - stack.setTagCompound(tag.getCompoundTag("Tags")); - - protoStack[slot] = stack; - } - } - } - - private void clear () { - for (int i = 0; i < getDrawerCount(); i++) { - protoStack[i] = ItemStack.EMPTY; - convRate[i] = 0; - } - - refresh(); - TileEntityDrawersComp.this.markDirty(); - } - - public void refresh () { - for (int i = 0; i < getDrawerCount(); i++) { - IDrawer drawer = getDrawer(i); - if (drawer instanceof CompDrawerData) - ((CompDrawerData) drawer).refresh(); - } - } - - private int getStackCapacity (int slot) { - if (convRate == null || convRate[slot] == 0) - return 0; - - int slotStacks = getBaseStackCapacity(); - - int stackLimit = convRate[0] * slotStacks; - return stackLimit / convRate[slot]; - } - - private int getBaseStackCapacity () { - return TileEntityDrawersComp.this.upgrades().getStorageMultiplier() * TileEntityDrawersComp.this.getEffectiveDrawerCapacity(); - } - - public void markAmountDirty () { - if (getWorld().isRemote) - return; - - IMessage message = new CountUpdateMessage(getPos(), 0, pooledCount); - NetworkRegistry.TargetPoint targetPoint = new NetworkRegistry.TargetPoint(getWorld().provider.getDimension(), getPos().getX(), getPos().getY(), getPos().getZ(), 500); - - StorageDrawers.network.sendToAllAround(message, targetPoint); - - TileEntityDrawersComp.this.markDirty(); - } - - public void markDirty (int slot) { - markBlockForUpdate(); - } - } - - private static class InventoryLookup extends InventoryCrafting - { - private ItemStack[] stackList; - - public InventoryLookup (int width, int height) { - super(null, width, height); - - stackList = new ItemStack[width * height]; - for (int i = 0; i < stackList.length; i++) - stackList[i] = ItemStack.EMPTY; - } - - @Override - public int getSizeInventory () - { - return this.stackList.length; - } - - @Override - @Nonnull - public ItemStack getStackInSlot (int slot) - { - return slot >= this.getSizeInventory() ? ItemStack.EMPTY : this.stackList[slot]; - } - - @Override - @Nonnull - public ItemStack removeStackFromSlot (int slot) { - return ItemStack.EMPTY; - } - - @Override - @Nonnull - public ItemStack decrStackSize (int slot, int count) { - return ItemStack.EMPTY; - } - - @Override - public void setInventorySlotContents (int slot, @Nonnull ItemStack stack) { - stackList[slot] = stack; - } - } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java index 461fbb525..03e5fd865 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java @@ -4,7 +4,9 @@ import com.jaquadro.minecraft.storagedrawers.api.event.DrawerPopulatedEvent; import com.jaquadro.minecraft.storagedrawers.api.storage.EnumBasicDrawer; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; import com.jaquadro.minecraft.storagedrawers.block.BlockStandardDrawers; +import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.StandardDrawerGroup; import com.jaquadro.minecraft.storagedrawers.config.ConfigManager; import com.jaquadro.minecraft.storagedrawers.inventory.ContainerDrawers1; import com.jaquadro.minecraft.storagedrawers.inventory.ContainerDrawers2; @@ -17,29 +19,32 @@ import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.capabilities.ICapabilityProvider; +import javax.annotation.Nonnull; + public class TileEntityDrawersStandard extends TileEntityDrawers { private static final String[] GUI_IDS = new String[] { null, StorageDrawers.MOD_ID + ":basicDrawers1", StorageDrawers.MOD_ID + ":basicDrawers2", null, StorageDrawers.MOD_ID + ":basicDrawers4" }; + private GroupData groupData; + private int capacity = 0; public TileEntityDrawersStandard () { - super(1); + this(1); } public TileEntityDrawersStandard (int count) { - super(count); - } + groupData = new GroupData(count); + groupData.setCapabilityProvider(this); - public void setDrawerCount (int count) { - initWithDrawerCount(count); + injectPortableData(groupData); } @Override - protected IDrawer createDrawer (int slot) { - return new StandardDrawerData(this, slot); + protected IDrawerGroup getGroup () { + return groupData; } @Override @@ -101,12 +106,25 @@ public int getDrawerCapacity () { return capacity; } + private class GroupData extends StandardDrawerGroup + { + public GroupData (int slotCount) { + super(slotCount); + } + + @Nonnull + @Override + protected DrawerData createDrawer (int slot) { + return new StandardDrawerData(slot); + } + } + private class StandardDrawerData extends DrawerData { private int slot; - public StandardDrawerData (ICapabilityProvider capProvider, int slot) { - super(capProvider); + public StandardDrawerData (int slot) { + super(); this.slot = slot; } @@ -128,11 +146,9 @@ protected void onItemChanged () { @Override protected void onAmountChanged () { - syncClientCount(slot, getStoredItemCount()); - if (getWorld() != null && !getWorld().isRemote) { + syncClientCount(slot, getStoredItemCount()); markDirty(); - markBlockForUpdate(); } } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntitySlave.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntitySlave.java index 4d7d30b86..38e553e2a 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntitySlave.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntitySlave.java @@ -73,24 +73,6 @@ public IDrawer getDrawer (int slot) { return controller.getDrawer(slot); } - @Override - public IDrawer getDrawerIfEnabled (int slot) { - TileEntityController controller = getController(); - if (controller == null || !controller.isValidSlave(getPos())) - return null; - - return controller.getDrawerIfEnabled(slot); - } - - @Override - public boolean isDrawerEnabled (int slot) { - TileEntityController controller = getController(); - if (controller == null || !controller.isValidSlave(getPos())) - return false; - - return controller.isDrawerEnabled(slot); - } - @Override public void markDirty () { TileEntityController controller = getController(); @@ -100,15 +82,6 @@ public void markDirty () { super.markDirty(); } - @Override - public boolean markDirtyIfNeeded () { - TileEntityController controller = getController(); - if (controller != null && controller.isValidSlave(getPos())) - return controller.markDirtyIfNeeded(); - - return false; - } - @Override public Iterable enumerateDrawersForInsertion (@Nonnull ItemStack stack, boolean strict) { TileEntityController controller = getController(); @@ -127,7 +100,7 @@ public Iterable enumerateDrawersForExtraction (@Nonnull ItemStack stack return controller.enumerateDrawersForExtraction(stack, strict); } - private DrawerItemHandler itemHandler = new DrawerItemHandler(this); + private DrawerItemHandler itemHandler = new DrawerItemHandler(this, this); @Override public boolean hasCapability (Capability capability, EnumFacing facing) { diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java index f494a45bd..fbe01f217 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java @@ -1,16 +1,15 @@ package com.jaquadro.minecraft.storagedrawers.block.tile.tiledata; import com.jaquadro.minecraft.chameleon.block.tiledata.TileDataShim; -import com.jaquadro.minecraft.storagedrawers.api.storage.EmptyDrawerAttributes; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; +import com.jaquadro.minecraft.storagedrawers.api.storage.*; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; import com.jaquadro.minecraft.storagedrawers.inventory.ItemStackHelper; import com.jaquadro.minecraft.storagedrawers.storage.BaseDrawerData; +import com.jaquadro.minecraft.storagedrawers.util.CompactingHelper; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; +import net.minecraft.world.World; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.common.capabilities.ICapabilityProvider; @@ -18,19 +17,23 @@ import net.minecraftforge.common.util.INBTSerializable; import javax.annotation.Nonnull; +import java.util.Stack; public class FractionalDrawerGroup extends TileDataShim implements IDrawerGroup { private FractionalStorage storage; private FractionalDrawer[] slots; - public FractionalDrawerGroup (ICapabilityProvider capProvider, int slotCount) { - storage = new FractionalStorage(capProvider, this, slotCount); + public FractionalDrawerGroup (int slotCount) { + storage = new FractionalStorage(this, slotCount); slots = new FractionalDrawer[slotCount]; - for (int i = 0; i < slotCount; i++) { + for (int i = 0; i < slotCount; i++) slots[i] = new FractionalDrawer(storage, i); - } + } + + public void setCapabilityProvider (ICapabilityProvider capProvider) { + storage.setCapabilityProvider(capProvider); } @Override @@ -39,10 +42,21 @@ public int getDrawerCount () { } @Override - public FractionalDrawer getDrawer (int slot) { + public IFractionalDrawer getDrawer (int slot) { + if (slot < 0 || slot >= slots.length) + return Drawers.DISABLED_FRACTIONAL; + return slots[slot]; } + public int getPooledCount () { + return storage.getPooledCount(); + } + + public void setPooledCount (int count) { + storage.setPooledCount(count); + } + @Override public void readFromNBT (NBTTagCompound tag) { if (tag.hasKey("Drawers")) @@ -55,6 +69,18 @@ public NBTTagCompound writeToNBT (NBTTagCompound tag) { return tag; } + protected World getWorld () { return null; } + + protected void log (String message) { } + + protected int getStackCapacity () { + return 0; + } + + protected void onItemChanged () { } + + protected void onAmountChanged () { } + private static class FractionalStorage implements INBTSerializable { @CapabilityInject(IDrawerAttributes.class) @@ -68,7 +94,7 @@ private static class FractionalStorage implements INBTSerializable resultStack = new Stack<>(); + + @Nonnull ItemStack lookupTarget = itemPrototype; + for (int i = 0; i < slotCount; i++) { + CompactingHelper.Result lookup = compacting.findHigherTier(lookupTarget); + if (lookup.getStack().isEmpty()) + break; + + resultStack.push(lookup); + lookupTarget = lookup.getStack(); + } + + int index = 0; + for (int n = resultStack.size(); index < n; index++) { + CompactingHelper.Result result = resultStack.pop(); + populateRawSlot(index, result.getStack(), result.getSize()); + group.log("Picked candidate " + result.getStack().toString() + " with conv=" + result.getSize()); + + for (int i = 0; i < index - 1; i++) + convRate[i] *= result.getSize(); + } + + if (index == slotCount) + return; + + populateRawSlot(index, itemPrototype, 1); + + lookupTarget = itemPrototype; + for (; index < slotCount; index++) { + CompactingHelper.Result lookup = compacting.findLowerTier(lookupTarget); + if (lookup.getStack().isEmpty()) + break; + + populateRawSlot(index, lookup.getStack(), lookup.getSize()); + group.log("Picked candidate " + lookup.getStack().toString() + " with conv=" + lookup.getSize()); + + for (int i = 0; i < index - 1; i++) + convRate[i] *= lookup.getSize(); + + lookupTarget = lookup.getStack(); + } + } + + private void populateRawSlot (int slot, @Nonnull ItemStack itemPrototype, int rate) { + protoStack[slot] = itemPrototype; + convRate[slot] = rate; } @Override @@ -310,26 +428,20 @@ public void deserializeNBT (NBTTagCompound tag) { convRate[slot] = slotTag.getByte("Conv"); } - for (int i = 0; i < slotCount; i++) - group.getDrawer(i).reset(); - - onItemChanged(); - } - - private void populateSlots(@Nonnull ItemStack stack) { - + resetDrawers(); + group.onItemChanged(); } - protected int getStackCapacity() { - return 0; + private void resetDrawers () { + for (int i = 0; i < slotCount; i++) { + IFractionalDrawer drawer = group.getDrawer(i); + if (drawer instanceof FractionalDrawer) + ((FractionalDrawer) drawer).reset(false); + } } - - protected void onItemChanged() { } - - protected void onAmountChanged() { } } - private static class FractionalDrawer extends BaseDrawerData + private static class FractionalDrawer extends BaseDrawerData implements IFractionalDrawer { private FractionalStorage storage; private int slot; @@ -407,8 +519,8 @@ public boolean isEnabled () { } @Override - protected void reset () { - super.reset(); + protected void reset (boolean notify) { + super.reset(notify); refreshOreDictMatches(); } @@ -422,5 +534,20 @@ public NBTTagCompound serializeNBT () { public void deserializeNBT (NBTTagCompound nbt) { // Handled by group } + + @Override + public int getConversionRate () { + return storage.getConversionRate(slot); + } + + @Override + public int getStoredItemRemainder () { + return storage.getStoredItemRemainder(slot); + } + + @Override + public boolean isSmallestUnit () { + return storage.isSmallestUnit(slot); + } } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java new file mode 100644 index 000000000..7ce96cfad --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java @@ -0,0 +1,69 @@ +package com.jaquadro.minecraft.storagedrawers.block.tile.tiledata; + +import com.jaquadro.minecraft.chameleon.block.tiledata.TileDataShim; +import com.jaquadro.minecraft.storagedrawers.api.storage.Drawers; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; +import com.jaquadro.minecraft.storagedrawers.storage.DrawerData; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraftforge.common.capabilities.ICapabilityProvider; +import net.minecraftforge.common.util.Constants; + +import javax.annotation.Nonnull; + +public abstract class StandardDrawerGroup extends TileDataShim implements IDrawerGroup +{ + private DrawerData[] slots; + + public StandardDrawerGroup (int slotCount) { + slots = new DrawerData[slotCount]; + for (int i = 0; i < slotCount; i++) + slots[i] = createDrawer(i); + } + + public void setCapabilityProvider (ICapabilityProvider capProvider) { + for (DrawerData slot : slots) + slot.setCapabilityProvider(capProvider); + } + + @Override + public int getDrawerCount () { + return slots.length; + } + + @Override + @Nonnull + public IDrawer getDrawer (int slot) { + if (slot < 0 || slot >= slots.length) + return Drawers.DISABLED; + + return slots[slot]; + } + + @Override + public void readFromNBT (NBTTagCompound tag) { + if (!tag.hasKey("Drawers")) + return; + + NBTTagList itemList = tag.getTagList("Items", Constants.NBT.TAG_COMPOUND); + for (int i = 0; i < itemList.tagCount(); i++) { + if (i >= 0 && i < slots.length) + slots[i].deserializeNBT(itemList.getCompoundTagAt(i)); + } + } + + @Override + public NBTTagCompound writeToNBT (NBTTagCompound tag) { + NBTTagList itemList = new NBTTagList(); + for (DrawerData slot : slots) + itemList.appendTag(slot.serializeNBT()); + + tag.setTag("Drawers", itemList); + + return tag; + } + + @Nonnull + protected abstract DrawerData createDrawer (int slot); +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java index ceae409eb..2088ce401 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java @@ -16,7 +16,7 @@ import javax.annotation.Nonnull; -public class UpgradeData extends TileDataShim implements INBTSerializable +public class UpgradeData extends TileDataShim { private final ItemStack[] upgrades; private int storageMultiplier; @@ -226,7 +226,7 @@ private void syncRedstoneLevel () { } @Override - public NBTTagList serializeNBT () { + public NBTTagCompound serializeNBT () { NBTTagList tagList = new NBTTagList(); for (int i = 0; i < upgrades.length; i++) { if (!upgrades[i].isEmpty()) { @@ -237,30 +237,40 @@ public NBTTagList serializeNBT () { } } - return tagList; + NBTTagCompound tag = new NBTTagCompound(); + tag.setTag("Upgrades", tag); + + return tag; } @Override - public void deserializeNBT (NBTTagList tagList) { + public void deserializeNBT (NBTTagCompound tag) { + for (int i = 0; i < upgrades.length; i++) + upgrades[i] = ItemStack.EMPTY; + + if (!tag.hasKey("Upgrades")) + return; + + NBTTagList tagList = tag.getTagList("Upgrades", Constants.NBT.TAG_COMPOUND); for (int i = 0; i < tagList.tagCount(); i++) { NBTTagCompound upgradeTag = tagList.getCompoundTagAt(i); int slot = upgradeTag.getByte("Slot"); - setUpgrade(slot, new ItemStack(upgradeTag)); + upgrades[slot] = new ItemStack(upgradeTag); } + + syncUpgrades(); } @Override public void readFromNBT (NBTTagCompound tag) { - if (tag.hasKey("Upgrades")) - deserializeNBT(tag.getTagList("Upgrades", Constants.NBT.TAG_COMPOUND)); + deserializeNBT(tag); } @Override public NBTTagCompound writeToNBT (NBTTagCompound tag) { - NBTTagList upgradeList = serializeNBT(); - if (upgradeList.tagCount() > 0) - tag.setTag("Upgrades", upgradeList); + NBTTagCompound stag = serializeNBT(); + tag.setTag("Upgrades", stag.getCompoundTag("Upgrades")); return tag; } diff --git a/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java b/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java index e56c94571..1be219202 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java +++ b/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java @@ -104,8 +104,8 @@ private void renderFastItemSet (ChamRender renderer, TileEntityDrawers tile, IBl for (int i = 0; i < drawerCount; i++) { renderStacks[i] = ItemStack.EMPTY; - IDrawer drawer = tile.getDrawerIfEnabled(i); - if (drawer == null || drawer.isEmpty()) + IDrawer drawer = tile.getDrawer(i); + if (!drawer.isEnabled() || drawer.isEmpty()) continue; ItemStack itemStack = drawer.getStoredItemPrototype(); diff --git a/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java b/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java index 8e4a4b450..c38cd9fcc 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java +++ b/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java @@ -108,8 +108,8 @@ public List getWailaBody (@Nonnull ItemStack itemStack, List cur if (SecurityManager.hasAccess(Minecraft.getMinecraft().player.getGameProfile(), tile)) { if (config.getConfig("display.content")) { for (int i = 0; i < tile.getDrawerCount(); i++) { - IDrawer drawer = tile.getDrawerIfEnabled(i); - if (drawer == null) + IDrawer drawer = tile.getDrawer(i); + if (!drawer.isEnabled()) continue; String name = I18n.format("storagedrawers.waila.empty"); diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerInventoryHelper.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerInventoryHelper.java index 9771d3368..d4d35c9b9 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerInventoryHelper.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerInventoryHelper.java @@ -15,8 +15,8 @@ public class DrawerInventoryHelper public static void dropInventoryItems (World world, BlockPos pos, IDrawerGroup group) { for (int i = 0; i < group.getDrawerCount(); i++) { - IDrawer drawer = group.getDrawerIfEnabled(i); - if (drawer == null) + IDrawer drawer = group.getDrawer(i); + if (!drawer.isEnabled()) continue; while (drawer.getStoredItemCount() > 0) { diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java index 4357df4c4..2bcc77dc8 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java @@ -2,7 +2,6 @@ import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.api.storage.*; -import net.minecraft.init.Items; import net.minecraft.item.ItemStack; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityInject; @@ -19,11 +18,9 @@ public class DrawerItemHandler implements IItemHandler private IDrawerGroup group; private ICapabilityProvider capProvider; - public DrawerItemHandler (IDrawerGroup group) { + public DrawerItemHandler (IDrawerGroup group, ICapabilityProvider capProvider) { this.group = group; - - if (group instanceof ICapabilityProvider) - capProvider = (ICapabilityProvider)group; + this.capProvider = capProvider; } @Override @@ -43,8 +40,8 @@ public ItemStack getStackInSlot (int slot) { slot = (slot >= 0 && slot < order.length) ? order[slot] : -1; } - IDrawer drawer = group.getDrawerIfEnabled(slot); - if (drawer == null || drawer.isEmpty()) + IDrawer drawer = group.getDrawer(slot); + if (!drawer.isEnabled() || drawer.isEmpty()) return ItemStack.EMPTY; ItemStack stack = drawer.getStoredItemPrototype().copy(); @@ -74,15 +71,13 @@ public ItemStack insertItem (int slot, @Nonnull ItemStack stack, boolean simulat } if (StorageDrawers.config.cache.enableItemConversion && orderedSlot > 0) { - IDrawer drawer = group.getDrawerIfEnabled(orderedSlot); - if (drawer != null && drawer.isEmpty()) { - if (prevSlot == -1 || !group.isDrawerEnabled(prevSlot)) + IDrawer drawer = group.getDrawer(orderedSlot); + if (!drawer.isEnabled() && drawer.isEmpty()) { + IDrawer prevDrawer = group.getDrawer(prevSlot); + if (!prevDrawer.isEnabled()) + return insertItemFullScan(stack, simulate); + else if (!prevDrawer.isEmpty()) return insertItemFullScan(stack, simulate); - else { - IDrawer prevDrawer = group.getDrawer(prevSlot); - if (!prevDrawer.isEmpty()) - return insertItemFullScan(stack, simulate); - } } } @@ -119,8 +114,8 @@ else if (group instanceof IPriorityGroup) { @Nonnull private ItemStack insertItemInternal (int slot, @Nonnull ItemStack stack, boolean simulate) { - IDrawer drawer = group.getDrawerIfEnabled(slot); - if (drawer == null || !drawer.canItemBeStored(stack)) + IDrawer drawer = group.getDrawer(slot); + if (!drawer.isEnabled() || !drawer.canItemBeStored(stack)) return stack; int availableCount = drawer.isEmpty() ? drawer.getMaxCapacity(stack) : drawer.getRemainingCapacity(); @@ -139,9 +134,8 @@ private ItemStack insertItemInternal (int slot, @Nonnull ItemStack stack, boolea if (!simulate) { if (drawer.isEmpty()) - drawer.setStoredItem(stack, insertCount); - else - drawer.setStoredItemCount(drawer.getStoredItemCount() + insertCount); + drawer.setStoredItem(stack); + drawer.setStoredItemCount(drawer.getStoredItemCount() + insertCount); } if (remainder == 0) @@ -165,8 +159,8 @@ public ItemStack extractItem (int slot, int amount, boolean simulate) { slot = (slot >= 0 && slot < order.length) ? order[slot] : -1; } - IDrawer drawer = group.getDrawerIfEnabled(slot); - if (drawer == null || drawer.isEmpty() || drawer.getStoredItemCount() == 0) + IDrawer drawer = group.getDrawer(slot); + if (!drawer.isEnabled() || drawer.isEmpty() || drawer.getStoredItemCount() == 0) return ItemStack.EMPTY; ItemStack returnStack = drawer.getStoredItemPrototype().copy(); @@ -186,11 +180,11 @@ public int getSlotLimit (int slot) { if (slotIsVirtual(slot)) return Integer.MAX_VALUE; - IDrawer drawer = group.getDrawerIfEnabled(slot); - if (drawer == null) + IDrawer drawer = group.getDrawer(slot); + if (!drawer.isEnabled()) return 0; if (drawer.isEmpty()) - return drawer.getDefaultMaxCapacity(); + return drawer.getMaxCapacity(ItemStack.EMPTY); return drawer.getMaxCapacity(); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/SlotDrawer.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/SlotDrawer.java index 40e17f29b..0185f0a26 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/SlotDrawer.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/SlotDrawer.java @@ -39,10 +39,9 @@ public ItemStack getStack () { @Override public void putStack (@Nonnull ItemStack stack) { - if (ItemStackHelper.isStackEncoded(stack)) - drawer.setStoredItem(stack, 0); - else - drawer.setStoredItem(stack, stack.getCount()); + drawer.setStoredItem(stack); + if (!ItemStackHelper.isStackEncoded(stack)) + drawer.setStoredItemCount(stack.getCount()); } @Override diff --git a/src/com/jaquadro/minecraft/storagedrawers/item/ItemDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/item/ItemDrawers.java index 62b27707f..1a49d4e9c 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/item/ItemDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/item/ItemDrawers.java @@ -48,9 +48,6 @@ public boolean placeBlockAt (@Nonnull ItemStack stack, EntityPlayer player, Worl tile.setDirection(side.ordinal()); if (tile instanceof TileEntityDrawersStandard) { - EnumBasicDrawer info = EnumBasicDrawer.byMetadata(stack.getMetadata()); - ((TileEntityDrawersStandard) tile).setDrawerCount(info.getDrawerCount()); - if (stack.hasTagCompound() && stack.getTagCompound().hasKey("tile")) tile.readFromPortableNBT(stack.getTagCompound().getCompoundTag("tile")); diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java index 8e3502919..c1040e540 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java @@ -22,7 +22,7 @@ protected BaseDrawerData () { } - protected void reset () { + protected void reset (boolean notify) { oreDictMatches = null; } diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java index caa90aaa1..0bdb38ef0 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java @@ -1,15 +1,12 @@ package com.jaquadro.minecraft.storagedrawers.storage; -import com.jaquadro.minecraft.storagedrawers.api.event.DrawerPopulatedEvent; import com.jaquadro.minecraft.storagedrawers.api.storage.EmptyDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*; import com.jaquadro.minecraft.storagedrawers.inventory.ItemStackHelper; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.common.capabilities.ICapabilityProvider; @@ -31,20 +28,16 @@ public class DrawerData extends BaseDrawerData private ItemStack protoStack; private int count; - private int stackCapacity; - - public DrawerData (ICapabilityProvider capProvider) { - //this.capProvider = capProvider; - - attrs = capProvider.getCapability(ATTR_CAPABILITY, null); - if (attrs == null) - attrs = new EmptyDrawerAttributes(); + public DrawerData () { + attrs = new EmptyDrawerAttributes(); protoStack = ItemStack.EMPTY; + } - //updateAttributeCache(); - - //postInit(); + public void setCapabilityProvider (ICapabilityProvider capProvider) { + IDrawerAttributes capAttrs = capProvider.getCapability(ATTR_CAPABILITY, null); + if (capAttrs != null) + attrs = capAttrs; } @Override @@ -54,14 +47,19 @@ public ItemStack getStoredItemPrototype () { } @Override + @Nonnull public IDrawer setStoredItem (@Nonnull ItemStack itemPrototype) { + return setStoredItem(itemPrototype, true); + } + + protected IDrawer setStoredItem (@Nonnull ItemStack itemPrototype, boolean notify) { if (areItemsEqual(itemPrototype)) { return this; } itemPrototype = ItemStackHelper.getItemPrototype(itemPrototype); if (itemPrototype.isEmpty()) { - reset(); + reset(notify); return this; } @@ -72,7 +70,9 @@ public IDrawer setStoredItem (@Nonnull ItemStack itemPrototype) { // TODO: Oredict blah blah // refreshOreDictMatches(); - onItemChanged(); + if (notify) + onItemChanged(); + return this; } @@ -89,6 +89,10 @@ public int getStoredItemCount () { @Override public void setStoredItemCount (int amount) { + setStoredItemCount(amount, true); + } + + protected void setStoredItemCount (int amount, boolean notify) { if (protoStack.isEmpty() || count == amount) return; @@ -99,13 +103,19 @@ public void setStoredItemCount (int amount) { count = Math.max(count, 0); if (amount == 0 && !attrs.isItemLocked(LockAttribute.LOCK_POPULATED)) - reset(); - else - onAmountChanged(); + reset(notify); + else { + if (notify) + onAmountChanged(); + } } @Override public int adjustStoredItemCount (int amount) { + return adjustStoredItemCount(amount, true); + } + + protected int adjustStoredItemCount (int amount, boolean notify) { if (protoStack.isEmpty() || amount == 0) return amount; @@ -116,7 +126,7 @@ public int adjustStoredItemCount (int amount) { int originalCount = count; count = Math.min(amount, getMaxCapacity()); - if (count != originalCount) + if (count != originalCount && notify) onAmountChanged(); if (attrs.isVoid()) @@ -126,7 +136,7 @@ public int adjustStoredItemCount (int amount) { } else { int originalCount = count; - setStoredItemCount(originalCount + amount); + setStoredItemCount(originalCount + amount, notify); return amount - (count - originalCount); } @@ -201,13 +211,14 @@ public void attributeChanged () { }*/ @Override - protected void reset () { + protected void reset (boolean notify) { protoStack = ItemStack.EMPTY; count = 0; - super.reset(); + super.reset(notify); - onItemChanged(); + if (notify) + onItemChanged(); } @Override @@ -232,12 +243,12 @@ public void deserializeNBT (NBTTagCompound nbt) { if (nbt.hasKey("Count")) tagCount = nbt.getInteger("Count"); - setStoredItem(tagItem); - setStoredItemCount(tagCount); + setStoredItem(tagItem, false); + setStoredItemCount(tagCount, false); } protected int getStackCapacity() { - return stackCapacity; + return 0; } protected void onItemChanged() { } diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/ICentralInventory.java b/src/com/jaquadro/minecraft/storagedrawers/storage/ICentralInventory.java deleted file mode 100644 index 92d431595..000000000 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/ICentralInventory.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.jaquadro.minecraft.storagedrawers.storage; - -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; -import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; - -import javax.annotation.Nonnull; - -public interface ICentralInventory -{ - @Nonnull - ItemStack getStoredItemPrototype (int slot); - - IDrawer setStoredItem (int slot, @Nonnull ItemStack itemPrototype, int amount); - - int getStoredItemCount (int slot); - - void setStoredItemCount (int slot, int amount); - - int getMaxCapacity (int slot); - - int getMaxCapacity (int slot, @Nonnull ItemStack itemPrototype); - - int getDefaultMaxCapacity (int slot); - - int getRemainingCapacity (int slot); - - int getStoredItemStackSize (int slot); - - int getItemCapacityForInventoryStack (int slot); - - int getConversionRate (int slot); - - int getStoredItemRemainder (int slot); - - boolean isSmallestUnit (int slot); - - //boolean isVoidSlot (int slot); - - //boolean isShroudedSlot (int slot); - - //boolean setIsSlotShrouded (int slot, boolean state); - - //boolean isSlotShowingQuantity (int slot); - - //boolean setIsSlotShowingQuantity (int slot, boolean state); - - //boolean isLocked (int slot, LockAttribute attr); - - void readFromNBT (int slot, NBTTagCompound tag); - - void writeToNBT (int slot, NBTTagCompound tag); -} diff --git a/src/com/jaquadro/minecraft/storagedrawers/util/CompactingHelper.java b/src/com/jaquadro/minecraft/storagedrawers/util/CompactingHelper.java new file mode 100644 index 000000000..660de473b --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/util/CompactingHelper.java @@ -0,0 +1,274 @@ +package com.jaquadro.minecraft.storagedrawers.util; + +import com.jaquadro.minecraft.storagedrawers.StorageDrawers; +import com.jaquadro.minecraft.storagedrawers.config.CompTierRegistry; +import com.jaquadro.minecraft.storagedrawers.storage.DrawerData; +import net.minecraft.inventory.InventoryCrafting; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.CraftingManager; +import net.minecraft.item.crafting.IRecipe; +import net.minecraft.item.crafting.Ingredient; +import net.minecraft.util.NonNullList; +import net.minecraft.util.ResourceLocation; +import net.minecraft.world.World; + +import javax.annotation.Nonnull; +import java.util.*; + +public class CompactingHelper +{ + private static InventoryLookup lookup1 = new InventoryLookup(1, 1); + private static InventoryLookup lookup2 = new InventoryLookup(2, 2); + private static InventoryLookup lookup3 = new InventoryLookup(3, 3); + + private World world; + + public class Result + { + @Nonnull + private ItemStack stack; + private int size; + + public Result (ItemStack stack, int size) { + this.stack = stack; + this.size = size; + } + + @Nonnull + public ItemStack getStack () { + return stack; + } + + public int getSize () { + return size; + } + } + + public CompactingHelper (World world) { + this.world = world; + } + + @Nonnull + public Result findHigherTier (@Nonnull ItemStack stack) { + if (!world.isRemote && StorageDrawers.config.cache.debugTrace) + StorageDrawers.log.info("Finding ascending candidates for " + stack.toString()); + + CompTierRegistry.Record record = StorageDrawers.compRegistry.findHigherTier(stack); + if (record != null) { + if (!world.isRemote && StorageDrawers.config.cache.debugTrace) + StorageDrawers.log.info("Found " + record.upper.toString() + " in registry with conv=" + record.convRate); + + return new Result(record.upper, record.convRate); + } + + List candidates = new ArrayList<>(); + + int lookupSize = setupLookup(lookup3, stack); + List fwdCandidates = findAllMatchingRecipes(lookup3); + + if (fwdCandidates.size() == 0) { + lookupSize = setupLookup(lookup2, stack); + fwdCandidates = findAllMatchingRecipes(lookup2); + } + + if (fwdCandidates.size() > 0) { + for (ItemStack match : fwdCandidates) { + setupLookup(lookup1, match); + List backCandidates = findAllMatchingRecipes(lookup1); + + for (ItemStack comp : backCandidates) { + if (comp.getCount() != lookupSize) + continue; + + if (!DrawerData.areItemsEqual(comp, stack, false)) + continue; + + candidates.add(match); + if (!world.isRemote && StorageDrawers.config.cache.debugTrace) + StorageDrawers.log.info("Found ascending candidate for " + stack.toString() + ": " + match.toString() + " size=" + lookupSize + ", inverse=" + comp.toString()); + + break; + } + } + } + + ItemStack modMatch = findMatchingModCandidate(stack, candidates); + if (!modMatch.isEmpty()) + return new Result(modMatch, lookupSize); + + if (candidates.size() > 0) + return new Result(candidates.get(0), lookupSize); + + if (!world.isRemote && StorageDrawers.config.cache.debugTrace) + StorageDrawers.log.info("No candidates found"); + + return new Result(ItemStack.EMPTY, 0); + } + + @Nonnull + public Result findLowerTier (@Nonnull ItemStack stack) { + if (!world.isRemote && StorageDrawers.config.cache.debugTrace) + StorageDrawers.log.info("Finding descending candidates for " + stack.toString()); + + CompTierRegistry.Record record = StorageDrawers.compRegistry.findLowerTier(stack); + if (record != null) { + if (!world.isRemote && StorageDrawers.config.cache.debugTrace) + StorageDrawers.log.info("Found " + record.lower.toString() + " in registry with conv=" + record.convRate); + + return new Result(record.lower, record.convRate); + } + + List candidates = new ArrayList<>(); + Map candidatesRate = new HashMap<>(); + + for (IRecipe recipe : CraftingManager.REGISTRY) { + ItemStack output = recipe.getRecipeOutput(); + if (!DrawerData.areItemsEqual(stack, output, true)) + continue; + + @Nonnull ItemStack match = tryMatch(stack, recipe.getIngredients()); + if (!match.isEmpty()) { + int lookupSize = setupLookup(lookup1, output); + List compMatches = findAllMatchingRecipes(lookup1); + for (ItemStack comp : compMatches) { + int recipeSize = recipe.getIngredients().size(); + if (DrawerData.areItemsEqual(match, comp, true) && comp.getCount() == recipeSize) { + candidates.add(match); + candidatesRate.put(match, recipeSize); + + if (!world.isRemote && StorageDrawers.config.cache.debugTrace) + StorageDrawers.log.info("Found descending candidate for " + stack.toString() + ": " + match.toString() + " size=" + recipeSize + ", inverse=" + comp.toString()); + } else if (!world.isRemote && StorageDrawers.config.cache.debugTrace) + StorageDrawers.log.info("Back-check failed for " + match.toString() + " size=" + lookupSize + ", inverse=" + comp.toString()); + } + } + } + + ItemStack modMatch = findMatchingModCandidate(stack, candidates); + if (!modMatch.isEmpty()) + return new Result(modMatch, candidatesRate.get(modMatch)); + + if (candidates.size() > 0) { + ItemStack match = candidates.get(0); + return new Result(match, candidatesRate.get(match)); + } + + if (!world.isRemote && StorageDrawers.config.cache.debugTrace) + StorageDrawers.log.info("No candidates found"); + + return new Result(ItemStack.EMPTY, 0); + } + + private List findAllMatchingRecipes (InventoryCrafting crafting) { + List candidates = new ArrayList<>(); + + for (Object aRecipeList : CraftingManager.REGISTRY) { + IRecipe recipe = (IRecipe) aRecipeList; + if (recipe.matches(crafting, world)) { + ItemStack result = recipe.getCraftingResult(crafting); + if (!result.isEmpty()) + candidates.add(result); + } + } + + return candidates; + } + + @Nonnull + private ItemStack findMatchingModCandidate (@Nonnull ItemStack reference, List candidates) { + ResourceLocation referenceName = reference.getItem().getRegistryName(); + if (referenceName != null) { + for (ItemStack candidate : candidates) { + ResourceLocation matchName = candidate.getItem().getRegistryName(); + if (matchName != null) { + if (referenceName.getResourceDomain().equals(matchName.getResourceDomain())) + return candidate; + } + } + } + + return ItemStack.EMPTY; + } + + @Nonnull + private ItemStack tryMatch (@Nonnull ItemStack stack, NonNullList ingredients) { + if (ingredients.size() != 9 && ingredients.size() != 4) + return ItemStack.EMPTY; + + Ingredient refIngredient = ingredients.get(0); + ItemStack[] refMatchingStacks = refIngredient.getMatchingStacks(); + if (refMatchingStacks.length == 0) + return ItemStack.EMPTY; + + for (int i = 1, n = ingredients.size(); i < n; i++) { + Ingredient ingredient = ingredients.get(i); + @Nonnull ItemStack match = ItemStack.EMPTY; + + for (ItemStack ingItemMatch : refMatchingStacks) { + if (ingredient.apply(ingItemMatch)) { + match = ingItemMatch; + break; + } + } + + if (match.isEmpty()) + return ItemStack.EMPTY; + } + + ItemStack match = findMatchingModCandidate(stack, Arrays.asList(refMatchingStacks)); + if (match.isEmpty()) + match = refMatchingStacks[0]; + + return match; + } + + private int setupLookup (InventoryLookup inv, @Nonnull ItemStack stack) { + for (int i = 0, n = inv.getSizeInventory(); i < n; i++) + inv.setInventorySlotContents(i, stack); + + return inv.getSizeInventory(); + } + + private static class InventoryLookup extends InventoryCrafting + { + private ItemStack[] stackList; + + public InventoryLookup (int width, int height) { + super(null, width, height); + + stackList = new ItemStack[width * height]; + for (int i = 0; i < stackList.length; i++) + stackList[i] = ItemStack.EMPTY; + } + + @Override + public int getSizeInventory () + { + return this.stackList.length; + } + + @Override + @Nonnull + public ItemStack getStackInSlot (int slot) + { + return slot >= this.getSizeInventory() ? ItemStack.EMPTY : this.stackList[slot]; + } + + @Override + @Nonnull + public ItemStack removeStackFromSlot (int slot) { + return ItemStack.EMPTY; + } + + @Override + @Nonnull + public ItemStack decrStackSize (int slot, int count) { + return ItemStack.EMPTY; + } + + @Override + public void setInventorySlotContents (int slot, @Nonnull ItemStack stack) { + stackList[slot] = stack; + } + } +} From 3c10cf8727e2408ee9ab60376346606edcfbb51b Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sun, 2 Jul 2017 02:40:52 -0400 Subject: [PATCH 14/22] Various bugfixes --- .../block/BlockStandardDrawers.java | 2 +- .../block/tile/TileEntityDrawers.java | 4 +- .../block/tile/TileEntityDrawersStandard.java | 67 ++++++++++++++++--- .../tile/tiledata/FractionalDrawerGroup.java | 8 +-- .../tile/tiledata/StandardDrawerGroup.java | 7 +- .../block/tile/tiledata/UpgradeData.java | 37 +++------- .../storagedrawers/core/ModBlocks.java | 4 ++ .../inventory/DrawerItemHandler.java | 2 +- .../storagedrawers/inventory/SlotDrawer.java | 4 +- 9 files changed, 87 insertions(+), 48 deletions(-) diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockStandardDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockStandardDrawers.java index 5165c0071..60e99c3a6 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockStandardDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockStandardDrawers.java @@ -186,7 +186,7 @@ public TileEntityDrawers createNewTileEntity (World world, int meta) { IBlockState state = getStateFromMeta(meta); EnumBasicDrawer type = state.getValue(BLOCK); - return new TileEntityDrawersStandard(type.getDrawerCount()); + return TileEntityDrawersStandard.createEntity(type.getDrawerCount()); } @Override diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index efc4226e7..13a2354c8 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -393,7 +393,7 @@ public int putItemsIntoSlot (int slot, @Nonnull ItemStack stack, int count) { return 0; if (drawer.isEmpty()) - drawer.setStoredItem(stack); + drawer = drawer.setStoredItem(stack); if (!drawer.canItemBeStored(stack)) return 0; @@ -768,7 +768,7 @@ public void clientUpdateCount (final int slot, final int count) { @SideOnly(Side.CLIENT) private void clientUpdateCountAsync (int slot, int count) { IDrawer drawer = getDrawer(slot); - if (!drawer.isEnabled() && drawer.getStoredItemCount() != count) + if (drawer.isEnabled() && drawer.getStoredItemCount() != count) drawer.setStoredItemCount(count); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java index 03e5fd865..93133e3c5 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java @@ -3,7 +3,6 @@ import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.api.event.DrawerPopulatedEvent; import com.jaquadro.minecraft.storagedrawers.api.storage.EnumBasicDrawer; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; import com.jaquadro.minecraft.storagedrawers.block.BlockStandardDrawers; import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.StandardDrawerGroup; @@ -17,7 +16,6 @@ import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.common.capabilities.ICapabilityProvider; import javax.annotation.Nonnull; @@ -27,24 +25,69 @@ public class TileEntityDrawersStandard extends TileEntityDrawers null, StorageDrawers.MOD_ID + ":basicDrawers1", StorageDrawers.MOD_ID + ":basicDrawers2", null, StorageDrawers.MOD_ID + ":basicDrawers4" }; - private GroupData groupData; - private int capacity = 0; - public TileEntityDrawersStandard () { - this(1); + public static class Slot1 extends TileEntityDrawersStandard + { + private GroupData groupData = new GroupData(1); + + public Slot1 () { + groupData.setCapabilityProvider(this); + injectPortableData(groupData); + } + + @Override + protected IDrawerGroup getGroup () { + return groupData; + } + } + + public static class Slot2 extends TileEntityDrawersStandard + { + private GroupData groupData = new GroupData(2); + + public Slot2 () { + groupData.setCapabilityProvider(this); + injectPortableData(groupData); + } + + @Override + protected IDrawerGroup getGroup () { + return groupData; + } } - public TileEntityDrawersStandard (int count) { - groupData = new GroupData(count); - groupData.setCapabilityProvider(this); + public static class Slot4 extends TileEntityDrawersStandard + { + private GroupData groupData = new GroupData(4); + + public Slot4 () { + groupData.setCapabilityProvider(this); + injectPortableData(groupData); + } + + @Override + protected IDrawerGroup getGroup () { + return groupData; + } + } - injectPortableData(groupData); + public static TileEntityDrawersStandard createEntity (int slotCount) { + switch (slotCount) { + case 1: + return new Slot1(); + case 2: + return new Slot2(); + case 4: + return new Slot4(); + default: + return null; + } } @Override protected IDrawerGroup getGroup () { - return groupData; + return null; } @Override @@ -108,6 +151,8 @@ public int getDrawerCapacity () { private class GroupData extends StandardDrawerGroup { + public GroupData () { } + public GroupData (int slotCount) { super(slotCount); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java index fbe01f217..fb37bc155 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java @@ -306,7 +306,7 @@ public int getStoredItemRemainder (int slot) { if (slot == 0) return pooledCount / baseRate(); - return pooledCount % (convRate[slot - 1] / convRate[slot]); + return (pooledCount / convRate[slot]) % (convRate[slot - 1] / convRate[slot]); } public boolean isSmallestUnit (int slot) { @@ -362,7 +362,7 @@ private void populateSlots (@Nonnull ItemStack itemPrototype) { if (index == slotCount) return; - populateRawSlot(index, itemPrototype, 1); + populateRawSlot(index++, itemPrototype, 1); lookupTarget = itemPrototype; for (; index < slotCount; index++) { @@ -370,10 +370,10 @@ private void populateSlots (@Nonnull ItemStack itemPrototype) { if (lookup.getStack().isEmpty()) break; - populateRawSlot(index, lookup.getStack(), lookup.getSize()); + populateRawSlot(index, lookup.getStack(), 1); group.log("Picked candidate " + lookup.getStack().toString() + " with conv=" + lookup.getSize()); - for (int i = 0; i < index - 1; i++) + for (int i = 0; i < index; i++) convRate[i] *= lookup.getSize(); lookupTarget = lookup.getStack(); diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java index 7ce96cfad..a82d85961 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java @@ -16,6 +16,8 @@ public abstract class StandardDrawerGroup extends TileDataShim implements IDrawe { private DrawerData[] slots; + public StandardDrawerGroup () { } + public StandardDrawerGroup (int slotCount) { slots = new DrawerData[slotCount]; for (int i = 0; i < slotCount; i++) @@ -46,7 +48,7 @@ public void readFromNBT (NBTTagCompound tag) { if (!tag.hasKey("Drawers")) return; - NBTTagList itemList = tag.getTagList("Items", Constants.NBT.TAG_COMPOUND); + NBTTagList itemList = tag.getTagList("Drawers", Constants.NBT.TAG_COMPOUND); for (int i = 0; i < itemList.tagCount(); i++) { if (i >= 0 && i < slots.length) slots[i].deserializeNBT(itemList.getCompoundTagAt(i)); @@ -55,6 +57,9 @@ public void readFromNBT (NBTTagCompound tag) { @Override public NBTTagCompound writeToNBT (NBTTagCompound tag) { + if (slots == null) + return tag; + NBTTagList itemList = new NBTTagList(); for (DrawerData slot : slots) itemList.appendTag(slot.serializeNBT()); diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java index 2088ce401..79daeda33 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java @@ -226,25 +226,7 @@ private void syncRedstoneLevel () { } @Override - public NBTTagCompound serializeNBT () { - NBTTagList tagList = new NBTTagList(); - for (int i = 0; i < upgrades.length; i++) { - if (!upgrades[i].isEmpty()) { - NBTTagCompound upgradeTag = upgrades[i].writeToNBT(new NBTTagCompound()); - upgradeTag.setByte("Slot", (byte)i); - - tagList.appendTag(upgradeTag); - } - } - - NBTTagCompound tag = new NBTTagCompound(); - tag.setTag("Upgrades", tag); - - return tag; - } - - @Override - public void deserializeNBT (NBTTagCompound tag) { + public void readFromNBT (NBTTagCompound tag) { for (int i = 0; i < upgrades.length; i++) upgrades[i] = ItemStack.EMPTY; @@ -262,16 +244,19 @@ public void deserializeNBT (NBTTagCompound tag) { syncUpgrades(); } - @Override - public void readFromNBT (NBTTagCompound tag) { - deserializeNBT(tag); - } - @Override public NBTTagCompound writeToNBT (NBTTagCompound tag) { - NBTTagCompound stag = serializeNBT(); - tag.setTag("Upgrades", stag.getCompoundTag("Upgrades")); + NBTTagList tagList = new NBTTagList(); + for (int i = 0; i < upgrades.length; i++) { + if (!upgrades[i].isEmpty()) { + NBTTagCompound upgradeTag = upgrades[i].writeToNBT(new NBTTagCompound()); + upgradeTag.setByte("Slot", (byte)i); + + tagList.appendTag(upgradeTag); + } + } + tag.setTag("Upgrades", tagList); return tag; } diff --git a/src/com/jaquadro/minecraft/storagedrawers/core/ModBlocks.java b/src/com/jaquadro/minecraft/storagedrawers/core/ModBlocks.java index 68fd68d8c..f5c97cc02 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/core/ModBlocks.java +++ b/src/com/jaquadro/minecraft/storagedrawers/core/ModBlocks.java @@ -70,6 +70,10 @@ public static void registerBlocks (RegistryEvent.Register event) { ); GameRegistry.registerTileEntity(TileEntityDrawersStandard.class, StorageDrawers.MOD_ID + ":basicdrawers"); + GameRegistry.registerTileEntity(TileEntityDrawersStandard.Slot1.class, StorageDrawers.MOD_ID + ":basicdrawers.1"); + GameRegistry.registerTileEntity(TileEntityDrawersStandard.Slot2.class, StorageDrawers.MOD_ID + ":basicdrawers.2"); + GameRegistry.registerTileEntity(TileEntityDrawersStandard.Slot4.class, StorageDrawers.MOD_ID + ":basicdrawers.4"); + GameRegistry.registerTileEntity(TileEntityKeyButton.class, StorageDrawers.MOD_ID + ":keybutton"); if (config.isBlockEnabled("compdrawers")) { diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java index 2bcc77dc8..11ac58d9b 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java @@ -134,7 +134,7 @@ private ItemStack insertItemInternal (int slot, @Nonnull ItemStack stack, boolea if (!simulate) { if (drawer.isEmpty()) - drawer.setStoredItem(stack); + drawer = drawer.setStoredItem(stack); drawer.setStoredItemCount(drawer.getStoredItemCount() + insertCount); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/SlotDrawer.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/SlotDrawer.java index 0185f0a26..e701407c0 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/SlotDrawer.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/SlotDrawer.java @@ -39,9 +39,9 @@ public ItemStack getStack () { @Override public void putStack (@Nonnull ItemStack stack) { - drawer.setStoredItem(stack); + IDrawer target = drawer.setStoredItem(stack); if (!ItemStackHelper.isStackEncoded(stack)) - drawer.setStoredItemCount(stack.getCount()); + target.setStoredItemCount(stack.getCount()); } @Override From 2e00c3de2ccb73341a822b08aba9361b24573713 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sun, 2 Jul 2017 12:48:37 -0400 Subject: [PATCH 15/22] comp drawer legacy loading --- .../block/tile/TileEntityDrawersComp.java | 81 -------- .../tile/tiledata/FractionalDrawerGroup.java | 53 +++++- .../renderer/TileEntityDrawersRenderer.java | 2 +- .../storage/CompDrawerData.java | 177 ------------------ 4 files changed, 53 insertions(+), 260 deletions(-) delete mode 100644 src/com/jaquadro/minecraft/storagedrawers/storage/CompDrawerData.java diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java index a5f855b3a..62ee276b0 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java @@ -1,7 +1,6 @@ package com.jaquadro.minecraft.storagedrawers.block.tile; import com.jaquadro.minecraft.storagedrawers.StorageDrawers; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.FractionalDrawerGroup; import com.jaquadro.minecraft.storagedrawers.config.ConfigManager; @@ -78,17 +77,6 @@ protected void onAmountChanged () { } } - // TODO - /* - public int getStoredItemRemainder (int slot) { - groupData.getDrawer(slot) - int count = centralInventory.getStoredItemCount(slot); - if (slot > 0 && convRate[slot] > 0) - count -= centralInventory.getStoredItemCount(slot - 1) * (convRate[slot - 1] / convRate[slot]); - - return count; - }*/ - @Override public Container createContainer (InventoryPlayer playerInventory, EntityPlayer playerIn) { return new ContainerDrawersComp(playerInventory, this); @@ -115,80 +103,11 @@ public int getDrawerCapacity () { return capacity; } - /* - @Override - public int putItemsIntoSlot (int slot, @Nonnull ItemStack stack, int count) { - int added = 0; - if (!stack.isEmpty() && convRate != null && convRate[0] == 0) { - populateSlots(stack); - - for (int i = 0; i < getDrawerCount(); i++) { - if (convRate[i] != 0 && BaseDrawerData.areItemsEqual(protoStack[i], stack)) - added = super.putItemsIntoSlot(i, stack, count); - } - - for (int i = 0; i < getDrawerCount(); i++) { - IDrawer drawer = getDrawer(i); - if (drawer instanceof CompDrawerData) - ((CompDrawerData) drawer).refresh(); - } - - - } - - return added + super.putItemsIntoSlot(slot, stack, count); - }*/ - @Override public boolean dataPacketRequiresRenderUpdate () { return true; } - /*@Override - public void readFromPortableNBT (NBTTagCompound tag) { - super.readFromPortableNBT(tag); - - pooledCount = 0; - - for (int i = 0; i < getDrawerCount(); i++) { - protoStack[i] = ItemStack.EMPTY; - convRate[i] = 0; - } - - super.readFromPortableNBT(tag); - - pooledCount = tag.getInteger("Count"); - - if (tag.hasKey("Conv0")) - convRate[0] = tag.getByte("Conv0"); - if (tag.hasKey("Conv1")) - convRate[1] = tag.getByte("Conv1"); - if (tag.hasKey("Conv2")) - convRate[2] = tag.getByte("Conv2"); - - for (int i = 0; i < getDrawerCount(); i++) { - IDrawer drawer = getDrawer(i); - if (drawer instanceof CompDrawerData) - ((CompDrawerData) drawer).refresh(); - } - } - - @Override - public NBTTagCompound writeToPortableNBT (NBTTagCompound tag) { - tag = super.writeToPortableNBT(tag); - - tag.setInteger("Count", pooledCount); - - if (convRate[0] > 0) - tag.setByte("Conv0", (byte)convRate[0]); - if (convRate[1] > 0) - tag.setByte("Conv1", (byte)convRate[1]); - if (convRate[2] > 0) - tag.setByte("Conv2", (byte)convRate[2]); - - return tag; - }*/ - @Override @SideOnly(Side.CLIENT) public void clientUpdateCount (final int slot, final int count) { diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java index fb37bc155..299f0d60d 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java @@ -6,6 +6,7 @@ import com.jaquadro.minecraft.storagedrawers.inventory.ItemStackHelper; import com.jaquadro.minecraft.storagedrawers.storage.BaseDrawerData; import com.jaquadro.minecraft.storagedrawers.util.CompactingHelper; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; @@ -34,6 +35,8 @@ public FractionalDrawerGroup (int slotCount) { public void setCapabilityProvider (ICapabilityProvider capProvider) { storage.setCapabilityProvider(capProvider); + for (FractionalDrawer slot : slots) + slot.setCapabilityProvider(capProvider); } @Override @@ -61,6 +64,8 @@ public void setPooledCount (int count) { public void readFromNBT (NBTTagCompound tag) { if (tag.hasKey("Drawers")) storage.deserializeNBT(tag.getCompoundTag("Drawers")); + else if (tag.hasKey("Slots")) + storage.deserializeLegacyNBT(tag); } @Override @@ -429,7 +434,42 @@ public void deserializeNBT (NBTTagCompound tag) { } resetDrawers(); - group.onItemChanged(); + } + + public void deserializeLegacyNBT (NBTTagCompound tag) { + for (int i = 0; i < slotCount; i++) { + protoStack[i] = ItemStack.EMPTY; + convRate[i] = 0; + } + + pooledCount = tag.getInteger("Count"); + + if (tag.hasKey("Conv0")) + convRate[0] = tag.getByte("Conv0"); + if (tag.hasKey("Conv1")) + convRate[1] = tag.getByte("Conv1"); + if (tag.hasKey("Conv2")) + convRate[2] = tag.getByte("Conv2"); + + NBTTagList slots = tag.getTagList("Slots", Constants.NBT.TAG_COMPOUND); + for (int i = 0; i < slotCount; i++) { + NBTTagCompound slot = slots.getCompoundTagAt(i); + if (!slot.hasKey("Item")) + continue; + + Item item = Item.getItemById(slot.getShort("Item")); + if (item == null) + continue; + + ItemStack stack = new ItemStack(item); + stack.setItemDamage(slot.getShort("Meta")); + if (slot.hasKey("Tags")) + stack.setTagCompound(slot.getCompoundTag("Tags")); + + protoStack[i] = stack; + } + + resetDrawers(); } private void resetDrawers () { @@ -443,6 +483,9 @@ private void resetDrawers () { private static class FractionalDrawer extends BaseDrawerData implements IFractionalDrawer { + @CapabilityInject(IDrawerAttributes.class) + static Capability ATTR_CAPABILITY = null; + private FractionalStorage storage; private int slot; @@ -451,6 +494,14 @@ private static class FractionalDrawer extends BaseDrawerData implements IFractio public FractionalDrawer (FractionalStorage storage, int slot) { this.storage = storage; this.slot = slot; + + attrs = new EmptyDrawerAttributes(); + } + + public void setCapabilityProvider (ICapabilityProvider capProvider) { + IDrawerAttributes capAttrs = capProvider.getCapability(ATTR_CAPABILITY, null); + if (capAttrs != null) + attrs = capAttrs; } @Nonnull diff --git a/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java b/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java index 1be219202..ef07a6caa 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java +++ b/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java @@ -335,7 +335,7 @@ private void renderIndicator (ChamRender renderer, TileEntityDrawers tile, IBloc GlStateManager.doPolygonOffset(-1, -10); - if (level == EnumUpgradeStatus.LEVEL1 && drawer.getMaxCapacity() > 0 && drawer.getRemainingCapacity() == 0) { + if (level == EnumUpgradeStatus.LEVEL1 && !drawer.isEmpty() && drawer.getRemainingCapacity() == 0) { renderer.setRenderBounds(statusArea.getX() * unit, statusArea.getY() * unit, 0, (statusArea.getX() + statusArea.getWidth()) * unit, (statusArea.getY() + statusArea.getHeight()) * unit, depth - frontDepth); renderer.state.setRotateTransform(ChamRender.ZPOS, side); diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/CompDrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/CompDrawerData.java deleted file mode 100644 index 778df3cd9..000000000 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/CompDrawerData.java +++ /dev/null @@ -1,177 +0,0 @@ -package com.jaquadro.minecraft.storagedrawers.storage; - -import com.jaquadro.minecraft.storagedrawers.api.storage.*; -import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*; -import com.jaquadro.minecraft.storagedrawers.inventory.ItemStackHelper; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; -import net.minecraftforge.common.capabilities.Capability; -import net.minecraftforge.common.capabilities.CapabilityInject; -import net.minecraftforge.common.capabilities.ICapabilityProvider; -import net.minecraftforge.common.util.Constants; -import net.minecraftforge.common.util.INBTSerializable; - -import javax.annotation.Nonnull; - -/*public class CompDrawerData extends BaseDrawerData implements IFractionalDrawer -{ - @CapabilityInject(IDrawerAttributes.class) - static Capability ATTR_CAPABILITY = null; - - private ICentralInventory central; - private int slot; - - IDrawerAttributes attrs; - - public CompDrawerData (ICentralInventory centralInventory, ICapabilityProvider capProvider, int slot) { - this.slot = slot; - this.central = centralInventory; - - attrs = capProvider.getCapability(ATTR_CAPABILITY, null); - if (attrs == null) - attrs = new EmptyDrawerAttributes(); - } - - @Override - @Nonnull - public ItemStack getStoredItemPrototype () { - return central.getStoredItemPrototype(slot); - } - - @Override - public IDrawer setStoredItem (@Nonnull ItemStack itemPrototype, int amount) { - IDrawer target = central.setStoredItem(slot, itemPrototype, amount); - refresh(); - - return target; - } - - @Override - public int getStoredItemCount () { - return central.getStoredItemCount(slot); - } - - @Override - public void setStoredItemCount (int amount) { - central.setStoredItemCount(slot, amount); - } - - @Override - public int getMaxCapacity () { - return central.getMaxCapacity(slot); - } - - @Override - public int getMaxCapacity (@Nonnull ItemStack itemPrototype) { - return central.getMaxCapacity(slot, itemPrototype); - } - - @Override - public int getDefaultMaxCapacity () { - return central.getDefaultMaxCapacity(slot); - } - - @Override - public int getRemainingCapacity () { - return central.getRemainingCapacity(slot); - } - - @Override - public int getStoredItemStackSize () { - return central.getStoredItemStackSize(slot); - } - - @Override - protected int getItemCapacityForInventoryStack () { - return central.getItemCapacityForInventoryStack(slot); - } - - @Override - public boolean canItemBeStored (@Nonnull ItemStack itemPrototype) { - if (getStoredItemPrototype().isEmpty() && !attrs.isItemLocked(LockAttribute.LOCK_EMPTY)) - return true; - - return areItemsEqual(itemPrototype); - } - - @Override - public boolean canItemBeExtracted (@Nonnull ItemStack itemPrototype) { - return areItemsEqual(itemPrototype); - } - - @Override - public boolean isEmpty () { - return getStoredItemPrototype().isEmpty(); - } - - @Override - public void writeToNBT (NBTTagCompound tag) { - central.writeToNBT(slot, tag); - } - - @Override - public void readFromNBT (NBTTagCompound tag) { - central.readFromNBT(slot, tag); - refresh(); - } - - @Override - public int getConversionRate () { - return central.getConversionRate(slot); - } - - @Override - public int getStoredItemRemainder () { - return central.getStoredItemRemainder(slot); - } - - @Override - public boolean isSmallestUnit () { - return central.isSmallestUnit(slot); - } - - public void refresh () { - reset(); - refreshOreDictMatches(); - } - - @Override - public boolean isVoid () { - return central.isVoidSlot(slot); - } - - @Override - public boolean isShrouded () { - return central.isShroudedSlot(slot); - } - - @Override - public boolean setIsShrouded (boolean state) { - return central.setIsSlotShrouded(slot, state); - } - - @Override - public boolean isShowingQuantity () { - return central.isSlotShowingQuantity(slot); - } - - @Override - public boolean setIsShowingQuantity (boolean state) { - return central.setIsSlotShowingQuantity(slot, state); - } - - @Override - public boolean isItemLocked (LockAttribute attr) { - return central.isLocked(slot, attr); - } - - @Override - public boolean canItemLock (LockAttribute attr) { - return false; - } - - @Override - public void setItemLocked (LockAttribute attr, boolean isLocked) { } -} -*/ \ No newline at end of file From f75822c963f910c5d28564078d6e90d4545904a9 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sun, 2 Jul 2017 15:54:10 -0400 Subject: [PATCH 16/22] Support for legacy basic storage conversion --- .../block/BlockStandardDrawers.java | 11 +++ .../block/tile/TileEntityDrawersStandard.java | 38 +++++++++++ .../tile/tiledata/StandardDrawerGroup.java | 18 ++++- .../storagedrawers/core/ModBlocks.java | 2 +- .../storagedrawers/storage/DrawerData.java | 67 ++++++++++--------- 5 files changed, 103 insertions(+), 33 deletions(-) diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockStandardDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockStandardDrawers.java index 60e99c3a6..242390647 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockStandardDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockStandardDrawers.java @@ -2,9 +2,11 @@ import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.api.storage.EnumBasicDrawer; +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; import com.jaquadro.minecraft.storagedrawers.block.dynamic.StatusModelData; import com.jaquadro.minecraft.storagedrawers.block.tile.TileEntityDrawers; import com.jaquadro.minecraft.storagedrawers.block.tile.TileEntityDrawersStandard; +import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.StandardDrawerGroup; import net.minecraft.block.BlockPlanks; import net.minecraft.block.material.Material; import net.minecraft.block.properties.IProperty; @@ -16,6 +18,7 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.NonNullList; @@ -29,6 +32,7 @@ import net.minecraftforge.fml.relauncher.SideOnly; import java.util.List; +import java.util.Random; public class BlockStandardDrawers extends BlockDrawers { @@ -199,4 +203,11 @@ protected BlockStateContainer createBlockState () { public IBlockState getActualState (IBlockState state, IBlockAccess worldIn, BlockPos pos) { return super.getActualState(state, worldIn, pos).withProperty(BLOCK, state.getValue(BLOCK)); } + + @Override + public void updateTick (World worldIn, BlockPos pos, IBlockState state, Random rand) { + TileEntity tile = worldIn.getTileEntity(pos); + if (tile instanceof TileEntityDrawersStandard.Legacy) + ((TileEntityDrawersStandard.Legacy) tile).replaceWithCurrent(); + } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java index 93133e3c5..c08268014 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java @@ -7,6 +7,7 @@ import com.jaquadro.minecraft.storagedrawers.block.BlockStandardDrawers; import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.StandardDrawerGroup; import com.jaquadro.minecraft.storagedrawers.config.ConfigManager; +import com.jaquadro.minecraft.storagedrawers.core.ModBlocks; import com.jaquadro.minecraft.storagedrawers.inventory.ContainerDrawers1; import com.jaquadro.minecraft.storagedrawers.inventory.ContainerDrawers2; import com.jaquadro.minecraft.storagedrawers.inventory.ContainerDrawers4; @@ -15,6 +16,7 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; +import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.MinecraftForge; import javax.annotation.Nonnull; @@ -72,6 +74,42 @@ protected IDrawerGroup getGroup () { } } + public static class Legacy extends TileEntityDrawersStandard + { + private GroupData groupData = new GroupData(4); + private static boolean processed; + + public Legacy () { + groupData.setCapabilityProvider(this); + injectPortableData(groupData); + } + + @Override + protected IDrawerGroup getGroup () { + return groupData; + } + + public void replaceWithCurrent () { + TileEntityDrawersStandard replacement = createEntity(groupData.getDrawerCount()); + if (replacement != null) { + replacement.deserializeNBT(serializeNBT()); + getWorld().setTileEntity(getPos(), replacement); + replacement.markDirty(); + } + } + + @Override + public void validate () { + super.validate(); + getWorld().scheduleBlockUpdate(getPos(), ModBlocks.basicDrawers, 1, 0); + } + + @Override + public NBTTagCompound writeToPortableNBT (NBTTagCompound tag) { + return super.writeToPortableNBT(tag); + } + } + public static TileEntityDrawersStandard createEntity (int slotCount) { switch (slotCount) { case 1: diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java index a82d85961..68e82bc68 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java @@ -45,8 +45,11 @@ public IDrawer getDrawer (int slot) { @Override public void readFromNBT (NBTTagCompound tag) { - if (!tag.hasKey("Drawers")) + if (!tag.hasKey("Drawers")) { + if (tag.hasKey("Slots")) + readFromLegacyNBT(tag); return; + } NBTTagList itemList = tag.getTagList("Drawers", Constants.NBT.TAG_COMPOUND); for (int i = 0; i < itemList.tagCount(); i++) { @@ -55,6 +58,19 @@ public void readFromNBT (NBTTagCompound tag) { } } + public void readFromLegacyNBT (NBTTagCompound tag) { + NBTTagList slotTags = tag.getTagList("Slots", Constants.NBT.TAG_COMPOUND); + + DrawerData[] realSlots = new DrawerData[slotTags.tagCount()]; + for (int i = 0; i < realSlots.length && i < slots.length; i++) + realSlots[i] = slots[i]; + + slots = realSlots; + + for (int i = 0; i < slots.length; i++) + slots[i].deserializeLegacyNBT(slotTags.getCompoundTagAt(i)); + } + @Override public NBTTagCompound writeToNBT (NBTTagCompound tag) { if (slots == null) diff --git a/src/com/jaquadro/minecraft/storagedrawers/core/ModBlocks.java b/src/com/jaquadro/minecraft/storagedrawers/core/ModBlocks.java index f5c97cc02..29ae8c5bb 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/core/ModBlocks.java +++ b/src/com/jaquadro/minecraft/storagedrawers/core/ModBlocks.java @@ -69,7 +69,7 @@ public static void registerBlocks (RegistryEvent.Register event) { new BlockKeyButton("keybutton", "keyButton") ); - GameRegistry.registerTileEntity(TileEntityDrawersStandard.class, StorageDrawers.MOD_ID + ":basicdrawers"); + GameRegistry.registerTileEntity(TileEntityDrawersStandard.Legacy.class, StorageDrawers.MOD_ID + ":basicdrawers"); GameRegistry.registerTileEntity(TileEntityDrawersStandard.Slot1.class, StorageDrawers.MOD_ID + ":basicdrawers.1"); GameRegistry.registerTileEntity(TileEntityDrawersStandard.Slot2.class, StorageDrawers.MOD_ID + ":basicdrawers.2"); GameRegistry.registerTileEntity(TileEntityDrawersStandard.Slot4.class, StorageDrawers.MOD_ID + ":basicdrawers.4"); diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java index 0bdb38ef0..ca018b531 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java @@ -5,6 +5,7 @@ import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*; import com.jaquadro.minecraft.storagedrawers.inventory.ItemStackHelper; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.capabilities.Capability; @@ -18,10 +19,6 @@ public class DrawerData extends BaseDrawerData @CapabilityInject(IDrawerAttributes.class) static Capability ATTR_CAPABILITY = null; - //private IStorageProvider storageProvider; - //private ICapabilityProvider capProvider; - //private int slot; - IDrawerAttributes attrs; @Nonnull @@ -76,6 +73,15 @@ protected IDrawer setStoredItem (@Nonnull ItemStack itemPrototype, boolean notif return this; } + protected IDrawer setStoredItemRaw (@Nonnull ItemStack itemPrototype) { + itemPrototype = ItemStackHelper.getItemPrototype(itemPrototype); + protoStack = itemPrototype; + protoStack.setCount(1); + count = 0; + + return this; + } + @Override public int getStoredItemCount () { if (protoStack.isEmpty()) @@ -110,6 +116,10 @@ protected void setStoredItemCount (int amount, boolean notify) { } } + protected void setStoredItemCountRaw (int amount) { + count = amount; + } + @Override public int adjustStoredItemCount (int amount) { return adjustStoredItemCount(amount, true); @@ -153,14 +163,6 @@ public int getMaxCapacity (@Nonnull ItemStack itemPrototype) { return itemPrototype.getItem().getItemStackLimit(itemPrototype) * getStackCapacity(); } - /*@Override - public int getDefaultMaxCapacity () { - if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) - return Integer.MAX_VALUE; - - return 64 * stackCapacity; - }*/ - @Override public int getRemainingCapacity () { if (protoStack.isEmpty()) @@ -172,14 +174,6 @@ public int getRemainingCapacity () { return getMaxCapacity() - getStoredItemCount(); } - /*@Override - protected int getItemCapacityForInventoryStack () { - if (attrs.isVoid()) - return Integer.MAX_VALUE; - else - return getMaxCapacity(); - }*/ - @Override public boolean canItemBeStored (@Nonnull ItemStack itemPrototype) { if (protoStack.isEmpty() && !attrs.isItemLocked(LockAttribute.LOCK_EMPTY)) @@ -201,15 +195,6 @@ public boolean isEmpty () { return protoStack.isEmpty(); } - /*@Override - public void attributeChanged () { - updateAttributeCache(); - }*/ - - /*private void updateAttributeCache () { - stackCapacity = storageProvider.getSlotStackCapacity(slot); - }*/ - @Override protected void reset (boolean notify) { protoStack = ItemStack.EMPTY; @@ -243,8 +228,28 @@ public void deserializeNBT (NBTTagCompound nbt) { if (nbt.hasKey("Count")) tagCount = nbt.getInteger("Count"); - setStoredItem(tagItem, false); - setStoredItemCount(tagCount, false); + setStoredItemRaw(tagItem); + setStoredItemCountRaw(tagCount); + } + + public void deserializeLegacyNBT (NBTTagCompound nbt) { + ItemStack tagItem = ItemStack.EMPTY; + int tagCount = 0; + + if (nbt.hasKey("Count")) + tagCount = nbt.getInteger("Count"); + if (nbt.hasKey("Item")) { + Item item = Item.getItemById(nbt.getShort("Item")); + if (item != null) { + tagItem = new ItemStack(item); + tagItem.setItemDamage(nbt.getShort("Meta")); + if (nbt.hasKey("Tags")) + tagItem.setTagCompound(nbt.getCompoundTag("Tags")); + } + } + + setStoredItemRaw(tagItem); + setStoredItemCountRaw(tagCount); } protected int getStackCapacity() { From adb6b1ee2abaa4433e95a7265158133669d04ef1 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Tue, 4 Jul 2017 21:20:42 -0400 Subject: [PATCH 17/22] More refactoring, especially on the controller and capabilities --- gradle.properties | 2 +- .../storagedrawers/StorageDrawers.java | 5 + .../storagedrawers/api/StorageDrawersApi.java | 2 +- .../api/capabilities/IItemRepository.java | 32 ++ .../storagedrawers/api/storage/IDrawer.java | 34 +- .../api/storage/IDrawerGroup.java | 26 +- .../api/storage/ISmartGroup.java | 20 -- .../block/tile/TileEntityController.java | 314 ++++++++---------- .../block/tile/TileEntityDrawers.java | 9 +- .../block/tile/TileEntityDrawersComp.java | 16 + .../block/tile/TileEntityDrawersStandard.java | 14 + .../block/tile/TileEntitySlave.java | 32 +- .../tile/tiledata/FractionalDrawerGroup.java | 14 + .../CapabilityItemRepository.java | 61 ++++ .../inventory/DrawerItemHandler.java | 87 ++--- .../storagedrawers/storage/DrawerData.java | 16 +- 16 files changed, 373 insertions(+), 311 deletions(-) create mode 100644 src/com/jaquadro/minecraft/storagedrawers/api/capabilities/IItemRepository.java delete mode 100644 src/com/jaquadro/minecraft/storagedrawers/api/storage/ISmartGroup.java create mode 100644 src/com/jaquadro/minecraft/storagedrawers/capabilities/CapabilityItemRepository.java diff --git a/gradle.properties b/gradle.properties index 8b8d0e420..fb1e1f1cc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx3G minecraft_base_version=1.12 minecraft_version=1.12 -mod_version=5.1.3 +mod_version=5.2.0 chameleon_version=4.0.1 chameleon_max_version=5.0.0 diff --git a/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java index bf1934b93..e43b89216 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java @@ -1,7 +1,9 @@ package com.jaquadro.minecraft.storagedrawers; +import com.jaquadro.minecraft.storagedrawers.capabilities.CapabilityItemRepository; import com.jaquadro.minecraft.storagedrawers.config.*; import com.jaquadro.minecraft.storagedrawers.core.*; +import com.jaquadro.minecraft.storagedrawers.core.capabilities.CapabilityDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.core.handlers.GuiHandler; import com.jaquadro.minecraft.storagedrawers.integration.LocalIntegrationRegistry; import com.jaquadro.minecraft.storagedrawers.network.BoolConfigUpdateMessage; @@ -59,6 +61,9 @@ public void preInit (FMLPreInitializationEvent event) { log = event.getModLog(); config = new ConfigManager(new File(event.getModConfigurationDirectory(), MOD_ID + ".cfg")); + CapabilityItemRepository.register(); + CapabilityDrawerAttributes.register(); + network = NetworkRegistry.INSTANCE.newSimpleChannel(MOD_ID); network.registerMessage(BoolConfigUpdateMessage.Handler.class, BoolConfigUpdateMessage.class, 0, Side.SERVER); diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/StorageDrawersApi.java b/src/com/jaquadro/minecraft/storagedrawers/api/StorageDrawersApi.java index b078c8062..d038d7d5e 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/api/StorageDrawersApi.java +++ b/src/com/jaquadro/minecraft/storagedrawers/api/StorageDrawersApi.java @@ -7,7 +7,7 @@ public class StorageDrawersApi { private static IStorageDrawersApi instance; - public static final String VERSION = "1.10.2-1.3.0"; + public static final String VERSION = "2.0.0"; /** * API entry point. diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/capabilities/IItemRepository.java b/src/com/jaquadro/minecraft/storagedrawers/api/capabilities/IItemRepository.java new file mode 100644 index 000000000..afc564abf --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/api/capabilities/IItemRepository.java @@ -0,0 +1,32 @@ +package com.jaquadro.minecraft.storagedrawers.api.capabilities; + +import net.minecraft.item.ItemStack; + +import javax.annotation.Nonnull; + +public interface IItemRepository +{ + /** + * Inserts an ItemStack into the inventory and returns the remainder. + * + * @param stack ItemStack to insert. + * @param simulate If true, the insertion is only simulated + * @return The remaining ItemStack that was not inserted. If the entire stack was accepted, returns + * ItemStack.EMPTY instead. + */ + @Nonnull + ItemStack insertItem (@Nonnull ItemStack stack, boolean simulate); + + /** + * Tries to extract the given ItemStack from the inventory. The returned value will be a matching ItemStack + * with a stack size equal to or less than amount, or the empty ItemStack if the item could not be found at all. + * The returned stack size may exceed the itemstack's getMaxStackSize() value. + * + * @param stack The item to extract. The stack size is ignored. + * @param amount Amount to extract (may be greater than the stacks max limit) + * @param simulate If true, the extraction is only simulated + * @return ItemStack extracted from the inventory, or ItemStack.EMPTY if nothing could be extracted. + */ + @Nonnull + ItemStack extractItem (@Nonnull ItemStack stack, int amount, boolean simulate); +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java index 727c9943b..e9bdc4962 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java @@ -77,6 +77,16 @@ default int getMaxCapacity () { */ int getRemainingCapacity (); + /** + * Gets the number of additional items that would be accepted by this drawer. + * + * Because a drawer may be able to handle items in excess of its full capacity, this value may be larger than + * the result of getRemainingCapacity(). + */ + default int getAcceptingRemainingCapacity () { + return getRemainingCapacity(); + } + /** * Gets the max stack size of the item type stored in this drawer. */ @@ -119,28 +129,4 @@ default int getStoredItemStackSize () { default boolean isEnabled () { return true; } - - /** - * 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); - - /** - * Called when a component attribute of a drawer (such as lock or void) changes state. - */ - // void attributeChanged (); - - // void writeToNBT (NBTTagCompound tag); - - // void readFromNBT (NBTTagCompound tag); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java index 30438b5fd..513e4df41 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java @@ -1,8 +1,13 @@ package com.jaquadro.minecraft.storagedrawers.api.storage; +import net.minecraft.util.EnumFacing; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.ICapabilityProvider; + import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface IDrawerGroup +public interface IDrawerGroup extends ICapabilityProvider { /** * Gets the number of drawers contained within this group. @@ -15,15 +20,14 @@ public interface IDrawerGroup @Nonnull IDrawer getDrawer (int slot); - /** - * Gets the drawer at the given slot within this group only if it is enabled. - */ - //IDrawer getDrawerIfEnabled (int slot); - - /** - * Gets whether the drawer in the given slot is usable. - */ - //boolean isDrawerEnabled (int slot); + @Override + default boolean hasCapability (@Nonnull Capability capability, @Nullable EnumFacing facing) { + return false; + } - //boolean markDirtyIfNeeded (); + @Nullable + @Override + default T getCapability (@Nonnull Capability capability, @Nullable EnumFacing facing) { + return null; + } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/ISmartGroup.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/ISmartGroup.java deleted file mode 100644 index 8795d77a8..000000000 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/ISmartGroup.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.jaquadro.minecraft.storagedrawers.api.storage; - -import net.minecraft.item.ItemStack; - -import javax.annotation.Nonnull; - -public interface ISmartGroup -{ - /** - * Gets a lazy enumeration of all slots that will accept at least one item of the given stack, ordered by - * insertion preference. - */ - Iterable enumerateDrawersForInsertion (@Nonnull ItemStack stack, boolean strict); - - /** - * Gets a lazy enumeration of all slots that will provide at least one item of the given stack, ordered by - * extraction preference. - */ - Iterable enumerateDrawersForExtraction (@Nonnull ItemStack stack, boolean strict); -} diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java index bf606cb7a..c596d8704 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java @@ -1,6 +1,7 @@ package com.jaquadro.minecraft.storagedrawers.block.tile; import com.jaquadro.minecraft.storagedrawers.StorageDrawers; +import com.jaquadro.minecraft.storagedrawers.api.capabilities.IItemRepository; import com.jaquadro.minecraft.storagedrawers.api.security.ISecurityProvider; import com.jaquadro.minecraft.storagedrawers.api.storage.*; @@ -25,12 +26,13 @@ import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.common.capabilities.ICapabilityProvider; -import net.minecraftforge.items.CapabilityItemHandler; +import net.minecraftforge.items.IItemHandler; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.*; -public class TileEntityController extends TileEntity implements IDrawerGroup, IPriorityGroup, ISmartGroup +public class TileEntityController extends TileEntity implements IDrawerGroup, IPriorityGroup { @CapabilityInject(IDrawerAttributes.class) public static Capability DRAWER_ATTRIBUTES_CAPABILITY = null; @@ -92,15 +94,9 @@ public int compareTo (SlotRecord other) { } } - private Queue searchQueue = new LinkedList(); - private Set searchDiscovered = new HashSet(); - private Comparator slotRecordComparator = new Comparator() - { - @Override - public int compare (SlotRecord o1, SlotRecord o2) { - return o1.priority - o2.priority; - } - }; + private Queue searchQueue = new LinkedList<>(); + private Set searchDiscovered = new HashSet<>(); + private Comparator slotRecordComparator = (o1, o2) -> o1.priority - o2.priority; private IDrawerAttributes getAttributes (Object obj) { IDrawerAttributes attrs = null; @@ -146,10 +142,10 @@ private int getSlotPriority (SlotRecord record) { return PRI_NORMAL; } - private Map storage = new HashMap(); - protected List drawerSlotList = new ArrayList(); + private Map storage = new HashMap<>(); + protected List drawerSlotList = new ArrayList<>(); - private ItemMetaCollectionRegistry drawerPrimaryLookup = new ItemMetaCollectionRegistry(); + private ItemMetaCollectionRegistry drawerPrimaryLookup = new ItemMetaCollectionRegistry<>(); protected int[] drawerSlots = new int[0]; private int range; @@ -207,53 +203,11 @@ public int interactPutItemsIntoInventory (EntityPlayer player) { } protected int insertItems (@Nonnull ItemStack stack, GameProfile profile) { - int itemsLeft = stack.getCount(); - - for (int slot : enumerateDrawersForInsertion(stack, false)) { - IDrawerGroup group = getGroupForDrawerSlot(slot); - if (group instanceof IProtectable) { - if (!SecurityManager.hasAccess(profile, (IProtectable)group)) - continue; - } - - IDrawer drawer = getDrawer(slot); - if (drawer.isEmpty()) - break; - - itemsLeft = insertItemsIntoDrawer(drawer, itemsLeft); + int remainder = new ProtectedItemRepository(profile).insertItem(stack, false).getCount(); + int added = stack.getCount() - remainder; - IDrawerAttributes attrs = getAttributes(group); - if (attrs.isVoid()) - itemsLeft = 0; - if (itemsLeft == 0) - break; - } - - int count = stack.getCount() - itemsLeft; - stack.setCount(itemsLeft); - - return count; - } - - protected int insertItemsIntoDrawer (IDrawer drawer, int itemCount) { - int capacity = drawer.getMaxCapacity(); - int storedItems = drawer.getStoredItemCount(); - - int storableItems = capacity - storedItems; - if (drawer instanceof IFractionalDrawer) { - IFractionalDrawer fracDrawer = (IFractionalDrawer)drawer; - if (!fracDrawer.isSmallestUnit() && fracDrawer.getStoredItemRemainder() > 0) - storableItems--; - } - - if (storableItems == 0) - return itemCount; - - int remainder = Math.max(itemCount - storableItems, 0); - storedItems += Math.min(itemCount, storableItems); - drawer.setStoredItemCount(storedItems); - - return remainder; + stack.setCount(remainder); + return added; } public void toggleProtection (GameProfile profile, ISecurityProvider provider) { @@ -435,7 +389,7 @@ private void rebuildPrimaryLookup (ItemMetaCollectionRegistry lookup continue; ItemStack item = drawer.getStoredItemPrototype(); - lookup.register(item.getItem(), item.getItemDamage(), record); + lookup.register(item.getItem(), item.getMetadata(), record); } } @@ -507,7 +461,7 @@ else if (te instanceof IDrawerGroup) { if (record.storage == group) return; - if (record.storage != null && record.storage != group) + if (record.storage != null) clearRecordInfo(coord, record); record.storage = group; @@ -648,10 +602,11 @@ public int getDrawerCount () { } @Override + @Nonnull public IDrawer getDrawer (int slot) { IDrawerGroup group = getGroupForDrawerSlot(slot); if (group == null) - return null; + return Drawers.DISABLED; return group.getDrawer(getLocalDrawerSlot(slot)); } @@ -661,136 +616,157 @@ public int[] getAccessibleDrawerSlots () { return drawerSlots; } - private DrawerItemHandler itemHandler = new DrawerItemHandler(this, this); + @CapabilityInject(IItemHandler.class) + static Capability ITEM_HANDLER_CAPABILITY = null; + @CapabilityInject(IItemRepository.class) + static Capability ITEM_REPOSITORY_CAPABILITY = null; + + private DrawerItemHandler itemHandler = new DrawerItemHandler(this); + private ItemRepository itemRepository = new ItemRepository(); @Override - public boolean hasCapability (Capability capability, EnumFacing facing) { - if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) + public boolean hasCapability (@Nonnull Capability capability, @Nullable EnumFacing facing) { + if (capability == ITEM_HANDLER_CAPABILITY) return true; + if (capability == ITEM_REPOSITORY_CAPABILITY) + return true; + return super.hasCapability(capability, facing); } @Override - public T getCapability (Capability capability, EnumFacing facing) { - if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) + @SuppressWarnings("unchecked") + public T getCapability (@Nonnull Capability capability, @Nullable EnumFacing facing) { + if (capability == ITEM_HANDLER_CAPABILITY) return (T) itemHandler; + if (capability == ITEM_REPOSITORY_CAPABILITY) + return (T) itemRepository; + return super.getCapability(capability, facing); } - private class DrawerStackIterator implements Iterable + private class ItemRepository implements IItemRepository { @Nonnull - private ItemStack stack; - private boolean strict; - private boolean insert; + @Override + public ItemStack insertItem (@Nonnull ItemStack stack, boolean simulate) { + Collection primaryRecords = drawerPrimaryLookup.getEntries(stack.getItem(), stack.getMetadata()); + Set checkedSlots = (simulate) ? new HashSet<>() : null; + + int amount = stack.getCount(); + if (primaryRecords != null) { + for (SlotRecord record : primaryRecords) { + IDrawerGroup candidateGroup = getGroupForSlotRecord(record); + if (candidateGroup == null) + continue; + + IDrawer drawer = candidateGroup.getDrawer(record.slot); + if (drawer.isEmpty() || !drawer.canItemBeStored(stack) || !hasAccess(candidateGroup, drawer)) + continue; + + amount = (simulate) + ? Math.max(amount - drawer.getAcceptingRemainingCapacity(), 0) + : drawer.adjustStoredItemCount(amount); + + if (amount == 0) + return ItemStack.EMPTY; + + if (simulate) + checkedSlots.add(record.index); + } + } + + for (int slot : drawerSlots) { + IDrawer drawer = getDrawer(slot); + if (!drawer.isEnabled() || !drawer.canItemBeStored(stack) || !hasAccess(getGroupForDrawerSlot(slot), drawer)) + continue; + if (simulate && checkedSlots.contains(slot)) + continue; + + if (drawer.isEmpty()) + drawer = drawer.setStoredItem(stack); - public DrawerStackIterator (@Nonnull ItemStack stack, boolean strict, boolean insert) { - this.stack = stack; - this.strict = strict; - this.insert = insert; + amount = (simulate) + ? Math.max(amount - drawer.getAcceptingRemainingCapacity(), 0) + : drawer.adjustStoredItemCount(amount); + + if (amount == 0) + return ItemStack.EMPTY; + } + + return stackResult(stack, amount); } + @Nonnull @Override - public Iterator iterator () { - if (this.stack == null) - return new ArrayList(0).iterator(); - - return new Iterator () - { - Collection primaryRecords = drawerPrimaryLookup.getEntries(stack.getItem(), stack.getItemDamage()); - Iterator iter1; - int index2; - Integer nextSlot = null; - - @Override - public boolean hasNext () { - if (nextSlot == null) - advance(); - return nextSlot != null; - } + public ItemStack extractItem (@Nonnull ItemStack stack, int amount, boolean simulate) { + Collection primaryRecords = drawerPrimaryLookup.getEntries(stack.getItem(), stack.getMetadata()); + Set checkedSlots = (simulate) ? new HashSet<>() : null; + + int remaining = amount; + for (SlotRecord record : primaryRecords) { + IDrawerGroup candidateGroup = getGroupForSlotRecord(record); + if (candidateGroup == null) + continue; - @Override - public Integer next () { - if (nextSlot == null) - advance(); + IDrawer drawer = candidateGroup.getDrawer(record.slot); + if (!drawer.canItemBeExtracted(stack) || !hasAccess(candidateGroup, drawer)) + continue; - Integer slot = nextSlot; - nextSlot = null; - return slot; - } + remaining = (simulate) + ? Math.max(remaining - drawer.getStoredItemCount(), 0) + : drawer.adjustStoredItemCount(-remaining); - private void advance () { - if (iter1 == null && primaryRecords != null && primaryRecords.size() > 0) - iter1 = primaryRecords.iterator(); - - if (iter1 != null) { - while (iter1.hasNext()) { - SlotRecord candidate = iter1.next(); - IDrawerGroup candidateGroup = getGroupForSlotRecord(candidate); - if (candidateGroup == null) - continue; - - IDrawer drawer = candidateGroup.getDrawer(candidate.slot); - - if (insert) { - IDrawerAttributes attrs = getAttributes(candidateGroup); - if (!(drawer.canItemBeStored(stack) && (drawer.isEmpty() || drawer.getRemainingCapacity() > 0 || attrs.isVoid()))) - continue; - } - else { - if (!(drawer.canItemBeExtracted(stack) && drawer.getStoredItemCount() > 0)) - continue; - } - - int slot = drawerSlotList.indexOf(candidate); - if (slot > -1) { - nextSlot = slot; - return; - } - } - } + if (remaining == 0) + return stackResult(stack, amount); - for (; index2 < drawerSlots.length; index2++) { - int slot = drawerSlots[index2]; - IDrawer drawer = getDrawer(slot); - if (!drawer.isEnabled()) - continue; - - if (strict) { - ItemStack proto = drawer.getStoredItemPrototype(); - if (!proto.isItemEqual(stack)) - continue; - } - - if (insert) { - IDrawerGroup group = getGroupForDrawerSlot(slot); - IDrawerAttributes attrs = getAttributes(group); - if (!(drawer.canItemBeStored(stack) && (drawer.isEmpty() || drawer.getRemainingCapacity() > 0 || attrs.isVoid()))) - continue; - } - else { - if (!(drawer.canItemBeExtracted(stack) && drawer.getStoredItemCount() > 0)) - continue; - } - - SlotRecord record = drawerSlotList.get(slot); - if (primaryRecords != null && primaryRecords.contains(record)) - continue; - - nextSlot = slot; - index2++; - return; - } - } - }; + if (simulate) + checkedSlots.add(record.index); + } + + for (int slot : drawerSlots) { + IDrawer drawer = getDrawer(slot); + if (!drawer.canItemBeExtracted(stack) || !hasAccess(getGroupForDrawerSlot(slot), drawer)) + continue; + if (simulate && checkedSlots.contains(slot)) + continue; + + if (remaining == 0) + return stackResult(stack, amount); + } + + return (amount == remaining) + ? ItemStack.EMPTY + : stackResult(stack, amount - remaining); + } + + protected boolean hasAccess (IDrawerGroup group, IDrawer drawer) { + return true; } - }; - public Iterable enumerateDrawersForInsertion (@Nonnull ItemStack stack, boolean strict) { - return new DrawerStackIterator(stack, strict, true); + private ItemStack stackResult (@Nonnull ItemStack stack, int amount) { + ItemStack result = stack.copy(); + result.setCount(amount); + return result; + } } - public Iterable enumerateDrawersForExtraction (@Nonnull ItemStack stack, boolean strict) { - return new DrawerStackIterator(stack, strict, false); + private class ProtectedItemRepository extends ItemRepository + { + private GameProfile profile; + + public ProtectedItemRepository (GameProfile profile) { + this.profile = profile; + } + + @Override + protected boolean hasAccess (IDrawerGroup group, IDrawer drawer) { + if (drawer.isEmpty()) + return false; + if (group instanceof IProtectable) + return SecurityManager.hasAccess(profile, (IProtectable)group); + + return true; + } } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index 13a2354c8..60d395f1c 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -23,10 +23,12 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; +import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.world.ILockableContainer; import net.minecraft.world.World; +import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.util.Constants; import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.fml.common.network.NetworkRegistry.TargetPoint; @@ -35,6 +37,7 @@ import net.minecraftforge.items.IItemHandler; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.EnumSet; import java.util.UUID; @@ -797,12 +800,12 @@ public IDrawer getDrawer (int slot) { private net.minecraftforge.items.IItemHandler itemHandler; protected IItemHandler createUnSidedHandler () { - return new DrawerItemHandler(getGroup(), this); + return new DrawerItemHandler(getGroup()); } @SuppressWarnings("unchecked") @Override - public T getCapability(net.minecraftforge.common.capabilities.Capability capability, net.minecraft.util.EnumFacing facing) + public T getCapability(@Nonnull Capability capability, @Nullable EnumFacing facing) { if (capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return (T) (itemHandler == null ? (itemHandler = createUnSidedHandler()) : itemHandler); @@ -813,7 +816,7 @@ public T getCapability(net.minecraftforge.common.capabilities.Capability } @Override - public boolean hasCapability(net.minecraftforge.common.capabilities.Capability capability, net.minecraft.util.EnumFacing facing) + public boolean hasCapability(@Nonnull Capability capability, @Nullable EnumFacing facing) { return capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || capability == CapabilityDrawerAttributes.DRAWER_ATTRIBUTES_CAPABILITY diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java index 62ee276b0..4f3abd45b 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java @@ -10,12 +10,17 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; +import net.minecraft.util.EnumFacing; import net.minecraft.world.World; +import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + public class TileEntityDrawersComp extends TileEntityDrawers { private GroupData groupData; @@ -75,6 +80,17 @@ protected void onAmountChanged () { markDirty(); } } + + @Override + public boolean hasCapability (@Nonnull Capability capability, @Nullable EnumFacing facing) { + return TileEntityDrawersComp.this.hasCapability(capability, facing); + } + + @Nullable + @Override + public T getCapability (@Nonnull Capability capability, @Nullable EnumFacing facing) { + return TileEntityDrawersComp.this.getCapability(capability, facing); + } } @Override diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java index c08268014..8801f2652 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java @@ -17,9 +17,12 @@ import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.EnumFacing; import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.common.capabilities.Capability; import javax.annotation.Nonnull; +import javax.annotation.Nullable; public class TileEntityDrawersStandard extends TileEntityDrawers { @@ -200,6 +203,17 @@ public GroupData (int slotCount) { protected DrawerData createDrawer (int slot) { return new StandardDrawerData(slot); } + + @Override + public boolean hasCapability (@Nonnull Capability capability, @Nullable EnumFacing facing) { + return TileEntityDrawersStandard.this.hasCapability(capability, facing); + } + + @Nullable + @Override + public T getCapability (@Nonnull Capability capability, @Nullable EnumFacing facing) { + return TileEntityDrawersStandard.this.getCapability(capability, facing); + } } private class StandardDrawerData extends DrawerData diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntitySlave.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntitySlave.java index 38e553e2a..64278f8c3 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntitySlave.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntitySlave.java @@ -1,9 +1,9 @@ package com.jaquadro.minecraft.storagedrawers.block.tile; import com.jaquadro.minecraft.chameleon.block.ChamTileEntity; +import com.jaquadro.minecraft.storagedrawers.api.storage.Drawers; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; -import com.jaquadro.minecraft.storagedrawers.api.storage.ISmartGroup; import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.ControllerData; import com.jaquadro.minecraft.storagedrawers.inventory.DrawerItemHandler; import net.minecraft.block.state.IBlockState; @@ -16,9 +16,10 @@ import net.minecraftforge.items.CapabilityItemHandler; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.ArrayList; -public class TileEntitySlave extends ChamTileEntity implements IDrawerGroup, IPriorityGroup, ISmartGroup +public class TileEntitySlave extends ChamTileEntity implements IDrawerGroup, IPriorityGroup { private static final int[] drawerSlots = new int[] { 0 }; @@ -65,10 +66,11 @@ public int getDrawerCount () { } @Override + @Nonnull public IDrawer getDrawer (int slot) { TileEntityController controller = getController(); if (controller == null || !controller.isValidSlave(getPos())) - return null; + return Drawers.DISABLED; return controller.getDrawer(slot); } @@ -82,35 +84,17 @@ public void markDirty () { super.markDirty(); } - @Override - public Iterable enumerateDrawersForInsertion (@Nonnull ItemStack stack, boolean strict) { - TileEntityController controller = getController(); - if (controller == null || !controller.isValidSlave(getPos())) - return new ArrayList<>(); - - return controller.enumerateDrawersForInsertion(stack, strict); - } - - @Override - public Iterable enumerateDrawersForExtraction (@Nonnull ItemStack stack, boolean strict) { - TileEntityController controller = getController(); - if (controller == null || !controller.isValidSlave(getPos())) - return new ArrayList<>(); - - return controller.enumerateDrawersForExtraction(stack, strict); - } - - private DrawerItemHandler itemHandler = new DrawerItemHandler(this, this); + private DrawerItemHandler itemHandler = new DrawerItemHandler(this); @Override - public boolean hasCapability (Capability capability, EnumFacing facing) { + public boolean hasCapability (@Nonnull Capability capability, @Nullable EnumFacing facing) { if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return true; return super.hasCapability(capability, facing); } @Override - public T getCapability (Capability capability, EnumFacing facing) { + public T getCapability (@Nonnull Capability capability, @Nullable EnumFacing facing) { if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return (T) itemHandler; return super.getCapability(capability, facing); diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java index 299f0d60d..6e124da31 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java @@ -45,6 +45,7 @@ public int getDrawerCount () { } @Override + @Nonnull public IFractionalDrawer getDrawer (int slot) { if (slot < 0 || slot >= slots.length) return Drawers.DISABLED_FRACTIONAL; @@ -286,6 +287,19 @@ public int getRemainingCapacity (int slot) { return rawRemaining / convRate[slot]; } + public int getAcceptingRemainingCapacity (int slot) { + if (baseStack().isEmpty() || convRate[slot] == 0) + return 0; + + if (attrs.isUnlimitedVending() || attrs.isVoid()) + return Integer.MAX_VALUE; + + int rawMaxCapacity = baseStack().getItem().getItemStackLimit(baseStack()) * group.getStackCapacity() * baseRate(); + int rawRemaining = rawMaxCapacity - pooledCount; + + return rawRemaining / convRate[slot]; + } + public boolean isEmpty (int slot) { return protoStack[slot].isEmpty(); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/capabilities/CapabilityItemRepository.java b/src/com/jaquadro/minecraft/storagedrawers/capabilities/CapabilityItemRepository.java new file mode 100644 index 000000000..82d35fe1f --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/capabilities/CapabilityItemRepository.java @@ -0,0 +1,61 @@ +package com.jaquadro.minecraft.storagedrawers.capabilities; + +import com.jaquadro.minecraft.storagedrawers.api.capabilities.IItemRepository; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.EnumFacing; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.CapabilityInject; +import net.minecraftforge.common.capabilities.CapabilityManager; +import net.minecraftforge.common.util.INBTSerializable; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class CapabilityItemRepository +{ + @CapabilityInject(IItemRepository.class) + public static Capability ITEM_REPOSITORY_CAPABILITY = null; + + public static void register () { + CapabilityManager.INSTANCE.register(IItemRepository.class, new DefaultStorage(), DefaultImplementation::new); + } + + private static class DefaultStorage implements Capability.IStorage + { + @Nullable + @Override + public NBTBase writeNBT (Capability capability, IItemRepository instance, EnumFacing side) { + if (instance instanceof INBTSerializable) + return ((INBTSerializable) instance).serializeNBT(); + + return new NBTTagCompound(); + } + + @Override + public void readNBT (Capability capability, IItemRepository instance, EnumFacing side, NBTBase nbt) { + if (instance instanceof INBTSerializable) { + @SuppressWarnings("unchecked") + INBTSerializable serializer = (INBTSerializable)instance; + serializer.deserializeNBT(nbt); + return; + } + } + } + + private static class DefaultImplementation implements IItemRepository + { + @Nonnull + @Override + public ItemStack insertItem (@Nonnull ItemStack stack, boolean simulate) { + return stack; + } + + @Nonnull + @Override + public ItemStack extractItem (@Nonnull ItemStack stack, int amount, boolean simulate) { + return ItemStack.EMPTY; + } + } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java index 11ac58d9b..d3fbfd59c 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java @@ -1,6 +1,7 @@ package com.jaquadro.minecraft.storagedrawers.inventory; import com.jaquadro.minecraft.storagedrawers.StorageDrawers; +import com.jaquadro.minecraft.storagedrawers.api.capabilities.IItemRepository; import com.jaquadro.minecraft.storagedrawers.api.storage.*; import net.minecraft.item.ItemStack; import net.minecraftforge.common.capabilities.Capability; @@ -12,15 +13,13 @@ public class DrawerItemHandler implements IItemHandler { - @CapabilityInject(IDrawerAttributes.class) - public static Capability DRAWER_ATTRIBUTES_CAPABILITY = null; + @CapabilityInject(IItemRepository.class) + public static Capability ITEM_REPOSITORY_CAPABILITY = null; private IDrawerGroup group; - private ICapabilityProvider capProvider; - public DrawerItemHandler (IDrawerGroup group, ICapabilityProvider capProvider) { + public DrawerItemHandler (IDrawerGroup group) { this.group = group; - this.capProvider = capProvider; } @Override @@ -86,27 +85,14 @@ else if (!prevDrawer.isEmpty()) @Nonnull private ItemStack insertItemFullScan (@Nonnull ItemStack stack, boolean simulate) { - if (group instanceof ISmartGroup) { - for (int i : ((ISmartGroup) group).enumerateDrawersForInsertion(stack, false)) { - stack = insertItemInternal(i, stack, simulate); - if (stack.isEmpty()) - break; - } - } - else if (group instanceof IPriorityGroup) { - int[] order = ((IPriorityGroup) group).getAccessibleDrawerSlots(); - for (int i = 0; i < order.length; i++) { - stack = insertItemInternal(i, stack, simulate); - if (stack.isEmpty()) - break; - } - } - else { - for (int i = 0; i < group.getDrawerCount(); i++) { - stack = insertItemInternal(i, stack, simulate); - if (stack.isEmpty()) - break; - } + IItemRepository itemRepo = group.getCapability(ITEM_REPOSITORY_CAPABILITY, null); + if (itemRepo != null) + return itemRepo.insertItem(stack, simulate); + + for (int i = 0; i < group.getDrawerCount(); i++) { + stack = insertItemInternal(i, stack, simulate); + if (stack.isEmpty()) + break; } return stack; @@ -115,36 +101,22 @@ else if (group instanceof IPriorityGroup) { @Nonnull private ItemStack insertItemInternal (int slot, @Nonnull ItemStack stack, boolean simulate) { IDrawer drawer = group.getDrawer(slot); - if (!drawer.isEnabled() || !drawer.canItemBeStored(stack)) + if (!drawer.canItemBeStored(stack)) return stack; - int availableCount = drawer.isEmpty() ? drawer.getMaxCapacity(stack) : drawer.getRemainingCapacity(); + if (drawer.isEmpty() && !simulate) + drawer.setStoredItem(stack); - // TODO: May not need void check here with updated storage implementation - IDrawerAttributes attrs = capProvider.getCapability(DRAWER_ATTRIBUTES_CAPABILITY, null); - if (attrs != null && attrs.isVoid()) - availableCount = Integer.MAX_VALUE; + int remainder = (simulate) + ? Math.max(stack.getCount() - drawer.getAcceptingRemainingCapacity(), 0) + : drawer.adjustStoredItemCount(stack.getCount()); - int stackSize = stack.getCount(); - int insertCount = Math.min(stackSize, availableCount); - int remainder = stackSize - insertCount; - - if (remainder == stackSize) + if (remainder == stack.getCount()) return stack; - - if (!simulate) { - if (drawer.isEmpty()) - drawer = drawer.setStoredItem(stack); - drawer.setStoredItemCount(drawer.getStoredItemCount() + insertCount); - } - if (remainder == 0) return ItemStack.EMPTY; - ItemStack returnStack = stack.copy(); - returnStack.setCount(remainder); - - return returnStack; + return stackResult(stack, remainder); } @Override @@ -163,16 +135,11 @@ public ItemStack extractItem (int slot, int amount, boolean simulate) { if (!drawer.isEnabled() || drawer.isEmpty() || drawer.getStoredItemCount() == 0) return ItemStack.EMPTY; - ItemStack returnStack = drawer.getStoredItemPrototype().copy(); - int stackSize = Math.min(drawer.getStoredItemCount(), amount); - stackSize = Math.min(stackSize, returnStack.getMaxStackSize()); - - returnStack.setCount(stackSize); + int remaining = (simulate) + ? Math.max(amount - drawer.getStoredItemCount(), 0) + : drawer.adjustStoredItemCount(-amount); - if (!simulate) - drawer.setStoredItemCount(drawer.getStoredItemCount() - stackSize); - - return returnStack; + return stackResult(drawer.getStoredItemPrototype(), amount - remaining); } @Override @@ -192,4 +159,10 @@ public int getSlotLimit (int slot) { private boolean slotIsVirtual (int slot) { return slot == 0; } + + private ItemStack stackResult (@Nonnull ItemStack stack, int amount) { + ItemStack result = stack.copy(); + result.setCount(amount); + return result; + } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java index ca018b531..781598e08 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java @@ -134,7 +134,7 @@ protected int adjustStoredItemCount (int amount, boolean notify) { return 0; int originalCount = count; - count = Math.min(amount, getMaxCapacity()); + count = Math.min(count + amount, getMaxCapacity()); if (count != originalCount && notify) onAmountChanged(); @@ -174,6 +174,17 @@ public int getRemainingCapacity () { return getMaxCapacity() - getStoredItemCount(); } + @Override + public int getAcceptingRemainingCapacity () { + if (protoStack.isEmpty()) + return 0; + + if (attrs.isUnlimitedVending() || attrs.isVoid()) + return Integer.MAX_VALUE; + + return getMaxCapacity() - getStoredItemCount(); + } + @Override public boolean canItemBeStored (@Nonnull ItemStack itemPrototype) { if (protoStack.isEmpty() && !attrs.isItemLocked(LockAttribute.LOCK_EMPTY)) @@ -209,6 +220,9 @@ protected void reset (boolean notify) { @Override public NBTTagCompound serializeNBT () { NBTTagCompound tag = new NBTTagCompound(); + if (protoStack.isEmpty()) + return tag; + NBTTagCompound item = new NBTTagCompound(); protoStack.writeToNBT(item); From 1e1ce8d0c960b5bc4e4315b1ec3489efadb87ae0 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Tue, 4 Jul 2017 23:54:03 -0400 Subject: [PATCH 18/22] More ugly refactor. But I think I fixed a vexing controller network populate bug! --- .../api/storage/IDrawerGroup.java | 6 + .../api/storage/IPriorityGroup.java | 10 - .../storagedrawers/block/BlockController.java | 1 - .../storagedrawers/block/BlockDrawers.java | 2 +- .../block/tile/TileEntityController.java | 16 +- .../block/tile/TileEntityDrawers.java | 200 +---------- .../block/tile/TileEntityDrawersComp.java | 2 +- .../block/tile/TileEntityDrawersStandard.java | 20 +- .../block/tile/TileEntitySlave.java | 6 +- .../tile/tiledata/FractionalDrawerGroup.java | 15 +- .../tile/tiledata/StandardDrawerGroup.java | 315 +++++++++++++++++- .../renderer/TileEntityDrawersRenderer.java | 4 +- .../inventory/ContainerDrawers.java | 2 +- .../inventory/DrawerItemHandler.java | 24 +- .../storage/DefaultStorageProvider.java | 123 ------- .../storagedrawers/storage/DrawerData.java | 10 +- .../storage/IStorageProvider.java | 36 -- .../storagedrawers/util/CompactingHelper.java | 8 +- .../{storage => util}/CountFormatter.java | 2 +- 19 files changed, 380 insertions(+), 422 deletions(-) delete mode 100644 src/com/jaquadro/minecraft/storagedrawers/api/storage/IPriorityGroup.java delete mode 100644 src/com/jaquadro/minecraft/storagedrawers/storage/DefaultStorageProvider.java delete mode 100644 src/com/jaquadro/minecraft/storagedrawers/storage/IStorageProvider.java rename src/com/jaquadro/minecraft/storagedrawers/{storage => util}/CountFormatter.java (97%) diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java index 513e4df41..f4d1b1f97 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java @@ -20,6 +20,12 @@ public interface IDrawerGroup extends ICapabilityProvider @Nonnull IDrawer getDrawer (int slot); + /** + * Gets the list of available drawer slots in priority order. + */ + @Nonnull + int[] getAccessibleDrawerSlots (); + @Override default boolean hasCapability (@Nonnull Capability capability, @Nullable EnumFacing facing) { return false; diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IPriorityGroup.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IPriorityGroup.java deleted file mode 100644 index 97b293e27..000000000 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IPriorityGroup.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.jaquadro.minecraft.storagedrawers.api.storage; - - -public interface IPriorityGroup -{ - /** - * Gets the list of available drawer slots in priority order. - */ - int[] getAccessibleDrawerSlots (); -} diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockController.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockController.java index 687b7436a..46eaea4b6 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockController.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockController.java @@ -44,7 +44,6 @@ public BlockController (String name) { setHardness(5f); setSoundType(SoundType.STONE); setLightOpacity(255); - setTickRandomly(true); setDefaultState(blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH)); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java index 085996bc2..a3dcb1eac 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java @@ -495,7 +495,7 @@ public void breakBlock (World world, BlockPos pos, IBlockState state) { } if (!tile.getDrawerAttributes().isUnlimitedVending()) - DrawerInventoryHelper.dropInventoryItems(world, pos, tile); + DrawerInventoryHelper.dropInventoryItems(world, pos, tile.getGroup()); } super.breakBlock(world, pos, state); diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java index c596d8704..e57d15818 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java @@ -8,6 +8,7 @@ import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IProtectable; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; import com.jaquadro.minecraft.storagedrawers.block.BlockSlave; +import com.jaquadro.minecraft.storagedrawers.core.ModBlocks; import com.jaquadro.minecraft.storagedrawers.inventory.DrawerItemHandler; import com.jaquadro.minecraft.storagedrawers.security.SecurityManager; import com.jaquadro.minecraft.storagedrawers.util.ItemMetaCollectionRegistry; @@ -32,7 +33,7 @@ import javax.annotation.Nullable; import java.util.*; -public class TileEntityController extends TileEntity implements IDrawerGroup, IPriorityGroup +public class TileEntityController extends TileEntity implements IDrawerGroup { @CapabilityInject(IDrawerAttributes.class) public static Capability DRAWER_ATTRIBUTES_CAPABILITY = null; @@ -165,6 +166,14 @@ public void printDebugInfo () { StorageDrawers.log.info(" Ticks since last update: " + (getWorld().getTotalWorldTime() - lastUpdateTime)); } + @Override + public void validate () { + super.validate(); + + if (!getWorld().isUpdateScheduled(getPos(), ModBlocks.controller)) + getWorld().scheduleUpdate(getPos(), ModBlocks.controller, 1); + } + @Override public boolean shouldRefresh (World world, BlockPos pos, IBlockState oldState, IBlockState newSate) { return oldState.getBlock() != newSate.getBlock(); @@ -456,8 +465,8 @@ else if (te instanceof TileEntitySlave) { ((TileEntitySlave) te).bindController(getPos()); } - else if (te instanceof IDrawerGroup) { - IDrawerGroup group = (IDrawerGroup)te; + else if (te instanceof TileEntityDrawers) { + IDrawerGroup group = ((TileEntityDrawers) te).getGroup(); if (record.storage == group) return; @@ -611,6 +620,7 @@ public IDrawer getDrawer (int slot) { return group.getDrawer(getLocalDrawerSlot(slot)); } + @Nonnull @Override public int[] getAccessibleDrawerSlots () { return drawerSlots; diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index 60d395f1c..134885dd1 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -41,7 +41,7 @@ import java.util.EnumSet; import java.util.UUID; -public abstract class TileEntityDrawers extends ChamLockableTileEntity implements IDrawerGroupInteractive, ISealable, IProtectable +public abstract class TileEntityDrawers extends ChamLockableTileEntity implements ISealable, IProtectable { private LockableData lockData = new LockableData(); private CustomNameData customNameData = new CustomNameData("storagedrawers.container.drawers"); @@ -147,19 +147,9 @@ protected TileEntityDrawers () { injectPortableData(upgradeData); injectPortableData(materialData); injectData(controllerData); - - //initWithDrawerCount(drawerCount); } - //protected abstract IDrawer createDrawer (int slot); - - /*protected void initWithDrawerCount (int drawerCount) { - drawers = new IDrawer[drawerCount]; - for (int i = 0; i < drawerCount; i++) - drawers[i] = createDrawer(i); - }*/ - - protected abstract IDrawerGroup getGroup (); + public abstract IDrawerGroup getGroup (); public IDrawerAttributes getDrawerAttributes () { return drawerAttributes; @@ -521,17 +511,6 @@ public void readFromPortableNBT (NBTTagCompound tag) { securityKey = null; if (tag.hasKey("Sec")) securityKey = tag.getString("Sec"); - - /* - NBTTagList slots = tag.getTagList("Slots", Constants.NBT.TAG_COMPOUND); - - drawers = new IDrawer[slots.tagCount()]; - for (int i = 0, n = drawers.length; i < n; i++) { - NBTTagCompound slot = slots.getCompoundTagAt(i); - drawers[i] = createDrawer(i); - if (drawers[i] instanceof INBTSerializable) - ((INBTSerializable)drawers[i]).deserializeNBT(slot); - }*/ } @Override @@ -565,182 +544,9 @@ public NBTTagCompound writeToPortableNBT (NBTTagCompound tag) { if (securityKey != null) tag.setString("Sec", securityKey); - /*NBTTagList slots = new NBTTagList(); - for (IDrawer drawer : drawers) { - if (drawer instanceof INBTSerializable) - slots.appendTag(((INBTSerializable)drawer).serializeNBT()); - else - slots.appendTag(new NBTTagCompound()); - } - - tag.setTag("Slots", slots);*/ - return tag; } - /*@Override - protected void readFromFixedNBT (NBTTagCompound tag) { - super.readFromFixedNBT(tag); - - setDirection(tag.getByte("Dir")); - - taped = false; - if (tag.hasKey("Tape")) - taped = tag.getBoolean("Tape"); - } - - @Override - protected NBTTagCompound writeToFixedNBT (NBTTagCompound tag) { - tag = super.writeToFixedNBT(tag); - - tag.setByte("Dir", (byte) direction); - - if (taped) - tag.setBoolean("Tape", true); - - return tag; - } - - @Override - public void readFromPortableNBT (NBTTagCompound tag) { - super.readFromPortableNBT(tag); - - upgrades = new ItemStack[upgrades.length]; - for (int i = 0; i < upgrades.length; i++) - upgrades[i] = ItemStack.EMPTY; - - material = null; - if (tag.hasKey("Mat")) - material = tag.getString("Mat"); - - drawerCapacity = tag.getInteger("Cap"); - - NBTTagList upgradeList = tag.getTagList("Upgrades", Constants.NBT.TAG_COMPOUND); - for (int i = 0; i < upgradeList.tagCount(); i++) { - NBTTagCompound upgradeTag = upgradeList.getCompoundTagAt(i); - - int slot = upgradeTag.getByte("Slot"); - setUpgrade(slot, new ItemStack(upgradeTag)); - } - - lockAttributes = null; - if (tag.hasKey("Lock")) - lockAttributes = LockAttribute.getEnumSet(tag.getByte("Lock")); - - shrouded = false; - if (tag.hasKey("Shr")) - shrouded = tag.getBoolean("Shr"); - - quantified = false; - if (tag.hasKey("Qua")) - quantified = tag.getBoolean("Qua"); - - owner = null; - if (tag.hasKey("Own")) - owner = UUID.fromString(tag.getString("Own")); - - securityKey = null; - if (tag.hasKey("Sec")) - securityKey = tag.getString("Sec"); - - hideUpgrade = false; - if (tag.hasKey("HideUp")) - hideUpgrade = tag.getBoolean("HideUp"); - - NBTTagList slots = tag.getTagList("Slots", Constants.NBT.TAG_COMPOUND); - - drawers = new IDrawer[slots.tagCount()]; - for (int i = 0, n = drawers.length; i < n; i++) { - NBTTagCompound slot = slots.getCompoundTagAt(i); - drawers[i] = createDrawer(i); - drawers[i].readFromNBT(slot); - } - - materialSide = ItemStack.EMPTY; - if (tag.hasKey("MatS")) - materialSide = new ItemStack(tag.getCompoundTag("MatS")); - - materialFront = ItemStack.EMPTY; - if (tag.hasKey("MatF")) - materialFront = new ItemStack(tag.getCompoundTag("MatF")); - - materialTrim = ItemStack.EMPTY; - if (tag.hasKey("MatT")) - materialTrim = new ItemStack(tag.getCompoundTag("MatT")); - - attributeChanged(); - } - - @Override - public NBTTagCompound writeToPortableNBT (NBTTagCompound tag) { - tag = super.writeToPortableNBT(tag); - - tag.setInteger("Cap", getDrawerCapacity()); - - if (material != null) - tag.setString("Mat", material); - - NBTTagList upgradeList = new NBTTagList(); - for (int i = 0; i < upgrades.length; i++) { - if (!upgrades[i].isEmpty()) { - NBTTagCompound upgradeTag = upgrades[i].writeToNBT(new NBTTagCompound()); - upgradeTag.setByte("Slot", (byte)i); - - upgradeList.appendTag(upgradeTag); - } - } - - if (upgradeList.tagCount() > 0) - tag.setTag("Upgrades", upgradeList); - - if (lockAttributes != null) - tag.setByte("Lock", (byte)LockAttribute.getBitfield(lockAttributes)); - - if (shrouded) - tag.setBoolean("Shr", true); - - if (quantified) - tag.setBoolean("Qua", true); - - if (owner != null) - tag.setString("Own", owner.toString()); - - if (securityKey != null) - tag.setString("Sec", securityKey); - - if (hideUpgrade) - tag.setBoolean("HideUp", true); - - NBTTagList slots = new NBTTagList(); - for (IDrawer drawer : drawers) { - NBTTagCompound slot = new NBTTagCompound(); - drawer.writeToNBT(slot); - slots.appendTag(slot); - } - - tag.setTag("Slots", slots); - - if (!materialSide.isEmpty()) { - NBTTagCompound itag = new NBTTagCompound(); - materialSide.writeToNBT(itag); - tag.setTag("MatS", itag); - } - - if (!materialFront.isEmpty()) { - NBTTagCompound itag = new NBTTagCompound(); - materialFront.writeToNBT(itag); - tag.setTag("MatF", itag); - } - - if (!materialTrim.isEmpty()) { - NBTTagCompound itag = new NBTTagCompound(); - materialTrim.writeToNBT(itag); - tag.setTag("MatT", itag); - } - - return tag; - }*/ - @Override public void markDirty () { if (isRedstone() && getWorld() != null) { @@ -786,12 +592,10 @@ public boolean shouldRefresh (World world, BlockPos pos, IBlockState oldState, I return oldState.getBlock() != newSate.getBlock(); } - @Override public int getDrawerCount () { return getGroup().getDrawerCount(); } - @Override @Nonnull public IDrawer getDrawer (int slot) { return getGroup().getDrawer(slot); diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java index 4f3abd45b..cfaf7f27e 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersComp.java @@ -35,7 +35,7 @@ public TileEntityDrawersComp () { } @Override - protected IDrawerGroup getGroup () { + public IDrawerGroup getGroup () { return groupData; } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java index 8801f2652..c100826b6 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java @@ -11,7 +11,6 @@ import com.jaquadro.minecraft.storagedrawers.inventory.ContainerDrawers1; import com.jaquadro.minecraft.storagedrawers.inventory.ContainerDrawers2; import com.jaquadro.minecraft.storagedrawers.inventory.ContainerDrawers4; -import com.jaquadro.minecraft.storagedrawers.storage.*; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; @@ -42,7 +41,7 @@ public Slot1 () { } @Override - protected IDrawerGroup getGroup () { + public IDrawerGroup getGroup () { return groupData; } } @@ -57,7 +56,7 @@ public Slot2 () { } @Override - protected IDrawerGroup getGroup () { + public IDrawerGroup getGroup () { return groupData; } } @@ -72,7 +71,7 @@ public Slot4 () { } @Override - protected IDrawerGroup getGroup () { + public IDrawerGroup getGroup () { return groupData; } } @@ -80,7 +79,6 @@ protected IDrawerGroup getGroup () { public static class Legacy extends TileEntityDrawersStandard { private GroupData groupData = new GroupData(4); - private static boolean processed; public Legacy () { groupData.setCapabilityProvider(this); @@ -88,7 +86,7 @@ public Legacy () { } @Override - protected IDrawerGroup getGroup () { + public IDrawerGroup getGroup () { return groupData; } @@ -127,7 +125,7 @@ public static TileEntityDrawersStandard createEntity (int slotCount) { } @Override - protected IDrawerGroup getGroup () { + public IDrawerGroup getGroup () { return null; } @@ -201,7 +199,7 @@ public GroupData (int slotCount) { @Nonnull @Override protected DrawerData createDrawer (int slot) { - return new StandardDrawerData(slot); + return new StandardDrawerData(this, slot); } @Override @@ -216,12 +214,12 @@ public T getCapability (@Nonnull Capability capability, @Nullable EnumFac } } - private class StandardDrawerData extends DrawerData + private class StandardDrawerData extends StandardDrawerGroup.DrawerData { private int slot; - public StandardDrawerData (int slot) { - super(); + public StandardDrawerData (StandardDrawerGroup group, int slot) { + super(group); this.slot = slot; } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntitySlave.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntitySlave.java index 64278f8c3..29f55bcc1 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntitySlave.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntitySlave.java @@ -7,8 +7,6 @@ import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.ControllerData; import com.jaquadro.minecraft.storagedrawers.inventory.DrawerItemHandler; import net.minecraft.block.state.IBlockState; -import com.jaquadro.minecraft.storagedrawers.api.storage.IPriorityGroup; -import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -17,9 +15,8 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.ArrayList; -public class TileEntitySlave extends ChamTileEntity implements IDrawerGroup, IPriorityGroup +public class TileEntitySlave extends ChamTileEntity implements IDrawerGroup { private static final int[] drawerSlots = new int[] { 0 }; @@ -47,6 +44,7 @@ public TileEntityController getController () { return controllerData.getController(this); } + @Nonnull @Override public int[] getAccessibleDrawerSlots () { TileEntityController controller = getController(); diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java index 6e124da31..fddf34d91 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/FractionalDrawerGroup.java @@ -24,13 +24,18 @@ public class FractionalDrawerGroup extends TileDataShim implements IDrawerGroup { private FractionalStorage storage; private FractionalDrawer[] slots; + private int[] order; public FractionalDrawerGroup (int slotCount) { storage = new FractionalStorage(this, slotCount); slots = new FractionalDrawer[slotCount]; - for (int i = 0; i < slotCount; i++) + order = new int[slotCount]; + + for (int i = 0; i < slotCount; i++) { slots[i] = new FractionalDrawer(storage, i); + order[i] = i; + } } public void setCapabilityProvider (ICapabilityProvider capProvider) { @@ -44,8 +49,8 @@ public int getDrawerCount () { return slots.length; } - @Override @Nonnull + @Override public IFractionalDrawer getDrawer (int slot) { if (slot < 0 || slot >= slots.length) return Drawers.DISABLED_FRACTIONAL; @@ -53,6 +58,12 @@ public IFractionalDrawer getDrawer (int slot) { return slots[slot]; } + @Nonnull + @Override + public int[] getAccessibleDrawerSlots () { + return order; + } + public int getPooledCount () { return storage.getPooledCount(); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java index 68e82bc68..d8e211a6a 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java @@ -1,12 +1,16 @@ package com.jaquadro.minecraft.storagedrawers.block.tile.tiledata; import com.jaquadro.minecraft.chameleon.block.tiledata.TileDataShim; -import com.jaquadro.minecraft.storagedrawers.api.storage.Drawers; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; -import com.jaquadro.minecraft.storagedrawers.storage.DrawerData; +import com.jaquadro.minecraft.storagedrawers.api.storage.*; +import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; +import com.jaquadro.minecraft.storagedrawers.inventory.ItemStackHelper; +import com.jaquadro.minecraft.storagedrawers.storage.BaseDrawerData; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.common.util.Constants; @@ -15,6 +19,7 @@ public abstract class StandardDrawerGroup extends TileDataShim implements IDrawerGroup { private DrawerData[] slots; + private int[] order; public StandardDrawerGroup () { } @@ -22,6 +27,9 @@ public StandardDrawerGroup (int slotCount) { slots = new DrawerData[slotCount]; for (int i = 0; i < slotCount; i++) slots[i] = createDrawer(i); + + order = new int[slotCount]; + syncSlots(); } public void setCapabilityProvider (ICapabilityProvider capProvider) { @@ -34,8 +42,8 @@ public int getDrawerCount () { return slots.length; } - @Override @Nonnull + @Override public IDrawer getDrawer (int slot) { if (slot < 0 || slot >= slots.length) return Drawers.DISABLED; @@ -43,6 +51,12 @@ public IDrawer getDrawer (int slot) { return slots[slot]; } + @Nonnull + @Override + public int[] getAccessibleDrawerSlots () { + return order; + } + @Override public void readFromNBT (NBTTagCompound tag) { if (!tag.hasKey("Drawers")) { @@ -87,4 +101,295 @@ public NBTTagCompound writeToNBT (NBTTagCompound tag) { @Nonnull protected abstract DrawerData createDrawer (int slot); + + public void syncSlots () { + int index = 0; + for (int i = 0; i < slots.length; i++) { + IDrawer drawer = getDrawer(i); + if (!drawer.isEmpty()) + order[index++] = i; + } + + if (index != slots.length) { + for (int i = 0; i < slots.length; i++) { + IDrawer drawer = getDrawer(i); + if (drawer.isEnabled() && drawer.isEmpty()) + order[index++] = i; + } + } + + if (index != slots.length) { + for (int i = 0; i < slots.length; i++) { + IDrawer drawer = getDrawer(i); + if (!drawer.isEnabled()) + order[index++] = i; + } + } + } + + public static class DrawerData extends BaseDrawerData + { + @CapabilityInject(IDrawerAttributes.class) + static Capability ATTR_CAPABILITY = null; + + IDrawerAttributes attrs; + StandardDrawerGroup group; + + @Nonnull + private ItemStack protoStack; + private int count; + + + public DrawerData (StandardDrawerGroup group) { + this.group = group; + attrs = new EmptyDrawerAttributes(); + protoStack = ItemStack.EMPTY; + } + + public void setCapabilityProvider (ICapabilityProvider capProvider) { + IDrawerAttributes capAttrs = capProvider.getCapability(ATTR_CAPABILITY, null); + if (capAttrs != null) + attrs = capAttrs; + } + + @Override + @Nonnull + public ItemStack getStoredItemPrototype () { + return protoStack; + } + + @Override + @Nonnull + public IDrawer setStoredItem (@Nonnull ItemStack itemPrototype) { + return setStoredItem(itemPrototype, true); + } + + protected IDrawer setStoredItem (@Nonnull ItemStack itemPrototype, boolean notify) { + if (areItemsEqual(itemPrototype)) { + return this; + } + + itemPrototype = ItemStackHelper.getItemPrototype(itemPrototype); + if (itemPrototype.isEmpty()) { + reset(notify); + return this; + } + + protoStack = itemPrototype; + protoStack.setCount(1); + count = 0; + + // TODO: Oredict blah blah + // refreshOreDictMatches(); + + group.syncSlots(); + if (notify) + onItemChanged(); + + return this; + } + + protected IDrawer setStoredItemRaw (@Nonnull ItemStack itemPrototype) { + itemPrototype = ItemStackHelper.getItemPrototype(itemPrototype); + protoStack = itemPrototype; + protoStack.setCount(1); + count = 0; + + return this; + } + + @Override + public int getStoredItemCount () { + if (protoStack.isEmpty()) + return 0; + + if (attrs.isUnlimitedVending()) + return Integer.MAX_VALUE; + + return count; + } + + @Override + public void setStoredItemCount (int amount) { + setStoredItemCount(amount, true); + } + + protected void setStoredItemCount (int amount, boolean notify) { + if (protoStack.isEmpty() || count == amount) + return; + + if (attrs.isUnlimitedVending()) + return; + + count = Math.min(amount, getMaxCapacity()); + count = Math.max(count, 0); + + if (amount == 0 && !attrs.isItemLocked(LockAttribute.LOCK_POPULATED)) + reset(notify); + else { + if (notify) + onAmountChanged(); + } + } + + protected void setStoredItemCountRaw (int amount) { + count = amount; + } + + @Override + public int adjustStoredItemCount (int amount) { + return adjustStoredItemCount(amount, true); + } + + protected int adjustStoredItemCount (int amount, boolean notify) { + if (protoStack.isEmpty() || amount == 0) + return amount; + + if (amount > 0) { + if (attrs.isUnlimitedVending()) + return 0; + + int originalCount = count; + count = Math.min(count + amount, getMaxCapacity()); + + if (count != originalCount && notify) + onAmountChanged(); + + if (attrs.isVoid()) + return 0; + + return amount - (count - originalCount); + } + else { + int originalCount = count; + setStoredItemCount(originalCount + amount, notify); + + return amount - (count - originalCount); + } + } + + @Override + public int getMaxCapacity (@Nonnull ItemStack itemPrototype) { + if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) + return Integer.MAX_VALUE; + + if (itemPrototype.isEmpty()) + return 64 * getStackCapacity(); + + return itemPrototype.getItem().getItemStackLimit(itemPrototype) * getStackCapacity(); + } + + @Override + public int getRemainingCapacity () { + if (protoStack.isEmpty()) + return 0; + + if (attrs.isUnlimitedVending()) + return Integer.MAX_VALUE; + + return getMaxCapacity() - getStoredItemCount(); + } + + @Override + public int getAcceptingRemainingCapacity () { + if (protoStack.isEmpty()) + return 0; + + if (attrs.isUnlimitedVending() || attrs.isVoid()) + return Integer.MAX_VALUE; + + return getMaxCapacity() - getStoredItemCount(); + } + + @Override + public boolean canItemBeStored (@Nonnull ItemStack itemPrototype) { + if (protoStack.isEmpty() && !attrs.isItemLocked(LockAttribute.LOCK_EMPTY)) + return true; + + return areItemsEqual(itemPrototype); + } + + @Override + public boolean canItemBeExtracted (@Nonnull ItemStack itemPrototype) { + if (protoStack.isEmpty()) + return false; + + return areItemsEqual(itemPrototype); + } + + @Override + public boolean isEmpty () { + return protoStack.isEmpty(); + } + + @Override + protected void reset (boolean notify) { + protoStack = ItemStack.EMPTY; + count = 0; + + super.reset(notify); + + group.syncSlots(); + if (notify) + onItemChanged(); + } + + @Override + public NBTTagCompound serializeNBT () { + NBTTagCompound tag = new NBTTagCompound(); + if (protoStack.isEmpty()) + return tag; + + NBTTagCompound item = new NBTTagCompound(); + protoStack.writeToNBT(item); + + tag.setTag("Item", item); + tag.setInteger("Count", count); + + return tag; + } + + @Override + public void deserializeNBT (NBTTagCompound nbt) { + ItemStack tagItem = ItemStack.EMPTY; + int tagCount = 0; + + if (nbt.hasKey("Item")) + tagItem = new ItemStack(nbt.getCompoundTag("Item")); + if (nbt.hasKey("Count")) + tagCount = nbt.getInteger("Count"); + + setStoredItemRaw(tagItem); + setStoredItemCountRaw(tagCount); + } + + public void deserializeLegacyNBT (NBTTagCompound nbt) { + ItemStack tagItem = ItemStack.EMPTY; + int tagCount = 0; + + if (nbt.hasKey("Count")) + tagCount = nbt.getInteger("Count"); + if (nbt.hasKey("Item")) { + Item item = Item.getItemById(nbt.getShort("Item")); + if (item != null) { + tagItem = new ItemStack(item); + tagItem.setItemDamage(nbt.getShort("Meta")); + if (nbt.hasKey("Tags")) + tagItem.setTagCompound(nbt.getCompoundTag("Tags")); + } + } + + setStoredItemRaw(tagItem); + setStoredItemCountRaw(tagCount); + } + + protected int getStackCapacity() { + return 0; + } + + protected void onItemChanged() { } + + protected void onAmountChanged() { } + } + } diff --git a/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java b/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java index ef07a6caa..3bc59d613 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java +++ b/src/com/jaquadro/minecraft/storagedrawers/client/renderer/TileEntityDrawersRenderer.java @@ -15,7 +15,7 @@ import com.jaquadro.minecraft.storagedrawers.block.tile.TileEntityDrawersComp; import com.jaquadro.minecraft.storagedrawers.client.model.component.DrawerSealedModel; import com.jaquadro.minecraft.storagedrawers.item.EnumUpgradeStatus; -import com.jaquadro.minecraft.storagedrawers.storage.CountFormatter; +import com.jaquadro.minecraft.storagedrawers.util.CountFormatter; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; @@ -191,7 +191,7 @@ private void renderFastItem (ChamRender renderer, @Nonnull ItemStack itemStack, List renderHandlers = StorageDrawers.renderRegistry.getRenderHandlers(); for (IRenderLabel renderHandler : renderHandlers) { - renderHandler.render(tile, tile, slot, 0, partialTickTime); + renderHandler.render(tile, tile.getGroup(), slot, 0, partialTickTime); } // At the time GL_LIGHT* are configured, the coordinates are transformed by the modelview diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/ContainerDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/ContainerDrawers.java index 7705b0280..47b53d5fb 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/ContainerDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/ContainerDrawers.java @@ -44,7 +44,7 @@ public ContainerDrawers (InventoryPlayer playerInventory, TileEntityDrawers tile storageSlots = new ArrayList<>(); for (int i = 0; i < tileEntity.getDrawerCount(); i++) - storageSlots.add(addSlotToContainer(new SlotDrawer(this, tileEntity, i, getStorageSlotX(i), getStorageSlotY(i)))); + storageSlots.add(addSlotToContainer(new SlotDrawer(this, tileEntity.getGroup(), i, getStorageSlotX(i), getStorageSlotY(i)))); upgradeSlots = new ArrayList<>(); for (int i = 0; i < 5; i++) diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java index d3fbfd59c..429ae3163 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/DrawerItemHandler.java @@ -6,7 +6,6 @@ import net.minecraft.item.ItemStack; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityInject; -import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.items.IItemHandler; import javax.annotation.Nonnull; @@ -34,10 +33,8 @@ public ItemStack getStackInSlot (int slot) { return ItemStack.EMPTY; slot -= 1; - if (group instanceof IPriorityGroup) { - int[] order = ((IPriorityGroup) group).getAccessibleDrawerSlots(); - slot = (slot >= 0 && slot < order.length) ? order[slot] : -1; - } + int[] order = group.getAccessibleDrawerSlots(); + slot = (slot >= 0 && slot < order.length) ? order[slot] : -1; IDrawer drawer = group.getDrawer(slot); if (!drawer.isEnabled() || drawer.isEmpty()) @@ -60,14 +57,9 @@ public ItemStack insertItem (int slot, @Nonnull ItemStack stack, boolean simulat } slot -= 1; - int orderedSlot = slot; - int prevSlot = slot - 1; - - if (group instanceof IPriorityGroup) { - int[] order = ((IPriorityGroup) group).getAccessibleDrawerSlots(); - orderedSlot = (slot >= 0 && slot < order.length) ? order[slot] : -1; - prevSlot = (slot >= 1 && slot < order.length) ? order[slot - 1] : -1; - } + int[] order = group.getAccessibleDrawerSlots(); + int orderedSlot = (slot >= 0 && slot < order.length) ? order[slot] : -1; + int prevSlot = (slot >= 1 && slot < order.length) ? order[slot - 1] : -1; if (StorageDrawers.config.cache.enableItemConversion && orderedSlot > 0) { IDrawer drawer = group.getDrawer(orderedSlot); @@ -126,10 +118,8 @@ public ItemStack extractItem (int slot, int amount, boolean simulate) { return ItemStack.EMPTY; slot -= 1; - if (group instanceof IPriorityGroup) { - int[] order = ((IPriorityGroup) group).getAccessibleDrawerSlots(); - slot = (slot >= 0 && slot < order.length) ? order[slot] : -1; - } + int[] order = group.getAccessibleDrawerSlots(); + slot = (slot >= 0 && slot < order.length) ? order[slot] : -1; IDrawer drawer = group.getDrawer(slot); if (!drawer.isEnabled() || drawer.isEmpty() || drawer.getStoredItemCount() == 0) diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/DefaultStorageProvider.java b/src/com/jaquadro/minecraft/storagedrawers/storage/DefaultStorageProvider.java deleted file mode 100644 index a33537636..000000000 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/DefaultStorageProvider.java +++ /dev/null @@ -1,123 +0,0 @@ -package com.jaquadro.minecraft.storagedrawers.storage; - -import com.jaquadro.minecraft.storagedrawers.StorageDrawers; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; -import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; -import com.jaquadro.minecraft.storagedrawers.network.CountUpdateMessage; -import net.minecraft.block.state.IBlockState; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.tileentity.TileEntity; - -public class DefaultStorageProvider implements IStorageProvider -{ - TileEntity tile; - IDrawerGroup group; - - public DefaultStorageProvider (TileEntity tileEntity, IDrawerGroup drawerGroup) { - tile = tileEntity; - group = drawerGroup; - } - - @Override - public boolean isCentrallyManaged () { - return false; - } - - @Override - public int getSlotCount (int slot) { - return 0; - } - - @Override - public void setSlotCount (int slot, int amount) { } - - @Override - public int getSlotStackCapacity (int slot) { - return 0; - } - - /*@Override - public boolean isLocked ( int slot, LockAttribute attr) { - return false; - } - - @Override - public boolean isVoid (int slot) { - return false; - } - - @Override - public boolean isShrouded (int slot) { - return false; - } - - @Override - public boolean setIsShrouded (int slot, boolean state) { - return false; - } - - @Override - public boolean isShowingQuantity (int slot) { - return false; - } - - @Override - public boolean setIsShowingQuantity (int slot, boolean state) { - return false; - } - - @Override - public boolean isStorageUnlimited (int slot) { - return false; - } - - @Override - public boolean isVendingUnlimited (int slot) { - return false; - }*/ - - @Override - public boolean isRedstone (int slot) { - return false; - } - - @Override - public void markAmountDirty (int slot) { - if (tile.getWorld().isRemote) - return; - - int count = group.getDrawer(slot).getStoredItemCount(); - - //IMessage message = new CountUpdateMessage(tile.getPos(), slot, count); - //NetworkRegistry.TargetPoint targetPoint = new NetworkRegistry.TargetPoint(tile.getWorld().provider.getDimensionId(), tile.getPos().getX(), tile.getPos().getY(), tile.getPos().getZ(), 500); - - for (Object playerObj : tile.getWorld().playerEntities) { - if (!(playerObj instanceof EntityPlayerMP)) - continue; - - EntityPlayerMP player = (EntityPlayerMP)playerObj; - if (player.getPosition().distanceSq(tile.getPos().getX(), tile.getPos().getY(), tile.getPos().getZ()) < 500 * 500) - StorageDrawers.network.sendTo(new CountUpdateMessage(tile.getPos(), slot, count), player); - } - - tile.markDirty(); - - //StorageDrawers.network.sendToAllAround(message, targetPoint); - - if (isRedstone(slot)) { - IBlockState state = tile.getWorld().getBlockState(tile.getPos()); - tile.getWorld().notifyNeighborsOfStateChange(tile.getPos(), state.getBlock(), false); - tile.getWorld().notifyNeighborsOfStateChange(tile.getPos().down(), state.getBlock(), false); - } - } - - @Override - public void markDirty (int slot) { - if (tile.getWorld().isRemote) - return; - - IBlockState state = tile.getWorld().getBlockState(tile.getPos()); - tile.getWorld().notifyBlockUpdate(tile.getPos(), state, state, 3); - tile.markDirty(); - } -} diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java index 781598e08..7af25f349 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java @@ -1,9 +1,10 @@ -package com.jaquadro.minecraft.storagedrawers.storage; +/*package com.jaquadro.minecraft.storagedrawers.storage; import com.jaquadro.minecraft.storagedrawers.api.storage.EmptyDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*; +import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.StandardDrawerGroup; import com.jaquadro.minecraft.storagedrawers.inventory.ItemStackHelper; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; @@ -20,13 +21,15 @@ public class DrawerData extends BaseDrawerData static Capability ATTR_CAPABILITY = null; IDrawerAttributes attrs; + StandardDrawerGroup group; @Nonnull private ItemStack protoStack; private int count; - public DrawerData () { + public DrawerData (StandardDrawerGroup group) { + this.group = group; attrs = new EmptyDrawerAttributes(); protoStack = ItemStack.EMPTY; } @@ -67,6 +70,7 @@ protected IDrawer setStoredItem (@Nonnull ItemStack itemPrototype, boolean notif // TODO: Oredict blah blah // refreshOreDictMatches(); + group.syncSlots(); if (notify) onItemChanged(); @@ -213,6 +217,7 @@ protected void reset (boolean notify) { super.reset(notify); + group.syncSlots(); if (notify) onItemChanged(); } @@ -275,3 +280,4 @@ protected void onItemChanged() { } protected void onAmountChanged() { } } +*/ \ No newline at end of file diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/IStorageProvider.java b/src/com/jaquadro/minecraft/storagedrawers/storage/IStorageProvider.java deleted file mode 100644 index 2b86a22e3..000000000 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/IStorageProvider.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.jaquadro.minecraft.storagedrawers.storage; - -import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; - -public interface IStorageProvider -{ - boolean isCentrallyManaged (); - - int getSlotCount (int slot); - - void setSlotCount (int slot, int amount); - - int getSlotStackCapacity (int slot); - - //boolean isLocked (int slot, LockAttribute attr); - - //boolean isVoid (int slot); - - //boolean isShrouded (int slot); - - //boolean setIsShrouded (int slot, boolean state); - - //boolean isShowingQuantity (int slot); - - //boolean setIsShowingQuantity (int slot, boolean state); - - //boolean isStorageUnlimited (int slot); - - //boolean isVendingUnlimited (int slot); - - boolean isRedstone (int slot); - - void markAmountDirty (int slot); - - void markDirty (int slot); -} diff --git a/src/com/jaquadro/minecraft/storagedrawers/util/CompactingHelper.java b/src/com/jaquadro/minecraft/storagedrawers/util/CompactingHelper.java index 660de473b..11af60eb3 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/util/CompactingHelper.java +++ b/src/com/jaquadro/minecraft/storagedrawers/util/CompactingHelper.java @@ -2,7 +2,7 @@ import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.config.CompTierRegistry; -import com.jaquadro.minecraft.storagedrawers.storage.DrawerData; +import com.jaquadro.minecraft.storagedrawers.storage.BaseDrawerData; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.CraftingManager; @@ -80,7 +80,7 @@ public Result findHigherTier (@Nonnull ItemStack stack) { if (comp.getCount() != lookupSize) continue; - if (!DrawerData.areItemsEqual(comp, stack, false)) + if (!BaseDrawerData.areItemsEqual(comp, stack, false)) continue; candidates.add(match); @@ -123,7 +123,7 @@ public Result findLowerTier (@Nonnull ItemStack stack) { for (IRecipe recipe : CraftingManager.REGISTRY) { ItemStack output = recipe.getRecipeOutput(); - if (!DrawerData.areItemsEqual(stack, output, true)) + if (!BaseDrawerData.areItemsEqual(stack, output, true)) continue; @Nonnull ItemStack match = tryMatch(stack, recipe.getIngredients()); @@ -132,7 +132,7 @@ public Result findLowerTier (@Nonnull ItemStack stack) { List compMatches = findAllMatchingRecipes(lookup1); for (ItemStack comp : compMatches) { int recipeSize = recipe.getIngredients().size(); - if (DrawerData.areItemsEqual(match, comp, true) && comp.getCount() == recipeSize) { + if (BaseDrawerData.areItemsEqual(match, comp, true) && comp.getCount() == recipeSize) { candidates.add(match); candidatesRate.put(match, recipeSize); diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/CountFormatter.java b/src/com/jaquadro/minecraft/storagedrawers/util/CountFormatter.java similarity index 97% rename from src/com/jaquadro/minecraft/storagedrawers/storage/CountFormatter.java rename to src/com/jaquadro/minecraft/storagedrawers/util/CountFormatter.java index 90eb2a953..3f70af913 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/CountFormatter.java +++ b/src/com/jaquadro/minecraft/storagedrawers/util/CountFormatter.java @@ -1,4 +1,4 @@ -package com.jaquadro.minecraft.storagedrawers.storage; +package com.jaquadro.minecraft.storagedrawers.util; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; import net.minecraft.client.gui.FontRenderer; From 0bbd6e7b0105b86fc5e9f6b83a8f3eb24cef3fd0 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Wed, 5 Jul 2017 02:37:55 -0400 Subject: [PATCH 19/22] Refactoring! More upgrade slots! Conversion upgrades! --- .../assets/storagedrawers/lang/en_US.lang | 2 + .../models/item/upgrade_conversion.json | 6 + .../recipes/upgrade_conversion.json | 18 ++ .../storagedrawers/textures/gui/drawers_1.png | Bin 1351 -> 1361 bytes .../storagedrawers/textures/gui/drawers_2.png | Bin 1382 -> 1393 bytes .../storagedrawers/textures/gui/drawers_4.png | Bin 1402 -> 1414 bytes .../textures/gui/drawers_comp.png | Bin 1540 -> 1552 bytes .../textures/items/upgrade_conversion.png | Bin 0 -> 488 bytes .../api/storage/IDrawerAttributes.java | 4 + .../storage/IDrawerAttributesModifiable.java | 4 + .../block/tile/TileEntityDrawers.java | 4 + .../block/tile/TileEntityDrawersComp.java | 5 + .../block/tile/TileEntityDrawersStandard.java | 20 ++ .../tile/tiledata/FractionalDrawerGroup.java | 107 +++---- .../tile/tiledata/StandardDrawerGroup.java | 45 ++- .../block/tile/tiledata/UpgradeData.java | 9 + .../config/OreDictRegistry.java | 1 + .../storagedrawers/core/ModItems.java | 5 + .../capabilities/BasicDrawerAttributes.java | 18 ++ .../inventory/ContainerDrawers.java | 6 +- .../storagedrawers/storage/DrawerData.java | 283 ------------------ .../storagedrawers/util/CompactingHelper.java | 7 +- .../storagedrawers/util/ItemStackMatcher.java | 28 ++ .../ItemStackOreMatcher.java} | 91 ++---- 24 files changed, 247 insertions(+), 416 deletions(-) create mode 100644 resources/assets/storagedrawers/models/item/upgrade_conversion.json create mode 100644 resources/assets/storagedrawers/recipes/upgrade_conversion.json create mode 100644 resources/assets/storagedrawers/textures/items/upgrade_conversion.png delete mode 100644 src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java create mode 100644 src/com/jaquadro/minecraft/storagedrawers/util/ItemStackMatcher.java rename src/com/jaquadro/minecraft/storagedrawers/{storage/BaseDrawerData.java => util/ItemStackOreMatcher.java} (62%) diff --git a/resources/assets/storagedrawers/lang/en_US.lang b/resources/assets/storagedrawers/lang/en_US.lang index 6d2607561..b3a946eaa 100644 --- a/resources/assets/storagedrawers/lang/en_US.lang +++ b/resources/assets/storagedrawers/lang/en_US.lang @@ -26,6 +26,8 @@ item.storagedrawers.upgradeStatus.level2.name=Status Upgrade (II) item.storagedrawers.upgradeStatus.level2.description=Shows fill level item.storagedrawers.upgradeVoid.name=Void Upgrade item.storagedrawers.upgradeVoid.description=Destroys excess items +item.storagedrawers.upgradeConversion.name=Conversion Upgrade +item.storagedrawers.upgradeConversion.description=Auto-converts compatible items item.storagedrawers.upgradeCreative.store.name=Creative Storage Upgrade item.storagedrawers.upgradeCreative.store.description=Near-infinite storage item.storagedrawers.upgradeCreative.vend.name=Creative Vending Upgrade diff --git a/resources/assets/storagedrawers/models/item/upgrade_conversion.json b/resources/assets/storagedrawers/models/item/upgrade_conversion.json new file mode 100644 index 000000000..bdb3edd9d --- /dev/null +++ b/resources/assets/storagedrawers/models/item/upgrade_conversion.json @@ -0,0 +1,6 @@ +{ + "parent": "item/generated", + "textures": { + "layer0": "storagedrawers:items/upgrade_conversion" + } +} \ No newline at end of file diff --git a/resources/assets/storagedrawers/recipes/upgrade_conversion.json b/resources/assets/storagedrawers/recipes/upgrade_conversion.json new file mode 100644 index 000000000..02fb1c1f1 --- /dev/null +++ b/resources/assets/storagedrawers/recipes/upgrade_conversion.json @@ -0,0 +1,18 @@ +{ + "conditions": [{ + "type": "minecraft:item_exists", + "item": "storagedrawers:upgrade_conversion" + }], + "type": "minecraft:crafting_shaped", + "pattern": [ + "#/#", + "/X/", + "#/#" + ], + "key": { + "/": { "ore": "stickWood", "type": "forge:ore_dict" }, + "#": { "item": "minecraft:dye", "data": 4 }, + "X": { "item": "storagedrawers:upgrade_template" } + }, + "result": { "item": "storagedrawers:upgrade_conversion" } +} \ No newline at end of file diff --git a/resources/assets/storagedrawers/textures/gui/drawers_1.png b/resources/assets/storagedrawers/textures/gui/drawers_1.png index 508fa947483e2cb2fde331a97b3b2e6f54628b62..a40220bceabc7bb4ed7911af0f42a177c777745d 100644 GIT binary patch literal 1361 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K5893O0R7}x|G$6%Z;_2(k{*axORfBc^xog=# zAOP(g0+!r!?yA#nwguMcduT(`c`{k(97?i z`$feY9zAZ)2g-KAkHg@j+IXJ}Yzt#Wwu=bmxgDKE2 zI`4!Va`!w4jbX@n%(`BO5qD4`Eb%KczL2$5%69Rcqvhr0>3;6Tr+UM;yiF-*o+g~s zQ^cKbX?XT!df#rn+v#whW~(vy`};p*Ik#|~8dHGZvhMEgi|SJ?YtJs+_lohxe4nE; zCZC$K@7aa6Rg6Errri2HAHwa+0!o^+tDm30FaO-{8P3tGa=G{Y_x-e6`1yzJJ(lm& z`Epm*@iMG%XHbX`_Gim_`+wP4%d;}CR?9W4yw?Bhi)p(VFjhfnLp_qo=c`9v@vm9V zzuTtU?-sY;x1o{QHe&sC`#AI2zLk<$^8V+E^MBtx-WMD1J=xj#Wm;&4;XA^EJn+JTAq3S zqR-)ik~Nd>aUq!gvM)*tVjItboV)IT&+&B!KsFqG`4bx6n0XPqRcF8&+kf5=o$>C5 zXoVFle_fdR_jvuXKs_ct{j%F{ukni?dSlPXGkyB>t^KU`&afWHl=;U{zLlTj-ig`r ez=De`Q1w+-{l12UT+`ZmkbX~BKbLh*2~7ZDEZge< literal 1351 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K5893O0R7}x|G$6%Z;_2(kexFr@Sya4_eOd-k zh_k>WvY3H^uLOh{vsE@<0}8UFJNh~@Fl_AS{*%ZG3P1DWKAGyuJ|}}&b9Ul&rpRvd*&hvz>*l*VsZIQ0(p9(r zhxwWJZ=bcd^R78u_J8*3)vE(vf4jWg|F&U$-CMnXl{FP6-?tu^oU{FQ;>Kn3cP#&u z^!ViYQ+Mv(<>eQ9%NZk6_xRtxx_8-4i}UjH&97g-KKpK-I+OC=A3wSM>oO`0>sU5~ zF)<|KLE?|y*PK3k_UxVM1~bdw-^htJuJAdAb zNA(+78#X-AZrE#IR)4ORbMfB&b;jRSHzd7e;JNm1($CM=yEosxWMCW3z#&W=I(?td z=9p%mxVGHmqeUDCJPr$2@Eor&R43j9phd-U2RIH#G93Q<2k2RYLj{xi58Ff-6K{m~ zeZD{cew}^x`TCdsh8=%i)&Ko;`_6QR%^=U-(PlVaagcu}gT*oKx(%!i28;yIhS0k_ zCB?^1+>Gy_=!t=cl7{ z?i;j!E(l|k5c|G5e7&1~QSl0k*$i*QzbBkgGH;)>nd89u?&|lJ-FIZN!sHwND%r)H z+5c;w<@wo%E2Hk#vsi8UKl|-{u}=^0S3R?-=X;*{bv;AFb6@~P{}p?X?)`iFr{cMX ze*#UMlVzXHr@D-l!ZEJsf z=lbR}h*F?xJgDqTo`d}H+;c+De(d=4^3U_P2fTmX$SR&yd}pbZEZnEWqzmgRxdWH% zw%wZZOlDc}m&XU^a-;xFm6^x!k@P(BJ zp6Mi>d${tzoW9`3vp|LUe*VYB7!I)zjplIW09|MxyPRpWl^?4eJnX_!|9_f)S>hhc z9lNR>yLW#++&bg0cEj7-+w-6A6RR-FXP9^FA^(TOcic_tpPsM-iz{+K!`5Hi^CzFW To%+9fFG#45PWx?*1qQxm6(F+ zwl4ZMdtKkIs;Ip;*C+5?erUdZ`}WZ1Z!cfI9P;_Od->1z*Z;1*cAV+Gk#DeNX^{m>3SSFf>6V$|CKPbY6em^;J>l*}HdnrNzaUe;J(9 z`dj%gs*q8`?(awc*Uk|~4j*o2-}S>^hfyKoiRgiY&)?o(%fIke{P+58b}VdSvJFXJ zyr1dE>)XGX)b=!z$zj6}?FOmhKmAUR=2rhwWSDx2eMg)1_bn-$NEYLC5klfOV~9pI zxCrvs3vGsc zHl^4+g@tKcTQvU;k8yaJGSspxndUVbK%Pm)38BU!nDJ>1H z4Vr0uP3Bg%T*Kj$Ay?*vJnJ!Sw=>~A(64^^;D5`NXC$-v{$_|Z9QG`(`Sj=2^Jg_Z zmS2|M-1eZo-m|Xkk<5SX*nt&&+U3kb&dc(bGS<2=^kV zFci-NCH(AoUu?bi?2Ezi>|@hE-#*@0U3GVh<;-H6rMqN+afYrOUxdxQ!@pqrb>G>O zE!&^Ysj=N5+j!<>;<=A~4srZ;^43f$C}vRxinu#pAazVc38<2aik>0T*Fzo)C8%Q~loCIHB@0)7Ah literal 1382 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K5893O0R7}x|G$6%Z;_2(kexFr@S(NK7QF68M2pgb==}0DQEWufistVobl2A{bvu2h9m0S zm2rQpe|&%TX!UY#iNk5%C$C<;I`aA3irYx+uy%e+6`{6H{U*&G)C>ni9$65b=xa!0g5q-s}lRzhZ0h*^>BxZap2(c;?|* zhaNwMv`=XYZfTqhiA+SJZ_G0e&Tf>L%W&oa(1>$DPhARUxX0rX`|r*6rDmzq7zC#6 zW-53tx9=3|fw|Q`P5~9*322B^Tb0C%Z{Nzk^}NhHz%Oq%qd4K*mjfca&a4SWa~Bl* z^fUR(ixls(KYn?VEYN&4Fe)o!{PE@GWJ~)5zUL3uFd59flYf8T+}cUAosZsRNhmM! ze>UgIvw$Ko#vk9F+^an6na5{(m_4Cf=>7aN_kY(G$C2L4*J^5Sz z^-SeonRAyvgfkp?&B@ThyPxI9wOjkb>(&)p$fpZ8B(F_ByYL9aH*HZ&GOw@Rdv-v4 z_H%jv^LGCaxe4?<`1kMM=j(6FtZQ%1`Bgz&sIt#z`(l+>e9hqa>r>M|&+lhm|8ncq zXC}`pyh<(MK85%fKUr2_->`M>wp(+a$xJK$@by6%i%~jn@wal8PgNftZ&aROxq5!G z`+}!C*ols3V2S`4^0hdkPqy()Hn5=MNiF|*zR<^<(fs^8IXSsMPo2)Z<4XAa?(XhjdG0+kq#KTJ{4f5a nso9eo`c7&i8E|4C$JU|<#Xba4!+xb^m~ub;H1OnajF zBdN&3=8J0{aO6A`x*7RM?%r9|Gt;zZ_?nm}m|pLAvQTNeLfA3G%krUT-&Zc_Xqd8p z?TWw4Rxe)nE-e3>c0}8{|7YL6eY@cCx0f$hN`HDj{oSVzrT_a}2= zI0UB)K@z_iWu{brIbHj0{?pP2GE4;lWjAIfPX<|bE)i(dqVo)=rrk;nzkB|Z)rY{3 z?oVT)vu^L2Ef&e-u;GOM-vgOfSxO8~6L#&|B~=qJbMqR@E8(^be-DQ6Sgtq|c}?b4fGNX!{$=<6 zL%D&v41XPqYd-wBe*WxFnc@uZt+@w(*L&6h!}Y&*^10_TougO9Gc&vrWZ<}VWO8HF z@BNv@&t^K;US&48_Uq!D)UykDA#S*#DW) z-#n>fSZkYiYt9)TUwSI+XO;={)qf+yxYdwxSO!)hHPNv59Zb9iQA z-~JL|KpBhC@}HJvEVKHuVNrrThoL)jv+@MXv*#17)XQ0##BK^^*dU^GvN+>gt{kv) zS<`IFSj~Vo==u2{Cn^ykaydk!&+&B!boyc&kHa%t_oey&jQffNH&k!E|F-O3k)lsM z+lAQp`0I801!m$5#|!_6?^yr0?SYToA7Eie7T6H-pGmf^#4$hLPy}Rvr>mdKI;Vst E0G;{+LjV8( literal 1402 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K5893O0R7}x|G$6%Z;_2(kexFr@SyV7R#W)ry z#981GSso9eo`c7&i8E|4C$JU|{9+ba4!+xb^m~qn~uTL_?xX zVeh)4=2hB7EZZIm-Bm2)FWcL4^BLEMl~v7as(QVqcy3q__E_T0#`HgS{UQ@xPNi#0 z?VnkH;?HJ_>W>v{pEpGOee&s3(elUNPEXhGj{W!N&RqNY+M1#_-SbJI>Q<#_Yr@ay;=ADmCO z-*smwR%H-S!;Q-J{qCsTxo@A{w`hs8&(F_4Z&&-PYu2dxF6=&Cl`m^Z%!>EL;H;*}&4U z;g95jQ+E5TH=GUq;k=hG$C)8c(k^26jc;0i7RIsH9TQ?W#6mQB!~P*>hkN~w^Z!EP z*taP%oNtpYiMIxCTB7X54 znB2I+n?2#dNrx#Qfqy&+M<+LG2sMP;NOnj8eI!gY+AM#-*q?2Y2gAHdBLUZG3tUGj=n;5faUp?z^{rYw5^Ahja_UztmoPK!D z!!}K+G|`5G1r6_#c8FH|STN)BW6O8b-T*_@fDwc?Oy)jteSQ2{%L6ighdo&nEOyw} z{n_!^F^^AFUAW=uLE9To70lZwP3Ac8y1DwPE&EQ(D`)H&RySAtP55{5{p`>CEaxn{ z`PY3x>FWBM|K2;q{SQAs=ll7GCTIWgFjUwB&A+M77&rUY_f!6*e9IrFFl|`>b&che z2quRa3=TKeOk?<6wPm~Ihr5qn9)CPJfByzSmg0iy>fdi)7wweUr_=Yh?%5e)V%O{* z?~B4a&wLX5j!*4>zI;2g|I3Y6pP4-0;azG23|c&?7ENkfp?$+j+ikbzJd-)L`Qyh2 zWh_SNT*cSQSw2;K_`6Ygg5~7-$?gk2?qFj`WFi`Y4EaiT3Gk3E)RXyGIp-4r}A=j8cB8|iYECe2OC7yd!Q5fqfix*N(^&A@JDO{;&ym2>XN z^Boqpa>S$?c%t2`y#eUK!xeA1&b+xHT2ZgVsNll@L^GydeQE#OFoq?@rgG2z{oki^ z8?H8PI0^(C?{nvzWj*jW;y-td%{S>L-Jc(rfh8O{;6Th@?x{v%pM&%!>;mccboFyt I=akR{03!bdDF6Tf diff --git a/resources/assets/storagedrawers/textures/gui/drawers_comp.png b/resources/assets/storagedrawers/textures/gui/drawers_comp.png index 5fc9aea1f56444bc4119e940834f6c507972f3b3..18c89d60f59d144bf8f73fd731ccffc52add19ed 100644 GIT binary patch delta 1174 zcmV;H1Zn$(43G?vDhlBT1qKrY_C2?pku)fOpGibPRCwC$-O)|sSQv)k1W^MG(8F9n z3EV;S{8d&qbi4qt##7`60_u_Ypv=lK}x4lduE^e>?6zqAl0g*JHnh z-dtW@PR`HI$9@}wdVYR>e?2)l`LExXE-nDY%gf8yZ=n}AH#f02bd?=?+X&eCKRE@U zX{$T^Haqs}|Fi2+!y$@10$6%@co_RFboS@ZpRwP<=*o_~+_5(u3E26+iVi^0`XTet z(b2)4((K~52-x|*f9Di{gYnbpfSvzm@kan%?Bf%K#4GoGT0at4|8M+D06Ho=a2Lr} z?)$PDfz_|^l~Vu+CIB3q?>-Ln+5h(hfP=Dr!Ee`}6G$Tf9E_i?_KU#J1b~C^+edvO z@I3*bdE0)@F9Kf)08QKVhkQifUjo2E*yVG6KNIlD4+7D&f5*p1j|uO<5dZ`e00<@k z5KI6dm;gX90f1mW2JE1$1yJtihgJzd2W2H6i+Rv8rvMNv`Us#bsa;mpg;dc2C@iT> zGT;6nSo9G02D(lscq?QYUJAz1dBTYa1|at#Rb4sc=+TLli&m-e{7JOfv&HwJI^S0 z@!j6un$ysna^n?s8|-etvO#oFZs9E(raA4*ao1)2oB}{F0f1lv0KuvWz|+%{Ie~K+ zR}}#$8`J6OY3Gf~oVMmLzR?4)+?a|k8<09u-s_Y zH=duqZq8xMp)R8>UkkqcPV(J`v;JMioWq$zT}HnK0E|ZfAXpVo2CD6|X>*2|!}vxI zz`o8O&1q{6b#Mv*!2|$;4RQ*wuSas5)7Bj7H+}o(_09V)EZ{&3X0zG*>wG>hFVnH# zPs^MFk^~_j1OS3nAm=hxeTYkJ(YIhk|ZrP?2-lc5A6lOF>NDBHLm zKy8nkHYalq{n_|gTvP-5;lup#&n69|H_UvnsGT)z6_nRt2Ux zRt4T4rvMO403etE(3IujKl@QG0#4t&E*1-axe#V~1TZ~2I~)7Gz}8#<+>iCYz}KU5X3y(Lltb<_e0ssI207*qoM6N<$f`U37M*si- delta 1156 zcmV-~1bh3C41^4jDhl5P6ay6^*tAkhku)fOlSxEDRCwC$-O)|sSQv)kB%%fypa(9X z1a6^_Nc53H!YwTX32LALih$*?L}UyJ362vVf6pVW*kO16iAMH|?Zn69(U-Ro)M32* zm#eF*R@?3U{r$Mj@2}m-=hwG)$9^ramGO97HrD7JcV8wWJ3p0KEEeU9jdl-Cn%{UJ zcz8JWYl4ktv)Sn8=4LdX&qbi6$?7lc{E%hRegse}ld%F8ldb{@e@@zusLOOZJ@!lJ z%=Pv4=<@RN*e~OtUS3{4zMh_*{^|Fniwi*U`uck8m(YpZ+uPV1y2=i{Z3OK6Uz`F^ zwbh+|n;m=g|Jn7ZVTdA+0G1vfACLVKT01{KKlV#Fy0RlLckE3^0(SoIq61LWe#ktT zOa^;Ovy0y%VCVmte^USkW8LY1oqxUfBY-yc@s2{`mHRF=j|7_kjeiM1OJxUcBl*gG zS5_mi`aQmL3IM?bfWi6c?LhDS|4aZFl;#D$T|Xy~MgSO$byxdEU_Aj~5Po~BPXvA@ z090?=_xVNOI{~0-yMD+=1pXxe48kt&^ZQJ|7as(oYEMs3e;yOwf+GM3CIApj03etE zKrjJ-U;+TaybahwSqq@t&kwZ{fELP1J{I$!W=;VhSo9GKstc*215j8}n`D0c zL9pl}fG(d5wED!JL;X3htLOj}U0ua*KC$Oee-7-zDF6hEKLS`f)Kh*uO^7}bfE9;& z%8#cB(HBkulTQRHMiT%CW<7vUhkDA7rwP#~0`TcjPx=AE>SmznblQ4Gxf}5A z?#`SIbIJ`^a>f3weL!HBL3?=~kI-fO%Bu)V! zm;gYqs!jp+^=NN%;D6)yf8O4G{K7&I)L=H7eSDqI=jF@z*srH%P63lo1SB3d-VNw+ z+~)LQj>naE=hxeT`}Ejqb28_+OSL;r0h3PzDl8KK2)1!OfWAGp+MLWebZ6t^!L0~n zP9NqpR|N8S@cpqOkU71dV@06uxCWCD1Rj&H1P~o70-4kMIdsR0K<4y*junBrV?`iy zdOydCK;3Z-lb-@8ldb{=HC6;Nr}uN{&c+pippUEwv_DP(AeaC^FadyI8!rM{71*3U z%xSI)%>Gyv*qq+aadEmkP63lo1S&`q00?GPU~_suhwfMvnCe&+cz>J%KrjJ-U;;o@ zmWTiBN4W?%e)qOmEd1p{nB@_`_~PQ?*sle)<^te;H2(tMj{SmQOLl&f!38)MsMUW4 W`c+;M;ymO40000P)WdKxlWgs#(AVGC!ATls9G$1lDIy5pmGczD9Ff}kRJ3!xc00007bV*G` z2jK?=1tTaK|iC^s|>3j^4P0(*hQlMjbT~23ZkNuwc`S z{cr_YIRy;QUjG2IU%z<|7r*}WI>WcCPy@bw`^6y2!^`mF#}5V$4i1L83`Z~<41N|c zOng%gcKXr1D{;F3q!9)lzj=+Ks9b9zL+7=8hD_uA484nvGh!Hkt_kK9m^cVWiPbP% z`G6w~K;YDcXK=w&4A0;J3iBe~Km;i;Gmrw?4Dx%|g5z)ni6J_0SFPQ15qB8Coc;RC zXSgPCyaNLl1mJ;+%L}-|09hW{TNnm_G=UVsXmkK^$s61T95{6coccfkD9pzVPl ATTR_CAPABILITY = null; - private FractionalStorage storage; private int slot; - IDrawerAttributes attrs; - public FractionalDrawer (FractionalStorage storage, int slot) { this.storage = storage; this.slot = slot; - - attrs = new EmptyDrawerAttributes(); - } - - public void setCapabilityProvider (ICapabilityProvider capProvider) { - IDrawerAttributes capAttrs = capProvider.getCapability(ATTR_CAPABILITY, null); - if (capAttrs != null) - attrs = capAttrs; } @Nonnull @@ -572,16 +590,18 @@ public int getRemainingCapacity () { } @Override - public boolean canItemBeStored (@Nonnull ItemStack itemPrototype) { - if (getStoredItemPrototype().isEmpty() && !attrs.isItemLocked(LockAttribute.LOCK_EMPTY)) - return true; + public int getAcceptingRemainingCapacity () { + return storage.getAcceptingRemainingCapacity(slot); + } - return areItemsEqual(itemPrototype); + @Override + public boolean canItemBeStored (@Nonnull ItemStack itemPrototype) { + return storage.canItemBeStored(slot, itemPrototype); } @Override public boolean canItemBeExtracted (@Nonnull ItemStack itemPrototype) { - return areItemsEqual(itemPrototype); + return storage.canItemBeExtracted(slot, itemPrototype); } @Override @@ -594,23 +614,6 @@ public boolean isEnabled () { return storage.isEnabled(slot); } - @Override - protected void reset (boolean notify) { - super.reset(notify); - refreshOreDictMatches(); - } - - @Override - public NBTTagCompound serializeNBT () { - // Handled by group - return new NBTTagCompound(); - } - - @Override - public void deserializeNBT (NBTTagCompound nbt) { - // Handled by group - } - @Override public int getConversionRate () { return storage.getConversionRate(slot); diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java index d8e211a6a..3dd1aa218 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java @@ -4,7 +4,8 @@ import com.jaquadro.minecraft.storagedrawers.api.storage.*; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; import com.jaquadro.minecraft.storagedrawers.inventory.ItemStackHelper; -import com.jaquadro.minecraft.storagedrawers.storage.BaseDrawerData; +import com.jaquadro.minecraft.storagedrawers.util.ItemStackMatcher; +import com.jaquadro.minecraft.storagedrawers.util.ItemStackOreMatcher; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -13,6 +14,7 @@ import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.common.util.Constants; +import net.minecraftforge.common.util.INBTSerializable; import javax.annotation.Nonnull; @@ -102,6 +104,12 @@ public NBTTagCompound writeToNBT (NBTTagCompound tag) { @Nonnull protected abstract DrawerData createDrawer (int slot); + public void syncAttributes () { + for (DrawerData drawer : slots) + drawer.syncAttributes(); + + } + public void syncSlots () { int index = 0; for (int i = 0; i < slots.length; i++) { @@ -127,7 +135,7 @@ public void syncSlots () { } } - public static class DrawerData extends BaseDrawerData + public static class DrawerData implements IDrawer, INBTSerializable { @CapabilityInject(IDrawerAttributes.class) static Capability ATTR_CAPABILITY = null; @@ -138,12 +146,13 @@ public static class DrawerData extends BaseDrawerData @Nonnull private ItemStack protoStack; private int count; - + private ItemStackMatcher matcher; public DrawerData (StandardDrawerGroup group) { this.group = group; attrs = new EmptyDrawerAttributes(); protoStack = ItemStack.EMPTY; + matcher = ItemStackMatcher.EMPTY; } public void setCapabilityProvider (ICapabilityProvider capProvider) { @@ -165,7 +174,7 @@ public IDrawer setStoredItem (@Nonnull ItemStack itemPrototype) { } protected IDrawer setStoredItem (@Nonnull ItemStack itemPrototype, boolean notify) { - if (areItemsEqual(itemPrototype)) { + if (matcher.matches(itemPrototype)) { return this; } @@ -179,8 +188,10 @@ protected IDrawer setStoredItem (@Nonnull ItemStack itemPrototype, boolean notif protoStack.setCount(1); count = 0; - // TODO: Oredict blah blah - // refreshOreDictMatches(); + if (attrs.isDictConvertible()) + matcher = new ItemStackOreMatcher(protoStack); + else + matcher = new ItemStackMatcher(protoStack); group.syncSlots(); if (notify) @@ -195,6 +206,11 @@ protected IDrawer setStoredItemRaw (@Nonnull ItemStack itemPrototype) { protoStack.setCount(1); count = 0; + if (attrs.isDictConvertible()) + matcher = new ItemStackOreMatcher(protoStack); + else + matcher = new ItemStackMatcher(protoStack); + return this; } @@ -306,7 +322,7 @@ public boolean canItemBeStored (@Nonnull ItemStack itemPrototype) { if (protoStack.isEmpty() && !attrs.isItemLocked(LockAttribute.LOCK_EMPTY)) return true; - return areItemsEqual(itemPrototype); + return matcher.matches(itemPrototype); } @Override @@ -314,7 +330,7 @@ public boolean canItemBeExtracted (@Nonnull ItemStack itemPrototype) { if (protoStack.isEmpty()) return false; - return areItemsEqual(itemPrototype); + return matcher.matches(itemPrototype); } @Override @@ -322,12 +338,10 @@ public boolean isEmpty () { return protoStack.isEmpty(); } - @Override protected void reset (boolean notify) { protoStack = ItemStack.EMPTY; count = 0; - - super.reset(notify); + matcher = ItemStackMatcher.EMPTY; group.syncSlots(); if (notify) @@ -383,6 +397,15 @@ public void deserializeLegacyNBT (NBTTagCompound nbt) { setStoredItemCountRaw(tagCount); } + public void syncAttributes () { + if (!protoStack.isEmpty()) { + if (attrs.isDictConvertible()) + matcher = new ItemStackOreMatcher(protoStack); + else + matcher = new ItemStackMatcher(protoStack); + } + } + protected int getStackCapacity() { return 0; } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java index 79daeda33..3168862d9 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/UpgradeData.java @@ -28,6 +28,7 @@ public class UpgradeData extends TileDataShim private boolean hasVoid; private boolean hasUnlimited; private boolean hasVending; + private boolean hasConversion; private IDrawerAttributesModifiable attrs; @@ -146,6 +147,10 @@ public boolean hasVendingUpgrade () { return hasVending; } + public boolean hasConversionUpgrade () { + return hasConversion; + } + private int getNextUpgradeSlot () { for (int i = 0; i < upgrades.length; i++) { if (upgrades[i].isEmpty()) @@ -167,6 +172,7 @@ private void syncUpgrades () { hasVoid = false; hasUnlimited = false; hasVending = false; + hasConversion = false; for (ItemStack stack : upgrades) { Item item = stack.getItem(); @@ -174,6 +180,8 @@ private void syncUpgrades () { hasOneStack = true; else if (item == ModItems.upgradeVoid) hasVoid = true; + else if (item == ModItems.upgradeConversion) + hasConversion = true; else if (item == ModItems.upgradeCreative) { EnumUpgradeCreative type = EnumUpgradeCreative.byMetadata(stack.getMetadata()); if (type == EnumUpgradeCreative.STORAGE) @@ -184,6 +192,7 @@ else if (type == EnumUpgradeCreative.VENDING) } attrs.setIsVoid(hasVoid); + attrs.setIsDictConvertible(hasConversion); attrs.setIsUnlimitedStorage(hasUnlimited); attrs.setIsUnlimitedVending(hasVending); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/config/OreDictRegistry.java b/src/com/jaquadro/minecraft/storagedrawers/config/OreDictRegistry.java index d524a5aa2..05bfba3d7 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/config/OreDictRegistry.java +++ b/src/com/jaquadro/minecraft/storagedrawers/config/OreDictRegistry.java @@ -5,6 +5,7 @@ import net.minecraft.item.ItemStack; import net.minecraftforge.oredict.OreDictionary; +import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.HashSet; import java.util.List; diff --git a/src/com/jaquadro/minecraft/storagedrawers/core/ModItems.java b/src/com/jaquadro/minecraft/storagedrawers/core/ModItems.java index 52bd81542..361c63b7b 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/core/ModItems.java +++ b/src/com/jaquadro/minecraft/storagedrawers/core/ModItems.java @@ -27,6 +27,8 @@ public class ModItems public static ItemDrawerKey drawerKey; @ObjectHolder(StorageDrawers.MOD_ID + ":upgrade_void") public static ItemUpgrade upgradeVoid; + @ObjectHolder(StorageDrawers.MOD_ID + ":upgrade_conversion") + public static ItemUpgrade upgradeConversion; @ObjectHolder(StorageDrawers.MOD_ID + ":upgrade_creative") public static ItemUpgradeCreative upgradeCreative; @ObjectHolder(StorageDrawers.MOD_ID + ":upgrade_redstone") @@ -59,6 +61,8 @@ public static void registerItems (RegistryEvent.Register event) { itemRegistry.register(new ItemUpgradeStatus("upgrade_status", makeName("upgradeStatus"))); if (StorageDrawers.config.cache.enableVoidUpgrades) itemRegistry.register(new ItemUpgrade("upgrade_void", makeName("upgradeVoid"))); + if (StorageDrawers.config.cache.enableItemConversion) + itemRegistry.register(new ItemUpgrade("upgrade_conversion", makeName("upgradeConversion"))); if (StorageDrawers.config.cache.enableCreativeUpgrades) itemRegistry.register(new ItemUpgradeCreative("upgrade_creative", makeName("upgradeCreative"))); if (StorageDrawers.config.cache.enableRedstoneUpgrades) @@ -81,6 +85,7 @@ public static void registerModels (ModelRegistryEvent event) { modelRegistry.registerItemVariants(upgradeTemplate); modelRegistry.registerItemVariants(upgradeVoid); + modelRegistry.registerItemVariants(upgradeConversion); modelRegistry.registerItemVariants(tape); modelRegistry.registerItemVariants(drawerKey); modelRegistry.registerItemVariants(shroudKey); diff --git a/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/BasicDrawerAttributes.java b/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/BasicDrawerAttributes.java index e440906db..6f471289b 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/BasicDrawerAttributes.java +++ b/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/BasicDrawerAttributes.java @@ -16,6 +16,7 @@ public class BasicDrawerAttributes implements IDrawerAttributes, IDrawerAttribut private boolean isVoid; private boolean isUnlimitedStorage; private boolean isUnlimitedVending; + private boolean isConversion; @Override public boolean canItemLock (LockAttribute attr) { @@ -116,6 +117,21 @@ public boolean setIsUnlimitedVending (boolean state) { return true; } + @Override + public boolean isDictConvertible () { + return isConversion; + } + + @Override + public boolean setIsDictConvertible (boolean state) { + if (isConversion != state) { + isConversion = state; + onAttributeChanged(); + } + + return true; + } + @Override public NBTTagCompound serializeNBT () { NBTTagCompound tag = new NBTTagCompound(); @@ -126,6 +142,7 @@ public NBTTagCompound serializeNBT () { tag.setBoolean("quant", isShowingQuantity); tag.setBoolean("unlimited", isUnlimitedStorage); tag.setBoolean("vending", isUnlimitedVending); + tag.setBoolean("conv", isConversion); return tag; } @@ -138,6 +155,7 @@ public void deserializeNBT (NBTTagCompound nbt) { isShowingQuantity = nbt.getBoolean("quant"); isUnlimitedStorage = nbt.getBoolean("unlimited"); isUnlimitedVending = nbt.getBoolean("vending"); + isConversion = nbt.getBoolean("conv"); } protected void onAttributeChanged () { } diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/ContainerDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/ContainerDrawers.java index 47b53d5fb..afab2c0d2 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/ContainerDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/ContainerDrawers.java @@ -6,10 +6,8 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; -import net.minecraft.inventory.IContainerListener; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -24,7 +22,7 @@ public class ContainerDrawers extends Container private static final int InventoryY = 117; private static final int HotbarY = 175; - private static final int UpgradeX = 44; + private static final int UpgradeX = 26; private static final int UpgradeY = 86; private IInventory upgradeInventory; @@ -47,7 +45,7 @@ public ContainerDrawers (InventoryPlayer playerInventory, TileEntityDrawers tile storageSlots.add(addSlotToContainer(new SlotDrawer(this, tileEntity.getGroup(), i, getStorageSlotX(i), getStorageSlotY(i)))); upgradeSlots = new ArrayList<>(); - for (int i = 0; i < 5; i++) + for (int i = 0; i < 7; i++) upgradeSlots.add(addSlotToContainer(new SlotUpgrade(upgradeInventory, i, UpgradeX + i * 18, UpgradeY))); playerSlots = new ArrayList<>(); diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java deleted file mode 100644 index 7af25f349..000000000 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/DrawerData.java +++ /dev/null @@ -1,283 +0,0 @@ -/*package com.jaquadro.minecraft.storagedrawers.storage; - -import com.jaquadro.minecraft.storagedrawers.api.storage.EmptyDrawerAttributes; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; -import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*; -import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.StandardDrawerGroup; -import com.jaquadro.minecraft.storagedrawers.inventory.ItemStackHelper; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraftforge.common.capabilities.Capability; -import net.minecraftforge.common.capabilities.CapabilityInject; -import net.minecraftforge.common.capabilities.ICapabilityProvider; - -import javax.annotation.Nonnull; - -public class DrawerData extends BaseDrawerData -{ - @CapabilityInject(IDrawerAttributes.class) - static Capability ATTR_CAPABILITY = null; - - IDrawerAttributes attrs; - StandardDrawerGroup group; - - @Nonnull - private ItemStack protoStack; - private int count; - - - public DrawerData (StandardDrawerGroup group) { - this.group = group; - attrs = new EmptyDrawerAttributes(); - protoStack = ItemStack.EMPTY; - } - - public void setCapabilityProvider (ICapabilityProvider capProvider) { - IDrawerAttributes capAttrs = capProvider.getCapability(ATTR_CAPABILITY, null); - if (capAttrs != null) - attrs = capAttrs; - } - - @Override - @Nonnull - public ItemStack getStoredItemPrototype () { - return protoStack; - } - - @Override - @Nonnull - public IDrawer setStoredItem (@Nonnull ItemStack itemPrototype) { - return setStoredItem(itemPrototype, true); - } - - protected IDrawer setStoredItem (@Nonnull ItemStack itemPrototype, boolean notify) { - if (areItemsEqual(itemPrototype)) { - return this; - } - - itemPrototype = ItemStackHelper.getItemPrototype(itemPrototype); - if (itemPrototype.isEmpty()) { - reset(notify); - return this; - } - - protoStack = itemPrototype; - protoStack.setCount(1); - count = 0; - - // TODO: Oredict blah blah - // refreshOreDictMatches(); - - group.syncSlots(); - if (notify) - onItemChanged(); - - return this; - } - - protected IDrawer setStoredItemRaw (@Nonnull ItemStack itemPrototype) { - itemPrototype = ItemStackHelper.getItemPrototype(itemPrototype); - protoStack = itemPrototype; - protoStack.setCount(1); - count = 0; - - return this; - } - - @Override - public int getStoredItemCount () { - if (protoStack.isEmpty()) - return 0; - - if (attrs.isUnlimitedVending()) - return Integer.MAX_VALUE; - - return count; - } - - @Override - public void setStoredItemCount (int amount) { - setStoredItemCount(amount, true); - } - - protected void setStoredItemCount (int amount, boolean notify) { - if (protoStack.isEmpty() || count == amount) - return; - - if (attrs.isUnlimitedVending()) - return; - - count = Math.min(amount, getMaxCapacity()); - count = Math.max(count, 0); - - if (amount == 0 && !attrs.isItemLocked(LockAttribute.LOCK_POPULATED)) - reset(notify); - else { - if (notify) - onAmountChanged(); - } - } - - protected void setStoredItemCountRaw (int amount) { - count = amount; - } - - @Override - public int adjustStoredItemCount (int amount) { - return adjustStoredItemCount(amount, true); - } - - protected int adjustStoredItemCount (int amount, boolean notify) { - if (protoStack.isEmpty() || amount == 0) - return amount; - - if (amount > 0) { - if (attrs.isUnlimitedVending()) - return 0; - - int originalCount = count; - count = Math.min(count + amount, getMaxCapacity()); - - if (count != originalCount && notify) - onAmountChanged(); - - if (attrs.isVoid()) - return 0; - - return amount - (count - originalCount); - } - else { - int originalCount = count; - setStoredItemCount(originalCount + amount, notify); - - return amount - (count - originalCount); - } - } - - @Override - public int getMaxCapacity (@Nonnull ItemStack itemPrototype) { - if (attrs.isUnlimitedStorage() || attrs.isUnlimitedVending()) - return Integer.MAX_VALUE; - - if (itemPrototype.isEmpty()) - return 64 * getStackCapacity(); - - return itemPrototype.getItem().getItemStackLimit(itemPrototype) * getStackCapacity(); - } - - @Override - public int getRemainingCapacity () { - if (protoStack.isEmpty()) - return 0; - - if (attrs.isUnlimitedVending()) - return Integer.MAX_VALUE; - - return getMaxCapacity() - getStoredItemCount(); - } - - @Override - public int getAcceptingRemainingCapacity () { - if (protoStack.isEmpty()) - return 0; - - if (attrs.isUnlimitedVending() || attrs.isVoid()) - return Integer.MAX_VALUE; - - return getMaxCapacity() - getStoredItemCount(); - } - - @Override - public boolean canItemBeStored (@Nonnull ItemStack itemPrototype) { - if (protoStack.isEmpty() && !attrs.isItemLocked(LockAttribute.LOCK_EMPTY)) - return true; - - return areItemsEqual(itemPrototype); - } - - @Override - public boolean canItemBeExtracted (@Nonnull ItemStack itemPrototype) { - if (protoStack.isEmpty()) - return false; - - return areItemsEqual(itemPrototype); - } - - @Override - public boolean isEmpty () { - return protoStack.isEmpty(); - } - - @Override - protected void reset (boolean notify) { - protoStack = ItemStack.EMPTY; - count = 0; - - super.reset(notify); - - group.syncSlots(); - if (notify) - onItemChanged(); - } - - @Override - public NBTTagCompound serializeNBT () { - NBTTagCompound tag = new NBTTagCompound(); - if (protoStack.isEmpty()) - return tag; - - NBTTagCompound item = new NBTTagCompound(); - protoStack.writeToNBT(item); - - tag.setTag("Item", item); - tag.setInteger("Count", count); - - return tag; - } - - @Override - public void deserializeNBT (NBTTagCompound nbt) { - ItemStack tagItem = ItemStack.EMPTY; - int tagCount = 0; - - if (nbt.hasKey("Item")) - tagItem = new ItemStack(nbt.getCompoundTag("Item")); - if (nbt.hasKey("Count")) - tagCount = nbt.getInteger("Count"); - - setStoredItemRaw(tagItem); - setStoredItemCountRaw(tagCount); - } - - public void deserializeLegacyNBT (NBTTagCompound nbt) { - ItemStack tagItem = ItemStack.EMPTY; - int tagCount = 0; - - if (nbt.hasKey("Count")) - tagCount = nbt.getInteger("Count"); - if (nbt.hasKey("Item")) { - Item item = Item.getItemById(nbt.getShort("Item")); - if (item != null) { - tagItem = new ItemStack(item); - tagItem.setItemDamage(nbt.getShort("Meta")); - if (nbt.hasKey("Tags")) - tagItem.setTagCompound(nbt.getCompoundTag("Tags")); - } - } - - setStoredItemRaw(tagItem); - setStoredItemCountRaw(tagCount); - } - - protected int getStackCapacity() { - return 0; - } - - protected void onItemChanged() { } - - protected void onAmountChanged() { } -} - -*/ \ No newline at end of file diff --git a/src/com/jaquadro/minecraft/storagedrawers/util/CompactingHelper.java b/src/com/jaquadro/minecraft/storagedrawers/util/CompactingHelper.java index 11af60eb3..250730aad 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/util/CompactingHelper.java +++ b/src/com/jaquadro/minecraft/storagedrawers/util/CompactingHelper.java @@ -2,7 +2,6 @@ import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.config.CompTierRegistry; -import com.jaquadro.minecraft.storagedrawers.storage.BaseDrawerData; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.CraftingManager; @@ -80,7 +79,7 @@ public Result findHigherTier (@Nonnull ItemStack stack) { if (comp.getCount() != lookupSize) continue; - if (!BaseDrawerData.areItemsEqual(comp, stack, false)) + if (!ItemStackOreMatcher.areItemsEqual(comp, stack, false)) continue; candidates.add(match); @@ -123,7 +122,7 @@ public Result findLowerTier (@Nonnull ItemStack stack) { for (IRecipe recipe : CraftingManager.REGISTRY) { ItemStack output = recipe.getRecipeOutput(); - if (!BaseDrawerData.areItemsEqual(stack, output, true)) + if (!ItemStackOreMatcher.areItemsEqual(stack, output, true)) continue; @Nonnull ItemStack match = tryMatch(stack, recipe.getIngredients()); @@ -132,7 +131,7 @@ public Result findLowerTier (@Nonnull ItemStack stack) { List compMatches = findAllMatchingRecipes(lookup1); for (ItemStack comp : compMatches) { int recipeSize = recipe.getIngredients().size(); - if (BaseDrawerData.areItemsEqual(match, comp, true) && comp.getCount() == recipeSize) { + if (ItemStackOreMatcher.areItemsEqual(match, comp, true) && comp.getCount() == recipeSize) { candidates.add(match); candidatesRate.put(match, recipeSize); diff --git a/src/com/jaquadro/minecraft/storagedrawers/util/ItemStackMatcher.java b/src/com/jaquadro/minecraft/storagedrawers/util/ItemStackMatcher.java new file mode 100644 index 000000000..6d71b69fe --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/util/ItemStackMatcher.java @@ -0,0 +1,28 @@ +package com.jaquadro.minecraft.storagedrawers.util; + +import net.minecraft.item.ItemStack; + +import javax.annotation.Nonnull; + +public class ItemStackMatcher +{ + public static ItemStackMatcher EMPTY = new ItemStackMatcher(ItemStack.EMPTY); + + @Nonnull + protected ItemStack stack; + + public ItemStackMatcher (@Nonnull ItemStack stack) { + this.stack = stack; + } + + public boolean matches (@Nonnull ItemStack stack) { + return areItemsEqual(this.stack, stack); + } + + public static boolean areItemsEqual (@Nonnull ItemStack stack1, @Nonnull ItemStack stack2) { + if (!stack1.isItemEqual(stack2)) + return false; + + return ItemStack.areItemStackTagsEqual(stack1, stack2); + } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/util/ItemStackOreMatcher.java similarity index 62% rename from src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java rename to src/com/jaquadro/minecraft/storagedrawers/util/ItemStackOreMatcher.java index c1040e540..bfce0e279 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/util/ItemStackOreMatcher.java @@ -1,39 +1,52 @@ -package com.jaquadro.minecraft.storagedrawers.storage; +package com.jaquadro.minecraft.storagedrawers.util; import com.jaquadro.minecraft.storagedrawers.StorageDrawers; -import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.oredict.OreDictionary; import javax.annotation.Nonnull; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; -public abstract class BaseDrawerData implements IDrawer, INBTSerializable +public class ItemStackOreMatcher extends ItemStackMatcher { private List oreDictMatches; - //private Map auxData; - - protected BaseDrawerData () { + public ItemStackOreMatcher (@Nonnull ItemStack stack) { + super(stack); + refreshOreDictMatches(); } - protected void reset (boolean notify) { - oreDictMatches = null; + @Override + public boolean matches (@Nonnull ItemStack stack) { + if (!this.stack.isItemEqual(stack)) { + if (oreDictMatches == null) + return false; + if (this.stack.getItem() == stack.getItem()) + return false; + + boolean oreMatch = false; + for (ItemStack oreDictMatch : oreDictMatches) { + if (stack.isItemEqual(oreDictMatch)) { + oreMatch = true; + break; + } + } + + if (!oreMatch) + return false; + } + + return ItemStack.areItemStackTagsEqual(this.stack, stack); } - protected void refreshOreDictMatches () { - ItemStack protoStack = getStoredItemPrototype(); - if (protoStack.isEmpty()) { + public void refreshOreDictMatches () { + if (stack.isEmpty()) { oreDictMatches = null; return; } - int[] oreIDs = OreDictionary.getOreIDs(protoStack); + int[] oreIDs = OreDictionary.getOreIDs(stack); if (oreIDs.length == 0) oreDictMatches = null; else { @@ -56,58 +69,12 @@ protected void refreshOreDictMatches () { } } - /*@Override - public Object getExtendedData (String key) { - if (auxData == null || !auxData.containsKey(key)) - return null; - - return auxData.get(key); - } - - @Override - public void setExtendedData (String key, Object data) { - if (auxData == null) - auxData = new HashMap<>(); - - auxData.put(key, data); - } - - @Override - public void attributeChanged () { }*/ - - public boolean areItemsEqual (@Nonnull ItemStack item) { - ItemStack protoStack = getStoredItemPrototype(); - if (!protoStack.isEmpty() && !protoStack.isItemEqual(item)) { - if (!StorageDrawers.config.cache.enableItemConversion) - return false; - if (oreDictMatches == null) - return false; - if (protoStack.getItem() == item.getItem()) - return false; - - boolean oreMatch = false; - for (ItemStack oreDictMatche : oreDictMatches) { - if (item.isItemEqual(oreDictMatche)) { - oreMatch = true; - break; - } - } - - if (!oreMatch) - return false; - } - - return ItemStack.areItemStackTagsEqual(protoStack, item); - } - public static boolean areItemsEqual (@Nonnull ItemStack stack1, @Nonnull ItemStack stack2) { return areItemsEqual(stack1, stack2, true); } public static boolean areItemsEqual (@Nonnull ItemStack stack1, @Nonnull ItemStack stack2, boolean oreDictStrictMode) { if (!stack1.isEmpty() && !stack2.isEmpty() && !stack1.isItemEqual(stack2)) { - if (!StorageDrawers.config.cache.enableItemConversion) - return false; if (stack1.getItemDamage() == OreDictionary.WILDCARD_VALUE || stack2.getItemDamage() == OreDictionary.WILDCARD_VALUE) return false; if (stack1.getItem() == stack2.getItem()) From 05a017498ebbb1f3f2fee17f629aadca72aadb20 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Wed, 5 Jul 2017 02:55:51 -0400 Subject: [PATCH 20/22] Making the multiplirs more generous --- .../minecraft/storagedrawers/config/ConfigManager.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/com/jaquadro/minecraft/storagedrawers/config/ConfigManager.java b/src/com/jaquadro/minecraft/storagedrawers/config/ConfigManager.java index afa23099c..a8b9f4173 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/config/ConfigManager.java +++ b/src/com/jaquadro/minecraft/storagedrawers/config/ConfigManager.java @@ -217,10 +217,10 @@ public void syncConfig () { config.get(sectionBlocksSlave.getQualifiedName(), "enabled", true).setLanguageKey(LANG_PREFIX + "prop.enabled").setRequiresMcRestart(true); cache.level2Mult = config.get(sectionUpgrades.getQualifiedName(), "level2Mult", 2).setLanguageKey(LANG_PREFIX + "upgrades.level2Mult").setRequiresWorldRestart(true).getInt(); - cache.level3Mult = config.get(sectionUpgrades.getQualifiedName(), "level3Mult", 3).setLanguageKey(LANG_PREFIX + "upgrades.level3Mult").setRequiresWorldRestart(true).getInt(); - cache.level4Mult = config.get(sectionUpgrades.getQualifiedName(), "level4Mult", 5).setLanguageKey(LANG_PREFIX + "upgrades.level4Mult").setRequiresWorldRestart(true).getInt(); - cache.level5Mult = config.get(sectionUpgrades.getQualifiedName(), "level5Mult", 8).setLanguageKey(LANG_PREFIX + "upgrades.level5Mult").setRequiresWorldRestart(true).getInt(); - cache.level6Mult = config.get(sectionUpgrades.getQualifiedName(), "level6Mult", 13).setLanguageKey(LANG_PREFIX + "upgrades.level6Mult").setRequiresWorldRestart(true).getInt(); + cache.level3Mult = config.get(sectionUpgrades.getQualifiedName(), "level3Mult", 4).setLanguageKey(LANG_PREFIX + "upgrades.level3Mult").setRequiresWorldRestart(true).getInt(); + cache.level4Mult = config.get(sectionUpgrades.getQualifiedName(), "level4Mult", 8).setLanguageKey(LANG_PREFIX + "upgrades.level4Mult").setRequiresWorldRestart(true).getInt(); + cache.level5Mult = config.get(sectionUpgrades.getQualifiedName(), "level5Mult", 16).setLanguageKey(LANG_PREFIX + "upgrades.level5Mult").setRequiresWorldRestart(true).getInt(); + cache.level6Mult = config.get(sectionUpgrades.getQualifiedName(), "level6Mult", 32).setLanguageKey(LANG_PREFIX + "upgrades.level6Mult").setRequiresWorldRestart(true).getInt(); getControllerRange(); From f907f50b90cfc747fa973acedcd83ff472221722 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Wed, 5 Jul 2017 12:57:05 -0400 Subject: [PATCH 21/22] A couple API changes to try and maintain compatibility with existing RS instances --- .../storagedrawers/api/storage/IDrawer.java | 16 +++++++++++++++- .../api/storage/attribute/IVoidable.java | 8 ++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IVoidable.java diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java index e9bdc4962..05cb454b3 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawer.java @@ -14,7 +14,7 @@ public interface IDrawer ItemStack getStoredItemPrototype (); /** - * Sets the type of the stored item and initializes it to the given amount. Any existing item will be replaced. + * Sets the type of the stored item and initializes it to 0. Any existing item will be replaced. * * @param itemPrototype An ItemStack representing the type, metadata, and tags of the item to store. * @return The IDrawer actually set with the prototype. Some drawer groups can redirect a set operation to another member. @@ -22,6 +22,20 @@ public interface IDrawer @Nonnull IDrawer setStoredItem (@Nonnull ItemStack itemPrototype); + /** + * 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 of items stored in this drawer. + * @return The IDrawer actually set with the prototype. Some drawer groups can redirect a set operation to another member. + */ + @Nonnull + default IDrawer setStoredItem (@Nonnull ItemStack itemPrototype, int amount) { + IDrawer drawer = setStoredItem(itemPrototype); + drawer.setStoredItemCount(amount); + return drawer; + } + /** * Gets the number of items stored in this drawer. */ diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IVoidable.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IVoidable.java new file mode 100644 index 000000000..eca49de96 --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IVoidable.java @@ -0,0 +1,8 @@ +package com.jaquadro.minecraft.storagedrawers.api.storage.attribute; + +@Deprecated +public interface IVoidable +{ + @Deprecated + boolean isVoid (); +} From ad8aa3a9b50378feb34e5af6d184a2738aefc395 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Wed, 5 Jul 2017 19:57:44 -0400 Subject: [PATCH 22/22] More refactoring --- .../storagedrawers/StorageDrawers.java | 4 +- .../api/capabilities/IItemRepository.java | 27 ++++++++ .../api/storage/IDrawerGroupInteractive.java | 18 ------ .../block/tile/TileEntityController.java | 49 +++++++++++++-- .../block/tile/TileEntityDrawers.java | 41 ++++++++++--- .../block/tile/TileEntityDrawersStandard.java | 2 - .../block/tile/TileEntitySlave.java | 61 +++++++++++++++++-- .../tile/tiledata/StandardDrawerGroup.java | 2 - .../capabilities/BasicDrawerAttributes.java | 2 +- .../CapabilityDrawerAttributes.java | 2 +- .../capabilities/CapabilityDrawerGroup.java | 59 ++++++++++++++++++ .../CapabilityItemRepository.java | 7 +++ 12 files changed, 229 insertions(+), 45 deletions(-) delete mode 100644 src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroupInteractive.java rename src/com/jaquadro/minecraft/storagedrawers/{core => }/capabilities/BasicDrawerAttributes.java (98%) rename src/com/jaquadro/minecraft/storagedrawers/{core => }/capabilities/CapabilityDrawerAttributes.java (98%) create mode 100644 src/com/jaquadro/minecraft/storagedrawers/capabilities/CapabilityDrawerGroup.java diff --git a/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java index e43b89216..172326819 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java @@ -1,9 +1,10 @@ package com.jaquadro.minecraft.storagedrawers; +import com.jaquadro.minecraft.storagedrawers.capabilities.CapabilityDrawerGroup; import com.jaquadro.minecraft.storagedrawers.capabilities.CapabilityItemRepository; import com.jaquadro.minecraft.storagedrawers.config.*; import com.jaquadro.minecraft.storagedrawers.core.*; -import com.jaquadro.minecraft.storagedrawers.core.capabilities.CapabilityDrawerAttributes; +import com.jaquadro.minecraft.storagedrawers.capabilities.CapabilityDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.core.handlers.GuiHandler; import com.jaquadro.minecraft.storagedrawers.integration.LocalIntegrationRegistry; import com.jaquadro.minecraft.storagedrawers.network.BoolConfigUpdateMessage; @@ -61,6 +62,7 @@ public void preInit (FMLPreInitializationEvent event) { log = event.getModLog(); config = new ConfigManager(new File(event.getModConfigurationDirectory(), MOD_ID + ".cfg")); + CapabilityDrawerGroup.register(); CapabilityItemRepository.register(); CapabilityDrawerAttributes.register(); diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/capabilities/IItemRepository.java b/src/com/jaquadro/minecraft/storagedrawers/api/capabilities/IItemRepository.java index afc564abf..38d5cfaa4 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/api/capabilities/IItemRepository.java +++ b/src/com/jaquadro/minecraft/storagedrawers/api/capabilities/IItemRepository.java @@ -1,11 +1,20 @@ package com.jaquadro.minecraft.storagedrawers.api.capabilities; import net.minecraft.item.ItemStack; +import net.minecraft.util.NonNullList; import javax.annotation.Nonnull; public interface IItemRepository { + /** + * Gets a list of all items in the inventory. The same item may appear multiple times with varying counts. + * + * @return A list of zero or more items in the inventory. + */ + @Nonnull + NonNullList getAllItems (); + /** * Inserts an ItemStack into the inventory and returns the remainder. * @@ -29,4 +38,22 @@ public interface IItemRepository */ @Nonnull ItemStack extractItem (@Nonnull ItemStack stack, int amount, boolean simulate); + + /** + * An item record representing an item and the amount stored. + * + * The ItemStack held by itemPrototype always reports a stack size of 1. + * IT IS IMPORTANT THAT YOU NEVER MODIFY itemPrototype. + */ + class ItemRecord + { + @Nonnull + public final ItemStack itemPrototype; + public final int count; + + public ItemRecord (@Nonnull ItemStack itemPrototype, int count) { + this.itemPrototype = itemPrototype; + this.count = count; + } + } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroupInteractive.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroupInteractive.java deleted file mode 100644 index 362b11b2b..000000000 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroupInteractive.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.jaquadro.minecraft.storagedrawers.api.storage; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; - -import javax.annotation.Nonnull; - -public interface IDrawerGroupInteractive extends IDrawerGroup -{ - @Nonnull - ItemStack takeItemsFromSlot (int slot, int count); - - int putItemsIntoSlot (int slot, @Nonnull ItemStack stack, int count); - - int interactPutCurrentItemIntoSlot (int slot, EntityPlayer player); - - int interactPutCurrentInventoryIntoSlot (int slot, EntityPlayer player); -} diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java index e57d15818..d38403f6d 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java @@ -16,12 +16,14 @@ import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.NetworkManager; import net.minecraft.network.play.server.SPacketUpdateTileEntity; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; +import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.common.capabilities.Capability; @@ -480,8 +482,20 @@ else if (te instanceof TileEntityDrawers) { drawerSlotList.add(new SlotRecord(group, coord, i)); } else { + IDrawerGroup group = te.getCapability(DRAWER_GROUP_CAPABILITY, null); + if (record.storage == group) + return; + if (record.storage != null) clearRecordInfo(coord, record); + if (group == null) + return; + + record.storage = group; + record.drawerStorageSize = group.getDrawerCount(); + + for (int i = 0, n = record.drawerStorageSize; i < n; i++) + drawerSlotList.add(new SlotRecord(group, coord, i)); } } @@ -626,22 +640,26 @@ public int[] getAccessibleDrawerSlots () { return drawerSlots; } + public IItemRepository getItemRepository () { + return itemRepository; + } + @CapabilityInject(IItemHandler.class) static Capability ITEM_HANDLER_CAPABILITY = null; @CapabilityInject(IItemRepository.class) static Capability ITEM_REPOSITORY_CAPABILITY = null; + @CapabilityInject(IDrawerGroup.class) + static Capability DRAWER_GROUP_CAPABILITY = null; private DrawerItemHandler itemHandler = new DrawerItemHandler(this); private ItemRepository itemRepository = new ItemRepository(); @Override public boolean hasCapability (@Nonnull Capability capability, @Nullable EnumFacing facing) { - if (capability == ITEM_HANDLER_CAPABILITY) - return true; - if (capability == ITEM_REPOSITORY_CAPABILITY) - return true; - - return super.hasCapability(capability, facing); + return capability == ITEM_HANDLER_CAPABILITY + || capability == ITEM_REPOSITORY_CAPABILITY + || capability == DRAWER_GROUP_CAPABILITY + || super.hasCapability(capability, facing); } @Override @@ -651,12 +669,31 @@ public T getCapability (@Nonnull Capability capability, @Nullable EnumFac return (T) itemHandler; if (capability == ITEM_REPOSITORY_CAPABILITY) return (T) itemRepository; + if (capability == DRAWER_GROUP_CAPABILITY) + return (T) this; return super.getCapability(capability, facing); } private class ItemRepository implements IItemRepository { + @Nonnull + @Override + public NonNullList getAllItems () { + NonNullList records = NonNullList.create(); + + for (int slot : drawerSlots) { + IDrawer drawer = getDrawer(slot); + if (drawer.isEmpty()) + continue; + + ItemStack stack = drawer.getStoredItemPrototype(); + records.add(new ItemRecord(stack, drawer.getStoredItemCount())); + } + + return records; + } + @Nonnull @Override public ItemStack insertItem (@Nonnull ItemStack stack, boolean simulate) { diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index 5aa1e8026..4ff8f84ec 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -4,6 +4,7 @@ import com.jaquadro.minecraft.chameleon.block.tiledata.CustomNameData; import com.jaquadro.minecraft.chameleon.block.tiledata.LockableData; import com.jaquadro.minecraft.storagedrawers.StorageDrawers; +import com.jaquadro.minecraft.storagedrawers.api.capabilities.IItemRepository; import com.jaquadro.minecraft.storagedrawers.api.security.ISecurityProvider; import com.jaquadro.minecraft.storagedrawers.api.storage.*; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*; @@ -11,8 +12,7 @@ import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.MaterialData; import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.UpgradeData; import com.jaquadro.minecraft.storagedrawers.core.ModItems; -import com.jaquadro.minecraft.storagedrawers.core.capabilities.BasicDrawerAttributes; -import com.jaquadro.minecraft.storagedrawers.core.capabilities.CapabilityDrawerAttributes; +import com.jaquadro.minecraft.storagedrawers.capabilities.BasicDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.inventory.DrawerItemHandler; import com.jaquadro.minecraft.storagedrawers.item.EnumUpgradeRedstone; import com.jaquadro.minecraft.storagedrawers.item.EnumUpgradeStorage; @@ -22,15 +22,13 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.world.ILockableContainer; import net.minecraft.world.World; import net.minecraftforge.common.capabilities.Capability; -import net.minecraftforge.common.util.Constants; -import net.minecraftforge.common.util.INBTSerializable; +import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.fml.common.network.NetworkRegistry.TargetPoint; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -41,7 +39,7 @@ import java.util.EnumSet; import java.util.UUID; -public abstract class TileEntityDrawers extends ChamLockableTileEntity implements ISealable, IProtectable +public abstract class TileEntityDrawers extends ChamLockableTileEntity implements ISealable, IProtectable, IDrawerGroup { private LockableData lockData = new LockableData(); private CustomNameData customNameData = new CustomNameData("storagedrawers.container.drawers"); @@ -596,15 +594,35 @@ public boolean shouldRefresh (World world, BlockPos pos, IBlockState oldState, I return oldState.getBlock() != newSate.getBlock(); } + @Override + @Deprecated public int getDrawerCount () { return getGroup().getDrawerCount(); } @Nonnull + @Override + @Deprecated public IDrawer getDrawer (int slot) { return getGroup().getDrawer(slot); } + @Nonnull + @Override + @Deprecated + public int[] getAccessibleDrawerSlots () { + return getGroup().getAccessibleDrawerSlots(); + } + + @CapabilityInject(IDrawerGroup.class) + public static Capability DRAWER_GROUP_CAPABILITY = null; + @CapabilityInject(IItemRepository.class) + public static Capability ITEM_REPOSITORY_CAPABILITY = null; + @CapabilityInject(IDrawerAttributes.class) + static Capability DRAWER_ATTRIBUTES_CAPABILITY = null; + @CapabilityInject(IItemHandler.class) + static Capability ITEM_HANDLER_CAPABILITY = null; + private net.minecraftforge.items.IItemHandler itemHandler; protected IItemHandler createUnSidedHandler () { @@ -615,10 +633,12 @@ protected IItemHandler createUnSidedHandler () { @Override public T getCapability(@Nonnull Capability capability, @Nullable EnumFacing facing) { - if (capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) + if (capability == ITEM_HANDLER_CAPABILITY) return (T) (itemHandler == null ? (itemHandler = createUnSidedHandler()) : itemHandler); - if (capability == CapabilityDrawerAttributes.DRAWER_ATTRIBUTES_CAPABILITY) + if (capability == DRAWER_ATTRIBUTES_CAPABILITY) return (T) drawerAttributes; + if (capability == DRAWER_GROUP_CAPABILITY) + return (T) getGroup(); return super.getCapability(capability, facing); } @@ -626,8 +646,9 @@ public T getCapability(@Nonnull Capability capability, @Nullable EnumFaci @Override public boolean hasCapability(@Nonnull Capability capability, @Nullable EnumFacing facing) { - return capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY - || capability == CapabilityDrawerAttributes.DRAWER_ATTRIBUTES_CAPABILITY + return capability == ITEM_HANDLER_CAPABILITY + || capability == DRAWER_ATTRIBUTES_CAPABILITY + || capability == DRAWER_GROUP_CAPABILITY || super.hasCapability(capability, facing); } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java index 7329728a3..834b2ca65 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawersStandard.java @@ -210,8 +210,6 @@ public int getDrawerCapacity () { private class GroupData extends StandardDrawerGroup { - public GroupData () { } - public GroupData (int slotCount) { super(slotCount); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntitySlave.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntitySlave.java index 29f55bcc1..224b8e6b7 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntitySlave.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntitySlave.java @@ -1,17 +1,22 @@ package com.jaquadro.minecraft.storagedrawers.block.tile; import com.jaquadro.minecraft.chameleon.block.ChamTileEntity; +import com.jaquadro.minecraft.storagedrawers.api.capabilities.IItemRepository; import com.jaquadro.minecraft.storagedrawers.api.storage.Drawers; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.ControllerData; import com.jaquadro.minecraft.storagedrawers.inventory.DrawerItemHandler; import net.minecraft.block.state.IBlockState; +import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; +import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.items.CapabilityItemHandler; +import net.minecraftforge.items.IItemHandler; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -82,19 +87,67 @@ public void markDirty () { super.markDirty(); } + @CapabilityInject(IItemHandler.class) + static Capability ITEM_HANDLER_CAPABILITY = null; + @CapabilityInject(IItemRepository.class) + static Capability ITEM_REPOSITORY_CAPABILITY = null; + @CapabilityInject(IDrawerGroup.class) + static Capability DRAWER_GROUP_CAPABILITY = null; + private DrawerItemHandler itemHandler = new DrawerItemHandler(this); + private ItemRepositoryProxy itemRepository = new ItemRepositoryProxy(); @Override public boolean hasCapability (@Nonnull Capability capability, @Nullable EnumFacing facing) { - if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) - return true; - return super.hasCapability(capability, facing); + return capability == ITEM_HANDLER_CAPABILITY + || capability == ITEM_REPOSITORY_CAPABILITY + || capability == DRAWER_GROUP_CAPABILITY + || super.hasCapability(capability, facing); } @Override + @SuppressWarnings("unchecked") public T getCapability (@Nonnull Capability capability, @Nullable EnumFacing facing) { - if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) + if (capability == ITEM_HANDLER_CAPABILITY) return (T) itemHandler; + if (capability == ITEM_REPOSITORY_CAPABILITY) + return (T) itemRepository; + if (capability == DRAWER_GROUP_CAPABILITY) + return (T) this; + return super.getCapability(capability, facing); } + + private class ItemRepositoryProxy implements IItemRepository + { + @Nonnull + @Override + public NonNullList getAllItems () { + TileEntityController controller = getController(); + if (controller == null || !controller.isValidSlave(getPos())) + return NonNullList.create(); + + return controller.getItemRepository().getAllItems(); + } + + @Nonnull + @Override + public ItemStack insertItem (@Nonnull ItemStack stack, boolean simulate) { + TileEntityController controller = getController(); + if (controller == null || !controller.isValidSlave(getPos())) + return stack; + + return controller.getItemRepository().insertItem(stack, simulate); + } + + @Nonnull + @Override + public ItemStack extractItem (@Nonnull ItemStack stack, int amount, boolean simulate) { + TileEntityController controller = getController(); + if (controller == null || !controller.isValidSlave(getPos())) + return ItemStack.EMPTY; + + return controller.getItemRepository().extractItem(stack, amount, simulate); + } + } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java index 3dd1aa218..c44c6c6a0 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/tiledata/StandardDrawerGroup.java @@ -23,8 +23,6 @@ public abstract class StandardDrawerGroup extends TileDataShim implements IDrawe private DrawerData[] slots; private int[] order; - public StandardDrawerGroup () { } - public StandardDrawerGroup (int slotCount) { slots = new DrawerData[slotCount]; for (int i = 0; i < slotCount; i++) diff --git a/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/BasicDrawerAttributes.java b/src/com/jaquadro/minecraft/storagedrawers/capabilities/BasicDrawerAttributes.java similarity index 98% rename from src/com/jaquadro/minecraft/storagedrawers/core/capabilities/BasicDrawerAttributes.java rename to src/com/jaquadro/minecraft/storagedrawers/capabilities/BasicDrawerAttributes.java index 6f471289b..651d2bac3 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/BasicDrawerAttributes.java +++ b/src/com/jaquadro/minecraft/storagedrawers/capabilities/BasicDrawerAttributes.java @@ -1,4 +1,4 @@ -package com.jaquadro.minecraft.storagedrawers.core.capabilities; +package com.jaquadro.minecraft.storagedrawers.capabilities; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributesModifiable; diff --git a/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/CapabilityDrawerAttributes.java b/src/com/jaquadro/minecraft/storagedrawers/capabilities/CapabilityDrawerAttributes.java similarity index 98% rename from src/com/jaquadro/minecraft/storagedrawers/core/capabilities/CapabilityDrawerAttributes.java rename to src/com/jaquadro/minecraft/storagedrawers/capabilities/CapabilityDrawerAttributes.java index a3bc3e46a..6b2a791b0 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/core/capabilities/CapabilityDrawerAttributes.java +++ b/src/com/jaquadro/minecraft/storagedrawers/capabilities/CapabilityDrawerAttributes.java @@ -1,4 +1,4 @@ -package com.jaquadro.minecraft.storagedrawers.core.capabilities; +package com.jaquadro.minecraft.storagedrawers.capabilities; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributes; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerAttributesModifiable; diff --git a/src/com/jaquadro/minecraft/storagedrawers/capabilities/CapabilityDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/capabilities/CapabilityDrawerGroup.java new file mode 100644 index 000000000..4f3124fd6 --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/capabilities/CapabilityDrawerGroup.java @@ -0,0 +1,59 @@ +package com.jaquadro.minecraft.storagedrawers.capabilities; + +import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; +import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.StandardDrawerGroup; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.EnumFacing; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.CapabilityInject; +import net.minecraftforge.common.capabilities.CapabilityManager; +import net.minecraftforge.common.util.INBTSerializable; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class CapabilityDrawerGroup +{ + @CapabilityInject(IDrawerGroup.class) + public static Capability DRAWER_GROUP_CAPABILITY = null; + + public static void register () { + CapabilityManager.INSTANCE.register(IDrawerGroup.class, new DefaultStorage(), DefaultImplementation::new); + } + + private static class DefaultStorage implements Capability.IStorage + { + @Nullable + @Override + public NBTBase writeNBT (Capability capability, IDrawerGroup instance, EnumFacing side) { + if (instance instanceof INBTSerializable) + return ((INBTSerializable) instance).serializeNBT(); + + return new NBTTagCompound(); + } + + @Override + public void readNBT (Capability capability, IDrawerGroup instance, EnumFacing side, NBTBase nbt) { + if (instance instanceof INBTSerializable) { + @SuppressWarnings("unchecked") + INBTSerializable serializer = (INBTSerializable)instance; + serializer.deserializeNBT(nbt); + return; + } + } + } + + private static class DefaultImplementation extends StandardDrawerGroup + { + public DefaultImplementation () { + super(1); + } + + @Nonnull + @Override + protected DrawerData createDrawer (int slot) { + return new StandardDrawerGroup.DrawerData(this); + } + } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/capabilities/CapabilityItemRepository.java b/src/com/jaquadro/minecraft/storagedrawers/capabilities/CapabilityItemRepository.java index 82d35fe1f..905f6cd60 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/capabilities/CapabilityItemRepository.java +++ b/src/com/jaquadro/minecraft/storagedrawers/capabilities/CapabilityItemRepository.java @@ -5,6 +5,7 @@ import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumFacing; +import net.minecraft.util.NonNullList; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.common.capabilities.CapabilityManager; @@ -46,6 +47,12 @@ public void readNBT (Capability capability, IItemRepository ins private static class DefaultImplementation implements IItemRepository { + @Nonnull + @Override + public NonNullList getAllItems () { + return NonNullList.create(); + } + @Nonnull @Override public ItemStack insertItem (@Nonnull ItemStack stack, boolean simulate) {