aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/xmod/tinkers/util/TinkersDryingRecipe.java
blob: 020fee505d295f91e67ec83b3fa57b423a813955 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package gtPlusPlus.xmod.tinkers.util;

import java.lang.reflect.Field;
import java.util.List;

import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;

import gregtech.api.enums.GT_Values;
import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.api.objects.data.AutoMap;
import gtPlusPlus.core.lib.CORE;
import gtPlusPlus.core.lib.LoadedMods;
import gtPlusPlus.core.recipe.common.CI;
import gtPlusPlus.core.util.reflect.ReflectionUtils;

public class TinkersDryingRecipe {

    public static AutoMap<TinkersDryingRecipe> recipes = new AutoMap<TinkersDryingRecipe>();

    public final int time;
    public final ItemStack input;
    public final ItemStack result;

    public static void generateAllDryingRecipes() {
        List<?> aRecipes = TinkersUtils.getDryingRecipes();
        if (aRecipes != null && aRecipes.size() > 0) {
            for (Object o : aRecipes) {
                Logger.INFO("Trying to generate recipe using object of type " + o.getClass().getSimpleName());
                generateFromTinkersRecipeObject(o);
            }
        } else {
            Logger.INFO(
                    "Error generating Drying recipes, map was either null or empty. Null? " + (aRecipes != null)
                            + ", Size: "
                            + aRecipes.size());
        }
        if (!recipes.isEmpty()) {
            Logger.INFO("Adding " + recipes.size() + " drying rack recipes to the dehydrator.");
            for (TinkersDryingRecipe r : recipes) {
                CORE.RA.addDehydratorRecipe(
                        new ItemStack[] { CI.getNumberedCircuit(16), r.input },
                        GT_Values.NF,
                        GT_Values.NF,
                        new ItemStack[] { r.result },
                        new int[] {},
                        r.time / 10,
                        30);
            }
        }
    }

    public static TinkersDryingRecipe generateFromTinkersRecipeObject(Object o) {
        Field aTime;
        Field aInput;
        Field aOutput;
        Class aTinkerClass = ReflectionUtils.getClass("tconstruct.library.crafting.DryingRackRecipes.DryingRecipe"); // o.getClass();
        if (aTinkerClass == null || !LoadedMods.TiCon) {
            Logger.INFO(
                    "Error generating Drying Recipe, could not find class. Exists? " + ReflectionUtils
                            .doesClassExist("tconstruct.library.crafting.DryingRackRecipes.DryingRecipe"));
            Class clazz = ReflectionUtils.getClass("tconstruct.library.crafting.DryingRackRecipes");
            Class[] y = clazz.getDeclaredClasses();
            if (y == null || y.length <= 0) {
                Logger.INFO("No hidden inner classes.");
                return null;
            } else {
                boolean found = false;
                for (Class h : y) {
                    Logger.INFO("Found hidden inner class: " + h.getCanonicalName());
                    if (h.getSimpleName().toLowerCase().equals("dryingrecipe")) {
                        Logger.INFO("Found correct recipe. Caching at correct location.");
                        ReflectionUtils.mCachedClasses
                                .put("tconstruct.library.crafting.DryingRackRecipes.DryingRecipe", h);
                        aTinkerClass = h;
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    return null;
                }
            }
        }
        aTime = ReflectionUtils.getField(aTinkerClass, "time");
        aInput = ReflectionUtils.getField(aTinkerClass, "input");
        aOutput = ReflectionUtils.getField(aTinkerClass, "result");
        try {
            int time_internal = aTime.getInt(o);
            ItemStack input_internal = (ItemStack) aInput.get(o);
            ItemStack result_internal = (ItemStack) aOutput.get(o);
            return new TinkersDryingRecipe(input_internal, time_internal, result_internal);
        } catch (Throwable b) {
            b.printStackTrace();
        }
        return null;
    }

    public TinkersDryingRecipe(final ItemStack input, final int time, final ItemStack result) {
        this.time = time;
        this.input = input;
        this.result = result;
        Logger.INFO(
                "Generating Drying Recipe. Input: " + input.getDisplayName() + ", Output: " + result.getDisplayName());
        recipes.add(this);
    }

    public boolean matches(ItemStack input) {
        if (input.hasTagCompound()) {
            input = input.copy();
            input.getTagCompound().removeTag("frypanKill");
            if (input.getTagCompound().hasNoTags()) {
                input.setTagCompound((NBTTagCompound) null);
            }
        }
        return ItemStack.areItemStacksEqual(this.input, input);
    }

    public ItemStack getResult() {
        return this.result.copy();
    }
}