Molecular Assembler Single Pattern Mode "works"
This commit is contained in:
+149
-9
@@ -1,12 +1,18 @@
|
||||
package appeng.helpers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.CraftingManager;
|
||||
import net.minecraft.item.crafting.IRecipe;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.world.World;
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.crafting.ICraftingPatternDetails;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.container.ContainerNull;
|
||||
@@ -21,8 +27,80 @@ public class PatternHelper implements ICraftingPatternDetails
|
||||
ItemStack correctOutput;
|
||||
IRecipe standardRecipe;
|
||||
|
||||
IAEItemStack inputs[];
|
||||
IAEItemStack outputs[];
|
||||
|
||||
boolean isCrafting = false;
|
||||
|
||||
class TestLookup
|
||||
{
|
||||
|
||||
final int slot;
|
||||
final int ref;
|
||||
final int hash;
|
||||
|
||||
public TestLookup(int slot, ItemStack i) {
|
||||
this( slot, i.getItem(), i.getItemDamage() );
|
||||
}
|
||||
|
||||
public TestLookup(int slot, Item item, int dmg) {
|
||||
this.slot = slot;
|
||||
ref = (dmg << Platform.DEF_OFFSET) | (Item.getIdFromItem( item ) & 0xffff);
|
||||
int offset = 3 * slot;
|
||||
hash = (ref << offset) | (ref >> (offset + 32));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
TestLookup b = (TestLookup) obj;
|
||||
return b.slot == slot && b.ref == ref;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
enum TestStatus
|
||||
{
|
||||
ACCEPT, DECLINE, TEST
|
||||
};
|
||||
|
||||
HashSet<TestLookup> failCache = new HashSet();
|
||||
HashSet<TestLookup> passCache = new HashSet();
|
||||
|
||||
private void markItemAs(int slotIndex, ItemStack i, TestStatus b)
|
||||
{
|
||||
if ( b == TestStatus.TEST ||i.hasTagCompound())
|
||||
return;
|
||||
|
||||
(b == TestStatus.ACCEPT ? passCache : failCache).add( new TestLookup( slotIndex, i ) );
|
||||
}
|
||||
|
||||
private TestStatus getStatus(int slotIndex, ItemStack i)
|
||||
{
|
||||
if ( crafting.getStackInSlot( slotIndex ) == null )
|
||||
return i == null ? TestStatus.ACCEPT : TestStatus.DECLINE;
|
||||
|
||||
if ( i == null )
|
||||
return TestStatus.DECLINE;
|
||||
|
||||
if ( i.hasTagCompound() )
|
||||
return TestStatus.TEST;
|
||||
|
||||
if ( passCache.contains( new TestLookup( slotIndex, i ) ) )
|
||||
return TestStatus.ACCEPT;
|
||||
|
||||
if ( failCache.contains( new TestLookup( slotIndex, i ) ) )
|
||||
return TestStatus.DECLINE;
|
||||
|
||||
return TestStatus.TEST;
|
||||
}
|
||||
|
||||
public PatternHelper(ItemStack is, World w) {
|
||||
NBTTagCompound encodedValue = is.getTagCompound();
|
||||
|
||||
@@ -36,29 +114,76 @@ public class PatternHelper implements ICraftingPatternDetails
|
||||
if ( isCrafting == false )
|
||||
throw new RuntimeException( "Only crafting recipes supported." );
|
||||
|
||||
List<IAEItemStack> in = new ArrayList();
|
||||
List<IAEItemStack> out = new ArrayList();
|
||||
|
||||
for (int x = 0; x < inTag.tagCount(); x++)
|
||||
{
|
||||
ItemStack gs = ItemStack.loadItemStackFromNBT( inTag.getCompoundTagAt( x ) );
|
||||
crafting.setInventorySlotContents( x, gs );
|
||||
|
||||
if ( gs != null && (!isCrafting || !gs.hasTagCompound()) )
|
||||
{
|
||||
markItemAs( x, gs, TestStatus.ACCEPT );
|
||||
in.add( AEApi.instance().storage().createItemStack( gs ) );
|
||||
}
|
||||
|
||||
testFrame.setInventorySlotContents( x, gs );
|
||||
}
|
||||
|
||||
standardRecipe = Platform.findMatchingRecipe( crafting, w );
|
||||
correctOutput = standardRecipe.getCraftingResult( crafting );
|
||||
|
||||
if ( isCrafting )
|
||||
{
|
||||
out.add( AEApi.instance().storage().createItemStack( correctOutput ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int x = 0; x < outTag.tagCount(); x++)
|
||||
{
|
||||
ItemStack gs = ItemStack.loadItemStackFromNBT( inTag.getCompoundTagAt( x ) );
|
||||
if ( gs != null )
|
||||
out.add( AEApi.instance().storage().createItemStack( gs ) );
|
||||
}
|
||||
}
|
||||
|
||||
outputs = out.toArray( new IAEItemStack[out.size()] );
|
||||
inputs = in.toArray( new IAEItemStack[in.size()] );
|
||||
|
||||
}
|
||||
|
||||
public boolean isValidItemForSlot(int slotIndex, ItemStack i, World w)
|
||||
{
|
||||
testFrame.setInventorySlotContents( slotIndex, i );
|
||||
boolean result = standardRecipe.matches( testFrame, w );
|
||||
TestStatus result = getStatus( slotIndex, i );
|
||||
|
||||
if ( result )
|
||||
switch (result)
|
||||
{
|
||||
case ACCEPT:
|
||||
return true;
|
||||
case DECLINE:
|
||||
return false;
|
||||
case TEST:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if ( !isCrafting )
|
||||
return false;
|
||||
|
||||
for (int x = 0; x < crafting.getSizeInventory(); x++)
|
||||
testFrame.setInventorySlotContents( x, crafting.getStackInSlot( x ) );
|
||||
|
||||
testFrame.setInventorySlotContents( slotIndex, i );
|
||||
|
||||
if ( standardRecipe.matches( testFrame, w ) )
|
||||
{
|
||||
ItemStack testOutput = standardRecipe.getCraftingResult( testFrame );
|
||||
|
||||
if ( Platform.isSameItemPrecise( correctOutput, testOutput ) )
|
||||
{
|
||||
testFrame.setInventorySlotContents( slotIndex, crafting.getStackInSlot( slotIndex ) );
|
||||
markItemAs( slotIndex, i, TestStatus.ACCEPT );
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -69,13 +194,30 @@ public class PatternHelper implements ICraftingPatternDetails
|
||||
if ( Platform.isSameItemPrecise( correctOutput, testOutput ) )
|
||||
{
|
||||
testFrame.setInventorySlotContents( slotIndex, crafting.getStackInSlot( slotIndex ) );
|
||||
markItemAs( slotIndex, i, TestStatus.ACCEPT );
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
markItemAs( slotIndex, i, TestStatus.DECLINE );
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getOutput(InventoryCrafting craftingInv, World w)
|
||||
{
|
||||
for (int x = 0; x < craftingInv.getSizeInventory(); x++)
|
||||
{
|
||||
if ( !isValidItemForSlot( x, craftingInv.getStackInSlot( x ), w ) )
|
||||
return null;
|
||||
}
|
||||
|
||||
if ( outputs != null && outputs.length > 0 )
|
||||
return outputs[0].getItemStack();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canSubstitute()
|
||||
{
|
||||
@@ -86,21 +228,19 @@ public class PatternHelper implements ICraftingPatternDetails
|
||||
@Override
|
||||
public boolean isCraftable()
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
return isCrafting;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEItemStack[] getInputs()
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
return inputs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEItemStack[] getOutputs()
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
return outputs;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user