From e1c6b18399e0c215dce77d5dc70b23676880dd43 Mon Sep 17 00:00:00 2001 From: AlgorithmX2 Date: Sat, 3 May 2014 19:21:33 -0500 Subject: [PATCH] Special Handlers are now properly wrapped in IMEMonitors for Storage Bus to function properly. --- .../registries/entries/ExternalIInv.java | 3 +- helpers/DualityInterface.java | 2 +- .../modules/helpers/FactorizationHandler.java | 4 +- .../modules/helpers/MFRDSUHandler.java | 5 +- .../modules/helpers/dead/GregTechHandler.java | 4 +- me/storage/MEMonitorIInventory.java | 19 +-- util/inv/AdaptorIInventory.java | 2 +- util/inv/IMEAdaptor.java | 147 ++++++++++++++++++ util/inv/IMEAdaptorIterator.java | 39 +++++ util/inv/ItemSlot.java | 29 +++- util/iterators/StackToSlotIterator.java | 2 +- 11 files changed, 234 insertions(+), 22 deletions(-) create mode 100644 util/inv/IMEAdaptor.java create mode 100644 util/inv/IMEAdaptorIterator.java diff --git a/core/features/registries/entries/ExternalIInv.java b/core/features/registries/entries/ExternalIInv.java index 9d971cc3..166d9309 100644 --- a/core/features/registries/entries/ExternalIInv.java +++ b/core/features/registries/entries/ExternalIInv.java @@ -8,6 +8,7 @@ import appeng.api.storage.IExternalStorageHandler; import appeng.api.storage.IMEInventory; import appeng.api.storage.StorageChannel; import appeng.me.storage.MEMonitorIInventory; +import appeng.util.InventoryAdaptor; public class ExternalIInv implements IExternalStorageHandler { @@ -22,7 +23,7 @@ public class ExternalIInv implements IExternalStorageHandler public IMEInventory getInventory(TileEntity te, ForgeDirection d, StorageChannel channel, BaseActionSource src) { if ( channel == StorageChannel.ITEMS && te instanceof IInventory ) - return new MEMonitorIInventory( (IInventory) te, d ); + return new MEMonitorIInventory( InventoryAdaptor.getAdaptor( (IInventory) te, d ) ); return null; } diff --git a/helpers/DualityInterface.java b/helpers/DualityInterface.java index 6c0cec91..8503de48 100644 --- a/helpers/DualityInterface.java +++ b/helpers/DualityInterface.java @@ -205,7 +205,7 @@ public class DualityInterface implements IGridTickable, ISegmentedInventory, ISt { public InterfaceInventory(DualityInterface tileInterface) { - super( tileInterface.storage, ForgeDirection.UP ); + super( InventoryAdaptor.getAdaptor( tileInterface.storage, ForgeDirection.UP ) ); mySource = new MachineSource( iHost ); } diff --git a/integration/modules/helpers/FactorizationHandler.java b/integration/modules/helpers/FactorizationHandler.java index de40fc4b..903a6e0c 100644 --- a/integration/modules/helpers/FactorizationHandler.java +++ b/integration/modules/helpers/FactorizationHandler.java @@ -7,6 +7,8 @@ import appeng.api.storage.IExternalStorageHandler; import appeng.api.storage.IMEInventory; import appeng.api.storage.StorageChannel; import appeng.integration.modules.FZ; +import appeng.me.storage.MEMonitorIInventory; +import appeng.util.inv.IMEAdaptor; public class FactorizationHandler implements IExternalStorageHandler { @@ -21,7 +23,7 @@ public class FactorizationHandler implements IExternalStorageHandler public IMEInventory getInventory(TileEntity te, ForgeDirection d, StorageChannel chan, BaseActionSource src) { if ( chan == StorageChannel.ITEMS ) - return FZ.instance.getFactorizationBarrel( te ); + return new MEMonitorIInventory( new IMEAdaptor( FZ.instance.getFactorizationBarrel( te ), src ) ); return null; } diff --git a/integration/modules/helpers/MFRDSUHandler.java b/integration/modules/helpers/MFRDSUHandler.java index 3c7a3141..11aa7bb0 100644 --- a/integration/modules/helpers/MFRDSUHandler.java +++ b/integration/modules/helpers/MFRDSUHandler.java @@ -7,6 +7,8 @@ import appeng.api.storage.IExternalStorageHandler; import appeng.api.storage.IMEInventory; import appeng.api.storage.StorageChannel; import appeng.integration.modules.DSU; +import appeng.me.storage.MEMonitorIInventory; +import appeng.util.inv.IMEAdaptor; public class MFRDSUHandler implements IExternalStorageHandler { @@ -21,8 +23,7 @@ public class MFRDSUHandler implements IExternalStorageHandler public IMEInventory getInventory(TileEntity te, ForgeDirection d, StorageChannel chan, BaseActionSource src) { if ( chan == StorageChannel.ITEMS ) - return DSU.instance.getDSU( te ); + return new MEMonitorIInventory( new IMEAdaptor( DSU.instance.getDSU( te ), src ) ); return null; } - } diff --git a/integration/modules/helpers/dead/GregTechHandler.java b/integration/modules/helpers/dead/GregTechHandler.java index d9e7dea4..1dab71fc 100644 --- a/integration/modules/helpers/dead/GregTechHandler.java +++ b/integration/modules/helpers/dead/GregTechHandler.java @@ -7,6 +7,8 @@ import appeng.api.storage.IExternalStorageHandler; import appeng.api.storage.IMEInventory; import appeng.api.storage.StorageChannel; import appeng.integration.modules.GT; +import appeng.me.storage.MEMonitorIInventory; +import appeng.util.inv.IMEAdaptor; public class GregTechHandler implements IExternalStorageHandler { @@ -21,7 +23,7 @@ public class GregTechHandler implements IExternalStorageHandler public IMEInventory getInventory(TileEntity te, ForgeDirection d, StorageChannel channel, BaseActionSource src) { if ( channel == StorageChannel.ITEMS ) - return GT.instance.getQuantumChest( te ); + return new MEMonitorIInventory( new IMEAdaptor( GT.instance.getQuantumChest( te ) ) ); return null; } diff --git a/me/storage/MEMonitorIInventory.java b/me/storage/MEMonitorIInventory.java index 87a6027f..fb9911c9 100644 --- a/me/storage/MEMonitorIInventory.java +++ b/me/storage/MEMonitorIInventory.java @@ -5,9 +5,7 @@ import java.util.Iterator; import java.util.Map.Entry; import java.util.TreeMap; -import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; -import net.minecraftforge.common.util.ForgeDirection; import appeng.api.AEApi; import appeng.api.config.AccessRestriction; import appeng.api.config.Actionable; @@ -22,7 +20,6 @@ import appeng.api.storage.data.IItemList; import appeng.util.InventoryAdaptor; import appeng.util.Platform; import appeng.util.inv.ItemSlot; -import appeng.util.item.AEItemStack; public class MEMonitorIInventory implements IMEInventory, IMEMonitor { @@ -47,8 +44,6 @@ public class MEMonitorIInventory implements IMEInventory, IMEMonit final IAEItemStack aeStack; }; - final IInventory internal; - final ForgeDirection side; final InventoryAdaptor adaptor; final TreeMap memory; @@ -69,11 +64,9 @@ public class MEMonitorIInventory implements IMEInventory, IMEMonit listeners.remove( l ); } - public MEMonitorIInventory(IInventory inv, ForgeDirection dir) { - adaptor = InventoryAdaptor.getAdaptor( inv, dir ); + public MEMonitorIInventory(InventoryAdaptor adaptor) { + this.adaptor = adaptor; memory = new TreeMap(); - internal = inv; - side = dir; } @Override @@ -135,7 +128,7 @@ public class MEMonitorIInventory implements IMEInventory, IMEMonit public IItemList getAvailableItems(IItemList out) { for (ItemSlot is : adaptor) - out.addStorage( AEItemStack.create( is.itemStack ) ); + out.addStorage( is.getAEItemStack() ); return out; } @@ -167,12 +160,12 @@ public class MEMonitorIInventory implements IMEInventory, IMEMonit for (ItemSlot is : adaptor) { CachedItemStack old = memory.get( is.slot ); - ItemStack newIS = is == null ? null : is.itemStack; + ItemStack newIS = is == null ? null : is.getItemStack(); ItemStack oldIS = old == null ? null : old.itemStack; if ( isDiffrent( newIS, oldIS ) ) { - CachedItemStack cis = new CachedItemStack( is.itemStack ); + CachedItemStack cis = new CachedItemStack( is.getItemStack() ); memory.put( is.slot, cis ); if ( old != null && old.aeStack != null ) @@ -203,7 +196,7 @@ public class MEMonitorIInventory implements IMEInventory, IMEMonit if ( diff != 0 && stack != null ) { - CachedItemStack cis = new CachedItemStack( is.itemStack ); + CachedItemStack cis = new CachedItemStack( is.getItemStack() ); memory.put( is.slot, cis ); IAEItemStack a = stack.copy(); diff --git a/util/inv/AdaptorIInventory.java b/util/inv/AdaptorIInventory.java index d8e48f25..90e5b162 100644 --- a/util/inv/AdaptorIInventory.java +++ b/util/inv/AdaptorIInventory.java @@ -342,7 +342,7 @@ public class AdaptorIInventory extends InventoryAdaptor public ItemSlot next() { is.slot = x; - is.itemStack = i.getStackInSlot( x++ ); + is.setItemStack( i.getStackInSlot( x++ ) ); return is; } diff --git a/util/inv/IMEAdaptor.java b/util/inv/IMEAdaptor.java new file mode 100644 index 00000000..27829634 --- /dev/null +++ b/util/inv/IMEAdaptor.java @@ -0,0 +1,147 @@ +package appeng.util.inv; + +import java.util.Iterator; + +import net.minecraft.item.ItemStack; +import appeng.api.AEApi; +import appeng.api.config.Actionable; +import appeng.api.config.FuzzyMode; +import appeng.api.networking.security.BaseActionSource; +import appeng.api.storage.IMEInventory; +import appeng.api.storage.data.IAEItemStack; +import appeng.api.storage.data.IItemList; +import appeng.util.InventoryAdaptor; +import appeng.util.item.AEItemStack; + +public class IMEAdaptor extends InventoryAdaptor +{ + + IMEInventory target; + BaseActionSource src; + + public IMEAdaptor(IMEInventory input, BaseActionSource src) { + target = input; + this.src = src; + } + + IItemList getList() + { + return target.getAvailableItems( AEApi.instance().storage().createItemList() ); + } + + @Override + public Iterator iterator() + { + return new IMEAdaptorIterator( getList() ); + } + + public ItemStack doRemoveItemsFuzzy(int how_many, ItemStack Filter, IInventoryDestination destination, Actionable type, FuzzyMode fuzzyMode) + { + IAEItemStack reqFilter = AEItemStack.create( Filter ); + if ( reqFilter == null ) + return null; + + IAEItemStack out = null; + + for (IAEItemStack req : getList().findFuzzy( reqFilter, fuzzyMode )) + { + if ( req != null ) + { + req.setStackSize( how_many ); + out = target.extractItems( req, type, src ); + if ( out != null ) + return out.getItemStack(); + } + } + + return null; + } + + public ItemStack doRemoveItems(int how_many, ItemStack Filter, IInventoryDestination destination, Actionable type) + { + IAEItemStack req = null; + + if ( Filter == null ) + { + IItemList list = getList(); + if ( !list.isEmpty() ) + req = list.getFirstItem(); + } + else + req = AEItemStack.create( Filter ); + + IAEItemStack out = null; + + if ( req != null ) + { + req.setStackSize( how_many ); + out = target.extractItems( req, type, src ); + } + + if ( out != null ) + return out.getItemStack(); + + return null; + } + + @Override + public ItemStack removeItems(int how_many, ItemStack Filter, IInventoryDestination destination) + { + return doRemoveItems( how_many, Filter, destination, Actionable.MODULATE ); + } + + @Override + public ItemStack simulateRemove(int how_many, ItemStack Filter, IInventoryDestination destination) + { + return doRemoveItems( how_many, Filter, destination, Actionable.SIMULATE ); + } + + @Override + public ItemStack removeSimilarItems(int how_many, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination destination) + { + if ( filter == null ) + return doRemoveItems( how_many, null, destination, Actionable.MODULATE ); + return doRemoveItemsFuzzy( how_many, filter, destination, Actionable.MODULATE, fuzzyMode ); + } + + @Override + public ItemStack simulateSimilarRemove(int how_many, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination destination) + { + if ( filter == null ) + return doRemoveItems( how_many, null, destination, Actionable.SIMULATE ); + return doRemoveItemsFuzzy( how_many, filter, destination, Actionable.SIMULATE, fuzzyMode ); + } + + @Override + public ItemStack addItems(ItemStack A) + { + IAEItemStack in = AEItemStack.create( A ); + if ( in != null ) + { + IAEItemStack out = target.injectItems( in, Actionable.MODULATE, src ); + if ( out != null ) + return out.getItemStack(); + } + return null; + } + + @Override + public ItemStack simulateAdd(ItemStack A) + { + IAEItemStack in = AEItemStack.create( A ); + if ( in != null ) + { + IAEItemStack out = target.injectItems( in, Actionable.SIMULATE, src ); + if ( out != null ) + return out.getItemStack(); + } + return null; + } + + @Override + public boolean containsItems() + { + return !getList().isEmpty(); + } + +} diff --git a/util/inv/IMEAdaptorIterator.java b/util/inv/IMEAdaptorIterator.java new file mode 100644 index 00000000..4f75defa --- /dev/null +++ b/util/inv/IMEAdaptorIterator.java @@ -0,0 +1,39 @@ +package appeng.util.inv; + +import java.util.Iterator; + +import appeng.api.storage.data.IAEItemStack; +import appeng.api.storage.data.IItemList; + +public class IMEAdaptorIterator implements Iterator +{ + + Iterator stack; + ItemSlot slot = new ItemSlot(); + int offset = 0; + + public IMEAdaptorIterator(IItemList availableItems) { + stack = availableItems.iterator(); + } + + @Override + public boolean hasNext() + { + return stack.hasNext(); + } + + @Override + public ItemSlot next() + { + IAEItemStack item = stack.next(); + slot.setAEItemStack( item ); + slot.slot = offset++; + return slot; + } + + @Override + public void remove() + { + throw new RuntimeException( "Not Implemented!" ); + } +} diff --git a/util/inv/ItemSlot.java b/util/inv/ItemSlot.java index fca95850..dd301edf 100644 --- a/util/inv/ItemSlot.java +++ b/util/inv/ItemSlot.java @@ -1,11 +1,38 @@ package appeng.util.inv; import net.minecraft.item.ItemStack; +import appeng.api.storage.data.IAEItemStack; +import appeng.util.item.AEItemStack; public class ItemSlot { public int slot; - public ItemStack itemStack; + + // one or the other.. + private IAEItemStack aeitemstack; + private ItemStack itemStack; + + public void setItemStack(ItemStack is) + { + aeitemstack = null; + itemStack = is; + } + + public void setAEItemStack(IAEItemStack is) + { + aeitemstack = is; + itemStack = null; + } + + public ItemStack getItemStack() + { + return itemStack == null ? (aeitemstack == null ? null : (itemStack = aeitemstack.getItemStack())) : itemStack; + } + + public IAEItemStack getAEItemStack() + { + return aeitemstack == null ? (itemStack == null ? null : (aeitemstack = AEItemStack.create( itemStack ))) : aeitemstack; + } } diff --git a/util/iterators/StackToSlotIterator.java b/util/iterators/StackToSlotIterator.java index 32119502..587d669f 100644 --- a/util/iterators/StackToSlotIterator.java +++ b/util/iterators/StackToSlotIterator.java @@ -26,7 +26,7 @@ public class StackToSlotIterator implements Iterator public ItemSlot next() { iss.slot = x++; - iss.itemStack = is.next(); + iss.setItemStack( is.next() ); return iss; }