Skip to content

Commit e8c0cf3

Browse files
committed
Convert TurtleRecipe to use ComputerConvertRecipe
The two recipes are pretty similar, so this allows us to substantially simplify the code. This now introduces the additional requirement that computers must be used to create turtles, rather than pocket computers or another turtle.
1 parent db825a7 commit e8c0cf3

File tree

3 files changed

+16
-55
lines changed

3 files changed

+16
-55
lines changed

src/main/java/dan200/computercraft/shared/turtle/recipes/TurtleRecipe.java

Lines changed: 14 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,79 +9,39 @@
99
import com.google.gson.JsonObject;
1010
import dan200.computercraft.shared.computer.core.ComputerFamily;
1111
import dan200.computercraft.shared.computer.items.IComputerItem;
12+
import dan200.computercraft.shared.computer.recipe.ComputerConvertRecipe;
1213
import dan200.computercraft.shared.turtle.items.TurtleItemFactory;
1314
import dan200.computercraft.shared.util.RecipeUtil;
14-
import net.minecraft.inventory.InventoryCrafting;
1515
import net.minecraft.item.ItemStack;
1616
import net.minecraft.item.crafting.IRecipe;
17-
import net.minecraft.item.crafting.Ingredient;
18-
import net.minecraft.item.crafting.ShapedRecipes;
1917
import net.minecraft.util.JsonUtils;
20-
import net.minecraft.util.NonNullList;
21-
import net.minecraft.world.World;
2218
import net.minecraftforge.common.crafting.CraftingHelper;
2319
import net.minecraftforge.common.crafting.IRecipeFactory;
2420
import net.minecraftforge.common.crafting.JsonContext;
2521

2622
import javax.annotation.Nonnull;
2723

28-
public class TurtleRecipe extends ShapedRecipes
24+
public class TurtleRecipe extends ComputerConvertRecipe
2925
{
30-
private final NonNullList<Ingredient> m_recipe;
31-
private final ComputerFamily m_family;
26+
private final ComputerFamily family;
3227

33-
public TurtleRecipe( String group, int width, int height, NonNullList<Ingredient> recipe, ComputerFamily family )
28+
public TurtleRecipe( String group, @Nonnull CraftingHelper.ShapedPrimer primer, ComputerFamily family )
3429
{
35-
super( group, width, height, recipe, TurtleItemFactory.create( -1, null, -1, family, null, null, 0, null ) );
36-
m_recipe = recipe;
37-
m_family = family;
38-
}
39-
40-
@Override
41-
public boolean matches( @Nonnull InventoryCrafting _inventory, @Nonnull World world )
42-
{
43-
return !getCraftingResult( _inventory ).isEmpty();
30+
super( group, primer, TurtleItemFactory.create( -1, null, -1, family, null, null, 0, null ) );
31+
this.family = family;
4432
}
4533

4634
@Nonnull
4735
@Override
48-
public ItemStack getCraftingResult( @Nonnull InventoryCrafting inventory )
36+
protected ItemStack convert( @Nonnull ItemStack stack )
4937
{
50-
// See if we match the recipe, and extract the input computercraft ID
51-
int computerID = -1;
52-
String label = null;
53-
for( int y = 0; y < 3; ++y )
54-
{
55-
for( int x = 0; x < 3; ++x )
56-
{
57-
ItemStack item = inventory.getStackInRowAndColumn( x, y );
58-
Ingredient target = m_recipe.get( x + y * 3 );
59-
60-
if( item.getItem() instanceof IComputerItem )
61-
{
62-
IComputerItem itemComputer = (IComputerItem) item.getItem();
63-
if( itemComputer.getFamily( item ) != m_family ) return ItemStack.EMPTY;
38+
IComputerItem item = (IComputerItem) stack.getItem();
39+
int computerID = item.getComputerID( stack );
40+
String label = item.getLabel( stack );
6441

65-
computerID = itemComputer.getComputerID( item );
66-
label = itemComputer.getLabel( item );
67-
}
68-
else if( !target.apply( item ) )
69-
{
70-
return ItemStack.EMPTY;
71-
}
72-
}
73-
}
42+
if( family == ComputerFamily.Beginners ) computerID = -1;
7443

75-
// Build a turtle with the same ID the computer had
76-
// Construct the new stack
77-
if( m_family != ComputerFamily.Beginners )
78-
{
79-
return TurtleItemFactory.create( computerID, label, -1, m_family, null, null, 0, null );
80-
}
81-
else
82-
{
83-
return TurtleItemFactory.create( -1, label, -1, m_family, null, null, 0, null );
84-
}
44+
return TurtleItemFactory.create( computerID, label, -1, family, null, null, 0, null );
8545
}
8646

8747
public static class Factory implements IRecipeFactory
@@ -92,7 +52,8 @@ public IRecipe parse( JsonContext context, JsonObject json )
9252
String group = JsonUtils.getString( json, "group", "" );
9353
ComputerFamily family = RecipeUtil.getFamily( json, "family" );
9454
CraftingHelper.ShapedPrimer primer = RecipeUtil.getPrimer( context, json );
95-
return new TurtleRecipe( group, primer.width, primer.height, primer.input, family );
55+
56+
return new TurtleRecipe( group, primer, family );
9657
}
9758
}
9859
}

src/main/resources/assets/computercraft/recipes/advanced_turtle.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"key": {
99
"#": { "item": "minecraft:gold_ingot" },
10-
"C": { "item": "computercraft:computer", "data": 16384 },
10+
"C": { "type": "computercraft:computer", "item": "computercraft:computer", "data": 16384, "family": "Advanced" },
1111
"I": { "item": "minecraft:chest" }
1212
},
1313
"family": "Advanced"

src/main/resources/assets/computercraft/recipes/normal_turtle.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"key": {
99
"#": { "item": "minecraft:iron_ingot" },
10-
"C": { "item": "computercraft:computer", "data": 0 },
10+
"C": { "type": "computercraft:computer", "item": "computercraft:computer", "family": "Normal" },
1111
"I": { "item": "minecraft:chest" }
1212
},
1313
"family": "Normal"

0 commit comments

Comments
 (0)