mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-12-12 19:20: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:
parent
db825a7aab
commit
e8c0cf3857
@ -9,79 +9,39 @@ package dan200.computercraft.shared.turtle.recipes;
|
|||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
import dan200.computercraft.shared.computer.core.ComputerFamily;
|
||||||
import dan200.computercraft.shared.computer.items.IComputerItem;
|
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.turtle.items.TurtleItemFactory;
|
||||||
import dan200.computercraft.shared.util.RecipeUtil;
|
import dan200.computercraft.shared.util.RecipeUtil;
|
||||||
import net.minecraft.inventory.InventoryCrafting;
|
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.item.crafting.IRecipe;
|
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.JsonUtils;
|
||||||
import net.minecraft.util.NonNullList;
|
|
||||||
import net.minecraft.world.World;
|
|
||||||
import net.minecraftforge.common.crafting.CraftingHelper;
|
import net.minecraftforge.common.crafting.CraftingHelper;
|
||||||
import net.minecraftforge.common.crafting.IRecipeFactory;
|
import net.minecraftforge.common.crafting.IRecipeFactory;
|
||||||
import net.minecraftforge.common.crafting.JsonContext;
|
import net.minecraftforge.common.crafting.JsonContext;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
public class TurtleRecipe extends ShapedRecipes
|
public class TurtleRecipe extends ComputerConvertRecipe
|
||||||
{
|
{
|
||||||
private final NonNullList<Ingredient> m_recipe;
|
private final ComputerFamily family;
|
||||||
private final ComputerFamily m_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 ) );
|
super( group, primer, TurtleItemFactory.create( -1, null, -1, family, null, null, 0, null ) );
|
||||||
m_recipe = recipe;
|
this.family = family;
|
||||||
m_family = family;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean matches( @Nonnull InventoryCrafting _inventory, @Nonnull World world )
|
|
||||||
{
|
|
||||||
return !getCraftingResult( _inventory ).isEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@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
|
IComputerItem item = (IComputerItem) stack.getItem();
|
||||||
int computerID = -1;
|
int computerID = item.getComputerID( stack );
|
||||||
String label = null;
|
String label = item.getLabel( stack );
|
||||||
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 );
|
|
||||||
|
|
||||||
if( item.getItem() instanceof IComputerItem )
|
if( family == ComputerFamily.Beginners ) computerID = -1;
|
||||||
{
|
|
||||||
IComputerItem itemComputer = (IComputerItem) item.getItem();
|
|
||||||
if( itemComputer.getFamily( item ) != m_family ) return ItemStack.EMPTY;
|
|
||||||
|
|
||||||
computerID = itemComputer.getComputerID( item );
|
return TurtleItemFactory.create( computerID, label, -1, family, null, null, 0, null );
|
||||||
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 );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Factory implements IRecipeFactory
|
public static class Factory implements IRecipeFactory
|
||||||
@ -92,7 +52,8 @@ public class TurtleRecipe extends ShapedRecipes
|
|||||||
String group = JsonUtils.getString( json, "group", "" );
|
String group = JsonUtils.getString( json, "group", "" );
|
||||||
ComputerFamily family = RecipeUtil.getFamily( json, "family" );
|
ComputerFamily family = RecipeUtil.getFamily( json, "family" );
|
||||||
CraftingHelper.ShapedPrimer primer = RecipeUtil.getPrimer( context, json );
|
CraftingHelper.ShapedPrimer primer = RecipeUtil.getPrimer( context, json );
|
||||||
return new TurtleRecipe( group, primer.width, primer.height, primer.input, family );
|
|
||||||
|
return new TurtleRecipe( group, primer, family );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
],
|
],
|
||||||
"key": {
|
"key": {
|
||||||
"#": { "item": "minecraft:gold_ingot" },
|
"#": { "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" }
|
"I": { "item": "minecraft:chest" }
|
||||||
},
|
},
|
||||||
"family": "Advanced"
|
"family": "Advanced"
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
],
|
],
|
||||||
"key": {
|
"key": {
|
||||||
"#": { "item": "minecraft:iron_ingot" },
|
"#": { "item": "minecraft:iron_ingot" },
|
||||||
"C": { "item": "computercraft:computer", "data": 0 },
|
"C": { "type": "computercraft:computer", "item": "computercraft:computer", "family": "Normal" },
|
||||||
"I": { "item": "minecraft:chest" }
|
"I": { "item": "minecraft:chest" }
|
||||||
},
|
},
|
||||||
"family": "Normal"
|
"family": "Normal"
|
||||||
|
Loading…
Reference in New Issue
Block a user