aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/xmod/gregtech/loaders/RecipeGen_Recycling.java
blob: 044ce58c6208e37f7e6762ed04450fca3976208e (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package gtPlusPlus.xmod.gregtech.loaders;

import static gregtech.api.enums.GT_Values.L;
import static gregtech.api.enums.GT_Values.M;

import java.util.ArrayList;
import java.util.Map;

import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;

import org.apache.commons.lang3.reflect.FieldUtils;

import gregtech.api.enums.Materials;
import gregtech.api.enums.OrePrefixes;
import gregtech.api.util.GT_ModHandler;
import gregtech.api.util.GT_OreDictUnificator;
import gregtech.api.util.GT_Utility;
import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.api.objects.data.AutoMap;
import gtPlusPlus.api.objects.data.Pair;
import gtPlusPlus.core.lib.CORE;
import gtPlusPlus.core.material.Material;
import gtPlusPlus.core.material.state.MaterialState;
import gtPlusPlus.core.util.Utils;
import gtPlusPlus.core.util.minecraft.ItemUtils;
import gtPlusPlus.core.util.reflect.ReflectionUtils;

public class RecipeGen_Recycling implements Runnable {

    public static AutoMap<Runnable> mQueuedRecyclingGenerators = new AutoMap<>();

    public static void executeGenerators() {
        if (mQueuedRecyclingGenerators.size() > 0) {
            for (Runnable R : mQueuedRecyclingGenerators.values()) {
                R.run();
            }
        }
    }

    final Material toGenerate;
    public static Map<String, ItemStack> mNameMap;

    public RecipeGen_Recycling(final Material M) {
        this.toGenerate = M;
        if (mNameMap == null) {
            mNameMap = this.getNameMap();
        }
        mQueuedRecyclingGenerators.put(this);
    }

    @Override
    public void run() {
        if (mNameMap != null) {
            generateRecipes(this.toGenerate);
        }
    }

    public static void generateRecipes(final Material material) {

        if (material != null) Logger.WARNING("Generating Recycling recipes for " + material.getLocalizedName());

        final OrePrefixes[] mValidPrefixesAsString = { OrePrefixes.ingot, OrePrefixes.ingotHot, OrePrefixes.nugget,
            OrePrefixes.plate, OrePrefixes.plateDense, OrePrefixes.plateDouble, OrePrefixes.plateTriple,
            OrePrefixes.plateQuadruple, OrePrefixes.plateQuintuple, OrePrefixes.stick, OrePrefixes.stickLong,
            OrePrefixes.bolt, OrePrefixes.screw, OrePrefixes.ring, OrePrefixes.rotor, OrePrefixes.gearGt,
            OrePrefixes.gearGtSmall, OrePrefixes.gear, OrePrefixes.block, OrePrefixes.cableGt01, OrePrefixes.cableGt02,
            OrePrefixes.cableGt04, OrePrefixes.cableGt08, OrePrefixes.cableGt12, OrePrefixes.wireFine,
            OrePrefixes.wireGt01, OrePrefixes.wireGt02, OrePrefixes.wireGt04, OrePrefixes.wireGt08,
            OrePrefixes.wireGt12, OrePrefixes.wireGt16, OrePrefixes.foil, OrePrefixes.frameGt, OrePrefixes.pipeHuge,
            OrePrefixes.pipeLarge, OrePrefixes.pipeMedium, OrePrefixes.pipeSmall, OrePrefixes.pipeTiny, };

        int mSlotIndex = 0;
        Pair<OrePrefixes, ItemStack>[] mValidPairs = new Pair[mValidPrefixesAsString.length];

        for (int r = 0; r < mValidPairs.length; r++) {
            ItemStack temp = getItemStackOfAmountFromOreDictNoBroken(
                mValidPrefixesAsString[r].name() + Utils.sanitizeString(material.getLocalizedName()),
                1);
            if (temp != null) {
                mValidPairs[mSlotIndex++] = new Pair<>(mValidPrefixesAsString[r], temp.copy());
            }
        }

        if (mValidPairs.length > 0) {
            int validCounter = 0;
            Pair<OrePrefixes, ItemStack>[] temp = mValidPairs;
            for (Pair<OrePrefixes, ItemStack> temp2 : mValidPairs) {
                if (temp2 != null) {
                    Logger.WARNING(
                        "Valid: " + temp2.getValue()
                            .getDisplayName());
                    validCounter++;
                }
            }
            Pair<OrePrefixes, ItemStack> temp3[] = new Pair[validCounter];
            int temp4 = 0;
            for (Pair<OrePrefixes, ItemStack> r : mValidPairs) {
                if (r != null) {
                    temp3[temp4++] = r;
                }
            }
            if (temp3.length > 0) {
                mValidPairs = temp3.clone();
            }
        }

        if (mValidPrefixesAsString.length >= 1) {
            for (final Pair<OrePrefixes, ItemStack> validPrefix : mValidPairs) {
                try {

                    if (material == null || validPrefix == null
                        || (material.getState() != MaterialState.SOLID && material.getState() != MaterialState.LIQUID)
                        || validPrefix.getKey() == OrePrefixes.ingotHot) {
                        continue;
                    }

                    final ItemStack tempStack = validPrefix.getValue();
                    final ItemStack mDust = getDust(material, validPrefix.getKey());
                    final Pair<OrePrefixes, ItemStack> mData = getDustData(material, validPrefix.getKey());
                    int mFluidAmount = (int) GT_Utility
                        .translateMaterialToFluidAmount(validPrefix.getKey().mMaterialAmount, true);

                    // Maceration
                    if (ItemUtils.checkForInvalidItems(tempStack)) {
                        // mValidItems[mSlotIndex++] = tempStack;
                        if ((mDust != null) && GT_ModHandler.addPulverisationRecipe(tempStack, mDust)) {
                            Logger.WARNING(
                                "Recycle Recipe: " + material.getLocalizedName()
                                    + " - Success - Recycle "
                                    + tempStack.getDisplayName()
                                    + " and obtain "
                                    + mDust.getDisplayName());
                        } else {
                            Logger.WARNING("Recycle Recipe: " + material.getLocalizedName() + " - Failed");
                            if (mDust == null) {
                                Logger.WARNING("Invalid Dust output.");
                            }
                        }
                    }

                    // Arc Furnace
                    if (ItemUtils.checkForInvalidItems(tempStack)) {}

                    // Fluid Extractor
                    if (ItemUtils.checkForInvalidItems(tempStack)) {
                        // mValidItems[mSlotIndex++] = tempStack;

                        int aFluidAmount = (int) ((L * validPrefix.getKey().mMaterialAmount)
                            / (M * tempStack.stackSize));
                        int aDuration = (int) Math.max(1, (24 * validPrefix.getKey().mMaterialAmount) / M);
                        boolean aGenFluidExtraction = CORE.RA.addFluidExtractionRecipe(
                            tempStack,
                            material.getFluidStack(aFluidAmount),
                            aDuration,
                            material.vVoltageMultiplier);
                        if (aGenFluidExtraction /*
                                                 * (mDust != null) && CORE.RA.addFluidExtractionRecipe(tempStack,
                                                 * material.getFluidStack(mFluidAmount), 30,
                                                 * material.vVoltageMultiplier)
                                                 */) {
                            Logger.WARNING(
                                "Fluid Recycle Recipe: " + material.getLocalizedName()
                                    + " - Success - Recycle "
                                    + tempStack.getDisplayName()
                                    + " and obtain "
                                    + aFluidAmount
                                    + "mb of "
                                    + material.getFluidStack(1)
                                        .getLocalizedName()
                                    + ". Time: "
                                    + aDuration
                                    + ", Voltage: "
                                    + material.vVoltageMultiplier);
                        } else {
                            Logger.WARNING("Fluid Recycle Recipe: " + material.getLocalizedName() + " - Failed");
                            if (mDust == null) {
                                Logger.WARNING("Invalid Dust output.");
                            }
                        }
                    }

                } catch (final Throwable t) {
                    t.printStackTrace();
                    // Utils.LOG_WARNING("Returning Null. Throwable Info:
                    // "+t.getMessage());
                    // Utils.LOG_WARNING("Throwable Info: "+t.toString());
                    // Utils.LOG_WARNING("Throwable Info:
                    // "+t.getCause().toString());
                }
            }
        }
    }

    public static Pair<OrePrefixes, ItemStack> getDustData(final Material aMaterial, final OrePrefixes aPrefix) {
        return getDustData(aMaterial, aPrefix.mMaterialAmount);
    }

    public static Pair<OrePrefixes, ItemStack> getDustData(final Material aMaterial, final long aMaterialAmount) {
        ItemStack mDust = null;
        OrePrefixes mPrefix = null;

        if (aMaterial == null || aMaterialAmount <= 0) {
            return null;
        }
        if ((((aMaterialAmount % M) == 0) || (aMaterialAmount >= (M * 16)))) {
            mDust = get(OrePrefixes.dust, aMaterial, aMaterialAmount / M);
            mPrefix = OrePrefixes.dust;
        }
        if ((mDust == null) && ((((aMaterialAmount * 4) % M) == 0) || (aMaterialAmount >= (M * 8)))) {
            mDust = get(OrePrefixes.dustSmall, aMaterial, (aMaterialAmount * 4) / M);
            mPrefix = OrePrefixes.dustSmall;
        }
        if ((mDust == null) && (((aMaterialAmount * 9) >= M))) {
            mDust = get(OrePrefixes.dustTiny, aMaterial, (aMaterialAmount * 9) / M);
            mPrefix = OrePrefixes.dustTiny;
        }

        if (mPrefix != null && mDust != null) {
            Logger.WARNING("Built valid dust pair.");
            return new Pair<>(mPrefix, mDust);
        } else {
            Logger.WARNING("mPrefix: " + (mPrefix != null));
            Logger.WARNING("mDust: " + (mDust != null));
        }
        Logger.WARNING("Failed to build valid dust pair.");
        return null;
    }

    public static ItemStack getDust(final Material aMaterial, final OrePrefixes aPrefix) {
        return aMaterial == null ? null : getDust(aMaterial, aPrefix.mMaterialAmount);
    }

    public static ItemStack getDust(final Material aMaterial, final long aMaterialAmount) {
        if (aMaterialAmount <= 0) {
            return null;
        }
        ItemStack rStack = null;
        if ((((aMaterialAmount % M) == 0) || (aMaterialAmount >= (M * 16)))) {
            Logger.WARNING("Trying to get a Dust");
            rStack = get(OrePrefixes.dust, aMaterial, aMaterialAmount / M);
        }
        if ((rStack == null) && ((((aMaterialAmount * 4) % M) == 0) || (aMaterialAmount >= (M * 8)))) {
            Logger.WARNING("Trying to get a Small Dust");
            rStack = get(OrePrefixes.dustSmall, aMaterial, (aMaterialAmount * 4) / M);
        }
        if ((rStack == null) && (((aMaterialAmount * 9) >= M))) {
            Logger.WARNING("Trying to get a Tiny Dust");
            rStack = get(OrePrefixes.dustTiny, aMaterial, (aMaterialAmount * 9) / M);
        }
        return rStack;
    }

    public static ItemStack get(final Object aName, final long aAmount) {
        return get(aName, null, aAmount, true, true);
    }

    public static ItemStack get(final Object aName, final ItemStack aReplacement, final long aAmount) {
        return get(aName, aReplacement, aAmount, true, true);
    }

    public static ItemStack get(final OrePrefixes aPrefix, final Material aMaterial, final long aAmount) {
        return get(aPrefix, aMaterial, null, aAmount);
    }

    public static ItemStack get(final OrePrefixes aPrefix, final Material aMaterial, final ItemStack aReplacement,
        final long aAmount) {
        return get(
            aPrefix.name() + Utils.sanitizeString(aMaterial.getLocalizedName()),
            aReplacement,
            aAmount,
            false,
            true);
    }

    public static ItemStack get(final Object aName, final ItemStack aReplacement, final long aAmount,
        final boolean aMentionPossibleTypos, final boolean aNoInvalidAmounts) {
        if (aNoInvalidAmounts && (aAmount < 1L)) {
            Logger.WARNING("Returning Null. Method: " + ReflectionUtils.getMethodName(0));
            Logger.WARNING("Called from method: " + ReflectionUtils.getMethodName(1));
            Logger.WARNING("Called from method: " + ReflectionUtils.getMethodName(2));
            Logger.WARNING("Called from method: " + ReflectionUtils.getMethodName(3));
            Logger.WARNING("Called from method: " + ReflectionUtils.getMethodName(4));
            return null;
        }
        if (!mNameMap.containsKey(aName.toString()) && aMentionPossibleTypos) {
            Logger.WARNING("Unknown Key for Unification, Typo? " + aName);
        }
        return GT_Utility.copyAmount(
            aAmount,
            new Object[] { mNameMap.get(aName.toString()), getFirstOre(aName, aAmount), aReplacement });
    }

    public static ItemStack getFirstOre(final Object aName, final long aAmount) {
        if (GT_Utility.isStringInvalid(aName)) {
            Logger.WARNING("Returning Null. Method: " + ReflectionUtils.getMethodName(0));
            Logger.WARNING("Called from method: " + ReflectionUtils.getMethodName(1));
            Logger.WARNING("Called from method: " + ReflectionUtils.getMethodName(2));
            Logger.WARNING("Called from method: " + ReflectionUtils.getMethodName(3));
            Logger.WARNING("Called from method: " + ReflectionUtils.getMethodName(4));
            return null;
        }
        final ItemStack tStack = mNameMap.get(aName.toString());
        if (GT_Utility.isStackValid(tStack)) {
            Logger.WARNING("Found valid stack.");
            return GT_Utility.copyAmount(aAmount, new Object[] { tStack });
        }
        return GT_Utility.copyAmount(aAmount, getOres(aName).toArray());
    }

    public static ArrayList<ItemStack> getOres(final Object aOreName) {
        final String aName = (aOreName == null) ? "" : aOreName.toString();
        final ArrayList<ItemStack> rList = new ArrayList<>();
        if (GT_Utility.isStringValid(aName)) {
            Logger.WARNING("Making a list of all OreDict entries for " + aOreName + ".");
            if (rList.addAll(OreDictionary.getOres(aName))) {
                Logger.WARNING("Added " + rList.size() + " elements to list.");
            } else {
                Logger.WARNING("Failed to Add Collection from oreDictionary, forcing an entry.");
                rList.add(ItemUtils.getItemStackOfAmountFromOreDict((String) aOreName, 1));
            }
        }
        return rList;
    }

    @SuppressWarnings("unchecked")
    public Map<String, ItemStack> getNameMap() {
        Map<String, ItemStack> tempMap;
        try {
            tempMap = (Map<String, ItemStack>) FieldUtils
                .readStaticField(GT_OreDictUnificator.class, "sName2StackMap", true);
            if (tempMap != null) {
                Logger.WARNING("Found 'sName2StackMap' in GT_OreDictUnificator.class.");
                return tempMap;
            }
        } catch (final IllegalAccessException e) {
            e.printStackTrace();
        }
        Logger.WARNING("Invalid map stored in GT_OreDictUnificator.class, unable to find sName2StackMap field.");
        return null;
    }

    public static ItemStack getItemStackOfAmountFromOreDictNoBroken(String oredictName, final int amount) {

        try {

            if (oredictName.contains("-") || oredictName.contains("_")) {
                oredictName = Utils.sanitizeString(oredictName, new char[] { '-', '_' });
            } else {
                oredictName = Utils.sanitizeString(oredictName);
            }

            // Adds a check to grab dusts using GT methodology if possible.
            ItemStack returnValue = null;
            if (oredictName.toLowerCase()
                .contains("dust")) {
                final String MaterialName = oredictName.toLowerCase()
                    .replace("dust", "");
                final Materials m = Materials.get(MaterialName);
                if (m != null && m != Materials._NULL) {
                    returnValue = ItemUtils.getGregtechDust(m, amount);
                    if (ItemUtils.checkForInvalidItems(returnValue)) {
                        return returnValue;
                    }
                }
            }
            if (returnValue == null) {
                returnValue = getItemStackOfAmountFromOreDict(oredictName, amount);
                if (ItemUtils.checkForInvalidItems(returnValue)) {
                    return returnValue.copy();
                }
            }
            return null;
        } catch (final Throwable t) {
            return null;
        }
    }

    public static ItemStack getItemStackOfAmountFromOreDict(String oredictName, final int amount) {
        String mTemp = oredictName;

        // Banned Materials and replacements for GT5.8 compat.

        if (oredictName.toLowerCase()
            .contains("ingotclay")) {
            return ItemUtils.getSimpleStack(Items.clay_ball, amount);
        }

        final ArrayList<ItemStack> oreDictList = OreDictionary.getOres(mTemp);
        if (!oreDictList.isEmpty()) {
            final ItemStack returnValue = oreDictList.get(0)
                .copy();
            returnValue.stackSize = amount;
            return returnValue;
        }
        return null;
        // return getItemStackOfAmountFromOreDictNoBroken(mTemp, amount);
    }
}