diff --git a/src/java/mmm/materials/MAlloyRecipe.java b/src/java/mmm/materials/MAlloyRecipe.java index 731dd8d..7ee07e3 100644 --- a/src/java/mmm/materials/MAlloyRecipe.java +++ b/src/java/mmm/materials/MAlloyRecipe.java @@ -184,7 +184,8 @@ public class MAlloyRecipe inputs[ i ] = this.inputs[ i ].copy( ); } - final MAlloyRecipe recipe = new MAlloyRecipe( this.burnTime , this.xp , this.output.copy( ) , inputs ); + final MAlloyRecipe recipe = new MAlloyRecipe( this.name , this.burnTime , this.xp , this.output.copy( ) , + inputs ); registry.byName.put( this.name , recipe ); registry.list.add( recipe ); this.name = null; @@ -210,17 +211,17 @@ public class MAlloyRecipe return new MAlloyRecipe.Builder( ); } + public final ResourceLocation name; public final int burnTime; public final float xp; public final ItemStack output; public final ItemStack[] inputs; - private MAlloyRecipe( final int burnTime , final float xp , final ItemStack output , final ItemStack[] inputs ) + private MAlloyRecipe( final ResourceLocation name , 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" ); - } + this.name = name; this.burnTime = burnTime; this.xp = xp; this.output = output; diff --git a/src/java/mmm/tech/base/TBAlloyFurnaceContainer.java b/src/java/mmm/tech/base/TBAlloyFurnaceContainer.java index 2d94dcd..d9d05b8 100644 --- a/src/java/mmm/tech/base/TBAlloyFurnaceContainer.java +++ b/src/java/mmm/tech/base/TBAlloyFurnaceContainer.java @@ -14,6 +14,7 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; +import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -81,14 +82,14 @@ public class TBAlloyFurnaceContainer } - public void setCurrentRecipe( final int index , final boolean confirm ) + public void setCurrentRecipe( final ResourceLocation name , final boolean confirm ) { - if ( index < 0 || index >= MAlloyRecipe.REGISTRY.getRecipes( ).size( ) ) { + MAlloyRecipe recipe = MAlloyRecipe.REGISTRY.getRecipe( name ); + if ( recipe == null ) { // XXX log return; } - 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 ] ); @@ -96,8 +97,8 @@ public class TBAlloyFurnaceContainer this.recipe.setInventorySlotContents( 6 , recipe.output ); if ( confirm ) { - this.tileEntity.currentRecipe = index; - URegistry.network.sendToAll( new TBAlloyFurnaceMessage( this.position , index , true ) ); + this.tileEntity.recipe = recipe; + URegistry.network.sendToAll( new TBAlloyFurnaceMessage( this.position , name , true ) ); } } diff --git a/src/java/mmm/tech/base/TBAlloyFurnaceGui.java b/src/java/mmm/tech/base/TBAlloyFurnaceGui.java index 0af69b0..2e0303e 100644 --- a/src/java/mmm/tech/base/TBAlloyFurnaceGui.java +++ b/src/java/mmm/tech/base/TBAlloyFurnaceGui.java @@ -2,6 +2,7 @@ package mmm.tech.base; import java.io.IOException; +import java.util.ArrayList; import mmm.Mmm; import mmm.materials.MAlloyRecipe; @@ -116,7 +117,7 @@ public class TBAlloyFurnaceGui } } - this.setRecipe( tileEntity.currentRecipe ); + this.setRecipe( getRecipeIndex( tileEntity.recipe ) ); } @@ -150,21 +151,29 @@ public class TBAlloyFurnaceGui 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 ); + this.bConfirm.enabled = ( this.currentRecipe != getRecipeIndex( this.container.tileEntity.recipe ) ); } } private void setRecipe( final int index ) { - this.container.setCurrentRecipe( index , false ); + ArrayList< MAlloyRecipe > list = MAlloyRecipe.REGISTRY.getRecipes( ); + MAlloyRecipe recipe = list.get( index ); + this.container.setCurrentRecipe( recipe.name , false ); URegistry.network.sendToServer( new TBAlloyFurnaceMessage( - ( (TBAlloyFurnaceContainer) this.inventorySlots ).position , index , false ) ); + ( (TBAlloyFurnaceContainer) this.inventorySlots ).position , recipe.name , false ) ); this.currentRecipe = index; this.enableConfigButtons( ); } + private static int getRecipeIndex( MAlloyRecipe recipe ) + { + return MAlloyRecipe.REGISTRY.getRecipes( ).indexOf( recipe ); + } + + @Override protected void drawGuiContainerBackgroundLayer( final float partialTicks , final int mouseX , final int mouseY ) { @@ -224,9 +233,11 @@ public class TBAlloyFurnaceGui } else if ( button == this.bPrevious ) { this.setRecipe( this.currentRecipe - 1 ); } else if ( button == this.bConfirm ) { + ArrayList< MAlloyRecipe > list = MAlloyRecipe.REGISTRY.getRecipes( ); + MAlloyRecipe recipe = list.get( this.currentRecipe ); URegistry.network.sendToServer( - new TBAlloyFurnaceMessage( this.container.tileEntity.getPos( ) , this.currentRecipe , true ) ); - this.container.tileEntity.currentRecipe = this.currentRecipe; + new TBAlloyFurnaceMessage( this.container.tileEntity.getPos( ) , recipe.name , true ) ); + this.container.tileEntity.recipe = recipe; } this.enableConfigButtons( ); diff --git a/src/java/mmm/tech/base/TBAlloyFurnaceMessage.java b/src/java/mmm/tech/base/TBAlloyFurnaceMessage.java index 21c82f8..7652d40 100644 --- a/src/java/mmm/tech/base/TBAlloyFurnaceMessage.java +++ b/src/java/mmm/tech/base/TBAlloyFurnaceMessage.java @@ -2,13 +2,16 @@ package mmm.tech.base; import io.netty.buffer.ByteBuf; +import mmm.materials.MAlloyRecipe; import mmm.utils.I_UMessage; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.inventory.Container; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import net.minecraftforge.fml.common.network.ByteBufUtils; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -18,7 +21,7 @@ public class TBAlloyFurnaceMessage implements I_UMessage { private BlockPos blockPos; - private int selectedIndex; + private ResourceLocation selected; private boolean confirm; @@ -28,10 +31,10 @@ public class TBAlloyFurnaceMessage } - public TBAlloyFurnaceMessage( final BlockPos blockPos , final int selectedIndex , final boolean confirm ) + public TBAlloyFurnaceMessage( final BlockPos blockPos , final ResourceLocation selected , final boolean confirm ) { this.blockPos = blockPos; - this.selectedIndex = selectedIndex; + this.selected = selected; this.confirm = confirm; } @@ -40,7 +43,7 @@ public class TBAlloyFurnaceMessage public void fromBytes( final ByteBuf buf ) { this.blockPos = new BlockPos( buf.readInt( ) , buf.readInt( ) , buf.readInt( ) ); - this.selectedIndex = buf.readShort( ); + this.selected = new ResourceLocation( ByteBufUtils.readUTF8String( buf ) , ByteBufUtils.readUTF8String( buf ) ); this.confirm = buf.readBoolean( ); } @@ -51,7 +54,8 @@ public class TBAlloyFurnaceMessage buf.writeInt( this.blockPos.getX( ) ); buf.writeInt( this.blockPos.getY( ) ); buf.writeInt( this.blockPos.getZ( ) ); - buf.writeShort( this.selectedIndex ); + ByteBufUtils.writeUTF8String( buf , this.selected.getResourceDomain( ) ); + ByteBufUtils.writeUTF8String( buf , this.selected.getResourcePath( ) ); buf.writeBoolean( this.confirm ); } @@ -67,7 +71,11 @@ public class TBAlloyFurnaceMessage } TBAlloyFurnaceTileEntity afte = (TBAlloyFurnaceTileEntity) te; - afte.currentRecipe = this.selectedIndex; + MAlloyRecipe recipe = MAlloyRecipe.REGISTRY.getRecipe( selected ); + if ( recipe == null ) { + recipe = MAlloyRecipe.REGISTRY.getRecipes( ).get( 0 ); + } + afte.recipe = recipe; } @@ -81,7 +89,7 @@ public class TBAlloyFurnaceMessage } final TBAlloyFurnaceContainer container = (TBAlloyFurnaceContainer) curCont; - container.setCurrentRecipe( this.selectedIndex , this.confirm ); + container.setCurrentRecipe( this.selected , this.confirm ); } } diff --git a/src/java/mmm/tech/base/TBAlloyFurnaceTileEntity.java b/src/java/mmm/tech/base/TBAlloyFurnaceTileEntity.java index d3c7bb1..9cd0a86 100644 --- a/src/java/mmm/tech/base/TBAlloyFurnaceTileEntity.java +++ b/src/java/mmm/tech/base/TBAlloyFurnaceTileEntity.java @@ -1,9 +1,11 @@ package mmm.tech.base; +import mmm.materials.MAlloyRecipe; import mmm.utils.UInventoryGrid; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.ResourceLocation; import net.minecraftforge.common.util.Constants.NBT; @@ -15,7 +17,7 @@ public class TBAlloyFurnaceTileEntity public final UInventoryGrid input; public final UInventoryGrid fuel; public final UInventoryGrid output; - public int currentRecipe; // XXX That's a terrible idea + public MAlloyRecipe recipe; public TBAlloyFurnaceTileEntity( ) @@ -23,7 +25,7 @@ public class TBAlloyFurnaceTileEntity this.input = new UInventoryGrid( "Input" , 3 , 5 ); this.fuel = new UInventoryGrid( "Fuel" , 2 , 2 ); this.output = new UInventoryGrid( "Output" , 2 , 5 ); - this.currentRecipe = 0; + this.recipe = MAlloyRecipe.REGISTRY.getRecipes( ).get( 0 ); } @@ -34,7 +36,11 @@ public class TBAlloyFurnaceTileEntity this.input.deserializeNBT( compound.getTagList( "Input" , NBT.TAG_COMPOUND ) ); this.fuel.deserializeNBT( compound.getTagList( "Fuel" , NBT.TAG_COMPOUND ) ); this.output.deserializeNBT( compound.getTagList( "Output" , NBT.TAG_COMPOUND ) ); - this.currentRecipe = compound.getInteger( "Recipe" ); + String recipeName = compound.getString( "Recipe" ); + this.recipe = MAlloyRecipe.REGISTRY.getRecipe( new ResourceLocation( recipeName ) ); + if ( this.recipe == null ) { + this.recipe = MAlloyRecipe.REGISTRY.getRecipes( ).get( 0 ); + } } @@ -45,7 +51,7 @@ public class TBAlloyFurnaceTileEntity compound.setTag( "Input" , this.input.serializeNBT( ) ); compound.setTag( "Fuel" , this.fuel.serializeNBT( ) ); compound.setTag( "Output" , this.output.serializeNBT( ) ); - compound.setInteger( "Recipe" , this.currentRecipe ); + compound.setString( "Recipe" , this.recipe.name.toString( ) ); return compound; }