2023-03-15 21:52:13 +00:00
|
|
|
// SPDX-FileCopyrightText: 2018 The CC: Tweaked Developers
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-03-13 14:14:36 +00:00
|
|
|
package dan200.computercraft.shared.computer.recipe;
|
|
|
|
|
2024-04-25 20:32:48 +00:00
|
|
|
import dan200.computercraft.shared.ModRegistry;
|
|
|
|
import dan200.computercraft.shared.computer.items.AbstractComputerItem;
|
|
|
|
import dan200.computercraft.shared.pocket.items.PocketComputerItem;
|
Try to make recipe serialisers more reusable
This attempts to reduce some duplication in recipe serialisation (and
deserialisation) by moving the structure of a recipe (group, category,
ingredients, result) into seprate types.
- Add ShapedRecipeSpec and ShapelessRecipeSpec, which store the core
properties of shaped and shapeless recipes. There's a couple of
additional classes here for handling some of the other shared or
complex logic.
- These classes are now used by two new Custom{Shaped,Shapeless}Recipe
classes, which are (mostly) equivalent to Minecraft's
shaped/shapeless recipes, just with support for nbt in results.
- All the other similar recipes now inherit from these base classes,
which allows us to reuse a lot of this serialisation code. Alas, the
total code size has still gone up - maybe there's too much
abstraction here :).
- Mostly unrelated, but fix the skull recipes using the wrong UUID
format.
This allows us to remove our mixin for nbt in recipes (as we just use
our custom recipe now) and simplify serialisation a bit - hopefully
making the switch to codecs a little easier.
2023-09-23 17:24:02 +00:00
|
|
|
import dan200.computercraft.shared.recipe.CustomShapedRecipe;
|
|
|
|
import dan200.computercraft.shared.recipe.ShapedRecipeSpec;
|
2024-04-25 20:32:48 +00:00
|
|
|
import net.minecraft.core.HolderLookup;
|
2021-08-03 20:46:53 +00:00
|
|
|
import net.minecraft.world.inventory.CraftingContainer;
|
2024-04-25 20:32:48 +00:00
|
|
|
import net.minecraft.world.item.Item;
|
2021-08-03 20:46:53 +00:00
|
|
|
import net.minecraft.world.item.ItemStack;
|
2024-04-25 20:32:48 +00:00
|
|
|
import net.minecraft.world.item.crafting.RecipeSerializer;
|
2018-03-13 14:14:36 +00:00
|
|
|
|
|
|
|
/**
|
Try to make recipe serialisers more reusable
This attempts to reduce some duplication in recipe serialisation (and
deserialisation) by moving the structure of a recipe (group, category,
ingredients, result) into seprate types.
- Add ShapedRecipeSpec and ShapelessRecipeSpec, which store the core
properties of shaped and shapeless recipes. There's a couple of
additional classes here for handling some of the other shared or
complex logic.
- These classes are now used by two new Custom{Shaped,Shapeless}Recipe
classes, which are (mostly) equivalent to Minecraft's
shaped/shapeless recipes, just with support for nbt in results.
- All the other similar recipes now inherit from these base classes,
which allows us to reuse a lot of this serialisation code. Alas, the
total code size has still gone up - maybe there's too much
abstraction here :).
- Mostly unrelated, but fix the skull recipes using the wrong UUID
format.
This allows us to remove our mixin for nbt in recipes (as we just use
our custom recipe now) and simplify serialisation a bit - hopefully
making the switch to codecs a little easier.
2023-09-23 17:24:02 +00:00
|
|
|
* A recipe which converts a computer from one form into another.
|
2018-03-13 14:14:36 +00:00
|
|
|
*/
|
2024-04-25 20:32:48 +00:00
|
|
|
public final class ComputerConvertRecipe extends CustomShapedRecipe {
|
|
|
|
private final Item result;
|
|
|
|
|
2024-01-31 20:55:14 +00:00
|
|
|
public ComputerConvertRecipe(ShapedRecipeSpec recipe) {
|
|
|
|
super(recipe);
|
2024-04-25 20:32:48 +00:00
|
|
|
this.result = recipe.result().getItem();
|
2018-03-13 14:14:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-04-25 20:32:48 +00:00
|
|
|
public ItemStack assemble(CraftingContainer inventory, HolderLookup.Provider registryAccess) {
|
|
|
|
// Find our computer item and copy the components across.
|
2021-01-09 19:22:58 +00:00
|
|
|
for (var i = 0; i < inventory.getContainerSize(); i++) {
|
2024-04-25 20:32:48 +00:00
|
|
|
var stack = inventory.getItem(i);
|
|
|
|
if (isComputerItem(stack.getItem())) {
|
|
|
|
var newStack = new ItemStack(result);
|
|
|
|
newStack.applyComponents(stack.getComponentsPatch());
|
|
|
|
return newStack;
|
|
|
|
}
|
2018-03-13 14:14:36 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 20:32:48 +00:00
|
|
|
return ItemStack.EMPTY;
|
2018-03-13 14:14:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-04-25 20:32:48 +00:00
|
|
|
public RecipeSerializer<ComputerConvertRecipe> getSerializer() {
|
|
|
|
return ModRegistry.RecipeSerializers.COMPUTER_CONVERT.get();
|
|
|
|
}
|
2018-03-13 14:14:36 +00:00
|
|
|
|
2024-04-25 20:32:48 +00:00
|
|
|
private static boolean isComputerItem(Item item) {
|
|
|
|
// TODO: Make this a little more general. Either with a tag, or a predicate on the recipe itself?
|
|
|
|
return item instanceof AbstractComputerItem || item instanceof PocketComputerItem;
|
2018-03-13 14:14:36 +00:00
|
|
|
}
|
|
|
|
}
|