aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/api/objects/minecraft/SafeTexture.java
blob: 60bf94632cd43dfaf81cae8328a995a27baecc1f (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
package gtPlusPlus.api.objects.minecraft;

import java.util.HashMap;

import net.minecraft.util.IIcon;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.GregTechAPI;
import gtPlusPlus.core.util.Utils;

/**
 * A Server Side safe object that can hold {@link IIcon}s.
 *
 * @author Alkalus
 *
 */
public class SafeTexture implements Runnable {

    @SideOnly(Side.CLIENT)
    private static final HashMap<Integer, IIcon> mHashToIconCache = new HashMap<>();

    @SideOnly(Side.CLIENT)
    private static final HashMap<String, Integer> mPathToHashCash = new HashMap<>();

    private static final HashMap<String, SafeTexture> mTextureObjectCache = new HashMap<>();

    private final int mHash;

    private final String mTextureName;

    private static String getKey(String aTexPath) {
        String aNameKey = Utils.sanitizeString(aTexPath);
        aNameKey = aNameKey.replace('/', ' ');
        aNameKey = aNameKey.toLowerCase();
        return aNameKey;
    }

    public static SafeTexture register(String aTexturePath) {
        String aNameKey = getKey(aTexturePath);
        SafeTexture g = mTextureObjectCache.get(aNameKey);
        if (g == null) {
            g = new SafeTexture(aTexturePath);
            mTextureObjectCache.put(aNameKey, g);
            mPathToHashCash.put(aTexturePath, aTexturePath.hashCode());
        }
        return g;
    }

    private SafeTexture(String aTexturePath) {
        mTextureName = aTexturePath;
        mHash = getKey(aTexturePath).hashCode();
        GregTechAPI.sGTBlockIconload.add(this);
    }

    @SideOnly(Side.CLIENT)
    public IIcon getIcon() {
        return mHashToIconCache.get(mHash);
    }

    @Override
    public void run() {
        mHashToIconCache.put(getKey(mTextureName).hashCode(), GregTechAPI.sBlockIcons.registerIcon(mTextureName));
    }
}