aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/pollution/BlockMatcher.java
blob: a701d323bdbe2e5d974ff3bab0cb9a6991bc9f33 (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
package gregtech.common.pollution;

import java.util.Map;
import java.util.Set;

import net.minecraft.block.Block;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraftforge.event.world.WorldEvent;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.registry.FMLControlledNamespacedRegistry;
import cpw.mods.fml.common.registry.GameData;
import gregtech.GTMod;

// Shamelessly Taken from BetterFoliage by octarine-noise
public class BlockMatcher {

    public Map<Class<?>, ColorOverrideType> whiteList = Maps.newHashMap();
    public Set<Class<?>> blackList = Sets.newHashSet();
    public Map<Integer, ColorOverrideType> blockIDs = Maps.newHashMap();

    public ColorOverrideType matchesID(int blockId) {
        return blockIDs.get(blockId);
    }

    public ColorOverrideType matchesID(Block block) {
        return blockIDs.get(Block.blockRegistry.getIDForObject(block));
    }

    public void updateClassList(String[] cfg) {
        whiteList.clear();
        blackList.clear();
        for (String line : cfg) {
            GTMod.GT_FML_LOGGER.info("Checking for block:" + line);
            String[] lines = line.split(":");
            ColorOverrideType type = null;
            if (lines.length > 1) {
                try {
                    type = ColorOverrideType.fromName(lines[1].trim());
                } catch (NumberFormatException e) {
                    GTMod.GT_FML_LOGGER.error(String.format("Invalid type [%s]", line));
                    continue;
                }
            }

            if (lines[0].startsWith("-")) {
                try {
                    blackList.add(Class.forName(lines[0].substring(1)));
                    GTMod.GT_FML_LOGGER.info("\t added blacklist:" + lines[0].substring(1));
                } catch (ClassNotFoundException ignored) {}
            } else {
                if (type == null) {
                    GTMod.GT_FML_LOGGER.error(String.format("Invalid type [%s]", line));
                    continue;
                }

                try {
                    whiteList.put(Class.forName(lines[0]), type);
                    GTMod.GT_FML_LOGGER.info("\t added whitelist:" + lines[0]);
                } catch (ClassNotFoundException ignored) {}
            }
        }
        // updateBlockIDs();
    }

    private void updateBlockIDs() {
        blockIDs.clear();
        FMLControlledNamespacedRegistry<Block> blockRegistry = GameData.getBlockRegistry();
        for (Block block : blockRegistry.typeSafeIterable()) {
            ColorOverrideType t = matchesClass(block);
            if (t != null) blockIDs.put(Block.blockRegistry.getIDForObject(block), t);
        }
    }

    private ColorOverrideType matchesClass(Block block) {
        for (Class<?> clazz : blackList) if (clazz.isAssignableFrom(block.getClass())) return null;
        for (Class<?> clazz : whiteList.keySet())
            if (clazz.isAssignableFrom(block.getClass())) return whiteList.get(clazz);
        return null;
    }

    /**
     * Caches block IDs on world load for fast lookup
     *
     * @param event
     */
    @SubscribeEvent
    public void handleWorldLoad(WorldEvent.Load event) {
        if (event.world instanceof WorldClient) {
            updateBlockIDs();
        }
    }
}