Merge pull request #1337 from thatsIch/e-1333-recipe-sorter

Fixes #1333: Updated old code parts related to recipes
This commit is contained in:
thatsIch
2015-04-28 19:44:55 +02:00
3 changed files with 63 additions and 47 deletions
+8 -3
View File
@@ -22,7 +22,6 @@ package appeng.core;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.util.WeightedRandomChestContent; import net.minecraft.util.WeightedRandomChestContent;
import net.minecraft.world.biome.BiomeGenBase; import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.ChestGenHooks; import net.minecraftforge.common.ChestGenHooks;
@@ -562,10 +561,16 @@ public final class Registration
registration.registerAchievements(); registration.registerAchievements();
if( AEConfig.instance.isFeatureEnabled( AEFeature.enableDisassemblyCrafting ) ) if( AEConfig.instance.isFeatureEnabled( AEFeature.enableDisassemblyCrafting ) )
CraftingManager.getInstance().getRecipeList().add( new DisassembleRecipe() ); {
GameRegistry.addRecipe( new DisassembleRecipe() );
RecipeSorter.register( "appliedenergistics2:disassemble", DisassembleRecipe.class, Category.SHAPELESS, "after:minecraft:shapeless" );
}
if( AEConfig.instance.isFeatureEnabled( AEFeature.enableFacadeCrafting ) ) if( AEConfig.instance.isFeatureEnabled( AEFeature.enableFacadeCrafting ) )
CraftingManager.getInstance().getRecipeList().add( new FacadeRecipe() ); {
GameRegistry.addRecipe( new FacadeRecipe() );
RecipeSorter.register( "appliedenergistics2:facade", FacadeRecipe.class, Category.SHAPED, "after:minecraft:shaped" );
}
} }
public void postInit( FMLPostInitializationEvent event ) public void postInit( FMLPostInitializationEvent event )
@@ -79,9 +79,7 @@ public enum AEFeature
AEFeature( String cat ) AEFeature( String cat )
{ {
this.category = cat; this(cat, true);
this.isVisible = !this.name().equals( "Core" );
this.defaultValue = true;
} }
AEFeature( String cat, boolean defaultValue ) AEFeature( String cat, boolean defaultValue )
@@ -21,12 +21,17 @@ package appeng.recipes.game;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCrafting; import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe; import net.minecraft.item.crafting.IRecipe;
import net.minecraft.world.World; import net.minecraft.world.World;
import com.google.common.base.Optional;
import appeng.api.AEApi; import appeng.api.AEApi;
import appeng.api.definitions.IBlocks; import appeng.api.definitions.IBlocks;
import appeng.api.definitions.IDefinitions; import appeng.api.definitions.IDefinitions;
@@ -39,109 +44,116 @@ import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList; import appeng.api.storage.data.IItemList;
public class DisassembleRecipe implements IRecipe public final class DisassembleRecipe implements IRecipe
{ {
private static final ItemStack MISMATCHED_STACK = null;
private final IMaterials mats;
private final IItems items;
private final IBlocks blocks;
private final Map<IItemDefinition, IItemDefinition> cellMappings; private final Map<IItemDefinition, IItemDefinition> cellMappings;
private final Map<IItemDefinition, IItemDefinition> nonCellMappings; private final Map<IItemDefinition, IItemDefinition> nonCellMappings;
public DisassembleRecipe() public DisassembleRecipe()
{ {
final IDefinitions definitions = AEApi.instance().definitions(); final IDefinitions definitions = AEApi.instance().definitions();
final IBlocks blocks = definitions.blocks();
final IItems items = definitions.items();
final IMaterials mats = definitions.materials();
this.blocks = definitions.blocks();
this.items = definitions.items();
this.mats = definitions.materials();
this.cellMappings = new HashMap<IItemDefinition, IItemDefinition>( 4 ); this.cellMappings = new HashMap<IItemDefinition, IItemDefinition>( 4 );
this.nonCellMappings = new HashMap<IItemDefinition, IItemDefinition>( 5 ); this.nonCellMappings = new HashMap<IItemDefinition, IItemDefinition>( 5 );
this.cellMappings.put( this.items.cell1k(), this.mats.cell1kPart() ); this.cellMappings.put( items.cell1k(), mats.cell1kPart() );
this.cellMappings.put( this.items.cell4k(), this.mats.cell4kPart() ); this.cellMappings.put( items.cell4k(), mats.cell4kPart() );
this.cellMappings.put( this.items.cell16k(), this.mats.cell16kPart() ); this.cellMappings.put( items.cell16k(), mats.cell16kPart() );
this.cellMappings.put( this.items.cell64k(), this.mats.cell64kPart() ); this.cellMappings.put( items.cell64k(), mats.cell64kPart() );
this.nonCellMappings.put( this.items.encodedPattern(), this.mats.blankPattern() ); this.nonCellMappings.put( items.encodedPattern(), mats.blankPattern() );
this.nonCellMappings.put( this.blocks.craftingStorage1k(), this.mats.cell1kPart() ); this.nonCellMappings.put( blocks.craftingStorage1k(), mats.cell1kPart() );
this.nonCellMappings.put( this.blocks.craftingStorage4k(), this.mats.cell4kPart() ); this.nonCellMappings.put( blocks.craftingStorage4k(), mats.cell4kPart() );
this.nonCellMappings.put( this.blocks.craftingStorage16k(), this.mats.cell16kPart() ); this.nonCellMappings.put( blocks.craftingStorage16k(), mats.cell16kPart() );
this.nonCellMappings.put( this.blocks.craftingStorage64k(), this.mats.cell64kPart() ); this.nonCellMappings.put( blocks.craftingStorage64k(), mats.cell64kPart() );
} }
@Override @Override
public boolean matches( InventoryCrafting inv, World w ) public boolean matches( InventoryCrafting inv, World w )
{ {
return this.getOutput( inv, false ) != null; return this.getOutput( inv ) != null;
} }
private ItemStack getOutput( InventoryCrafting inv, boolean createFacade ) @Nullable
private ItemStack getOutput( IInventory inventory )
{ {
ItemStack hasCell = null; int itemCount = 0;
ItemStack output = MISMATCHED_STACK;
for( int x = 0; x < inv.getSizeInventory(); x++ ) for( int slotIndex = 0; slotIndex < inventory.getSizeInventory(); slotIndex++ )
{ {
ItemStack is = inv.getStackInSlot( x ); ItemStack stackInSlot = inventory.getStackInSlot( slotIndex );
if( is != null ) if( stackInSlot != null )
{ {
if( hasCell != null ) // needs a single input in the recipe
return null; itemCount++;
if ( itemCount > 1 )
return MISMATCHED_STACK;
hasCell = this.getCellOutput( is ); // handle storage cells
for( ItemStack storageCellStack : this.getCellOutput( stackInSlot ).asSet() )
// make sure the storage cell is empty...
if( hasCell != null )
{ {
IMEInventory<IAEItemStack> cellInv = AEApi.instance().registries().cell().getCellInventory( is, null, StorageChannel.ITEMS ); // make sure the storage cell stackInSlot empty...
IMEInventory<IAEItemStack> cellInv = AEApi.instance().registries().cell().getCellInventory( stackInSlot, null, StorageChannel.ITEMS );
if( cellInv != null ) if( cellInv != null )
{ {
IItemList<IAEItemStack> list = cellInv.getAvailableItems( StorageChannel.ITEMS.createList() ); IItemList<IAEItemStack> list = cellInv.getAvailableItems( StorageChannel.ITEMS.createList() );
if( !list.isEmpty() ) if( !list.isEmpty() )
return null; return null;
} }
output = storageCellStack;
} }
hasCell = this.getNonCellOutput( is ); // handle crafting storage blocks
for( ItemStack craftingStorageStack : this.getNonCellOutput( stackInSlot ).asSet() )
if( hasCell == null ) {
return null; output = craftingStorageStack;
}
} }
} }
return hasCell; return output;
} }
private ItemStack getCellOutput( ItemStack compared ) @Nonnull
private Optional<ItemStack> getCellOutput( ItemStack compared )
{ {
for( Map.Entry<IItemDefinition, IItemDefinition> entry : this.cellMappings.entrySet() ) for( Map.Entry<IItemDefinition, IItemDefinition> entry : this.cellMappings.entrySet() )
{ {
if( entry.getKey().isSameAs( compared ) ) if( entry.getKey().isSameAs( compared ) )
{ {
return entry.getValue().maybeStack( 1 ).get(); return entry.getValue().maybeStack( 1 );
} }
} }
return null; return Optional.absent();
} }
private ItemStack getNonCellOutput( ItemStack compared ) @Nonnull
private Optional<ItemStack> getNonCellOutput( ItemStack compared )
{ {
for( Map.Entry<IItemDefinition, IItemDefinition> entry : this.nonCellMappings.entrySet() ) for( Map.Entry<IItemDefinition, IItemDefinition> entry : this.nonCellMappings.entrySet() )
{ {
if( entry.getKey().isSameAs( compared ) ) if( entry.getKey().isSameAs( compared ) )
{ {
return entry.getValue().maybeStack( 1 ).get(); return entry.getValue().maybeStack( 1 );
} }
} }
return null; return Optional.absent();
} }
@Nullable
@Override @Override
public ItemStack getCraftingResult( InventoryCrafting inv ) public ItemStack getCraftingResult( InventoryCrafting inv )
{ {
return this.getOutput( inv, true ); return this.getOutput( inv );
} }
@Override @Override
@@ -150,6 +162,7 @@ public class DisassembleRecipe implements IRecipe
return 1; return 1;
} }
@Nullable
@Override @Override
public ItemStack getRecipeOutput() // no default output.. public ItemStack getRecipeOutput() // no default output..
{ {