Skip to content

Commit

Permalink
Remove hard-coded disassembly item from crafting blocks, use recipe I…
Browse files Browse the repository at this point in the history
…Ds instead
  • Loading branch information
62832 committed Sep 10, 2023
1 parent fae9975 commit d7bdcbb
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 39 deletions.
63 changes: 44 additions & 19 deletions src/main/java/appeng/block/crafting/CraftingBlockItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemLike> disassemblyExtra;

public CraftingBlockItem(Block id, Item.Properties props, Supplier<ItemLike> disassemblyExtra) {
public CraftingBlockItem(Block id, Item.Properties props) {
super(id, props);
this.disassemblyExtra = disassemblyExtra;
}

@NotNull
@Override
public InteractionResultHolder<ItemStack> 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<ItemStack> 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());
}
}
19 changes: 7 additions & 12 deletions src/main/java/appeng/core/definitions/AEBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -182,17 +181,13 @@ public final class AEBlocks {
public static final BlockDefinition<CreativeEnergyCellBlock> CREATIVE_ENERGY_CELL = block("Creative Energy Cell", AEBlockIds.CREATIVE_ENERGY_CELL, CreativeEnergyCellBlock::new);

public static final BlockDefinition<CraftingUnitBlock> CRAFTING_UNIT = block("Crafting Unit", AEBlockIds.CRAFTING_UNIT, () -> new CraftingUnitBlock(CraftingUnitType.UNIT));
public static final BlockDefinition<CraftingUnitBlock> CRAFTING_ACCELERATOR = craftingBlock("Crafting Co-Processing Unit", AEBlockIds.CRAFTING_ACCELERATOR, () -> new CraftingUnitBlock(CraftingUnitType.ACCELERATOR), () -> AEItems.ENGINEERING_PROCESSOR);
public static final BlockDefinition<CraftingUnitBlock> CRAFTING_STORAGE_1K = craftingBlock("1k Crafting Storage", AEBlockIds.CRAFTING_STORAGE_1K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_1K), () -> AEItems.CELL_COMPONENT_1K);
public static final BlockDefinition<CraftingUnitBlock> CRAFTING_STORAGE_4K = craftingBlock("4k Crafting Storage",AEBlockIds.CRAFTING_STORAGE_4K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_4K), () -> AEItems.CELL_COMPONENT_4K);
public static final BlockDefinition<CraftingUnitBlock> CRAFTING_STORAGE_16K = craftingBlock("16k Crafting Storage", AEBlockIds.CRAFTING_STORAGE_16K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_16K), () -> AEItems.CELL_COMPONENT_16K);
public static final BlockDefinition<CraftingUnitBlock> CRAFTING_STORAGE_64K = craftingBlock("64k Crafting Storage", AEBlockIds.CRAFTING_STORAGE_64K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_64K), () -> AEItems.CELL_COMPONENT_64K);
public static final BlockDefinition<CraftingUnitBlock> CRAFTING_STORAGE_256K = craftingBlock("256k Crafting Storage", AEBlockIds.CRAFTING_STORAGE_256K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_256K), () -> AEItems.CELL_COMPONENT_256K);
public static final BlockDefinition<CraftingMonitorBlock> CRAFTING_MONITOR = craftingBlock("Crafting Monitor",AEBlockIds.CRAFTING_MONITOR, () -> new CraftingMonitorBlock(CraftingUnitType.MONITOR), () -> AEParts.STORAGE_MONITOR);

private static <T extends Block> BlockDefinition<T> craftingBlock(String englishName, ResourceLocation id, Supplier<T> blockSupplier, Supplier<ItemLike> disassemblyExtra) {
return block(englishName, id, blockSupplier, (block, props) -> new CraftingBlockItem(block, props, disassemblyExtra));
}
public static final BlockDefinition<CraftingUnitBlock> CRAFTING_ACCELERATOR = block("Crafting Co-Processing Unit", AEBlockIds.CRAFTING_ACCELERATOR, () -> new CraftingUnitBlock(CraftingUnitType.ACCELERATOR), CraftingBlockItem::new);
public static final BlockDefinition<CraftingUnitBlock> CRAFTING_STORAGE_1K = block("1k Crafting Storage", AEBlockIds.CRAFTING_STORAGE_1K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_1K), CraftingBlockItem::new);
public static final BlockDefinition<CraftingUnitBlock> CRAFTING_STORAGE_4K = block("4k Crafting Storage",AEBlockIds.CRAFTING_STORAGE_4K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_4K), CraftingBlockItem::new);
public static final BlockDefinition<CraftingUnitBlock> CRAFTING_STORAGE_16K = block("16k Crafting Storage", AEBlockIds.CRAFTING_STORAGE_16K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_16K), CraftingBlockItem::new);
public static final BlockDefinition<CraftingUnitBlock> CRAFTING_STORAGE_64K = block("64k Crafting Storage", AEBlockIds.CRAFTING_STORAGE_64K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_64K), CraftingBlockItem::new);
public static final BlockDefinition<CraftingUnitBlock> CRAFTING_STORAGE_256K = block("256k Crafting Storage", AEBlockIds.CRAFTING_STORAGE_256K, () -> new CraftingUnitBlock(CraftingUnitType.STORAGE_256K), CraftingBlockItem::new);
public static final BlockDefinition<CraftingMonitorBlock> CRAFTING_MONITOR = block("Crafting Monitor",AEBlockIds.CRAFTING_MONITOR, () -> new CraftingMonitorBlock(CraftingUnitType.MONITOR), CraftingBlockItem::new);

public static final BlockDefinition<PatternProviderBlock> PATTERN_PROVIDER = block("ME Pattern Provider", AEBlockIds.PATTERN_PROVIDER, PatternProviderBlock::new);
public static final BlockDefinition<MolecularAssemblerBlock> MOLECULAR_ASSEMBLER = block("Molecular Assembler", AEBlockIds.MOLECULAR_ASSEMBLER, () -> new MolecularAssemblerBlock(metalProps().noOcclusion()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,43 +586,43 @@ public void buildRecipes(Consumer<FinishedRecipe> 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")
Expand Down

0 comments on commit d7bdcbb

Please sign in to comment.