From 4a5e7631708329c553a3fdbc976e133c1269786d Mon Sep 17 00:00:00 2001 From: Maya <10861407+serenibyss@users.noreply.github.com> Date: Fri, 10 Jan 2025 10:19:06 -0700 Subject: [PATCH] Add cell + pattern disassemble recipes like AE2 (#257) --- .../java/com/glodblock/github/FluidCraft.java | 1 + .../glodblock/github/loader/RecipeLoader.java | 5 + .../loader/recipe/DisassembleRecipe.java | 140 ++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 src/main/java/com/glodblock/github/loader/recipe/DisassembleRecipe.java diff --git a/src/main/java/com/glodblock/github/FluidCraft.java b/src/main/java/com/glodblock/github/FluidCraft.java index 49467a135..4a6a51afd 100644 --- a/src/main/java/com/glodblock/github/FluidCraft.java +++ b/src/main/java/com/glodblock/github/FluidCraft.java @@ -81,6 +81,7 @@ public static void postInit(FMLPostInitializationEvent event) { RecipeLoader.addInfiniteWaterCell(); } RecipeLoader.runTerminalRecipe(); + RecipeLoader.addDisassembleRecipe(); if (ModAndClassUtil.isV2) { CalculatorV2PluginLoader.installCalculatorV2Plugins(); diff --git a/src/main/java/com/glodblock/github/loader/RecipeLoader.java b/src/main/java/com/glodblock/github/loader/RecipeLoader.java index 1f2a84175..282e66f9c 100644 --- a/src/main/java/com/glodblock/github/loader/RecipeLoader.java +++ b/src/main/java/com/glodblock/github/loader/RecipeLoader.java @@ -71,6 +71,7 @@ import com.glodblock.github.common.Config; import com.glodblock.github.common.item.FCBaseItemCell; import com.glodblock.github.common.storage.CellType; +import com.glodblock.github.loader.recipe.DisassembleRecipe; import com.glodblock.github.loader.recipe.WirelessTerminalEnergyRecipe; import com.glodblock.github.loader.recipe.WirelessTerminalQuantumBridgeRecipe; import com.glodblock.github.loader.recipe.WirelessTerminalRecipe; @@ -757,4 +758,8 @@ public static void runTerminalRecipe() { Arrays.stream(term).filter(Objects::nonNull).toArray()); } + + public static void addDisassembleRecipe() { + GameRegistry.addRecipe(new DisassembleRecipe()); + } } diff --git a/src/main/java/com/glodblock/github/loader/recipe/DisassembleRecipe.java b/src/main/java/com/glodblock/github/loader/recipe/DisassembleRecipe.java new file mode 100644 index 000000000..1b3f63b77 --- /dev/null +++ b/src/main/java/com/glodblock/github/loader/recipe/DisassembleRecipe.java @@ -0,0 +1,140 @@ +package com.glodblock.github.loader.recipe; + +import static com.glodblock.github.loader.ItemAndBlockHolder.*; + +import java.util.HashMap; +import java.util.Map; + +import javax.annotation.Nullable; + +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.InventoryCrafting; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.IRecipe; +import net.minecraft.world.World; + +import appeng.api.AEApi; +import appeng.api.storage.IMEInventory; +import appeng.api.storage.StorageChannel; +import appeng.api.storage.data.IAEFluidStack; +import appeng.api.storage.data.IItemList; +import appeng.util.IterationCounter; +import appeng.util.Platform; + +public class DisassembleRecipe implements IRecipe { + + private final Map cellMappings; + private final Map nonCellMappings; + + public DisassembleRecipe() { + this.cellMappings = new HashMap<>(16); + this.nonCellMappings = new HashMap<>(1); + + this.cellMappings.put(new ItemStack(CELL1K), new ItemStack(CELL_PART, 1, 0)); + this.cellMappings.put(new ItemStack(CELL4K), new ItemStack(CELL_PART, 1, 1)); + this.cellMappings.put(new ItemStack(CELL16K), new ItemStack(CELL_PART, 1, 2)); + this.cellMappings.put(new ItemStack(CELL64K), new ItemStack(CELL_PART, 1, 3)); + this.cellMappings.put(new ItemStack(CELL256K), new ItemStack(CELL_PART, 1, 4)); + this.cellMappings.put(new ItemStack(CELL1024K), new ItemStack(CELL_PART, 1, 5)); + this.cellMappings.put(new ItemStack(CELL4096K), new ItemStack(CELL_PART, 1, 6)); + this.cellMappings.put(new ItemStack(CELL16384K), new ItemStack(CELL_PART, 1, 7)); + + this.cellMappings.put(new ItemStack(CELL1KM), new ItemStack(CELL_PART, 1, 0)); + this.cellMappings.put(new ItemStack(CELL4KM), new ItemStack(CELL_PART, 1, 1)); + this.cellMappings.put(new ItemStack(CELL16KM), new ItemStack(CELL_PART, 1, 2)); + this.cellMappings.put(new ItemStack(CELL64KM), new ItemStack(CELL_PART, 1, 3)); + this.cellMappings.put(new ItemStack(CELL256KM), new ItemStack(CELL_PART, 1, 4)); + this.cellMappings.put(new ItemStack(CELL1024KM), new ItemStack(CELL_PART, 1, 5)); + this.cellMappings.put(new ItemStack(CELL4096KM), new ItemStack(CELL_PART, 1, 6)); + this.cellMappings.put(new ItemStack(CELL16384KM), new ItemStack(CELL_PART, 1, 7)); + + this.nonCellMappings.put( + new ItemStack(PATTERN), + AEApi.instance().definitions().materials().blankPattern().maybeStack(1).get()); + } + + @Override + public boolean matches(final InventoryCrafting inv, final World w) { + return this.getOutput(inv) != null; + } + + @SuppressWarnings("unchecked") + private ItemStack getOutput(final IInventory inventory) { + int itemCount = 0; + ItemStack output = null; + + for (int slotIndex = 0; slotIndex < inventory.getSizeInventory(); slotIndex++) { + final ItemStack stackInSlot = inventory.getStackInSlot(slotIndex); + if (stackInSlot != null) { + // needs a single input in the recipe + itemCount++; + if (itemCount > 1) { + return null; + } + + // handle storage cells + final ItemStack storageCellStack = this.getCellOutput(stackInSlot); + if (storageCellStack != null) { + // make sure the storage cell stackInSlot empty... + final IMEInventory cellInv = AEApi.instance().registries().cell() + .getCellInventory(stackInSlot, null, StorageChannel.FLUIDS); + if (cellInv != null) { + final IItemList list = cellInv + .getAvailableItems(StorageChannel.FLUIDS.createList(), IterationCounter.fetchNewId()); + if (!list.isEmpty()) { + return null; + } + } + + output = storageCellStack; + } + + // handle crafting storage blocks + final ItemStack craftingStorageStack = this.getNonCellOutput(stackInSlot); + if (craftingStorageStack != null) { + output = craftingStorageStack; + } + } + } + + return output; + } + + private ItemStack getCellOutput(final ItemStack compared) { + for (final Map.Entry entry : this.cellMappings.entrySet()) { + if (Platform.isSameItemType(compared, entry.getKey())) { + return entry.getValue().copy(); + } + } + + return null; + } + + private ItemStack getNonCellOutput(final ItemStack compared) { + for (final Map.Entry entry : this.nonCellMappings.entrySet()) { + if (Platform.isSameItemType(compared, entry.getKey())) { + return entry.getValue().copy(); + } + } + + return null; + } + + @Nullable + @Override + public ItemStack getCraftingResult(final InventoryCrafting inv) { + return this.getOutput(inv); + } + + @Override + public int getRecipeSize() { + return 1; + } + + @Nullable + @Override + public ItemStack getRecipeOutput() // no default output.. + { + return null; + } +}