aboutsummaryrefslogtreecommitdiff
path: root/gtpp/src/main/java/gtPlusPlus/xmod/thaumcraft/objects/ThreadAspectScanner.java
blob: 1f053220ab94812c63d271e7920e7763d532a7d6 (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
package gtPlusPlus.xmod.thaumcraft.objects;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.api.objects.data.AutoMap;
import gtPlusPlus.api.objects.data.Pair;
import gtPlusPlus.core.util.Utils;
import gtPlusPlus.core.util.data.FileUtils;
import gtPlusPlus.core.util.minecraft.ItemUtils;
import gtPlusPlus.xmod.thaumcraft.commands.CommandDumpAspects;

public class ThreadAspectScanner extends Thread {

    public static boolean mDoWeScan = false;
    private static final Map<String, AutoMap<ItemStack>> mAllGameContent = new HashMap<>();
    public final File mAspectCacheFile;

    public ThreadAspectScanner() {
        mAspectCacheFile = FileUtils.getFile("config/GTplusplus", "AspectInfo", "txt");
        mDoWeScan = true;
    }

    private void tryCacheObject(ItemStack aStack) {
        if (aStack == null) {
            return;
        }
        String nameKey;
        try {
            nameKey = ItemUtils.getUnlocalizedItemName(aStack);
        } catch (NullPointerException n) {
            try {
                nameKey = Utils.sanitizeString(
                    aStack.getDisplayName()
                        .toLowerCase());
            } catch (NullPointerException n2) {
                try {
                    nameKey = aStack.getItem()
                        .getUnlocalizedName();
                } catch (NullPointerException n3) {
                    nameKey = "BadItemsGalore";
                }
            }
        }
        AutoMap<ItemStack> m = new AutoMap<>();
        if (mAllGameContent.containsKey(nameKey)) {
            m = mAllGameContent.get(nameKey);
        }
        m.put(aStack);
        mAllGameContent.put(nameKey, m);
    }

    @SuppressWarnings({ "rawtypes" })
    @Override
    public void run() {
        if (mDoWeScan) {
            Iterator iterator;
            Logger.INFO("Finding Blocks and Items to scan for Aspect data.");
            long mBlocksCounter = 0;
            long mItemsCounter = 0;

            // First, find blocks
            iterator = Block.blockRegistry.getKeys()
                .iterator();
            while (iterator.hasNext()) {
                String s = (String) iterator.next();
                Block block = (Block) Block.blockRegistry.getObject(s);
                if (block != null) {
                    tryCacheObject(ItemUtils.getSimpleStack(block));
                    mBlocksCounter++;
                }
            }
            Logger.INFO("Completed Block Scan. Counted " + mBlocksCounter);

            // Second Find items, Skipping things that exist.
            iterator = Item.itemRegistry.getKeys()
                .iterator();
            while (iterator.hasNext()) {
                String s = (String) iterator.next();
                Item item = (Item) Item.itemRegistry.getObject(s);
                if (item != null) {
                    if (item.getHasSubtypes()) {
                        List q1 = new ArrayList();
                        item.getSubItems(item, item.getCreativeTab(), q1);
                        if (q1 != null && q1.size() > 0) {
                            for (int e = 0; e < q1.size(); e++) {
                                ItemStack check = ItemUtils.simpleMetaStack(item, e, 1);
                                if (check != null) {
                                    tryCacheObject(check);
                                    mItemsCounter++;
                                }
                            }
                        } else {
                            tryCacheObject(ItemUtils.getSimpleStack(item));
                            mItemsCounter++;
                        }
                    } else {
                        tryCacheObject(ItemUtils.getSimpleStack(item));
                        mItemsCounter++;
                    }
                }
            }
            Logger.INFO("Completed Item Scan. Counted " + mItemsCounter);

            Set<String> y = mAllGameContent.keySet();
            Logger.INFO("Beginning iteration of " + y.size() + " itemstacks for aspect information.");

            for (String key : y) {
                // Logger.INFO("Looking for key: "+key);
                if (mAllGameContent.containsKey(key)) {
                    AutoMap<ItemStack> group = mAllGameContent.get(key);
                    if (group == null || group.size() <= 0) {
                        continue;
                    }
                    for (ItemStack stack : group) {
                        thaumcraft.api.aspects.AspectList a = thaumcraft.common.lib.crafting.ThaumcraftCraftingManager
                            .getObjectTags(stack);
                        if (a == null) {
                            continue;
                        } else {
                            AutoMap<Pair<String, Integer>> aspectPairs = new AutoMap<>();
                            for (thaumcraft.api.aspects.Aspect c : a.getAspectsSortedAmount()) {
                                if (c != null) {
                                    aspectPairs.put(new Pair<>(c.getName(), a.getAmount(c)));
                                }
                            }
                            try {
                                List<String> mList = new ArrayList<>();
                                mList.add(
                                    stack.getDisplayName() + " | Meta: "
                                        + stack.getItemDamage()
                                        + " | Unlocal: "
                                        + stack.getUnlocalizedName());
                                for (Pair<String, Integer> r : aspectPairs) {
                                    if (r != null) {
                                        mList.add(r.getKey() + " x" + r.getValue());
                                    }
                                }
                                mList.add("");
                                if (mAspectCacheFile != null && mList.size() >= 3) {
                                    FileUtils.appendListToFile(mAspectCacheFile, mList);
                                }
                            } catch (Throwable t) {
                                Logger.INFO("Error while iterating one item. " + t);
                            }
                        }
                    }
                }
            }
            Logger.INFO(
                "Completed Aspect Iteration. AspectInfo.txt is now available to process in the GTplusplus configuration folder.");
            CommandDumpAspects.mLastScanTime = System.currentTimeMillis();
        }
    }
}