diff --git a/src/java/mmm/materials/MAlloyRecipe.java b/src/java/mmm/materials/MAlloyRecipe.java index 3ca6498..731dd8d 100644 --- a/src/java/mmm/materials/MAlloyRecipe.java +++ b/src/java/mmm/materials/MAlloyRecipe.java @@ -1,7 +1,15 @@ package mmm.materials; +import java.util.ArrayList; +import java.util.HashMap; + +import mmm.Mmm; +import mmm.utils.UItemId; +import net.minecraft.block.Block; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; @@ -9,6 +17,198 @@ public class MAlloyRecipe { public static final int MAX_ALLOY_INPUTS = 6; + public static final MAlloyRecipe.I_Registry REGISTRY = new MAlloyRecipe.Registry( ); + + public static interface I_Registry + { + public MAlloyRecipe getRecipe( ResourceLocation name ); + + + public ArrayList< MAlloyRecipe > getRecipes( ); + } + + private static class Registry + implements MAlloyRecipe.I_Registry + { + private final HashMap< ResourceLocation , MAlloyRecipe > byName = new HashMap<>( ); + private final ArrayList< MAlloyRecipe > list = new ArrayList<>( ); + + + @Override + public MAlloyRecipe getRecipe( final ResourceLocation name ) + { + return this.byName.get( name ); + } + + + @Override + public ArrayList< MAlloyRecipe > getRecipes( ) + { + return this.list; + } + + } + + public static class Builder + { + private ResourceLocation name; + private int burnTime; + private float xp; + private ItemStack output; + private final ItemStack[] inputs = new ItemStack[ MAlloyRecipe.MAX_ALLOY_INPUTS ]; + private int nInputs; + + + private Builder( ) + { + // EMPTY + } + + + public MAlloyRecipe.Builder setName( final String name ) + { + this.name = new ResourceLocation( Mmm.ID , name ); + return this; + } + + + public MAlloyRecipe.Builder setBurnTime( final int burnTime ) + { + this.burnTime = burnTime; + return this; + } + + + public MAlloyRecipe.Builder setExperience( final float xp ) + { + this.xp = xp; + return this; + } + + + public MAlloyRecipe.Builder setOutput( final Item item ) + { + this.output = new ItemStack( item ); + return this; + } + + + public MAlloyRecipe.Builder setOutput( final Item item , final int amount ) + { + this.output = new ItemStack( item , amount ); + return this; + } + + + public MAlloyRecipe.Builder setOutput( final Block block ) + { + this.output = new ItemStack( block ); + return this; + } + + + public MAlloyRecipe.Builder setOutput( final Block block , final int amount ) + { + this.output = new ItemStack( block , amount ); + return this; + } + + + public MAlloyRecipe.Builder setOutput( final ItemStack stack ) + { + this.output = stack; + return this; + } + + + public MAlloyRecipe.Builder addInput( final Item item ) + { + return this.addInput( new ItemStack( item ) ); + } + + + public MAlloyRecipe.Builder addInput( final Item item , final int amount ) + { + return this.addInput( new ItemStack( item , amount ) ); + } + + + public MAlloyRecipe.Builder addInput( final Block block , final int amount ) + { + return this.addInput( new ItemStack( block , amount ) ); + } + + + public MAlloyRecipe.Builder addInput( final Block block ) + { + return this.addInput( new ItemStack( block ) ); + } + + + public MAlloyRecipe.Builder addInput( final ItemStack itemStack ) + { + final int n = this.findItemType( UItemId.fromItemStack( itemStack ) ); + if ( n != -1 ) { + this.inputs[ n ].stackSize += itemStack.stackSize; + } else { + this.inputs[ this.nInputs++ ] = itemStack; + } + return this; + } + + + public MAlloyRecipe.Builder register( ) + { + final MAlloyRecipe.Registry registry = (MAlloyRecipe.Registry) MAlloyRecipe.REGISTRY; + + if ( this.name == null ) { + throw new IllegalStateException( "alloy recipe has no identifier" ); + } else if ( registry.byName.containsKey( this.name ) ) { + throw new IllegalStateException( "duplicate alloy recipe identifier " + this.name ); + } + if ( this.output == null ) { + throw new IllegalStateException( "alloy recipe has no output" ); + } + if ( this.burnTime == 0 ) { + throw new IllegalStateException( "alloy recipe has no burn time" ); + } + if ( this.nInputs == 0 ) { + throw new IllegalStateException( "alloy recipe has no inputs" ); + } + if ( this.findItemType( UItemId.fromItemStack( this.output ) ) != -1 ) { + throw new IllegalStateException( "alloy recipe needs its output to create itself" ); + } + + final ItemStack[] inputs = new ItemStack[ this.nInputs ]; + for ( int i = 0 ; i < inputs.length ; i++ ) { + inputs[ i ] = this.inputs[ i ].copy( ); + } + + final MAlloyRecipe recipe = new MAlloyRecipe( this.burnTime , this.xp , this.output.copy( ) , inputs ); + registry.byName.put( this.name , recipe ); + registry.list.add( recipe ); + this.name = null; + + return this; + } + + + private int findItemType( final UItemId id ) + { + for ( int i = 0 ; i < this.nInputs ; i++ ) { + if ( UItemId.fromItemStack( this.inputs[ i ] ).equals( id ) ) { + return i; + } + } + return -1; + } + } + + + public static MAlloyRecipe.Builder build( ) + { + return new MAlloyRecipe.Builder( ); + } public final int burnTime; public final float xp; @@ -16,7 +216,7 @@ public class MAlloyRecipe public final ItemStack[] inputs; - MAlloyRecipe( final int burnTime , final float xp , final ItemStack output , final ItemStack[] inputs ) + private MAlloyRecipe( final int burnTime , final float xp , final ItemStack output , final ItemStack[] inputs ) { if ( inputs.length < 1 || inputs.length > MAlloyRecipe.MAX_ALLOY_INPUTS ) { throw new IllegalArgumentException( "invalid alloy recipe" ); diff --git a/src/java/mmm/materials/MAlloyRecipesRegistry.java b/src/java/mmm/materials/MAlloyRecipesRegistry.java deleted file mode 100644 index 0a0b620..0000000 --- a/src/java/mmm/materials/MAlloyRecipesRegistry.java +++ /dev/null @@ -1,67 +0,0 @@ -package mmm.materials; - - -import java.util.ArrayList; -import java.util.Set; - -import com.google.common.collect.HashMultimap; - -import mmm.utils.UItemId; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; - - - -public enum MAlloyRecipesRegistry { - INSTANCE; - - private final HashMultimap< UItemId , MAlloyRecipe > recipes = HashMultimap.create( ); - - - public void addRecipe( final int burnTime , final float xp , final Item output , final Object... params ) - { - this.addRecipe( burnTime , xp , new ItemStack( output ) , params ); - } - - - public void addRecipe( final int burnTime , final float xp , final ItemStack output , final Object... params ) - { - final int nParams = params.length; - final ItemStack[] inputs = new ItemStack[ nParams ]; - for ( int i = 0 ; i < nParams ; i++ ) { - final Object param = params[ i ]; - if ( param instanceof ItemStack ) { - inputs[ i ] = (ItemStack) param; - } else if ( param instanceof Item ) { - inputs[ i ] = new ItemStack( (Item) param ); - } else { - throw new IllegalArgumentException( "invalid alloy recipe input type" ); - } - } - this.addRecipe( burnTime , xp , output , inputs ); - } - - - public void addRecipe( final int burnTime , final float xp , final ItemStack output , final ItemStack[] inputs ) - { - final MAlloyRecipe recipe = new MAlloyRecipe( burnTime , xp , output , inputs ); - this.recipes.put( UItemId.fromItemStack( output ) , recipe ); - } - - - public Set< MAlloyRecipe > getRecipe( final UItemId item ) - { - return this.recipes.get( item ); - } - - - @SideOnly( Side.CLIENT ) - public ArrayList< MAlloyRecipe > getSortedRecipes( ) - { - final ArrayList< MAlloyRecipe > output = new ArrayList<>( this.recipes.values( ) ); - output.sort( ( r1 , r2 ) -> r1.getLocalizedName( ).compareTo( r2.getLocalizedName( ) ) ); - return output; - } -} diff --git a/src/java/mmm/materials/Materials.java b/src/java/mmm/materials/Materials.java index 2b61609..dc63afb 100644 --- a/src/java/mmm/materials/Materials.java +++ b/src/java/mmm/materials/Materials.java @@ -48,7 +48,8 @@ public class Materials URegistry.addBlock( ORE_CUPRITE = new MOCuprite( ) ); // Alloy recipes - MAlloyRecipesRegistry.INSTANCE.addRecipe( 1600 , 0.1f , ITEM_COKE , Items.COAL ); + MAlloyRecipe.build( ).setName( "materials/coke" ).setBurnTime( 1600 ).setExperience( 0.1f ) + .setOutput( ITEM_COKE ).addInput( Items.COAL ).register( ); } diff --git a/src/java/mmm/tech/base/TBAlloyFurnaceContainer.java b/src/java/mmm/tech/base/TBAlloyFurnaceContainer.java index 3bbff7f..2d94dcd 100644 --- a/src/java/mmm/tech/base/TBAlloyFurnaceContainer.java +++ b/src/java/mmm/tech/base/TBAlloyFurnaceContainer.java @@ -1,10 +1,7 @@ package mmm.tech.base; -import java.util.ArrayList; - import mmm.materials.MAlloyRecipe; -import mmm.materials.MAlloyRecipesRegistry; import mmm.utils.UContainers; import mmm.utils.UInventoryDisplay; import mmm.utils.UInventoryGrid; @@ -25,8 +22,6 @@ import net.minecraft.world.World; public class TBAlloyFurnaceContainer extends Container { - private final ArrayList< MAlloyRecipe > recipes = MAlloyRecipesRegistry.INSTANCE.getSortedRecipes( ); - public final TBAlloyFurnaceTileEntity tileEntity; public final World world; public final BlockPos position; @@ -86,14 +81,14 @@ public class TBAlloyFurnaceContainer } - public void setCurrentRecipe( int index , boolean confirm ) + public void setCurrentRecipe( final int index , final boolean confirm ) { - if ( index < 0 || index >= this.recipes.size( ) ) { + if ( index < 0 || index >= MAlloyRecipe.REGISTRY.getRecipes( ).size( ) ) { // XXX log return; } - MAlloyRecipe recipe = this.recipes.get( index ); + final MAlloyRecipe recipe = MAlloyRecipe.REGISTRY.getRecipes( ).get( index ); this.recipe.clear( ); for ( int i = 0 ; i < recipe.inputs.length ; i++ ) { this.recipe.setInventorySlotContents( i , recipe.inputs[ i ] ); @@ -102,7 +97,7 @@ public class TBAlloyFurnaceContainer if ( confirm ) { this.tileEntity.currentRecipe = index; - URegistry.network.sendToAll( new TBAlloyFurnaceMessage( position , index , true ) ); + URegistry.network.sendToAll( new TBAlloyFurnaceMessage( this.position , index , true ) ); } } diff --git a/src/java/mmm/tech/base/TBAlloyFurnaceGui.java b/src/java/mmm/tech/base/TBAlloyFurnaceGui.java index b712971..0af69b0 100644 --- a/src/java/mmm/tech/base/TBAlloyFurnaceGui.java +++ b/src/java/mmm/tech/base/TBAlloyFurnaceGui.java @@ -4,7 +4,7 @@ package mmm.tech.base; import java.io.IOException; import mmm.Mmm; -import mmm.materials.MAlloyRecipesRegistry; +import mmm.materials.MAlloyRecipe; import mmm.utils.URegistry; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiButton; @@ -148,7 +148,7 @@ public class TBAlloyFurnaceGui boolean visible = ( this.selectedTab == Tab.CONFIG ); this.bNext.visible = this.bPrevious.visible = this.bConfirm.visible = visible; - this.bNext.enabled = this.currentRecipe < MAlloyRecipesRegistry.INSTANCE.getSortedRecipes( ).size( ) - 1; + this.bNext.enabled = this.currentRecipe < MAlloyRecipe.REGISTRY.getRecipes( ).size( ) - 1; this.bPrevious.enabled = this.currentRecipe > 0; this.bConfirm.enabled = ( this.currentRecipe != this.container.tileEntity.currentRecipe ); } diff --git a/src/java/mmm/tech/base/TechBase.java b/src/java/mmm/tech/base/TechBase.java index ce988c1..62ed93d 100644 --- a/src/java/mmm/tech/base/TechBase.java +++ b/src/java/mmm/tech/base/TechBase.java @@ -2,7 +2,7 @@ package mmm.tech.base; import mmm.Mmm; -import mmm.materials.MAlloyRecipesRegistry; +import mmm.materials.MAlloyRecipe; import mmm.utils.URegistry; import net.minecraft.init.Items; import net.minecraft.item.Item; @@ -33,8 +33,9 @@ public class TechBase URegistry.addClientMessage( TBAlloyFurnaceMessage.class ); // FIXME test, remove this later - MAlloyRecipesRegistry.INSTANCE.addRecipe( 200 , 0.05f , Items.COOKED_CHICKEN , Items.COOKED_BEEF , - Items.COOKED_PORKCHOP ); + MAlloyRecipe.build( ).setName( "test" ).setBurnTime( 200 ).setExperience( .05f ) + .setOutput( Items.COOKED_CHICKEN , 10 ).addInput( Items.COOKED_BEEF ).addInput( Items.COOKED_PORKCHOP ) + .register( ); }