Refactored the BaseActionSource (#3063)

* Refactored the BaseActionSource

It now uses an interface `IActionSource` instead of a concrete class and further
subclasses. 

Instead of relying on a specific class for a certain action type, it now
uses methods with Optional as return values to determine a player or
machine issuing an action. Refer to the JavaDocs for the exact behaviour.

* `IActionHost` no longer extends `IGridHost`

It never used the additional functionality and if needed the `IGridNode`
will also provide a reference to the corresponding `IGridHost`.

Due to most crafting related GUIs being hardcoded to `IGridHost`, 
they no longer work when `IActionHost` is not extending it. Actually
`IActionHost` is the better solution for it, as it prevents us from looking
the grid up via the `IGridHost` and potentially finding a wrong grid.

* Interfaces now only lock the currently worked slot.

This allows `DualityInterface#onChangeInventory` to update slots of the same interface, should they be the source for a requested item and therefore updating their working set accordingly to queue further crafting requests for unfulfilled stack requirements.
This commit is contained in:
yueh
2017-09-12 11:39:00 +02:00
committed by GitHub
parent 771a944058
commit 970630a90d
81 changed files with 572 additions and 465 deletions
@@ -25,6 +25,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable;
@@ -66,9 +67,8 @@ import appeng.api.networking.crafting.ICraftingProvider;
import appeng.api.networking.crafting.ICraftingProviderHelper;
import appeng.api.networking.energy.IEnergySource;
import appeng.api.networking.events.MENetworkCraftingPatternChange;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.security.IActionHost;
import appeng.api.networking.security.MachineSource;
import appeng.api.networking.security.IActionSource;
import appeng.api.networking.ticking.IGridTickable;
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
@@ -88,6 +88,7 @@ import appeng.capabilities.Capabilities;
import appeng.core.settings.TickRates;
import appeng.me.GridAccessException;
import appeng.me.helpers.AENetworkProxy;
import appeng.me.helpers.MachineSource;
import appeng.me.storage.MEMonitorIInventory;
import appeng.me.storage.MEMonitorPassThrough;
import appeng.me.storage.NullInventory;
@@ -118,8 +119,8 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
private final MultiCraftingTracker craftingTracker;
private final AENetworkProxy gridProxy;
private final IInterfaceHost iHost;
private final BaseActionSource mySource;
private final BaseActionSource interfaceRequestSource;
private final IActionSource mySource;
private final IActionSource interfaceRequestSource;
private final ConfigManager cm = new ConfigManager( this );
private final AppEngInternalAEInventory config = new AppEngInternalAEInventory( this, NUMBER_OF_CONFIG_SLOTS );
private final AppEngInternalInventory storage = new AppEngInternalInventory( this, NUMBER_OF_STORAGE_SLOTS );
@@ -132,7 +133,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
private List<ICraftingPatternDetails> craftingList = null;
private List<ItemStack> waitingToSend = null;
private IMEInventory<IAEItemStack> destination;
private boolean isWorking = false;
private int isWorking = -1;
private final Accessor accessor = new Accessor();
public DualityInterface( final AENetworkProxy networkProxy, final IInterfaceHost ih )
@@ -164,7 +165,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
@Override
public void onChangeInventory( final IItemHandler inv, final int slot, final InvOperation mc, final ItemStack removed, final ItemStack added )
{
if( this.isWorking )
if( this.isWorking == slot )
{
return;
}
@@ -407,34 +408,34 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
req = null;
}
final ItemStack Stored = this.storage.getStackInSlot( slot );
final ItemStack stored = this.storage.getStackInSlot( slot );
if( req == null && !Stored.isEmpty() )
if( req == null && !stored.isEmpty() )
{
final IAEItemStack work = AEApi.instance().storage().createItemStack( Stored );
final IAEItemStack work = AEApi.instance().storage().createItemStack( stored );
this.requireWork[slot] = work.setStackSize( -work.getStackSize() );
return;
}
else if( req != null )
{
if( Stored.isEmpty() ) // need to add stuff!
if( stored.isEmpty() ) // need to add stuff!
{
this.requireWork[slot] = req.copy();
return;
}
else if( req.isSameType( Stored ) ) // same type ( qty different? )!
else if( req.isSameType( stored ) ) // same type ( qty different? )!
{
if( req.getStackSize() != Stored.getCount() )
if( req.getStackSize() != stored.getCount() )
{
this.requireWork[slot] = req.copy();
this.requireWork[slot].setStackSize( req.getStackSize() - Stored.getCount() );
this.requireWork[slot].setStackSize( req.getStackSize() - stored.getCount() );
return;
}
}
else
// Stored != null; dispose!
{
final IAEItemStack work = AEApi.instance().storage().createItemStack( Stored );
final IAEItemStack work = AEApi.instance().storage().createItemStack( stored );
this.requireWork[slot] = work.setStackSize( -work.getStackSize() );
return;
}
@@ -657,7 +658,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
private boolean usePlan( final int x, final IAEItemStack itemStack )
{
final InventoryAdaptor adaptor = this.getAdaptor( x );
this.isWorking = true;
this.isWorking = x;
boolean changed = false;
try
@@ -742,7 +743,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
this.updatePlan( x );
}
this.isWorking = false;
this.isWorking = -1;
return changed;
}
@@ -865,7 +866,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
this.craftingTracker.cancel();
}
public IStorageMonitorable getMonitorable( final BaseActionSource src, final IStorageMonitorable myInterface )
public IStorageMonitorable getMonitorable( final IActionSource src, final IStorageMonitorable myInterface )
{
if( Platform.canAccess( this.gridProxy, src ) )
{
@@ -1275,10 +1276,34 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
private class InterfaceRequestSource extends MachineSource
{
private final InterfaceRequestContext context;
public InterfaceRequestSource( final IActionHost v )
public InterfaceRequestSource( IActionHost v )
{
super( v );
this.context = new InterfaceRequestContext();
}
@Override
public <T> Optional<T> context( Class<T> key )
{
if( key == InterfaceRequestContext.class )
{
return (Optional<T>) Optional.of( this.context );
}
return super.context( key );
}
}
private class InterfaceRequestContext implements Comparable<Integer>
{
@Override
public int compareTo( Integer o )
{
return Integer.compare( DualityInterface.this.priority, o );
}
}
@@ -1292,9 +1317,12 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
}
@Override
public IAEItemStack injectItems( final IAEItemStack input, final Actionable type, final BaseActionSource src )
public IAEItemStack injectItems( final IAEItemStack input, final Actionable type, final IActionSource src )
{
if( src instanceof InterfaceRequestSource )
final Optional<InterfaceRequestContext> context = src.context( InterfaceRequestContext.class );
final boolean isInterface = context.isPresent();
if( isInterface )
{
return input;
}
@@ -1303,9 +1331,12 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
}
@Override
public IAEItemStack extractItems( final IAEItemStack request, final Actionable type, final BaseActionSource src )
public IAEItemStack extractItems( final IAEItemStack request, final Actionable type, final IActionSource src )
{
if( src instanceof InterfaceRequestSource )
final Optional<InterfaceRequestContext> context = src.context( InterfaceRequestContext.class );
final boolean hasLowerOrEqualPriority = context.map( c -> c.compareTo( DualityInterface.this.priority ) <= 0 ).orElse( false );
if( hasLowerOrEqualPriority )
{
return null;
}
@@ -1319,7 +1350,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn
@Nullable
@Override
public IStorageMonitorable getInventory( BaseActionSource src )
public IStorageMonitorable getInventory( IActionSource src )
{
return DualityInterface.this.getMonitorable( src, DualityInterface.this );
}