1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-12 11:10:29 +00:00

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.
This commit is contained in:
SquidDev 2018-03-13 14:21:39 +00:00
parent db825a7aab
commit e8c0cf3857
3 changed files with 16 additions and 55 deletions

View File

@ -9,79 +9,39 @@ package dan200.computercraft.shared.turtle.recipes;
import com.google.gson.JsonObject;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.items.IComputerItem;
import dan200.computercraft.shared.computer.recipe.ComputerConvertRecipe;
import dan200.computercraft.shared.turtle.items.TurtleItemFactory;
import dan200.computercraft.shared.util.RecipeUtil;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.util.JsonUtils;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.common.crafting.IRecipeFactory;
import net.minecraftforge.common.crafting.JsonContext;
import javax.annotation.Nonnull;
public class TurtleRecipe extends ShapedRecipes
public class TurtleRecipe extends ComputerConvertRecipe
{
private final NonNullList<Ingredient> m_recipe;
private final ComputerFamily m_family;
private final ComputerFamily family;
public TurtleRecipe( String group, int width, int height, NonNullList<Ingredient> recipe, ComputerFamily family )
public TurtleRecipe( String group, @Nonnull CraftingHelper.ShapedPrimer primer, ComputerFamily family )
{
super( group, width, height, recipe, TurtleItemFactory.create( -1, null, -1, family, null, null, 0, null ) );
m_recipe = recipe;
m_family = family;
}
@Override
public boolean matches( @Nonnull InventoryCrafting _inventory, @Nonnull World world )
{
return !getCraftingResult( _inventory ).isEmpty();
super( group, primer, TurtleItemFactory.create( -1, null, -1, family, null, null, 0, null ) );
this.family = family;
}
@Nonnull
@Override
public ItemStack getCraftingResult( @Nonnull InventoryCrafting inventory )
protected ItemStack convert( @Nonnull ItemStack stack )
{
// See if we match the recipe, and extract the input computercraft ID
int computerID = -1;
String label = null;
for( int y = 0; y < 3; ++y )
{
for( int x = 0; x < 3; ++x )
{
ItemStack item = inventory.getStackInRowAndColumn( x, y );
Ingredient target = m_recipe.get( x + y * 3 );
IComputerItem item = (IComputerItem) stack.getItem();
int computerID = item.getComputerID( stack );
String label = item.getLabel( stack );
if( item.getItem() instanceof IComputerItem )
{
IComputerItem itemComputer = (IComputerItem) item.getItem();
if( itemComputer.getFamily( item ) != m_family ) return ItemStack.EMPTY;
if( family == ComputerFamily.Beginners ) computerID = -1;
computerID = itemComputer.getComputerID( item );
label = itemComputer.getLabel( item );
}
else if( !target.apply( item ) )
{
return ItemStack.EMPTY;
}
}
}
// Build a turtle with the same ID the computer had
// Construct the new stack
if( m_family != ComputerFamily.Beginners )
{
return TurtleItemFactory.create( computerID, label, -1, m_family, null, null, 0, null );
}
else
{
return TurtleItemFactory.create( -1, label, -1, m_family, null, null, 0, null );
}
return TurtleItemFactory.create( computerID, label, -1, family, null, null, 0, null );
}
public static class Factory implements IRecipeFactory
@ -92,7 +52,8 @@ public class TurtleRecipe extends ShapedRecipes
String group = JsonUtils.getString( json, "group", "" );
ComputerFamily family = RecipeUtil.getFamily( json, "family" );
CraftingHelper.ShapedPrimer primer = RecipeUtil.getPrimer( context, json );
return new TurtleRecipe( group, primer.width, primer.height, primer.input, family );
return new TurtleRecipe( group, primer, family );
}
}
}

View File

@ -7,7 +7,7 @@
],
"key": {
"#": { "item": "minecraft:gold_ingot" },
"C": { "item": "computercraft:computer", "data": 16384 },
"C": { "type": "computercraft:computer", "item": "computercraft:computer", "data": 16384, "family": "Advanced" },
"I": { "item": "minecraft:chest" }
},
"family": "Advanced"

View File

@ -7,7 +7,7 @@
],
"key": {
"#": { "item": "minecraft:iron_ingot" },
"C": { "item": "computercraft:computer", "data": 0 },
"C": { "type": "computercraft:computer", "item": "computercraft:computer", "family": "Normal" },
"I": { "item": "minecraft:chest" }
},
"family": "Normal"