pick 97420a31d The big reformat of 2020
This commit is contained in:
@@ -18,7 +18,6 @@
|
||||
|
||||
package appeng.parts.misc;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
@@ -46,317 +45,271 @@ 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.
|
||||
* 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 IActionSource mySource;
|
||||
private final IItemHandler itemHandler;
|
||||
private final IGridProxyable proxyable;
|
||||
private final InventoryCache cache;
|
||||
class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAEItemStack>, ITickingMonitor {
|
||||
private final Map<IMEMonitorHandlerReceiver<IAEItemStack>, Object> listeners = new HashMap<>();
|
||||
private IActionSource mySource;
|
||||
private final IItemHandler itemHandler;
|
||||
private final IGridProxyable proxyable;
|
||||
private final InventoryCache cache;
|
||||
|
||||
ItemHandlerAdapter( IItemHandler itemHandler, IGridProxyable proxy )
|
||||
{
|
||||
this.itemHandler = itemHandler;
|
||||
this.proxyable = proxy;
|
||||
this.cache = new InventoryCache( this.itemHandler );
|
||||
}
|
||||
ItemHandlerAdapter(IItemHandler itemHandler, IGridProxyable proxy) {
|
||||
this.itemHandler = itemHandler;
|
||||
this.proxyable = proxy;
|
||||
this.cache = new InventoryCache(this.itemHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEItemStack injectItems( IAEItemStack iox, Actionable type, IActionSource src )
|
||||
{
|
||||
ItemStack orgInput = iox.createItemStack();
|
||||
ItemStack remaining = orgInput;
|
||||
@Override
|
||||
public IAEItemStack injectItems(IAEItemStack iox, Actionable type, IActionSource src) {
|
||||
ItemStack orgInput = iox.createItemStack();
|
||||
ItemStack remaining = orgInput;
|
||||
|
||||
int slotCount = this.itemHandler.getSlots();
|
||||
boolean simulate = ( type == Actionable.SIMULATE );
|
||||
int slotCount = this.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.isEmpty(); i++ )
|
||||
{
|
||||
remaining = this.itemHandler.insertItem( i, remaining, 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.isEmpty(); i++) {
|
||||
remaining = this.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;
|
||||
}
|
||||
// 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 )
|
||||
{
|
||||
try
|
||||
{
|
||||
this.proxyable.getProxy().getTick().alertDevice( this.proxyable.getProxy().getNode() );
|
||||
}
|
||||
catch( GridAccessException ex )
|
||||
{
|
||||
// meh
|
||||
}
|
||||
}
|
||||
if (type == Actionable.MODULATE) {
|
||||
try {
|
||||
this.proxyable.getProxy().getTick().alertDevice(this.proxyable.getProxy().getNode());
|
||||
} catch (GridAccessException ex) {
|
||||
// meh
|
||||
}
|
||||
}
|
||||
|
||||
return AEItemStack.fromItemStack( remaining );
|
||||
}
|
||||
return AEItemStack.fromItemStack(remaining);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEItemStack extractItems( IAEItemStack request, Actionable mode, IActionSource src )
|
||||
{
|
||||
@Override
|
||||
public IAEItemStack extractItems(IAEItemStack request, Actionable mode, IActionSource src) {
|
||||
|
||||
ItemStack requestedItemStack = request.createItemStack();
|
||||
int remainingSize = requestedItemStack.getCount();
|
||||
ItemStack requestedItemStack = request.createItemStack();
|
||||
int remainingSize = requestedItemStack.getCount();
|
||||
|
||||
// Use this to gather the requested items
|
||||
ItemStack gathered = ItemStack.EMPTY;
|
||||
// Use this to gather the requested items
|
||||
ItemStack gathered = ItemStack.EMPTY;
|
||||
|
||||
final boolean simulate = ( mode == Actionable.SIMULATE );
|
||||
final boolean simulate = (mode == Actionable.SIMULATE);
|
||||
|
||||
for( int i = 0; i < this.itemHandler.getSlots(); i++ )
|
||||
{
|
||||
ItemStack stackInInventorySlot = this.itemHandler.getStackInSlot( i );
|
||||
for (int i = 0; i < this.itemHandler.getSlots(); i++) {
|
||||
ItemStack stackInInventorySlot = this.itemHandler.getStackInSlot(i);
|
||||
|
||||
if( !Platform.itemComparisons().isSameItem( stackInInventorySlot, requestedItemStack ) )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (!Platform.itemComparisons().isSameItem(stackInInventorySlot, requestedItemStack)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ItemStack extracted;
|
||||
int stackSizeCurrentSlot = stackInInventorySlot.getCount();
|
||||
int remainingCurrentSlot = Math.min( remainingSize, stackSizeCurrentSlot );
|
||||
ItemStack extracted;
|
||||
int stackSizeCurrentSlot = stackInInventorySlot.getCount();
|
||||
int remainingCurrentSlot = Math.min(remainingSize, stackSizeCurrentSlot);
|
||||
|
||||
// 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 = this.itemHandler.extractItem( i, remainingCurrentSlot, simulate );
|
||||
if( !extracted.isEmpty() )
|
||||
{
|
||||
if( extracted.getCount() > remainingCurrentSlot )
|
||||
{
|
||||
// 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 %s is broken. Returned %s items while only requesting %d.",
|
||||
this.itemHandler.getClass().getName(), extracted.toString(), remainingCurrentSlot );
|
||||
extracted.setCount( remainingCurrentSlot );
|
||||
}
|
||||
// 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 = this.itemHandler.extractItem(i, remainingCurrentSlot, simulate);
|
||||
if (!extracted.isEmpty()) {
|
||||
if (extracted.getCount() > remainingCurrentSlot) {
|
||||
// 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 %s is broken. Returned %s items while only requesting %d.",
|
||||
this.itemHandler.getClass().getName(), extracted.toString(), remainingCurrentSlot);
|
||||
extracted.setCount(remainingCurrentSlot);
|
||||
}
|
||||
|
||||
// We're just gonna use the first stack we get our hands on as the template for the rest.
|
||||
// In case some stupid itemhandler (aka forge) returns an internal state we have to do a second
|
||||
// expensive copy again.
|
||||
if( gathered.isEmpty() )
|
||||
{
|
||||
gathered = extracted.copy();
|
||||
}
|
||||
else
|
||||
{
|
||||
gathered.grow( extracted.getCount() );
|
||||
}
|
||||
remainingCurrentSlot -= extracted.getCount();
|
||||
}
|
||||
}
|
||||
while( !extracted.isEmpty() && remainingCurrentSlot > 0 );
|
||||
// We're just gonna use the first stack we get our hands on as the template for
|
||||
// the rest.
|
||||
// In case some stupid itemhandler (aka forge) returns an internal state we have
|
||||
// to do a second
|
||||
// expensive copy again.
|
||||
if (gathered.isEmpty()) {
|
||||
gathered = extracted.copy();
|
||||
} else {
|
||||
gathered.grow(extracted.getCount());
|
||||
}
|
||||
remainingCurrentSlot -= extracted.getCount();
|
||||
}
|
||||
} while (!extracted.isEmpty() && remainingCurrentSlot > 0);
|
||||
|
||||
remainingSize -= stackSizeCurrentSlot - remainingCurrentSlot;
|
||||
remainingSize -= stackSizeCurrentSlot - remainingCurrentSlot;
|
||||
|
||||
// Done?
|
||||
if( remainingSize <= 0 )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Done?
|
||||
if (remainingSize <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( !gathered.isEmpty() )
|
||||
{
|
||||
if( mode == Actionable.MODULATE )
|
||||
{
|
||||
try
|
||||
{
|
||||
this.proxyable.getProxy().getTick().alertDevice( this.proxyable.getProxy().getNode() );
|
||||
}
|
||||
catch( GridAccessException ex )
|
||||
{
|
||||
// meh
|
||||
}
|
||||
}
|
||||
if (!gathered.isEmpty()) {
|
||||
if (mode == Actionable.MODULATE) {
|
||||
try {
|
||||
this.proxyable.getProxy().getTick().alertDevice(this.proxyable.getProxy().getNode());
|
||||
} catch (GridAccessException ex) {
|
||||
// meh
|
||||
}
|
||||
}
|
||||
|
||||
return AEItemStack.fromItemStack( gathered );
|
||||
}
|
||||
return AEItemStack.fromItemStack(gathered);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickRateModulation onTick()
|
||||
{
|
||||
List<IAEItemStack> changes = this.cache.update();
|
||||
if( !changes.isEmpty() )
|
||||
{
|
||||
this.postDifference( changes );
|
||||
return TickRateModulation.URGENT;
|
||||
}
|
||||
else
|
||||
{
|
||||
return TickRateModulation.SLOWER;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public TickRateModulation onTick() {
|
||||
List<IAEItemStack> changes = this.cache.update();
|
||||
if (!changes.isEmpty()) {
|
||||
this.postDifference(changes);
|
||||
return TickRateModulation.URGENT;
|
||||
} else {
|
||||
return TickRateModulation.SLOWER;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setActionSource( final IActionSource mySource )
|
||||
{
|
||||
this.mySource = mySource;
|
||||
}
|
||||
@Override
|
||||
public void setActionSource(final IActionSource mySource) {
|
||||
this.mySource = mySource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IItemList<IAEItemStack> getAvailableItems( IItemList<IAEItemStack> out )
|
||||
{
|
||||
return this.cache.getAvailableItems( out );
|
||||
}
|
||||
@Override
|
||||
public IItemList<IAEItemStack> getAvailableItems(IItemList<IAEItemStack> out) {
|
||||
return this.cache.getAvailableItems(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IItemStorageChannel getChannel()
|
||||
{
|
||||
return AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class );
|
||||
}
|
||||
@Override
|
||||
public IItemStorageChannel getChannel() {
|
||||
return AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener( final IMEMonitorHandlerReceiver<IAEItemStack> l, final Object verificationToken )
|
||||
{
|
||||
this.listeners.put( l, verificationToken );
|
||||
}
|
||||
@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 );
|
||||
}
|
||||
@Override
|
||||
public void removeListener(final IMEMonitorHandlerReceiver<IAEItemStack> l) {
|
||||
this.listeners.remove(l);
|
||||
}
|
||||
|
||||
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, this.mySource );
|
||||
}
|
||||
else
|
||||
{
|
||||
i.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
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, this.mySource);
|
||||
} else {
|
||||
i.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class InventoryCache
|
||||
{
|
||||
private IAEItemStack[] cachedAeStacks = new IAEItemStack[0];
|
||||
private final IItemHandler itemHandler;
|
||||
private static class InventoryCache {
|
||||
private IAEItemStack[] cachedAeStacks = new IAEItemStack[0];
|
||||
private final IItemHandler itemHandler;
|
||||
|
||||
public InventoryCache( IItemHandler itemHandler )
|
||||
{
|
||||
this.itemHandler = itemHandler;
|
||||
}
|
||||
public InventoryCache(IItemHandler itemHandler) {
|
||||
this.itemHandler = itemHandler;
|
||||
}
|
||||
|
||||
public IItemList<IAEItemStack> getAvailableItems( IItemList<IAEItemStack> out )
|
||||
{
|
||||
Arrays.stream( this.cachedAeStacks ).forEach( out::add );
|
||||
return out;
|
||||
}
|
||||
public IItemList<IAEItemStack> getAvailableItems(IItemList<IAEItemStack> out) {
|
||||
Arrays.stream(this.cachedAeStacks).forEach(out::add);
|
||||
return out;
|
||||
}
|
||||
|
||||
public List<IAEItemStack> update()
|
||||
{
|
||||
final List<IAEItemStack> changes = new ArrayList<>();
|
||||
final int slots = this.itemHandler.getSlots();
|
||||
public List<IAEItemStack> update() {
|
||||
final List<IAEItemStack> changes = new ArrayList<>();
|
||||
final int slots = this.itemHandler.getSlots();
|
||||
|
||||
// Make room for new slots
|
||||
if( slots > this.cachedAeStacks.length )
|
||||
{
|
||||
this.cachedAeStacks = Arrays.copyOf( this.cachedAeStacks, slots );
|
||||
}
|
||||
// Make room for new slots
|
||||
if (slots > this.cachedAeStacks.length) {
|
||||
this.cachedAeStacks = Arrays.copyOf(this.cachedAeStacks, slots);
|
||||
}
|
||||
|
||||
for( int slot = 0; slot < slots; slot++ )
|
||||
{
|
||||
// Save the old stuff
|
||||
final IAEItemStack oldAeIS = this.cachedAeStacks[slot];
|
||||
final ItemStack newIS = this.itemHandler.getStackInSlot( slot );
|
||||
for (int slot = 0; slot < slots; slot++) {
|
||||
// Save the old stuff
|
||||
final IAEItemStack oldAeIS = this.cachedAeStacks[slot];
|
||||
final ItemStack newIS = this.itemHandler.getStackInSlot(slot);
|
||||
|
||||
this.handlePossibleSlotChanges( slot, oldAeIS, newIS, changes );
|
||||
}
|
||||
this.handlePossibleSlotChanges(slot, oldAeIS, newIS, changes);
|
||||
}
|
||||
|
||||
// Handle cases where the number of slots actually is lower now than before
|
||||
if( slots < this.cachedAeStacks.length )
|
||||
{
|
||||
for( int slot = slots; slot < this.cachedAeStacks.length; slot++ )
|
||||
{
|
||||
final IAEItemStack aeStack = this.cachedAeStacks[slot];
|
||||
// Handle cases where the number of slots actually is lower now than before
|
||||
if (slots < this.cachedAeStacks.length) {
|
||||
for (int slot = slots; slot < this.cachedAeStacks.length; slot++) {
|
||||
final IAEItemStack aeStack = this.cachedAeStacks[slot];
|
||||
|
||||
if( aeStack != null )
|
||||
{
|
||||
final IAEItemStack a = aeStack.copy();
|
||||
a.setStackSize( -a.getStackSize() );
|
||||
changes.add( a );
|
||||
}
|
||||
}
|
||||
if (aeStack != null) {
|
||||
final IAEItemStack a = aeStack.copy();
|
||||
a.setStackSize(-a.getStackSize());
|
||||
changes.add(a);
|
||||
}
|
||||
}
|
||||
|
||||
// Reduce the cache size
|
||||
this.cachedAeStacks = Arrays.copyOf( this.cachedAeStacks, slots );
|
||||
}
|
||||
// Reduce the cache size
|
||||
this.cachedAeStacks = Arrays.copyOf(this.cachedAeStacks, slots);
|
||||
}
|
||||
|
||||
return changes;
|
||||
}
|
||||
return changes;
|
||||
}
|
||||
|
||||
private void handlePossibleSlotChanges( int slot, IAEItemStack oldAeIS, ItemStack newIS, List<IAEItemStack> changes )
|
||||
{
|
||||
if( oldAeIS != null && oldAeIS.isSameType( newIS ) )
|
||||
{
|
||||
this.handleStackSizeChanged( slot, oldAeIS, newIS, changes );
|
||||
}
|
||||
else
|
||||
{
|
||||
this.handleItemChanged( slot, oldAeIS, newIS, changes );
|
||||
}
|
||||
}
|
||||
private void handlePossibleSlotChanges(int slot, IAEItemStack oldAeIS, ItemStack newIS,
|
||||
List<IAEItemStack> changes) {
|
||||
if (oldAeIS != null && oldAeIS.isSameType(newIS)) {
|
||||
this.handleStackSizeChanged(slot, oldAeIS, newIS, changes);
|
||||
} else {
|
||||
this.handleItemChanged(slot, oldAeIS, newIS, changes);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleStackSizeChanged( int slot, IAEItemStack oldAeIS, ItemStack newIS, List<IAEItemStack> changes )
|
||||
{
|
||||
// Still the same item, but amount might have changed
|
||||
final long diff = newIS.getCount() - oldAeIS.getStackSize();
|
||||
private void handleStackSizeChanged(int slot, IAEItemStack oldAeIS, ItemStack newIS,
|
||||
List<IAEItemStack> changes) {
|
||||
// Still the same item, but amount might have changed
|
||||
final long diff = newIS.getCount() - oldAeIS.getStackSize();
|
||||
|
||||
if( diff != 0 )
|
||||
{
|
||||
final IAEItemStack stack = oldAeIS.copy();
|
||||
stack.setStackSize( newIS.getCount() );
|
||||
if (diff != 0) {
|
||||
final IAEItemStack stack = oldAeIS.copy();
|
||||
stack.setStackSize(newIS.getCount());
|
||||
|
||||
this.cachedAeStacks[slot] = stack;
|
||||
this.cachedAeStacks[slot] = stack;
|
||||
|
||||
final IAEItemStack a = stack.copy();
|
||||
a.setStackSize( diff );
|
||||
changes.add( a );
|
||||
}
|
||||
}
|
||||
final IAEItemStack a = stack.copy();
|
||||
a.setStackSize(diff);
|
||||
changes.add(a);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleItemChanged( int slot, IAEItemStack oldAeIS, ItemStack newIS, List<IAEItemStack> changes )
|
||||
{
|
||||
// Completely different item
|
||||
this.cachedAeStacks[slot] = AEItemStack.fromItemStack( newIS );
|
||||
private void handleItemChanged(int slot, IAEItemStack oldAeIS, ItemStack newIS, List<IAEItemStack> changes) {
|
||||
// Completely different item
|
||||
this.cachedAeStacks[slot] = AEItemStack.fromItemStack(newIS);
|
||||
|
||||
// If we had a stack previously in this slot, notify the network about its disappearance
|
||||
if( oldAeIS != null )
|
||||
{
|
||||
oldAeIS.setStackSize( -oldAeIS.getStackSize() );
|
||||
changes.add( oldAeIS );
|
||||
}
|
||||
// If we had a stack previously in this slot, notify the network 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( this.cachedAeStacks[slot] != null )
|
||||
{
|
||||
changes.add( this.cachedAeStacks[slot] );
|
||||
}
|
||||
}
|
||||
}
|
||||
// Notify the network about the new stack. Note that this is null if newIS was
|
||||
// null
|
||||
if (this.cachedAeStacks[slot] != null) {
|
||||
changes.add(this.cachedAeStacks[slot]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user