half-undo wip

This commit is contained in:
Salomão
2021-06-16 22:21:07 -03:00
parent 01fe5c7a8b
commit 0fb393820b
3 changed files with 194 additions and 104 deletions
@@ -19,6 +19,7 @@
package appeng.parts.misc; package appeng.parts.misc;
import javax.annotation.Nullable;
import appeng.api.AEApi; import appeng.api.AEApi;
import appeng.api.config.Actionable; import appeng.api.config.Actionable;
import appeng.api.config.Settings; import appeng.api.config.Settings;
@@ -35,20 +36,17 @@ import appeng.core.AELog;
import appeng.me.GridAccessException; import appeng.me.GridAccessException;
import appeng.me.helpers.IGridProxyable; import appeng.me.helpers.IGridProxyable;
import appeng.me.storage.ITickingMonitor; import appeng.me.storage.ITickingMonitor;
import appeng.util.Platform;
import appeng.util.inv.ItemHandlerIterator; import appeng.util.inv.ItemHandlerIterator;
import appeng.util.inv.ItemSlot; import appeng.util.inv.ItemSlot;
import appeng.util.item.AEItemStack; import appeng.util.item.AEItemStack;
import com.google.common.primitives.Ints;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap; import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
import org.apache.commons.lang3.tuple.Pair; import net.minecraftforge.items.ItemHandlerHelper;
import java.util.ArrayList; import java.util.*;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/** /**
@@ -63,6 +61,8 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
private final InventoryCache cache; private final InventoryCache cache;
private StorageFilter mode; private StorageFilter mode;
private ItemStack stackCache = null;
ItemHandlerAdapter( IItemHandler itemHandler, IGridProxyable proxy ) ItemHandlerAdapter( IItemHandler itemHandler, IGridProxyable proxy )
{ {
this.itemHandler = itemHandler; this.itemHandler = itemHandler;
@@ -78,21 +78,28 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
@Override @Override
public IAEItemStack injectItems( IAEItemStack iox, Actionable type, IActionSource src ) public IAEItemStack injectItems( IAEItemStack iox, Actionable type, IActionSource src )
{ {
// Try to reuse the cached stack
@Nullable ItemStack currentCached = stackCache;
stackCache = null;
ItemStack orgInput = iox.createItemStack(); ItemStack orgInput;
ItemStack remaining = orgInput; if( currentCached != null && iox.isSameType( currentCached ) )
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 ); // Cache is suitable, just update the count
if( remaining.isEmpty() ) orgInput = currentCached;
{ currentCached.setCount( Ints.saturatedCast( iox.getStackSize() ) );
break; }
} else
{
// We need a new stack :-(
orgInput = iox.createItemStack();
}
ItemStack remaining = ItemHandlerHelper.insertItem( this.itemHandler, orgInput, type == Actionable.SIMULATE );
// Store the stack in the cache for next time.
if (!remaining.isEmpty() && remaining != orgInput)
{
stackCache = remaining;
} }
// At this point, we still have some items left... // At this point, we still have some items left...
@@ -121,8 +128,7 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
@Override @Override
public IAEItemStack extractItems( IAEItemStack request, Actionable mode, IActionSource src ) public IAEItemStack extractItems( IAEItemStack request, Actionable mode, IActionSource src )
{ {
ItemStack requestedItemStack = request.createItemStack(); int remainingSize = Ints.saturatedCast(request.getStackSize());
int remainingSize = requestedItemStack.getCount();
// Use this to gather the requested items // Use this to gather the requested items
ItemStack gathered = ItemStack.EMPTY; ItemStack gathered = ItemStack.EMPTY;
@@ -133,7 +139,7 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
{ {
ItemStack stackInInventorySlot = this.itemHandler.getStackInSlot( i ); ItemStack stackInInventorySlot = this.itemHandler.getStackInSlot( i );
if( !Platform.itemComparisons().isSameItem( stackInInventorySlot, requestedItemStack ) ) if (!request.isSameType(stackInInventorySlot))
{ {
continue; continue;
} }
@@ -268,7 +274,7 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
private static class InventoryCache implements Iterable<ItemSlot> private static class InventoryCache implements Iterable<ItemSlot>
{ {
private IItemList<IAEItemStack> cachedAeStacks = AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList(); private IAEItemStack[] cachedAeStacks = new IAEItemStack[0];
private final IItemHandler itemHandler; private final IItemHandler itemHandler;
private final StorageFilter mode; private final StorageFilter mode;
@@ -280,7 +286,7 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
public IItemList<IAEItemStack> getAvailableItems( IItemList<IAEItemStack> out ) public IItemList<IAEItemStack> getAvailableItems( IItemList<IAEItemStack> out )
{ {
this.cachedAeStacks.forEach( out::add ); Arrays.stream( this.cachedAeStacks ).forEach( out::add );
return out; return out;
} }
@@ -292,63 +298,90 @@ class ItemHandlerAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<IAE
public List<IAEItemStack> update() public List<IAEItemStack> update()
{ {
final List<IAEItemStack> changes = new ArrayList<>(); final List<IAEItemStack> changes = new ArrayList<>();
final int slots = this.itemHandler.getSlots();
IItemList<IAEItemStack> storage = AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList(); // Make room for new slots
if( slots > this.cachedAeStacks.length )
{
this.cachedAeStacks = Arrays.copyOf( this.cachedAeStacks, slots );
}
for( final ItemSlot is : this ) for( final ItemSlot is : this )
{ {
final ItemStack stackInSlot = !is.isExtractable() && this.getMode() == StorageFilter.EXTRACTABLE_ONLY ? ItemStack.EMPTY : is.getItemStack(); // Save the old stuff
if( !stackInSlot.isEmpty() ) final IAEItemStack oldAeIS = this.cachedAeStacks[is.getSlot()];
{ final ItemStack newIS = !is.isExtractable() && this.getMode() == StorageFilter.EXTRACTABLE_ONLY ? ItemStack.EMPTY : is.getItemStack();
storage.add( AEItemStack.fromItemStack( stackInSlot ) );
} this.handlePossibleSlotChanges( is.getSlot(), oldAeIS, newIS, changes );
} }
// Handle cases where the number of slots actually is lower now than before
IItemList<IAEItemStack> newCachedAeStacks = AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList(); if( slots < this.cachedAeStacks.length )
Iterator<IAEItemStack> cachedAeStacksIterator = cachedAeStacks.iterator();
while ( cachedAeStacksIterator.hasNext() )
{ {
IAEItemStack cachedStack = cachedAeStacksIterator.next(); for( int slot = slots; slot < this.cachedAeStacks.length; slot++ )
IAEItemStack storedStack = storage.findPrecise( cachedStack );
if( storedStack == null )
{ {
changes.add( cachedStack.setStackSize( -cachedStack.getStackSize() ) ); final IAEItemStack aeStack = this.cachedAeStacks[slot];
}
else if( aeStack != null )
{
newCachedAeStacks.add( storedStack );
if( cachedStack.getStackSize() != storedStack.getStackSize() )
{ {
handleStackSizeChanged( cachedStack, storedStack, changes ); final IAEItemStack a = aeStack.copy();
a.setStackSize( -a.getStackSize() );
changes.add( a );
} }
} }
}
for( IAEItemStack storedStack : storage ) // Reduce the cache size
{ this.cachedAeStacks = Arrays.copyOf( this.cachedAeStacks, slots );
if( cachedAeStacks.findPrecise( storedStack ) == null )
{
newCachedAeStacks.add( storedStack );
changes.add( storedStack.copy() );
}
} }
this.cachedAeStacks = newCachedAeStacks;
return changes; return changes;
} }
private void handleStackSizeChanged( IAEItemStack cachedStack, IAEItemStack storedStack, List<IAEItemStack> 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 // Still the same item, but amount might have changed
final long diff = storedStack.getStackSize() - cachedStack.getStackSize(); final long diff = newIS.getCount() - oldAeIS.getStackSize();
if( diff != 0 ) if( diff != 0 )
{ {
final IAEItemStack diffStack = cachedStack.copy(); final IAEItemStack stack = oldAeIS.copy();
diffStack.setStackSize( diff ); stack.setStackSize( newIS.getCount() );
changes.add( diffStack );
this.cachedAeStacks[slot] = stack;
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 );
// 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] );
} }
} }
@@ -1,5 +1,6 @@
package appeng.parts.misc; package appeng.parts.misc;
import javax.annotation.Nullable;
import appeng.api.AEApi; import appeng.api.AEApi;
import appeng.api.config.Actionable; import appeng.api.config.Actionable;
import appeng.api.networking.security.IActionSource; import appeng.api.networking.security.IActionSource;
@@ -16,15 +17,15 @@ import appeng.me.GridAccessException;
import appeng.me.helpers.IGridProxyable; import appeng.me.helpers.IGridProxyable;
import appeng.me.storage.ITickingMonitor; import appeng.me.storage.ITickingMonitor;
import appeng.util.item.AEItemStack; import appeng.util.item.AEItemStack;
import com.google.common.primitives.Ints;
import com.jaquadro.minecraft.storagedrawers.api.capabilities.IItemRepository; import com.jaquadro.minecraft.storagedrawers.api.capabilities.IItemRepository;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap; import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import java.util.ArrayList; import java.util.*;
import java.util.Iterator; import java.util.stream.Collectors;
import java.util.List;
import java.util.Map;
/** /**
* Wraps an Item Repository in such a way that it can be used as an IMEInventory for items. * Wraps an Item Repository in such a way that it can be used as an IMEInventory for items.
@@ -39,6 +40,7 @@ class ItemRepositoryAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<
private final IGridProxyable proxyable; private final IGridProxyable proxyable;
private final InventoryCache cache; private final InventoryCache cache;
private ItemStack stackCache;
ItemRepositoryAdapter( IItemRepository itemRepository, IGridProxyable proxy ) ItemRepositoryAdapter( IItemRepository itemRepository, IGridProxyable proxy )
{ {
@@ -50,12 +52,29 @@ class ItemRepositoryAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<
@Override @Override
public IAEItemStack injectItems( IAEItemStack iox, Actionable type, IActionSource src ) public IAEItemStack injectItems( IAEItemStack iox, Actionable type, IActionSource src )
{ {
ItemStack orgInput = iox.createItemStack(); // Try to reuse the cached stack
ItemStack remaining = orgInput; @Nullable ItemStack currentCached = stackCache;
stackCache = null;
boolean simulate = ( type == Actionable.SIMULATE ); ItemStack orgInput;
if( currentCached != null && iox.isSameType( currentCached ) )
{
// Cache is suitable, just update the count
orgInput = currentCached;
currentCached.setCount( Ints.saturatedCast( iox.getStackSize() ) );
}
else
{
// We need a new stack :-(
orgInput = iox.createItemStack();
}
ItemStack remaining = this.itemRepository.insertItem( orgInput, type == Actionable.SIMULATE );
remaining = this.itemRepository.insertItem( remaining, simulate ); // Store the stack in the cache for next time.
if (!remaining.isEmpty() && remaining != orgInput)
{
stackCache = remaining;
}
// At this point, we still have some items left... // At this point, we still have some items left...
if( remaining == orgInput ) if( remaining == orgInput )
@@ -83,12 +102,11 @@ class ItemRepositoryAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<
@Override @Override
public IAEItemStack extractItems( IAEItemStack request, Actionable mode, IActionSource src ) public IAEItemStack extractItems( IAEItemStack request, Actionable mode, IActionSource src )
{ {
ItemStack requestedItemStack = request.getDefinition(); int remainingSize = Ints.saturatedCast(request.getStackSize());
int remainingSize = (int) Math.min( Integer.MAX_VALUE, request.getStackSize() );
final boolean simulate = ( mode == Actionable.SIMULATE ); final boolean simulate = ( mode == Actionable.SIMULATE );
ItemStack extracted = this.itemRepository.extractItem( requestedItemStack, remainingSize, simulate ); ItemStack extracted = this.itemRepository.extractItem( request.getDefinition(), remainingSize, simulate );
if( extracted.getCount() > remainingSize ) if( extracted.getCount() > remainingSize )
{ {
@@ -184,7 +202,7 @@ class ItemRepositoryAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<
private static class InventoryCache private static class InventoryCache
{ {
private IItemList<IAEItemStack> cachedAeStacks = AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList(); private IAEItemStack[] cachedAeStacks = new IAEItemStack[0];
private final IItemRepository iItemRepository; private final IItemRepository iItemRepository;
public InventoryCache( IItemRepository iItemRepository ) public InventoryCache( IItemRepository iItemRepository )
@@ -194,7 +212,7 @@ class ItemRepositoryAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<
public IItemList<IAEItemStack> getAvailableItems( IItemList<IAEItemStack> out ) public IItemList<IAEItemStack> getAvailableItems( IItemList<IAEItemStack> out )
{ {
this.cachedAeStacks.forEach( out::add ); Arrays.stream( this.cachedAeStacks ).forEach( out::add );
return out; return out;
} }
@@ -202,54 +220,93 @@ class ItemRepositoryAdapter implements IMEInventory<IAEItemStack>, IBaseMonitor<
{ {
final List<IAEItemStack> changes = new ArrayList<>(); final List<IAEItemStack> changes = new ArrayList<>();
IItemList<IAEItemStack> storage = AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList(); List<IAEItemStack> out = this.iItemRepository.getAllItems().stream().map( s -> AEItemStack.fromItemStack( s.itemPrototype ).setStackSize( s.count ) ).collect( Collectors.toList() );
this.iItemRepository.getAllItems().stream().map( s -> AEItemStack.fromItemStack( s.itemPrototype ).setStackSize( s.count ) ).forEach( storage::add );
IItemList<IAEItemStack> newCachedAeStacks = AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList(); final int size = out.size();
Iterator<IAEItemStack> cachedAeStacksIterator = cachedAeStacks.iterator(); // Make room for new slots
while ( cachedAeStacksIterator.hasNext() ) if( size > this.cachedAeStacks.length )
{ {
IAEItemStack cachedStack = cachedAeStacksIterator.next(); this.cachedAeStacks = Arrays.copyOf( this.cachedAeStacks, size );
IAEItemStack storedStack = storage.findPrecise( cachedStack ); }
if( storedStack == null )
for( int x = 0; x < size; x++ )
{
// Save the old stuff
final IAEItemStack oldAeIS = this.cachedAeStacks[x];
final IAEItemStack newIS = out.get( x );
this.handlePossibleSlotChanges( x, oldAeIS, newIS, changes );
}
// Handle cases where the number of slots actually is lower now than before
if( size < this.cachedAeStacks.length )
{
for( int x = 0; x < this.cachedAeStacks.length; x++ )
{ {
changes.add( cachedStack.setStackSize( -cachedStack.getStackSize() ) ); final IAEItemStack aeStack = this.cachedAeStacks[x];
}
else if( aeStack != null )
{
newCachedAeStacks.add( storedStack );
if( cachedStack.getStackSize() != storedStack.getStackSize() )
{ {
handleStackSizeChanged( cachedStack, storedStack, changes ); final IAEItemStack a = aeStack.copy();
a.setStackSize( -a.getStackSize() );
changes.add( a );
} }
} }
}
for( IAEItemStack storedStack : storage ) // Reduce the cache size
{ this.cachedAeStacks = Arrays.copyOf( this.cachedAeStacks, size );
if( cachedAeStacks.findPrecise( storedStack ) == null )
{
newCachedAeStacks.add( storedStack );
changes.add( storedStack.copy() );
}
} }
this.cachedAeStacks = newCachedAeStacks;
return changes; return changes;
} }
private void handleStackSizeChanged( IAEItemStack cachedStack, IAEItemStack storedStack, List<IAEItemStack> changes ) private void handlePossibleSlotChanges( int slot, IAEItemStack oldAeIS, IAEItemStack 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, IAEItemStack newIS, List<IAEItemStack> changes )
{ {
// Still the same item, but amount might have changed // Still the same item, but amount might have changed
final long diff = storedStack.getStackSize() - cachedStack.getStackSize(); final long diff = newIS.getStackSize() - oldAeIS.getStackSize();
if( diff != 0 ) if( diff != 0 )
{ {
final IAEItemStack diffStack = cachedStack.copy(); final IAEItemStack stack = oldAeIS.copy();
diffStack.setStackSize( diff ); stack.setStackSize( newIS.getStackSize() );
changes.add( diffStack );
this.cachedAeStacks[slot] = stack;
final IAEItemStack a = stack.copy();
a.setStackSize( diff );
changes.add( a );
}
}
private void handleItemChanged( int slot, IAEItemStack oldAeIS, IAEItemStack newIS, List<IAEItemStack> changes )
{
// Completely different item
this.cachedAeStacks[slot] = 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 );
}
// 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] );
} }
} }
@@ -52,7 +52,7 @@ public class ItemHandlerIterator implements Iterator<ItemSlot>
{ {
throw new NoSuchElementException(); throw new NoSuchElementException();
} }
this.itemSlot.setExtractable( !this.itemHandler.extractItem( this.slot, 1, true ).isEmpty() ); this.itemSlot.setExtractable( !this.itemHandler.extractItem( this.slot, Integer.MAX_VALUE, true ).isEmpty() );
this.itemSlot.setItemStack( this.itemHandler.getStackInSlot( this.slot ) ); this.itemSlot.setItemStack( this.itemHandler.getStackInSlot( this.slot ) );
this.itemSlot.setSlot( this.slot ); this.itemSlot.setSlot( this.slot );
this.slot++; this.slot++;