Skip to content

Commit 7c21836

Browse files
committed
Merge pull request dan200#528 from SquidDev-CC/ComputerCraft/feature/computer-upgrade
Add recipes to upgrade computers
2 parents dbcae81 + bb2eab0 commit 7c21836

File tree

15 files changed

+291
-72
lines changed

15 files changed

+291
-72
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: 15 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,82 +7,41 @@
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;
12+
import dan200.computercraft.shared.computer.recipe.ComputerConvertRecipe;
1313
import dan200.computercraft.shared.turtle.items.TurtleItemFactory;
1414
import dan200.computercraft.shared.util.RecipeUtil;
15-
import net.minecraft.inventory.InventoryCrafting;
1615
import net.minecraft.item.ItemStack;
1716
import net.minecraft.item.crafting.IRecipe;
18-
import net.minecraft.item.crafting.Ingredient;
19-
import net.minecraft.item.crafting.ShapedRecipes;
2017
import net.minecraft.util.JsonUtils;
21-
import net.minecraft.util.NonNullList;
22-
import net.minecraft.world.World;
2318
import net.minecraftforge.common.crafting.CraftingHelper;
2419
import net.minecraftforge.common.crafting.IRecipeFactory;
2520
import net.minecraftforge.common.crafting.JsonContext;
2621

2722
import javax.annotation.Nonnull;
2823

29-
public class TurtleRecipe extends ShapedRecipes
24+
public class TurtleRecipe extends ComputerConvertRecipe
3025
{
31-
private final NonNullList<Ingredient> m_recipe;
32-
private final ComputerFamily m_family;
26+
private final ComputerFamily family;
3327

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

4734
@Nonnull
4835
@Override
49-
public ItemStack getCraftingResult( @Nonnull InventoryCrafting inventory )
36+
protected ItemStack convert( @Nonnull ItemStack stack )
5037
{
51-
// See if we match the recipe, and extract the input computercraft ID
52-
int computerID = -1;
53-
String label = null;
54-
for( int y = 0; y < 3; ++y )
55-
{
56-
for( int x = 0; x < 3; ++x )
57-
{
58-
ItemStack item = inventory.getStackInRowAndColumn( x, y );
59-
Ingredient target = m_recipe.get( x + y * 3 );
60-
61-
if( item.getItem() instanceof IComputerItem )
62-
{
63-
IComputerItem itemComputer = (IComputerItem) item.getItem();
64-
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 );
6541

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

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

8847
public static class Factory implements IRecipeFactory
@@ -91,20 +50,10 @@ public static class Factory implements IRecipeFactory
9150
public IRecipe parse( JsonContext context, JsonObject json )
9251
{
9352
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-
53+
ComputerFamily family = RecipeUtil.getFamily( json, "family" );
10654
CraftingHelper.ShapedPrimer primer = RecipeUtil.getPrimer( context, json );
107-
return new TurtleRecipe( group, primer.width, primer.height, primer.input, family );
55+
56+
return new TurtleRecipe( group, primer, family );
10857
}
10958
}
11059
}

0 commit comments

Comments
 (0)