Skip to content

Commit db825a7

Browse files
committed
Add recipes to convert computer items into their upgraded counterparts
This adds IComputerItem.withFamily(ItemStack, ComputerFamily) as well as a ComputerFamilyRecipe class. Each type of computer (normal, turtle, pocket) defines a recipe using this class, as they require a different number of gold ingots to upgrade.
1 parent 3b3dd80 commit db825a7

File tree

13 files changed

+275
-17
lines changed

13 files changed

+275
-17
lines changed

src/main/java/dan200/computercraft/shared/computer/items/IComputerItem.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ public interface IComputerItem
1616
int getComputerID( @Nonnull ItemStack stack );
1717
String getLabel( @Nonnull ItemStack stack );
1818
ComputerFamily getFamily( @Nonnull ItemStack stack );
19+
ItemStack withFamily(@Nonnull ItemStack stack, @Nonnull ComputerFamily family);
1920
}

src/main/java/dan200/computercraft/shared/computer/items/ItemComputer.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
public class ItemComputer extends ItemComputerBase
2828
{
2929
public static int HIGHEST_DAMAGE_VALUE_ID = 16382;
30-
30+
3131
public ItemComputer( Block block )
3232
{
3333
super( block );
@@ -87,7 +87,7 @@ public boolean placeBlockAt( @Nonnull ItemStack stack, @Nonnull EntityPlayer pla
8787
TileEntity tile = world.getTileEntity( pos );
8888
if( tile != null && tile instanceof IComputerTile )
8989
{
90-
IComputerTile computer = (IComputerTile)tile;
90+
IComputerTile computer = (IComputerTile) tile;
9191
setupComputerAfterPlacement( stack, computer );
9292
}
9393
return true;
@@ -146,10 +146,16 @@ public int getComputerID( @Nonnull ItemStack stack )
146146
else
147147
{
148148
int damage = stack.getItemDamage() & 0x3fff;
149-
return ( damage - 1 );
149+
return (damage - 1);
150150
}
151151
}
152152

153+
@Override
154+
public ItemStack withFamily( @Nonnull ItemStack stack, @Nonnull ComputerFamily family )
155+
{
156+
return ComputerItemFactory.create( getComputerID( stack ), getLabel( stack ), family );
157+
}
158+
153159
@Override
154160
public ComputerFamily getFamily( int damage )
155161
{
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package dan200.computercraft.shared.computer.recipe;
2+
3+
import dan200.computercraft.shared.computer.items.IComputerItem;
4+
import net.minecraft.inventory.InventoryCrafting;
5+
import net.minecraft.item.ItemStack;
6+
import net.minecraft.item.crafting.Ingredient;
7+
import net.minecraft.item.crafting.ShapedRecipes;
8+
import net.minecraft.world.World;
9+
import net.minecraftforge.common.crafting.CraftingHelper;
10+
11+
import javax.annotation.Nonnull;
12+
13+
/**
14+
* Represents a recipe which converts a computer from one form into another.
15+
*/
16+
public abstract class ComputerConvertRecipe extends ShapedRecipes
17+
{
18+
public ComputerConvertRecipe( String group, @Nonnull CraftingHelper.ShapedPrimer primer, @Nonnull ItemStack result )
19+
{
20+
super( group, primer.width, primer.height, primer.input, result );
21+
}
22+
23+
@Nonnull
24+
protected abstract ItemStack convert( @Nonnull ItemStack stack );
25+
26+
@Override
27+
public boolean matches( @Nonnull InventoryCrafting inventory, @Nonnull World world )
28+
{
29+
// See if we match the recipe, and extract the input computercraft ID
30+
ItemStack computerStack = null;
31+
for( int y = 0; y < 3; ++y )
32+
{
33+
for( int x = 0; x < 3; ++x )
34+
{
35+
ItemStack stack = inventory.getStackInRowAndColumn( x, y );
36+
Ingredient target = getIngredients().get( x + y * 3 );
37+
38+
// First verify we match the ingredient
39+
if( !target.apply( stack ) ) return false;
40+
41+
// We want to ensure we have a computer item somewhere in the recipe
42+
if( stack.getItem() instanceof IComputerItem ) computerStack = stack;
43+
}
44+
}
45+
46+
return computerStack != null;
47+
}
48+
49+
@Nonnull
50+
@Override
51+
public ItemStack getCraftingResult( @Nonnull InventoryCrafting inventory )
52+
{
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+
59+
// If we're a computer, convert!
60+
if( item.getItem() instanceof IComputerItem ) return convert( item );
61+
}
62+
}
63+
64+
return ItemStack.EMPTY;
65+
}
66+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package dan200.computercraft.shared.computer.recipe;
2+
3+
import com.google.gson.JsonObject;
4+
import dan200.computercraft.shared.computer.core.ComputerFamily;
5+
import dan200.computercraft.shared.computer.items.IComputerItem;
6+
import dan200.computercraft.shared.util.RecipeUtil;
7+
import net.minecraft.item.ItemStack;
8+
import net.minecraft.item.crafting.IRecipe;
9+
import net.minecraft.util.JsonUtils;
10+
import net.minecraftforge.common.crafting.CraftingHelper;
11+
import net.minecraftforge.common.crafting.IRecipeFactory;
12+
import net.minecraftforge.common.crafting.JsonContext;
13+
14+
import javax.annotation.Nonnull;
15+
16+
public class ComputerFamilyRecipe extends ComputerConvertRecipe
17+
{
18+
private final ComputerFamily family;
19+
20+
public ComputerFamilyRecipe( String group, @Nonnull CraftingHelper.ShapedPrimer primer, @Nonnull ItemStack result, ComputerFamily family )
21+
{
22+
super( group, primer, result );
23+
this.family = family;
24+
}
25+
26+
@Nonnull
27+
@Override
28+
protected ItemStack convert( @Nonnull ItemStack stack )
29+
{
30+
return ((IComputerItem) stack.getItem()).withFamily( stack, family );
31+
}
32+
33+
public static class Factory implements IRecipeFactory
34+
{
35+
@Override
36+
public IRecipe parse( JsonContext context, JsonObject json )
37+
{
38+
String group = JsonUtils.getString( json, "group", "" );
39+
ComputerFamily family = RecipeUtil.getFamily( json, "family" );
40+
41+
CraftingHelper.ShapedPrimer primer = RecipeUtil.getPrimer( context, json );
42+
ItemStack result = deserializeItem( JsonUtils.getJsonObject( json, "result" ), false );
43+
44+
return new ComputerFamilyRecipe( group, primer, result, family );
45+
}
46+
}
47+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package dan200.computercraft.shared.computer.recipe;
2+
3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonSyntaxException;
5+
import dan200.computercraft.shared.computer.core.ComputerFamily;
6+
import dan200.computercraft.shared.computer.items.IComputerItem;
7+
import dan200.computercraft.shared.util.RecipeUtil;
8+
import net.minecraft.item.Item;
9+
import net.minecraft.item.ItemStack;
10+
import net.minecraft.item.crafting.Ingredient;
11+
import net.minecraft.util.JsonUtils;
12+
import net.minecraft.util.ResourceLocation;
13+
import net.minecraftforge.common.crafting.IIngredientFactory;
14+
import net.minecraftforge.common.crafting.JsonContext;
15+
import net.minecraftforge.fml.common.registry.ForgeRegistries;
16+
17+
import javax.annotation.Nonnull;
18+
import javax.annotation.Nullable;
19+
20+
/**
21+
* Represents an ingredient which requires an item to have a specific
22+
* computer family. This allows us to have operations which only work
23+
* on normal or
24+
*/
25+
public class ComputerIngredient extends Ingredient
26+
{
27+
private final IComputerItem item;
28+
private final ComputerFamily family;
29+
30+
public <T extends Item & IComputerItem> ComputerIngredient( T item, int data, ComputerFamily family )
31+
{
32+
super( new ItemStack( item, 1, data ) );
33+
34+
this.item = item;
35+
this.family = family;
36+
}
37+
38+
@Override
39+
public boolean apply( @Nullable ItemStack stack )
40+
{
41+
return stack != null && stack.getItem() == item && item.getFamily( stack ) == family;
42+
}
43+
44+
public static class Factory implements IIngredientFactory
45+
{
46+
@Nonnull
47+
@Override
48+
public Ingredient parse( JsonContext context, JsonObject json )
49+
{
50+
String itemName = context.appendModId( JsonUtils.getString( json, "item" ) );
51+
int data = JsonUtils.getInt( json, "data", 0 );
52+
ComputerFamily family = RecipeUtil.getFamily( json, "family" );
53+
54+
Item item = ForgeRegistries.ITEMS.getValue( new ResourceLocation( itemName ) );
55+
56+
if( item == null ) throw new JsonSyntaxException( "Unknown item '" + itemName + "'" );
57+
if( !(item instanceof IComputerItem) )
58+
{
59+
throw new JsonSyntaxException( "Item '" + itemName + "' is not a computer item" );
60+
}
61+
62+
63+
return new ComputerIngredient( (Item & IComputerItem) item, data, family );
64+
}
65+
}
66+
}

src/main/java/dan200/computercraft/shared/pocket/items/ItemPocketComputer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,15 @@ public ComputerFamily getFamily( @Nonnull ItemStack stack )
371371
}
372372
}
373373

374+
@Override
375+
public ItemStack withFamily( @Nonnull ItemStack stack, @Nonnull ComputerFamily family )
376+
{
377+
return PocketComputerItemFactory.create(
378+
getComputerID( stack ), getLabel( stack ), getColour( stack ),
379+
family, getUpgrade( stack )
380+
);
381+
}
382+
374383
// IMedia
375384

376385
@Override

src/main/java/dan200/computercraft/shared/turtle/items/ItemTurtleBase.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ else if( right != null )
170170
}
171171
}
172172

173+
@Override
174+
public ItemStack withFamily( @Nonnull ItemStack stack, @Nonnull ComputerFamily family )
175+
{
176+
return TurtleItemFactory.create(
177+
getComputerID( stack ), getLabel( stack ),
178+
getColour( stack ), family,
179+
getUpgrade( stack, TurtleSide.Left ), getUpgrade( stack, TurtleSide.Right ),
180+
getFuelLevel( stack ), getOverlay( stack )
181+
);
182+
}
183+
173184
@Override
174185
public ItemStack setColour( ItemStack stack, int colour )
175186
{

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package dan200.computercraft.shared.turtle.recipes;
88

99
import com.google.gson.JsonObject;
10-
import com.google.gson.JsonSyntaxException;
1110
import dan200.computercraft.shared.computer.core.ComputerFamily;
1211
import dan200.computercraft.shared.computer.items.IComputerItem;
1312
import dan200.computercraft.shared.turtle.items.TurtleItemFactory;
@@ -91,18 +90,7 @@ public static class Factory implements IRecipeFactory
9190
public IRecipe parse( JsonContext context, JsonObject json )
9291
{
9392
String group = JsonUtils.getString( json, "group", "" );
94-
95-
String familyName = JsonUtils.getString( json, "family" );
96-
ComputerFamily family;
97-
try
98-
{
99-
family = ComputerFamily.valueOf( familyName );
100-
}
101-
catch( IllegalArgumentException e )
102-
{
103-
throw new JsonSyntaxException( "Unknown computer family '" + familyName + "'" );
104-
}
105-
93+
ComputerFamily family = RecipeUtil.getFamily( json, "family" );
10694
CraftingHelper.ShapedPrimer primer = RecipeUtil.getPrimer( context, json );
10795
return new TurtleRecipe( group, primer.width, primer.height, primer.input, family );
10896
}

src/main/java/dan200/computercraft/shared/util/RecipeUtil.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.google.common.collect.Maps;
1010
import com.google.common.collect.Sets;
1111
import com.google.gson.*;
12+
import dan200.computercraft.shared.computer.core.ComputerFamily;
1213
import net.minecraft.item.crafting.Ingredient;
1314
import net.minecraft.util.JsonUtils;
1415
import net.minecraft.util.NonNullList;
@@ -102,4 +103,17 @@ public static NonNullList<Ingredient> getIngredients( JsonContext context, JsonO
102103

103104
return ings;
104105
}
106+
107+
public static ComputerFamily getFamily( JsonObject json, String name )
108+
{
109+
String familyName = JsonUtils.getString( json, name );
110+
try
111+
{
112+
return ComputerFamily.valueOf( familyName );
113+
}
114+
catch( IllegalArgumentException e )
115+
{
116+
throw new JsonSyntaxException( "Unknown computer family '" + familyName + "' for field " + name );
117+
}
118+
}
105119
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
"recipes" : {
33
"impostor_shaped" : "dan200.computercraft.shared.util.ImpostorRecipe$Factory",
44
"impostor_shapeless" : "dan200.computercraft.shared.util.ImpostorShapelessRecipe$Factory",
5-
"turtle" : "dan200.computercraft.shared.turtle.recipes.TurtleRecipe$Factory"
5+
"turtle" : "dan200.computercraft.shared.turtle.recipes.TurtleRecipe$Factory",
6+
"computer_upgrade" : "dan200.computercraft.shared.computer.recipe.ComputerFamilyRecipe$Factory"
7+
},
8+
"ingredients": {
9+
"computer": "dan200.computercraft.shared.computer.recipe.ComputerIngredient$Factory"
610
}
711
}

0 commit comments

Comments
 (0)