Remove External Storage Handler (#2417) (#2508)

* Implemented an adapter for IItemHandler so it can be used by the Storage Bus.
* Added update hook for inject/extract to ItemHandlerAdapter.
* Implemented ItemHandler and FluidHandler capabilities for the condenser, as replacement for the Void Inventories.
* Removed external storage handler, added capability-based way of accessing a monitorable ME network via the storage bus. Removed special case inventories for the matter condenser.
* Implemented InventoryAdaptor for IItemHandler. This also now fixes molecular assemblers interaction with part interfaces.
This commit is contained in:
shartte
2016-10-26 22:58:23 +02:00
committed by yueh
parent 057754c851
commit b977ee89ee
34 changed files with 1203 additions and 1065 deletions
@@ -0,0 +1,334 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.parts.misc;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import appeng.api.config.Actionable;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.storage.IBaseMonitor;
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.IMEMonitorHandlerReceiver;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.core.AELog;
import appeng.me.storage.ITickingMonitor;
import appeng.util.Platform;
import appeng.util.item.AEItemStack;
/**
* Wraps an Item Handler in such a way that it can be used as an IMEInventory for items.
*/
class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAEItemStack>, ITickingMonitor
{
private final Map<IMEMonitorHandlerReceiver<IAEItemStack>, Object> listeners = new HashMap<>();
private BaseActionSource mySource;
private final IItemHandler itemHandler;
private ItemStack[] cachedStacks = new ItemStack[0];
private IAEItemStack[] cachedAeStacks = new IAEItemStack[0];
ItemHandlerAdapter( IItemHandler itemHandler )
{
this.itemHandler = itemHandler;
}
@Override
public IAEItemStack injectItems( IAEItemStack iox, Actionable type, BaseActionSource src )
{
ItemStack orgInput = iox.getItemStack();
ItemStack remaining = orgInput;
int slotCount = itemHandler.getSlots();
boolean simulate = ( type == Actionable.SIMULATE );
// This uses a brute force approach and tries to jam it in every slot the inventory exposes.
for( int i = 0; i < slotCount && remaining != null; i++ )
{
remaining = itemHandler.insertItem( i, remaining, simulate );
}
// At this point, we still have some items left...
if( remaining == orgInput )
{
// The stack remained unmodified, target inventory is full
return iox;
}
if( type == Actionable.MODULATE )
{
this.onTick();
}
return AEItemStack.create( remaining );
}
@Override
public IAEItemStack extractItems( IAEItemStack request, Actionable mode, BaseActionSource src )
{
ItemStack req = request.getItemStack();
int remainingSize = req.stackSize;
// Use this to gather the requested items
ItemStack gathered = null;
final boolean simulate = ( mode == Actionable.SIMULATE );
for( int i = 0; i < itemHandler.getSlots(); i++ )
{
ItemStack sub = itemHandler.getStackInSlot( i );
if( !Platform.isSameItem( sub, req ) )
{
continue;
}
ItemStack extracted;
// We have to loop here because according to the docs, the handler shouldn't return a stack with size > maxSize, even if we
// request more. So even if it returns a valid stack, it might have more stuff.
do
{
extracted = itemHandler.extractItem( i, remainingSize, simulate );
if( extracted != null )
{
if( extracted.stackSize > remainingSize )
{
// Something broke. It should never return more than we requested... We're going to silently eat the remainder
AELog.warn( "Mod that provided item handler {} is broken. Returned {} items, even though we requested {}.",
itemHandler.getClass().getSimpleName(), extracted.stackSize, remainingSize );
extracted.stackSize = remainingSize;
}
// We're just gonna use the first stack we get our hands on as the template for the rest
if( gathered == null )
{
gathered = extracted;
}
else
{
gathered.stackSize += extracted.stackSize;
}
remainingSize -= gathered.stackSize;
}
}
while( extracted != null && remainingSize > 0 );
// Done?
if( remainingSize <= 0 )
{
break;
}
}
if( gathered != null )
{
if( mode == Actionable.MODULATE )
{
this.onTick();
}
return AEItemStack.create( gathered );
}
return null;
}
@Override
public TickRateModulation onTick()
{
LinkedList<IAEItemStack> changes = new LinkedList<>();
int slots = itemHandler.getSlots();
// Make room for new slots
if( slots > cachedStacks.length )
{
cachedStacks = Arrays.copyOf( cachedStacks, slots );
cachedAeStacks = Arrays.copyOf( cachedAeStacks, slots );
}
for( int slot = 0; slot < slots; slot++ )
{
// Save the old stuff
ItemStack oldIS = cachedStacks[slot];
IAEItemStack oldAeIS = cachedAeStacks[slot];
ItemStack newIS = itemHandler.getStackInSlot( slot );
if( this.isDifferent( newIS, oldIS ) )
{
addItemChange( slot, oldAeIS, newIS, changes );
}
else if( newIS != null && oldIS != null )
{
addPossibleStackSizeChange( slot, oldAeIS, newIS, changes );
}
}
// Handle cases where the number of slots actually is lower now than before
if( slots < cachedStacks.length )
{
for( int slot = slots; slot < cachedStacks.length; slot++ )
{
IAEItemStack aeStack = cachedAeStacks[slot];
if( aeStack != null )
{
IAEItemStack a = aeStack.copy();
a.setStackSize( -a.getStackSize() );
changes.add( a );
}
}
// Reduce the cache size
cachedStacks = Arrays.copyOf( cachedStacks, slots );
cachedAeStacks = Arrays.copyOf( cachedAeStacks, slots );
}
if( !changes.isEmpty() )
{
this.postDifference( changes );
return TickRateModulation.URGENT;
}
else
{
return TickRateModulation.SLOWER;
}
}
private void addItemChange( int slot, IAEItemStack oldAeIS, ItemStack newIS, List<IAEItemStack> changes )
{
// Completely different item
cachedStacks[slot] = newIS;
cachedAeStacks[slot] = AEItemStack.create( newIS );
// If we had a stack previously in this slot, notify the newtork about its disappearance
if( oldAeIS != null )
{
oldAeIS.setStackSize( -oldAeIS.getStackSize() );
changes.add( oldAeIS );
}
// Notify the network about the new stack. Note that this is null if newIS was null
if( cachedAeStacks[slot] != null )
{
changes.add( cachedAeStacks[slot] );
}
}
private void addPossibleStackSizeChange( int slot, IAEItemStack oldAeIS, ItemStack newIS, List<IAEItemStack> changes )
{
// Still the same item, but amount might have changed
long diff = newIS.stackSize - oldAeIS.getStackSize();
if( diff != 0 )
{
IAEItemStack stack = oldAeIS.copy();
stack.setStackSize( newIS.stackSize );
cachedStacks[slot] = newIS;
cachedAeStacks[slot] = stack;
final IAEItemStack a = stack.copy();
a.setStackSize( diff );
changes.add( a );
}
}
private boolean isDifferent( final ItemStack a, final ItemStack b )
{
if( a == b && b == null )
{
return false;
}
return a == null || b == null || !Platform.isSameItemPrecise( a, b );
}
private void postDifference( Iterable<IAEItemStack> a )
{
final Iterator<Map.Entry<IMEMonitorHandlerReceiver<IAEItemStack>, Object>> i = this.listeners.entrySet().iterator();
while( i.hasNext() )
{
final Map.Entry<IMEMonitorHandlerReceiver<IAEItemStack>, Object> l = i.next();
final IMEMonitorHandlerReceiver<IAEItemStack> key = l.getKey();
if( key.isValid( l.getValue() ) )
{
key.postChange( this, a, mySource );
}
else
{
i.remove();
}
}
}
@Override
public void setActionSource( final BaseActionSource mySource )
{
this.mySource = mySource;
}
@Override
public IItemList<IAEItemStack> getAvailableItems( IItemList<IAEItemStack> out )
{
for( int i = 0; i < itemHandler.getSlots(); i++ )
{
out.addStorage( AEItemStack.create( itemHandler.getStackInSlot( i ) ) );
}
return out;
}
@Override
public StorageChannel getChannel()
{
return StorageChannel.ITEMS;
}
@Override
public void addListener( final IMEMonitorHandlerReceiver<IAEItemStack> l, final Object verificationToken )
{
this.listeners.put( l, verificationToken );
}
@Override
public void removeListener( final IMEMonitorHandlerReceiver<IAEItemStack> l )
{
this.listeners.remove( l );
}
}
@@ -38,11 +38,9 @@ import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import appeng.api.config.Actionable;
import appeng.api.config.Upgrades;
import appeng.api.implementations.tiles.ITileStorageMonitorable;
import appeng.api.networking.IGridNode;
import appeng.api.networking.crafting.ICraftingLink;
import appeng.api.networking.crafting.ICraftingPatternDetails;
@@ -50,7 +48,6 @@ import appeng.api.networking.crafting.ICraftingProviderHelper;
import appeng.api.networking.events.MENetworkChannelsChanged;
import appeng.api.networking.events.MENetworkEventSubscribe;
import appeng.api.networking.events.MENetworkPowerStatusChange;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.ticking.IGridTickable;
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
@@ -75,7 +72,7 @@ import appeng.util.Platform;
import appeng.util.inv.IInventoryDestination;
public class PartInterface extends PartBasicState implements IGridTickable, IStorageMonitorable, IInventoryDestination, IInterfaceHost, ISidedInventory, IAEAppEngInventory, ITileStorageMonitorable, IPriorityHost
public class PartInterface extends PartBasicState implements IGridTickable, IStorageMonitorable, IInventoryDestination, IInterfaceHost, ISidedInventory, IAEAppEngInventory, IPriorityHost
{
public static final ResourceLocation MODEL_BASE = new ResourceLocation( AppEng.MOD_ID, "part/interface_base" );
@@ -345,12 +342,6 @@ public class PartInterface extends PartBasicState implements IGridTickable, ISto
return super.getHost().getTile();
}
@Override
public IStorageMonitorable getMonitorable( final EnumFacing side, final BaseActionSource src )
{
return this.duality.getMonitorable( side, src, this );
}
@Override
public boolean pushPattern( final ICraftingPatternDetails patternDetails, final InventoryCrafting table )
{
@@ -30,9 +30,12 @@ import net.minecraft.inventory.IInventory;
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.ResourceLocation;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import appeng.api.AEApi;
import appeng.api.config.AccessRestriction;
@@ -56,17 +59,19 @@ import appeng.api.networking.ticking.TickingRequest;
import appeng.api.parts.IPartCollisionHelper;
import appeng.api.parts.IPartHost;
import appeng.api.storage.ICellContainer;
import appeng.api.storage.IExternalStorageHandler;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.IMEMonitorHandlerReceiver;
import appeng.api.storage.IStorageMonitorable;
import appeng.api.storage.IStorageMonitorableAccessor;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList;
import appeng.api.util.AECableType;
import appeng.api.util.AEPartLocation;
import appeng.api.util.IConfigManager;
import appeng.capabilities.Capabilities;
import appeng.core.AppEng;
import appeng.core.settings.TickRates;
import appeng.core.stats.Achievements;
@@ -76,6 +81,7 @@ import appeng.helpers.IPriorityHost;
import appeng.helpers.Reflected;
import appeng.items.parts.PartModels;
import appeng.me.GridAccessException;
import appeng.me.storage.ITickingMonitor;
import appeng.me.storage.MEInventoryHandler;
import appeng.me.storage.MEMonitorIInventory;
import appeng.parts.automation.PartUpgradeable;
@@ -86,12 +92,7 @@ import appeng.util.prioitylist.FuzzyPriorityList;
import appeng.util.prioitylist.PrecisePriorityList;
// TODO: BC Integration
//@Interface( iname = IntegrationType.BuildCraftTransport, iface = "buildcraft.api.transport.IPipeConnection" )
public class PartStorageBus extends PartUpgradeable implements IGridTickable, ICellContainer, IMEMonitorHandlerReceiver<IAEItemStack> /*
* ,
* IPipeConnection
*/, IPriorityHost
public class PartStorageBus extends PartUpgradeable implements IGridTickable, ICellContainer, IMEMonitorHandlerReceiver<IAEItemStack>, IPriorityHost
{
public static final ResourceLocation MODEL_BASE = new ResourceLocation( AppEng.MOD_ID, "part/storage_bus_base" );
@@ -115,8 +116,8 @@ public class PartStorageBus extends PartUpgradeable implements IGridTickable, IC
private final AppEngInternalAEInventory Config = new AppEngInternalAEInventory( this, 63 );
private int priority = 0;
private boolean cached = false;
private MEMonitorIInventory monitor = null;
private MEInventoryHandler handler = null;
private ITickingMonitor monitor = null;
private MEInventoryHandler<? extends IAEStack> handler = null;
private int handlerHash = 0;
private boolean wasActive = false;
private byte resetCacheLogic = 0;
@@ -367,6 +368,41 @@ public class PartStorageBus extends PartUpgradeable implements IGridTickable, IC
Platform.postListChanges( before, after, this, this.mySrc );
}
@SuppressWarnings( "unchecked" )
private IMEInventory<? extends IAEItemStack> getInventoryWrapper( TileEntity target )
{
EnumFacing targetSide = this.getSide().getFacing().getOpposite();
// Prioritize a handler to directly link to another ME network
IStorageMonitorableAccessor accessor = target.getCapability( Capabilities.STORAGE_MONITORABLE_ACCESSOR, targetSide );
if( accessor != null )
{
IStorageMonitorable inventory = accessor.getInventory( mySrc );
if( inventory != null )
{
return inventory.getItemInventory();
}
// So this could / can be a design decision. If the tile does support our custom capability,
// but it does not return an inventory for the action source, we do NOT fall back to using
// IItemHandler's, as that might circumvent the security setings, and might also cause
// performance issues.
return null;
}
// Check via cap for IItemHandler
IItemHandler handlerExt = target.getCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, targetSide );
if( handlerExt != null )
{
return new ItemHandlerAdapter( handlerExt );
}
return null;
}
public MEInventoryHandler getInternalHandler()
{
if( this.cached )
@@ -391,58 +427,54 @@ public class PartStorageBus extends PartUpgradeable implements IGridTickable, IC
this.monitor = null;
if( target != null )
{
final IExternalStorageHandler esh = AEApi.instance().registries().externalStorage().getHandler( target, this.getSide().getFacing().getOpposite(), StorageChannel.ITEMS, this.mySrc );
if( esh != null )
IMEInventory<? extends IAEStack> inv = getInventoryWrapper( target );
if( inv instanceof MEMonitorIInventory )
{
final IMEInventory inv = esh.getInventory( target, this.getSide().getFacing().getOpposite(), StorageChannel.ITEMS, this.mySrc );
final MEMonitorIInventory h = (MEMonitorIInventory) inv;
h.setMode( (StorageFilter) this.getConfigManager().getSetting( Settings.STORAGE_FILTER ) );
}
if( inv instanceof MEMonitorIInventory )
if( inv instanceof ITickingMonitor )
{
this.monitor = (ITickingMonitor) inv;
this.monitor.setActionSource( new MachineSource( this ) );
}
if( inv != null )
{
this.checkInterfaceVsStorageBus( target, this.getSide().getOpposite() );
this.handler = new MEInventoryHandler<>( inv, StorageChannel.ITEMS );
this.handler.setBaseAccess( (AccessRestriction) this.getConfigManager().getSetting( Settings.ACCESS ) );
this.handler.setWhitelist( this.getInstalledUpgrades( Upgrades.INVERTER ) > 0 ? IncludeExclude.BLACKLIST : IncludeExclude.WHITELIST );
this.handler.setPriority( this.priority );
final IItemList<IAEItemStack> priorityList = AEApi.instance().storage().createItemList();
final int slotsToUse = 18 + this.getInstalledUpgrades( Upgrades.CAPACITY ) * 9;
for( int x = 0; x < this.Config.getSizeInventory() && x < slotsToUse; x++ )
{
final MEMonitorIInventory h = (MEMonitorIInventory) inv;
h.setMode( (StorageFilter) this.getConfigManager().getSetting( Settings.STORAGE_FILTER ) );
h.setActionSource( new MachineSource( this ) );
final IAEItemStack is = this.Config.getAEStackInSlot( x );
if( is != null )
{
priorityList.add( is );
}
}
if( inv instanceof MEMonitorIInventory )
if( this.getInstalledUpgrades( Upgrades.FUZZY ) > 0 )
{
this.monitor = (MEMonitorIInventory) inv;
this.handler.setPartitionList( new FuzzyPriorityList( priorityList, (FuzzyMode) this.getConfigManager().getSetting( Settings.FUZZY_MODE ) ) );
}
else
{
this.handler.setPartitionList( new PrecisePriorityList( priorityList ) );
}
if( inv != null )
if( inv instanceof IBaseMonitor )
{
this.checkInterfaceVsStorageBus( target, this.getSide().getOpposite() );
this.handler = new MEInventoryHandler( inv, StorageChannel.ITEMS );
this.handler.setBaseAccess( (AccessRestriction) this.getConfigManager().getSetting( Settings.ACCESS ) );
this.handler.setWhitelist( this.getInstalledUpgrades( Upgrades.INVERTER ) > 0 ? IncludeExclude.BLACKLIST : IncludeExclude.WHITELIST );
this.handler.setPriority( this.priority );
final IItemList<IAEItemStack> priorityList = AEApi.instance().storage().createItemList();
final int slotsToUse = 18 + this.getInstalledUpgrades( Upgrades.CAPACITY ) * 9;
for( int x = 0; x < this.Config.getSizeInventory() && x < slotsToUse; x++ )
{
final IAEItemStack is = this.Config.getAEStackInSlot( x );
if( is != null )
{
priorityList.add( is );
}
}
if( this.getInstalledUpgrades( Upgrades.FUZZY ) > 0 )
{
this.handler.setPartitionList( new FuzzyPriorityList( priorityList, (FuzzyMode) this.getConfigManager().getSetting( Settings.FUZZY_MODE ) ) );
}
else
{
this.handler.setPartitionList( new PrecisePriorityList( priorityList ) );
}
if( inv instanceof IMEMonitor )
{
( (IBaseMonitor) inv ).addListener( this, this.handler );
}
( (IBaseMonitor) inv ).addListener( this, this.handler );
}
}
}