aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/thatgravyboat/skyblockhud/textures/Textures.java
blob: c77bd40f8d1ee53bce6250ffb6aa4662711823bf (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
package com.thatgravyboat.skyblockhud.textures;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.thatgravyboat.skyblockhud.SkyblockHud;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import net.minecraft.client.resources.IResource;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraft.util.ResourceLocation;

public class Textures implements IResourceManagerReloadListener {

    private static final TextureObject DEFAULT_TEXTURE = new TextureObject("Default");

    private static final Gson gson = new GsonBuilder().create();
    public static final List<TextureObject> styles = Lists.newArrayList(DEFAULT_TEXTURE);
    public static TextureObject texture = DEFAULT_TEXTURE;

    public static void setTexture(int selected) {
        if (selected >= styles.size() || selected < 0) {
            texture = DEFAULT_TEXTURE;
            SkyblockHud.config.misc.style = 0;
        } else {
            texture = styles.get(selected);
        }
    }

    @Override
    public void onResourceManagerReload(IResourceManager resourceManager) {
        styles.clear();
        styles.add(DEFAULT_TEXTURE);
        DEFAULT_TEXTURE.displayName = "Default";
        try {
            ResourceLocation stylesData = new ResourceLocation("skyblockhud:data/styles.json");

            for (IResource resource : resourceManager.getAllResources(stylesData)) {
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) {
                    JsonObject jsonObject = gson.fromJson(reader, JsonObject.class);
                    for (JsonElement json : jsonObject.getAsJsonArray("styles")) {
                        styles.add(TextureObject.decode((JsonObject) json));
                    }
                    if (DEFAULT_TEXTURE.displayName.equals("Default") && jsonObject.has("defaultDisplayName") && jsonObject.get("defaultDisplayName").isJsonPrimitive()) {
                        DEFAULT_TEXTURE.displayName = jsonObject.get("defaultDisplayName").getAsString();
                    }
                }
            }
        } catch (Exception ignored) {}

        if (SkyblockHud.config != null) setTexture(SkyblockHud.config.misc.style);
    }
}