From 13c3fcc8978a57b134535dc23c3b43e00fe9d3f7 Mon Sep 17 00:00:00 2001 From: 90 Date: Sun, 10 Sep 2023 10:01:23 +0100 Subject: [PATCH] Remove hard-coded disassembly item from crafting blocks, use recipe IDs instead --- .../block/crafting/CraftingBlockItem.java | 63 +++++++++++++------ .../appeng/core/definitions/AEBlocks.java | 19 +++--- .../providers/recipes/CraftingRecipes.java | 16 ++--- 3 files changed, 59 insertions(+), 39 deletions(-) diff --git a/src/main/java/appeng/block/crafting/CraftingBlockItem.java b/src/main/java/appeng/block/crafting/CraftingBlockItem.java index 59ce08a9800..1b204d34d56 100644 --- a/src/main/java/appeng/block/crafting/CraftingBlockItem.java +++ b/src/main/java/appeng/block/crafting/CraftingBlockItem.java @@ -18,50 +18,75 @@ package appeng.block.crafting; -import java.util.function.Supplier; +import org.jetbrains.annotations.NotNull; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.ItemLike; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import appeng.block.AEBaseBlockItem; import appeng.core.AEConfig; -import appeng.core.definitions.AEBlocks; +import appeng.core.AELog; +import appeng.core.AppEng; import appeng.util.InteractionUtil; /** * Item that allows uncrafting CPU parts by disassembling them back into the crafting unit and the extra item. */ public class CraftingBlockItem extends AEBaseBlockItem { - /** - * This can be retrieved when disassembling the crafting unit. - */ - protected final Supplier disassemblyExtra; - - public CraftingBlockItem(Block id, Item.Properties props, Supplier disassemblyExtra) { + public CraftingBlockItem(Block id, Item.Properties props) { super(id, props); - this.disassemblyExtra = disassemblyExtra; } + @NotNull @Override - public InteractionResultHolder use(Level level, Player player, InteractionHand hand) { - if (AEConfig.instance().isDisassemblyCraftingEnabled() && InteractionUtil.isInAlternateUseMode(player)) { - int itemCount = player.getItemInHand(hand).getCount(); - player.setItemInHand(hand, ItemStack.EMPTY); + public InteractionResultHolder use(@NotNull Level level, @NotNull Player player, + @NotNull InteractionHand hand) { + return InteractionUtil.isInAlternateUseMode(player) && disassemble(player.getItemInHand(hand), level, player) + ? InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide()) + : super.use(level, player, hand); + } + + private boolean disassemble(ItemStack stack, Level level, Player player) { + if (!AEConfig.instance().isDisassemblyCraftingEnabled()) { + return false; + } - player.getInventory().placeItemBackInInventory(AEBlocks.CRAFTING_UNIT.stack(itemCount)); - player.getInventory().placeItemBackInInventory(new ItemStack(disassemblyExtra.get(), itemCount)); + var recipe = level.getRecipeManager().byKey(getRecipeId()); - return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide()); + if (recipe.isEmpty()) { + AELog.debug("Cannot disassemble crafting block because its crafting recipe doesn't exist: %s", + getRecipeId()); + return false; } - return super.use(level, player, hand); + + if (level.isClientSide()) { + return true; + } + + var inventory = player.getInventory(); + + if (inventory.getSelected() != stack) { + return false; + } + + inventory.setItem(inventory.selected, ItemStack.EMPTY); + + for (var ingredient : recipe.get().getIngredients()) { + var ingredientStack = new ItemStack(ingredient.getItems()[0].getItem(), inventory.getSelected().getCount()); + inventory.placeItemBackInInventory(ingredientStack); + } + + return true; } - private void disassemble(ItemStack stack, Player player) { + protected ResourceLocation getRecipeId() { + return AppEng.makeId("network/crafting/" + BuiltInRegistries.ITEM.getKey(this).getPath()); } } diff --git a/src/main/java/appeng/core/definitions/AEBlocks.java b/src/main/java/appeng/core/definitions/AEBlocks.java index 53f5d4bf671..124f79243ea 100644 --- a/src/main/java/appeng/core/definitions/AEBlocks.java +++ b/src/main/java/appeng/core/definitions/AEBlocks.java @@ -35,7 +35,6 @@ import net.minecraft.world.entity.EntityType; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.Item; -import net.minecraft.world.level.ItemLike; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.RotatedPillarBlock; @@ -182,17 +181,13 @@ public final class AEBlocks { public static final BlockDefinition CREATIVE_ENERGY_CELL = block("Creative Energy Cell", AEBlockIds.CREATIVE_ENERGY_CELL, CreativeEnergyCellBlock::new); public static final BlockDefinition CRAFTING_UNIT = block("Crafting Unit", AEBlockIds.CRAFTING_UNIT, () -> new CraftingUnitBlock(CraftingUnitType.UNIT)); - public static final BlockDefinition CRAFTING_ACCELERATOR = craftingBlock("Crafting Co-Processing Unit", AEBlockIds.CRAFTING_ACCELERATOR, () -> new CraftingUnitBlock(CraftingUnitType.ACCELERATOR), () -> AEItems.ENGINEERING_PROCESSOR); - public static final BlockDefinition CRAFTING_STORAGE_1K = craftingBlock("1k Crafting Storage", AEBlockIds.CRAFTING_STORAGE_1K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_1K), () -> AEItems.CELL_COMPONENT_1K); - public static final BlockDefinition CRAFTING_STORAGE_4K = craftingBlock("4k Crafting Storage",AEBlockIds.CRAFTING_STORAGE_4K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_4K), () -> AEItems.CELL_COMPONENT_4K); - public static final BlockDefinition CRAFTING_STORAGE_16K = craftingBlock("16k Crafting Storage", AEBlockIds.CRAFTING_STORAGE_16K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_16K), () -> AEItems.CELL_COMPONENT_16K); - public static final BlockDefinition CRAFTING_STORAGE_64K = craftingBlock("64k Crafting Storage", AEBlockIds.CRAFTING_STORAGE_64K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_64K), () -> AEItems.CELL_COMPONENT_64K); - public static final BlockDefinition CRAFTING_STORAGE_256K = craftingBlock("256k Crafting Storage", AEBlockIds.CRAFTING_STORAGE_256K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_256K), () -> AEItems.CELL_COMPONENT_256K); - public static final BlockDefinition CRAFTING_MONITOR = craftingBlock("Crafting Monitor",AEBlockIds.CRAFTING_MONITOR, () -> new CraftingMonitorBlock(CraftingUnitType.MONITOR), () -> AEParts.STORAGE_MONITOR); - - private static BlockDefinition craftingBlock(String englishName, ResourceLocation id, Supplier blockSupplier, Supplier disassemblyExtra) { - return block(englishName, id, blockSupplier, (block, props) -> new CraftingBlockItem(block, props, disassemblyExtra)); - } + public static final BlockDefinition CRAFTING_ACCELERATOR = block("Crafting Co-Processing Unit", AEBlockIds.CRAFTING_ACCELERATOR, () -> new CraftingUnitBlock(CraftingUnitType.ACCELERATOR), CraftingBlockItem::new); + public static final BlockDefinition CRAFTING_STORAGE_1K = block("1k Crafting Storage", AEBlockIds.CRAFTING_STORAGE_1K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_1K), CraftingBlockItem::new); + public static final BlockDefinition CRAFTING_STORAGE_4K = block("4k Crafting Storage",AEBlockIds.CRAFTING_STORAGE_4K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_4K), CraftingBlockItem::new); + public static final BlockDefinition CRAFTING_STORAGE_16K = block("16k Crafting Storage", AEBlockIds.CRAFTING_STORAGE_16K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_16K), CraftingBlockItem::new); + public static final BlockDefinition CRAFTING_STORAGE_64K = block("64k Crafting Storage", AEBlockIds.CRAFTING_STORAGE_64K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_64K), CraftingBlockItem::new); + public static final BlockDefinition CRAFTING_STORAGE_256K = block("256k Crafting Storage", AEBlockIds.CRAFTING_STORAGE_256K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_256K), CraftingBlockItem::new); + public static final BlockDefinition CRAFTING_MONITOR = block("Crafting Monitor",AEBlockIds.CRAFTING_MONITOR, () -> new CraftingMonitorBlock(CraftingUnitType.MONITOR), CraftingBlockItem::new); public static final BlockDefinition PATTERN_PROVIDER = block("ME Pattern Provider", AEBlockIds.PATTERN_PROVIDER, PatternProviderBlock::new); public static final BlockDefinition MOLECULAR_ASSEMBLER = block("Molecular Assembler", AEBlockIds.MOLECULAR_ASSEMBLER, () -> new MolecularAssemblerBlock(metalProps().noOcclusion())); diff --git a/src/main/java/appeng/datagen/providers/recipes/CraftingRecipes.java b/src/main/java/appeng/datagen/providers/recipes/CraftingRecipes.java index 592fc953158..3792e5f7d86 100644 --- a/src/main/java/appeng/datagen/providers/recipes/CraftingRecipes.java +++ b/src/main/java/appeng/datagen/providers/recipes/CraftingRecipes.java @@ -586,43 +586,43 @@ public void buildRecipes(Consumer consumer) { .define('c', AEParts.GLASS_CABLE.item(AEColor.TRANSPARENT)) .define('d', AEItems.LOGIC_PROCESSOR) .unlockedBy("has_calculation_processor", has(AEItems.CALCULATION_PROCESSOR)) - .save(consumer, AppEng.makeId("network/crafting/cpu_crafting_unit")); + .save(consumer, AppEng.makeId("network/crafting/crafting_unit")); ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC, AEBlocks.CRAFTING_STORAGE_1K) .requires(AEBlocks.CRAFTING_UNIT) .requires(AEItems.CELL_COMPONENT_1K) .unlockedBy("has_crafting_unit", has(AEBlocks.CRAFTING_UNIT)) - .save(consumer, AppEng.makeId("network/crafting/1k_cpu_crafting_storage")); + .save(consumer, AppEng.makeId("network/crafting/1k_crafting_storage")); ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC, AEBlocks.CRAFTING_STORAGE_4K) .requires(AEItems.CELL_COMPONENT_4K) .requires(AEBlocks.CRAFTING_UNIT) .unlockedBy("has_crafting_unit", has(AEBlocks.CRAFTING_UNIT)) - .save(consumer, AppEng.makeId("network/crafting/4k_cpu_crafting_storage")); + .save(consumer, AppEng.makeId("network/crafting/4k_crafting_storage")); ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC, AEBlocks.CRAFTING_STORAGE_16K) .requires(AEItems.CELL_COMPONENT_16K) .requires(AEBlocks.CRAFTING_UNIT) .unlockedBy("has_crafting_unit", has(AEBlocks.CRAFTING_UNIT)) - .save(consumer, AppEng.makeId("network/crafting/16k_cpu_crafting_storage")); + .save(consumer, AppEng.makeId("network/crafting/16k_crafting_storage")); ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC, AEBlocks.CRAFTING_STORAGE_64K) .requires(AEBlocks.CRAFTING_UNIT) .requires(AEItems.CELL_COMPONENT_64K) .unlockedBy("has_crafting_unit", has(AEBlocks.CRAFTING_UNIT)) - .save(consumer, AppEng.makeId("network/crafting/64k_cpu_crafting_storage")); + .save(consumer, AppEng.makeId("network/crafting/64k_crafting_storage")); ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC, AEBlocks.CRAFTING_STORAGE_256K) .requires(AEItems.CELL_COMPONENT_256K) .requires(AEBlocks.CRAFTING_UNIT) .unlockedBy("has_crafting_unit", has(AEBlocks.CRAFTING_UNIT)) - .save(consumer, AppEng.makeId("network/crafting/256k_cpu_crafting_storage")); + .save(consumer, AppEng.makeId("network/crafting/256k_crafting_storage")); ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC, AEBlocks.CRAFTING_ACCELERATOR) .requires(AEItems.ENGINEERING_PROCESSOR) .requires(AEBlocks.CRAFTING_UNIT) .unlockedBy("has_crafting_unit", has(AEBlocks.CRAFTING_UNIT)) - .save(consumer, AppEng.makeId("network/crafting/cpu_crafting_accelerator")); + .save(consumer, AppEng.makeId("network/crafting/crafting_accelerator")); ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC, AEBlocks.CRAFTING_MONITOR) .requires(AEParts.STORAGE_MONITOR) .requires(AEBlocks.CRAFTING_UNIT) .unlockedBy("has_crafting_unit", has(AEBlocks.CRAFTING_UNIT)) - .save(consumer, AppEng.makeId("network/crafting/cpu_crafting_monitor")); + .save(consumer, AppEng.makeId("network/crafting/crafting_monitor")); ShapedRecipeBuilder.shaped(RecipeCategory.MISC, AEBlocks.MOLECULAR_ASSEMBLER) .pattern("aba")