aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/loaders/misc/GT_JsonLoader.java
blob: d46c7eae0b8833ae9a1b2b1327aa17d08af29fa5 (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
package gregtech.loaders.misc;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;

import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.IReloadableResourceManager;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraft.util.ResourceLocation;

import org.json.JSONObject;

import gregtech.api.util.GT_Log;

@SideOnly(Side.CLIENT)
public class GT_JsonLoader implements IResourceManagerReloadListener {
	private JSONObject json;
	private ResourceLocation jsonLocation;
	private IResourceManager resourceManager;

	public String getString(String key) {
		String s = "";
		try {
			s = this.json.getString(key);
		}
		catch (Exception e) {
			GT_Log.err.println("GT_JsonLoader" + e);
		}
		return s;
	}

	public void loadJson() {
		this.json = new JSONObject("{}");
		try {
			BufferedInputStream bis = new BufferedInputStream(this.resourceManager.getResource(this.jsonLocation).getInputStream());
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			
			for (int result = bis.read(); result != -1; result = bis.read()) {
				bos.write((byte)result);
			}

			this.json = new JSONObject(bos.toString("UTF-8"));
		}
		catch (Exception e) {
			GT_Log.err.println("GT_JsonLoader: " + e);
		}
	}

	public void onResourceManagerReload(IResourceManager rm) {
		loadJson();
	}

	public GT_JsonLoader (String resourcePath) {
		GT_Log.err.println("GT_JsonLoader: Init");
		this.jsonLocation = new ResourceLocation("gregtech", resourcePath);
		this.resourceManager = Minecraft.getMinecraft().getResourceManager();
		((IReloadableResourceManager) Minecraft.getMinecraft().getResourceManager()).registerReloadListener(this);
		loadJson();
	}
}