2014-06-06 06:26:01 +02:00
|
|
|
package appeng.crafting;
|
|
|
|
|
|
|
|
import appeng.api.AEApi;
|
|
|
|
import appeng.api.config.Actionable;
|
|
|
|
import appeng.api.networking.security.BaseActionSource;
|
|
|
|
import appeng.api.storage.IMEInventory;
|
|
|
|
import appeng.api.storage.StorageChannel;
|
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
|
|
|
import appeng.api.storage.data.IItemList;
|
|
|
|
|
|
|
|
public class MECraftingInventory implements IMEInventory<IAEItemStack>
|
|
|
|
{
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
final MECraftingInventory par;
|
|
|
|
|
2014-06-06 06:26:01 +02:00
|
|
|
final IMEInventory<IAEItemStack> target;
|
|
|
|
final IItemList<IAEItemStack> localCache;
|
2014-06-16 07:48:43 +02:00
|
|
|
|
|
|
|
final boolean logExtracted;
|
2014-06-06 06:26:01 +02:00
|
|
|
final IItemList<IAEItemStack> extractedCache;
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
final boolean logInjections;
|
|
|
|
final IItemList<IAEItemStack> injectedCache;
|
|
|
|
|
|
|
|
final boolean logMissing;
|
|
|
|
final IItemList<IAEItemStack> missingCache;
|
|
|
|
|
2014-06-19 08:28:45 +02:00
|
|
|
public MECraftingInventory() {
|
|
|
|
localCache = AEApi.instance().storage().createItemList();
|
|
|
|
extractedCache = null;
|
|
|
|
injectedCache = null;
|
|
|
|
missingCache = null;
|
|
|
|
this.logExtracted = false;
|
|
|
|
this.logInjections = false;
|
|
|
|
this.logMissing = false;
|
|
|
|
target = null;
|
|
|
|
par = null;
|
|
|
|
}
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
public MECraftingInventory(MECraftingInventory parrent) {
|
|
|
|
this.target = parrent;
|
|
|
|
this.logExtracted = parrent.logExtracted;
|
|
|
|
this.logInjections = parrent.logInjections;
|
|
|
|
this.logMissing = parrent.logMissing;
|
|
|
|
|
2014-06-19 08:28:45 +02:00
|
|
|
if ( logMissing )
|
|
|
|
missingCache = AEApi.instance().storage().createItemList();
|
|
|
|
else
|
|
|
|
missingCache = null;
|
|
|
|
|
|
|
|
if ( logExtracted )
|
|
|
|
extractedCache = AEApi.instance().storage().createItemList();
|
|
|
|
else
|
|
|
|
extractedCache = null;
|
|
|
|
|
|
|
|
if ( logInjections )
|
|
|
|
injectedCache = AEApi.instance().storage().createItemList();
|
|
|
|
else
|
|
|
|
injectedCache = null;
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
localCache = target.getAvailableItems( AEApi.instance().storage().createItemList() );
|
|
|
|
|
|
|
|
par = parrent;
|
|
|
|
}
|
|
|
|
|
|
|
|
public MECraftingInventory(IMEInventory<IAEItemStack> target, boolean logExtracted, boolean logInjections, boolean logMissing) {
|
2014-06-06 06:26:01 +02:00
|
|
|
this.target = target;
|
2014-06-16 07:48:43 +02:00
|
|
|
this.logExtracted = logExtracted;
|
|
|
|
this.logInjections = logInjections;
|
|
|
|
this.logMissing = logMissing;
|
2014-06-19 08:28:45 +02:00
|
|
|
|
|
|
|
if ( logMissing )
|
|
|
|
missingCache = AEApi.instance().storage().createItemList();
|
|
|
|
else
|
|
|
|
missingCache = null;
|
|
|
|
|
|
|
|
if ( logExtracted )
|
|
|
|
extractedCache = AEApi.instance().storage().createItemList();
|
|
|
|
else
|
|
|
|
extractedCache = null;
|
|
|
|
|
|
|
|
if ( logInjections )
|
|
|
|
injectedCache = AEApi.instance().storage().createItemList();
|
|
|
|
else
|
|
|
|
injectedCache = null;
|
|
|
|
|
2014-06-06 06:26:01 +02:00
|
|
|
localCache = target.getAvailableItems( AEApi.instance().storage().createItemList() );
|
2014-06-16 07:48:43 +02:00
|
|
|
par = null;
|
2014-06-06 06:26:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IAEItemStack injectItems(IAEItemStack input, Actionable mode, BaseActionSource src)
|
|
|
|
{
|
|
|
|
if ( input == null )
|
|
|
|
return null;
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
if ( mode == Actionable.MODULATE )
|
|
|
|
{
|
|
|
|
if ( logInjections )
|
|
|
|
injectedCache.add( input );
|
2014-06-06 06:26:01 +02:00
|
|
|
localCache.add( input );
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
2014-06-06 06:26:01 +02:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IAEItemStack extractItems(IAEItemStack request, Actionable mode, BaseActionSource src)
|
|
|
|
{
|
|
|
|
if ( request == null )
|
|
|
|
return null;
|
|
|
|
|
|
|
|
IAEItemStack list = localCache.findPrecise( request );
|
|
|
|
if ( list == null || list.getStackSize() == 0 )
|
|
|
|
return null;
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
if ( mode == Actionable.MODULATE && logExtracted )
|
2014-06-06 06:26:01 +02:00
|
|
|
extractedCache.add( request );
|
|
|
|
|
|
|
|
if ( list.getStackSize() >= request.getStackSize() )
|
|
|
|
{
|
|
|
|
if ( mode == Actionable.MODULATE )
|
|
|
|
list.decStackSize( request.getStackSize() );
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
|
|
|
IAEItemStack ret = request.copy();
|
|
|
|
ret.setStackSize( list.getStackSize() );
|
|
|
|
if ( mode == Actionable.MODULATE )
|
|
|
|
list.reset();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IItemList<IAEItemStack> getAvailableItems(IItemList<IAEItemStack> out)
|
|
|
|
{
|
|
|
|
for (IAEItemStack is : localCache)
|
|
|
|
out.add( is );
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IItemList<IAEItemStack> getItemList()
|
|
|
|
{
|
|
|
|
return localCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public StorageChannel getChannel()
|
|
|
|
{
|
|
|
|
return StorageChannel.ITEMS;
|
|
|
|
}
|
|
|
|
|
2014-09-19 03:30:26 +02:00
|
|
|
public boolean commit(BaseActionSource src)
|
2014-06-16 07:48:43 +02:00
|
|
|
{
|
2014-09-19 03:30:26 +02:00
|
|
|
IItemList<IAEItemStack> added = AEApi.instance().storage().createItemList();
|
|
|
|
IItemList<IAEItemStack> pulled = AEApi.instance().storage().createItemList();
|
|
|
|
boolean failed = false;
|
|
|
|
|
|
|
|
if ( logExtracted )
|
|
|
|
{
|
|
|
|
for (IAEItemStack extra : extractedCache)
|
|
|
|
{
|
|
|
|
IAEItemStack result = null;
|
|
|
|
pulled.add( result = target.extractItems( extra, Actionable.MODULATE, src ) );
|
|
|
|
|
|
|
|
if ( result == null || result.getStackSize() != extra.getStackSize() )
|
|
|
|
{
|
|
|
|
failed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( failed )
|
|
|
|
{
|
|
|
|
for (IAEItemStack is : pulled)
|
|
|
|
target.injectItems( is, Actionable.MODULATE, src );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
if ( logInjections )
|
|
|
|
{
|
|
|
|
for (IAEItemStack injec : injectedCache)
|
2014-09-19 03:30:26 +02:00
|
|
|
{
|
|
|
|
IAEItemStack result = null;
|
|
|
|
added.add( result = target.injectItems( injec, Actionable.MODULATE, src ) );
|
|
|
|
|
|
|
|
if ( result != null )
|
|
|
|
{
|
|
|
|
failed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
|
|
|
|
2014-09-19 03:30:26 +02:00
|
|
|
if ( failed )
|
2014-06-16 07:48:43 +02:00
|
|
|
{
|
2014-09-19 03:30:26 +02:00
|
|
|
for (IAEItemStack is : added)
|
|
|
|
target.extractItems( is, Actionable.MODULATE, src );
|
|
|
|
|
|
|
|
for (IAEItemStack is : pulled)
|
|
|
|
target.injectItems( is, Actionable.MODULATE, src );
|
|
|
|
|
|
|
|
return false;
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( logMissing && par != null )
|
|
|
|
{
|
|
|
|
for (IAEItemStack extra : missingCache)
|
|
|
|
par.addMissing( extra );
|
|
|
|
}
|
2014-09-19 03:30:26 +02:00
|
|
|
|
|
|
|
return true;
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addMissing(IAEItemStack extra)
|
|
|
|
{
|
|
|
|
missingCache.add( extra );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ignore(IAEItemStack what)
|
|
|
|
{
|
2014-06-22 09:00:38 +02:00
|
|
|
IAEItemStack list = localCache.findPrecise( what );
|
|
|
|
if ( list != null )
|
|
|
|
list.setStackSize( 0 );
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
2014-06-06 06:26:01 +02:00
|
|
|
}
|