2014-04-25 03:55:29 +02:00
|
|
|
package appeng.helpers;
|
|
|
|
|
2014-05-08 07:58:43 +02:00
|
|
|
import java.util.ArrayList;
|
2014-06-08 18:25:46 +02:00
|
|
|
import java.util.HashMap;
|
2014-05-08 07:58:43 +02:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-04-25 03:55:29 +02:00
|
|
|
import net.minecraft.inventory.InventoryCrafting;
|
2014-05-08 07:58:43 +02:00
|
|
|
import net.minecraft.item.Item;
|
2014-04-25 03:55:29 +02:00
|
|
|
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;
|
2014-05-08 07:58:43 +02:00
|
|
|
import appeng.api.AEApi;
|
2014-05-09 06:10:12 +02:00
|
|
|
import appeng.api.networking.crafting.ICraftingPatternDetails;
|
2014-04-25 03:55:29 +02:00
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
|
|
|
import appeng.container.ContainerNull;
|
2014-06-08 18:25:46 +02:00
|
|
|
import appeng.util.ItemSorters;
|
2014-04-25 03:55:29 +02:00
|
|
|
import appeng.util.Platform;
|
2014-06-19 08:28:45 +02:00
|
|
|
import appeng.util.item.AEItemStack;
|
2014-04-25 03:55:29 +02:00
|
|
|
|
2014-06-08 18:25:46 +02:00
|
|
|
public class PatternHelper implements ICraftingPatternDetails, Comparable<PatternHelper>
|
2014-04-25 03:55:29 +02:00
|
|
|
{
|
|
|
|
|
2014-05-09 06:10:12 +02:00
|
|
|
final ItemStack patternItem;
|
2014-06-19 08:28:45 +02:00
|
|
|
private IAEItemStack aepattern;
|
2014-04-25 03:55:29 +02:00
|
|
|
|
2014-05-09 06:10:12 +02:00
|
|
|
final InventoryCrafting crafting = new InventoryCrafting( new ContainerNull(), 3, 3 );
|
|
|
|
final InventoryCrafting testFrame = new InventoryCrafting( new ContainerNull(), 3, 3 );
|
2014-04-25 03:55:29 +02:00
|
|
|
|
2014-05-09 06:10:12 +02:00
|
|
|
final ItemStack correctOutput;
|
|
|
|
final IRecipe standardRecipe;
|
2014-05-08 07:58:43 +02:00
|
|
|
|
2014-06-08 18:25:46 +02:00
|
|
|
final IAEItemStack condencedInputs[];
|
2014-06-16 07:48:43 +02:00
|
|
|
final IAEItemStack condencedOutputs[];
|
2014-05-09 06:10:12 +02:00
|
|
|
final IAEItemStack inputs[];
|
|
|
|
final IAEItemStack outputs[];
|
|
|
|
|
|
|
|
final boolean isCrafting;
|
2014-06-08 18:25:46 +02:00
|
|
|
public int priority = 0;
|
2014-04-25 03:55:29 +02:00
|
|
|
|
2014-05-08 07:58:43 +02:00
|
|
|
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)
|
|
|
|
{
|
2014-05-09 06:10:12 +02:00
|
|
|
if ( b == TestStatus.TEST || i.hasTagCompound() )
|
2014-05-08 07:58:43 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-25 03:55:29 +02:00
|
|
|
public PatternHelper(ItemStack is, World w) {
|
|
|
|
NBTTagCompound encodedValue = is.getTagCompound();
|
|
|
|
|
|
|
|
if ( encodedValue == null )
|
2014-05-09 06:10:12 +02:00
|
|
|
throw new RuntimeException( "No pattern here!" );
|
2014-04-25 03:55:29 +02:00
|
|
|
|
|
|
|
NBTTagList inTag = encodedValue.getTagList( "in", 10 );
|
|
|
|
NBTTagList outTag = encodedValue.getTagList( "out", 10 );
|
|
|
|
isCrafting = encodedValue.getBoolean( "crafting" );
|
2014-05-09 06:10:12 +02:00
|
|
|
patternItem = is;
|
2014-06-19 08:28:45 +02:00
|
|
|
aepattern = AEItemStack.create( is );
|
2014-04-25 03:55:29 +02:00
|
|
|
|
2014-05-08 07:58:43 +02:00
|
|
|
List<IAEItemStack> in = new ArrayList();
|
|
|
|
List<IAEItemStack> out = new ArrayList();
|
|
|
|
|
2014-04-25 03:55:29 +02:00
|
|
|
for (int x = 0; x < inTag.tagCount(); x++)
|
|
|
|
{
|
|
|
|
ItemStack gs = ItemStack.loadItemStackFromNBT( inTag.getCompoundTagAt( x ) );
|
|
|
|
crafting.setInventorySlotContents( x, gs );
|
2014-05-08 07:58:43 +02:00
|
|
|
|
|
|
|
if ( gs != null && (!isCrafting || !gs.hasTagCompound()) )
|
|
|
|
{
|
|
|
|
markItemAs( x, gs, TestStatus.ACCEPT );
|
|
|
|
}
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
in.add( AEApi.instance().storage().createItemStack( gs ) );
|
2014-04-25 03:55:29 +02:00
|
|
|
testFrame.setInventorySlotContents( x, gs );
|
|
|
|
}
|
|
|
|
|
2014-05-08 07:58:43 +02:00
|
|
|
if ( isCrafting )
|
|
|
|
{
|
2014-05-20 06:13:43 +02:00
|
|
|
standardRecipe = Platform.findMatchingRecipe( crafting, w );
|
2014-07-24 17:59:57 +02:00
|
|
|
if ( standardRecipe != null )
|
|
|
|
{
|
|
|
|
correctOutput = standardRecipe.getCraftingResult( crafting );
|
|
|
|
out.add( AEApi.instance().storage().createItemStack( correctOutput ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new RuntimeException( "No pattern here!" );
|
2014-05-08 07:58:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-20 06:13:43 +02:00
|
|
|
standardRecipe = null;
|
|
|
|
correctOutput = null;
|
|
|
|
|
2014-05-08 07:58:43 +02:00
|
|
|
for (int x = 0; x < outTag.tagCount(); x++)
|
|
|
|
{
|
2014-05-23 04:41:54 +02:00
|
|
|
ItemStack gs = ItemStack.loadItemStackFromNBT( outTag.getCompoundTagAt( x ) );
|
2014-05-08 07:58:43 +02:00
|
|
|
if ( gs != null )
|
|
|
|
out.add( AEApi.instance().storage().createItemStack( gs ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
outputs = out.toArray( new IAEItemStack[out.size()] );
|
|
|
|
inputs = in.toArray( new IAEItemStack[in.size()] );
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
HashMap<IAEItemStack, IAEItemStack> tmpOutputs = new HashMap<IAEItemStack, IAEItemStack>();
|
|
|
|
for (IAEItemStack io : outputs)
|
|
|
|
{
|
|
|
|
if ( io == null )
|
|
|
|
continue;
|
2014-06-08 18:25:46 +02:00
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
IAEItemStack g = tmpOutputs.get( io );
|
|
|
|
if ( g == null )
|
|
|
|
tmpOutputs.put( io, io.copy() );
|
|
|
|
else
|
|
|
|
g.add( io );
|
|
|
|
}
|
|
|
|
|
|
|
|
HashMap<IAEItemStack, IAEItemStack> tmpInputs = new HashMap<IAEItemStack, IAEItemStack>();
|
2014-06-08 18:25:46 +02:00
|
|
|
for (IAEItemStack io : inputs)
|
|
|
|
{
|
2014-06-16 07:48:43 +02:00
|
|
|
if ( io == null )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
IAEItemStack g = tmpInputs.get( io );
|
2014-06-08 18:25:46 +02:00
|
|
|
if ( g == null )
|
2014-06-16 07:48:43 +02:00
|
|
|
tmpInputs.put( io, io.copy() );
|
2014-06-08 18:25:46 +02:00
|
|
|
else
|
|
|
|
g.add( io );
|
|
|
|
}
|
2014-07-24 17:59:57 +02:00
|
|
|
|
|
|
|
if ( tmpOutputs.isEmpty() || tmpInputs.isEmpty() )
|
|
|
|
throw new RuntimeException( "No pattern here!" );
|
|
|
|
|
2014-06-08 18:25:46 +02:00
|
|
|
int offset = 0;
|
2014-06-16 07:48:43 +02:00
|
|
|
condencedInputs = new IAEItemStack[tmpInputs.size()];
|
|
|
|
for (IAEItemStack io : tmpInputs.values())
|
2014-06-08 18:25:46 +02:00
|
|
|
condencedInputs[offset++] = io;
|
2014-06-16 07:48:43 +02:00
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
condencedOutputs = new IAEItemStack[tmpOutputs.size()];
|
|
|
|
for (IAEItemStack io : tmpOutputs.values())
|
|
|
|
condencedOutputs[offset++] = io;
|
2014-04-25 03:55:29 +02:00
|
|
|
}
|
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
synchronized public boolean isValidItemForSlot(int slotIndex, ItemStack i, World w)
|
2014-04-25 03:55:29 +02:00
|
|
|
{
|
2014-05-20 06:13:43 +02:00
|
|
|
if ( isCrafting == false )
|
2014-06-17 05:36:35 +02:00
|
|
|
{
|
2014-05-20 06:13:43 +02:00
|
|
|
throw new RuntimeException( "Only crafting recipes supported." );
|
2014-06-17 05:36:35 +02:00
|
|
|
}
|
2014-05-20 06:13:43 +02:00
|
|
|
|
2014-05-08 07:58:43 +02:00
|
|
|
TestStatus result = getStatus( slotIndex, i );
|
|
|
|
|
|
|
|
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 ) );
|
|
|
|
|
2014-04-25 03:55:29 +02:00
|
|
|
testFrame.setInventorySlotContents( slotIndex, i );
|
|
|
|
|
2014-05-08 07:58:43 +02:00
|
|
|
if ( standardRecipe.matches( testFrame, w ) )
|
2014-04-25 03:55:29 +02:00
|
|
|
{
|
|
|
|
ItemStack testOutput = standardRecipe.getCraftingResult( testFrame );
|
|
|
|
|
|
|
|
if ( Platform.isSameItemPrecise( correctOutput, testOutput ) )
|
|
|
|
{
|
|
|
|
testFrame.setInventorySlotContents( slotIndex, crafting.getStackInSlot( slotIndex ) );
|
2014-05-08 07:58:43 +02:00
|
|
|
markItemAs( slotIndex, i, TestStatus.ACCEPT );
|
2014-04-25 03:55:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ItemStack testOutput = CraftingManager.getInstance().findMatchingRecipe( testFrame, w );
|
|
|
|
|
|
|
|
if ( Platform.isSameItemPrecise( correctOutput, testOutput ) )
|
|
|
|
{
|
|
|
|
testFrame.setInventorySlotContents( slotIndex, crafting.getStackInSlot( slotIndex ) );
|
2014-05-08 07:58:43 +02:00
|
|
|
markItemAs( slotIndex, i, TestStatus.ACCEPT );
|
2014-04-25 03:55:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-08 07:58:43 +02:00
|
|
|
markItemAs( slotIndex, i, TestStatus.DECLINE );
|
2014-04-25 03:55:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-08 07:58:43 +02:00
|
|
|
@Override
|
|
|
|
public ItemStack getOutput(InventoryCrafting craftingInv, World w)
|
|
|
|
{
|
2014-05-20 06:13:43 +02:00
|
|
|
if ( isCrafting == false )
|
|
|
|
throw new RuntimeException( "Only crafting recipes supported." );
|
|
|
|
|
2014-05-08 07:58:43 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-25 03:55:29 +02:00
|
|
|
@Override
|
|
|
|
public boolean canSubstitute()
|
|
|
|
{
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isCraftable()
|
|
|
|
{
|
2014-05-08 07:58:43 +02:00
|
|
|
return isCrafting;
|
2014-04-25 03:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IAEItemStack[] getInputs()
|
|
|
|
{
|
2014-05-08 07:58:43 +02:00
|
|
|
return inputs;
|
2014-04-25 03:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IAEItemStack[] getOutputs()
|
|
|
|
{
|
2014-05-08 07:58:43 +02:00
|
|
|
return outputs;
|
2014-04-25 03:55:29 +02:00
|
|
|
}
|
2014-05-08 07:58:43 +02:00
|
|
|
|
2014-05-09 06:10:12 +02:00
|
|
|
@Override
|
|
|
|
public ItemStack getPattern()
|
|
|
|
{
|
|
|
|
return patternItem;
|
|
|
|
}
|
|
|
|
|
2014-06-08 18:25:46 +02:00
|
|
|
@Override
|
|
|
|
public IAEItemStack[] getCondencedInputs()
|
|
|
|
{
|
|
|
|
return condencedInputs;
|
|
|
|
}
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
@Override
|
|
|
|
public IAEItemStack[] getCondencedOutputs()
|
|
|
|
{
|
|
|
|
return condencedOutputs;
|
|
|
|
}
|
|
|
|
|
2014-06-08 18:25:46 +02:00
|
|
|
@Override
|
|
|
|
public int compareTo(PatternHelper o)
|
|
|
|
{
|
|
|
|
return ItemSorters.compareInt( o.priority, priority );
|
|
|
|
}
|
2014-06-16 07:48:43 +02:00
|
|
|
|
2014-06-19 08:28:45 +02:00
|
|
|
@Override
|
|
|
|
public int hashCode()
|
|
|
|
{
|
|
|
|
return aepattern.hashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj)
|
|
|
|
{
|
|
|
|
return aepattern.equals( ((PatternHelper) obj).aepattern );
|
|
|
|
}
|
2014-07-18 05:38:57 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setPriority(int priority)
|
|
|
|
{
|
|
|
|
this.priority = priority;
|
|
|
|
}
|
2014-04-25 03:55:29 +02:00
|
|
|
}
|