From b74c2d127b62c04ed0faddfe9229f7bd2fff3e38 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Mon, 10 Nov 2014 01:06:16 -0500 Subject: [PATCH] Hoppers can insert items into drawers again. --- .../block/tile/TileEntityDrawers.java | 27 ++++++++--- .../inventory/ISideManager.java | 9 ++++ .../inventory/InventoryStack.java | 3 ++ .../inventory/StorageInventory.java | 46 +++++++++++++------ .../storage/DefaultStorageProvider.java | 3 ++ 5 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 src/com/jaquadro/minecraft/storagedrawers/inventory/ISideManager.java diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index 6b58dce..2db4ab8 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.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.api.inventory.IDrawerInventory; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; +import com.jaquadro.minecraft.storagedrawers.inventory.ISideManager; import com.jaquadro.minecraft.storagedrawers.inventory.StorageInventory; import com.jaquadro.minecraft.storagedrawers.network.CountUpdateMessage; import com.jaquadro.minecraft.storagedrawers.storage.DrawerData; @@ -30,6 +31,8 @@ public abstract class TileEntityDrawers extends TileEntity implements IDrawerGro private IDrawer[] drawers; private IDrawerInventory inventory; + private int[] autoSides = new int[] { 0, 1 }; + private int direction; private int drawerCapacity = 1; private int storageLevel = 1; @@ -44,12 +47,16 @@ public abstract class TileEntityDrawers extends TileEntity implements IDrawerGro protected abstract IStorageProvider getStorageProvider (); + protected ISideManager getSideManager () { + return new DefaultSideManager(); + } + protected void initWithDrawerCount (int drawerCount) { drawers = new IDrawer[drawerCount]; for (int i = 0; i < drawerCount; i++) drawers[i] = new DrawerData(getStorageProvider(), i); - inventory = new StorageInventory(this); + inventory = new StorageInventory(this, getSideManager()); } public int getDirection () { @@ -58,13 +65,13 @@ public abstract class TileEntityDrawers extends TileEntity implements IDrawerGro public void setDirection (int direction) { this.direction = direction % 6; - // TODO: Deal with sides - /*autoSides = new int[] { 0, 1, ForgeDirection.OPPOSITES[direction], 2, 3 }; + + autoSides = new int[] { 0, 1, ForgeDirection.OPPOSITES[direction], 2, 3 }; if (direction == 2 || direction == 3) { autoSides[3] = 4; autoSides[4] = 5; - }*/ + } } public int getStorageLevel () { @@ -191,7 +198,7 @@ public abstract class TileEntityDrawers extends TileEntity implements IDrawerGro drawers[i].readFromNBT(slot); } - inventory = new StorageInventory(this); + inventory = new StorageInventory(this, getSideManager()); } @Override @@ -222,7 +229,7 @@ public abstract class TileEntityDrawers extends TileEntity implements IDrawerGro @Override public void markDirty () { - + inventory.markDirty(); super.markDirty(); } @@ -348,4 +355,12 @@ public abstract class TileEntityDrawers extends TileEntity implements IDrawerGro public boolean isItemValidForSlot (int slot, ItemStack stack) { return inventory.isItemValidForSlot(slot, stack); } + + private class DefaultSideManager implements ISideManager + { + @Override + public int[] getSlotsForSide (int side) { + return autoSides; + } + } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/ISideManager.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/ISideManager.java new file mode 100644 index 0000000..8d8f2d9 --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/ISideManager.java @@ -0,0 +1,9 @@ +package com.jaquadro.minecraft.storagedrawers.inventory; + +/** + * Created by Justin on 11/9/2014. + */ +public interface ISideManager +{ + public int[] getSlotsForSide (int side); +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryStack.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryStack.java index b85ccb0..d9be05d 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryStack.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryStack.java @@ -51,6 +51,9 @@ public abstract class InventoryStack } public int getDiff () { + if (nativeStack == null) + return 0; + int diffNative = nativeStack.stackSize - nativeCount; int diffIn = inStack.stackSize - inCount; int diffOut = outStack.stackSize - outCount; diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/StorageInventory.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/StorageInventory.java index c905b98..c7610e4 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/StorageInventory.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/StorageInventory.java @@ -11,10 +11,13 @@ import net.minecraft.item.ItemStack; public class StorageInventory implements IDrawerInventory { private final IDrawerGroup group; + private final ISideManager sideMan; + private final int[] inventorySlots; - public StorageInventory (IDrawerGroup drawerGroup) { + public StorageInventory (IDrawerGroup drawerGroup, ISideManager sideManager) { group = drawerGroup; + sideMan = sideManager; inventorySlots = new int[group.getDrawerCount() * SlotType.values.length]; for (int i = 0, n = inventorySlots.length; i < n; i++) @@ -38,8 +41,7 @@ public class StorageInventory implements IDrawerInventory @Override public int[] getAccessibleSlotsFromSide (int side) { - // TODO - return new int[0]; + return sideMan.getSlotsForSide(side); } @Override @@ -143,6 +145,11 @@ public class StorageInventory implements IDrawerInventory int insertCount = (item != null) ? item.stackSize : 0; switch (getInventorySlotType(slot)) { case INPUT: + if (drawer.isEmpty()) { + setInventorySlotContents(drawer, item); + return; + } + ItemStack inStack = adapter.getInventoryStack(SlotType.INPUT); if (inStack != null) insertCount -= inStack.stackSize; @@ -153,16 +160,7 @@ public class StorageInventory implements IDrawerInventory insertCount -= outStack.stackSize; break; case STORAGE: - if (item == null) - drawer.setStoredItem(null, 0); - else { - insertCount = item.stackSize; - if (insertCount > drawer.getMaxCapacity()) - insertCount = drawer.getMaxCapacity(); - - drawer.setStoredItem(item, insertCount); - item.stackSize -= insertCount; - } + setInventorySlotContents(drawer, item); return; } @@ -174,6 +172,21 @@ public class StorageInventory implements IDrawerInventory } } + private void setInventorySlotContents (IDrawer drawer, ItemStack item) { + if (item == null) + drawer.setStoredItem(null, 0); + else { + drawer.setStoredItem(item, 0); + + int insertCount = item.stackSize; + if (insertCount > drawer.getMaxCapacity()) + insertCount = drawer.getMaxCapacity(); + + drawer.setStoredItemCount(insertCount); + item.stackSize -= insertCount; + } + } + @Override public String getInventoryName () { return null; @@ -191,7 +204,14 @@ public class StorageInventory implements IDrawerInventory @Override public void markDirty () { + for (int i = 0, n = group.getDrawerCount(); i < n; i++) { + if (!group.isDrawerEnabled(i)) + continue; + IDrawer drawer = group.getDrawer(i); + if (drawer instanceof IInventoryAdapter) + ((IInventoryAdapter) drawer).syncInventory(); + } } @Override diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/DefaultStorageProvider.java b/src/com/jaquadro/minecraft/storagedrawers/storage/DefaultStorageProvider.java index 59886cd..145f57a 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/DefaultStorageProvider.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/DefaultStorageProvider.java @@ -52,6 +52,9 @@ public class DefaultStorageProvider implements IStorageProvider @Override public void markDirty (int slot) { + if (tile.getWorldObj().isRemote) + return; + tile.getWorldObj().markBlockForUpdate(tile.xCoord, tile.yCoord, tile.zCoord); } }