aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/recipe/maps/FormingPressBackend.java
blob: ccf6e63c29a4ac39ec2579b0b29ce6131f7154d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package gregtech.api.recipe.maps;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.FluidStack;

import gregtech.api.enums.GTValues;
import gregtech.api.enums.ItemList;
import gregtech.api.recipe.RecipeMapBackend;
import gregtech.api.recipe.RecipeMapBackendPropertiesBuilder;
import gregtech.api.util.GTRecipe;
import gregtech.api.util.GTUtility;
import gregtech.api.util.MethodsReturnNonnullByDefault;

/**
 * Special Class for Forming Press handling.
 */
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public class FormingPressBackend extends RecipeMapBackend {

    public FormingPressBackend(RecipeMapBackendPropertiesBuilder propertiesBuilder) {
        super(propertiesBuilder);
    }

    @Override
    protected GTRecipe modifyFoundRecipe(GTRecipe recipe, ItemStack[] items, FluidStack[] fluids,
        @Nullable ItemStack specialSlot) {
        for (ItemStack mold : items) {
            if (ItemList.Shape_Mold_Credit.isStackEqual(mold, false, true)) {
                NBTTagCompound nbt = mold.getTagCompound();
                if (nbt == null) nbt = new NBTTagCompound();
                if (!nbt.hasKey("credit_security_id")) nbt.setLong("credit_security_id", System.nanoTime());
                mold.setTagCompound(nbt);

                recipe = recipe.copy();
                recipe.mCanBeBuffered = false;
                recipe.mOutputs[0].setTagCompound(nbt);
                return recipe;
            }
        }
        return recipe;
    }

    @Override
    protected GTRecipe findFallback(ItemStack[] items, FluidStack[] fluids, @Nullable ItemStack specialSlot) {
        if (items.length < 2) {
            return null;
        }
        return findRenamingRecipe(items);
    }

    @Nullable
    private GTRecipe findRenamingRecipe(ItemStack[] inputs) {
        ItemStack mold = findNameMoldIndex(inputs);
        if (mold == null) return null;
        ItemStack input = findStackToRename(inputs, mold);
        if (input == null) return null;
        ItemStack output = GTUtility.copyAmount(1, input);
        if (output == null) return null;
        output.setStackDisplayName(mold.getDisplayName());
        return GTValues.RA.stdBuilder()
            .itemInputs(GTUtility.copyAmount(0, mold), GTUtility.copyAmount(1, input))
            .itemOutputs(output)
            .duration(128)
            .eut(8)
            .noBuffer()
            .nbtSensitive()
            .build()
            .orElse(null);
    }

    @Nullable
    private ItemStack findNameMoldIndex(ItemStack[] inputs) {
        for (ItemStack stack : inputs) {
            if (ItemList.Shape_Mold_Name.isStackEqual(stack, false, true)) return stack;
        }
        return null;
    }

    @Nullable
    private ItemStack findStackToRename(ItemStack[] inputs, ItemStack mold) {
        for (ItemStack stack : inputs) {
            if (stack == mold || stack == null) continue;
            return stack;
        }
        return null;
    }
}