aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/util/Constants.java
blob: a2cd4bb0fcdcf4a2dada99b6671b63e534bb0f0a (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
/*
 * Copyright (C) 2022-2024 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe;
import io.github.moulberry.notenoughupdates.events.RepositoryReloadEvent;
import io.github.moulberry.notenoughupdates.recipes.EssenceUpgrades;
import io.github.moulberry.notenoughupdates.recipes.NeuRecipe;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.lang.reflect.Type;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;

@NEUAutoSubscribe
public class Constants {

	private static class PatternSerializer implements JsonDeserializer<Pattern>, JsonSerializer<Pattern> {
		@Override
		public Pattern deserialize(
			JsonElement json,
			Type typeOfT,
			JsonDeserializationContext context
		) throws JsonParseException {
			return Pattern.compile(json.getAsString());
		}

		@Override
		public JsonElement serialize(Pattern src, Type typeOfSrc, JsonSerializationContext context) {
			return new JsonPrimitive(src.pattern());
		}
	}

	private static final Gson gson = new GsonBuilder()
		.setPrettyPrinting()
		.registerTypeAdapter(Pattern.class, new PatternSerializer())
		.create();

	public static JsonObject BONUSES;
	public static JsonObject DISABLE;
	public static JsonObject ENCHANTS;
	public static JsonObject LEVELING;
	public static JsonObject MISC;
	public static JsonObject PETNUMS;
	public static JsonObject PETS;
	public static JsonObject PARENTS;
	public static JsonObject ESSENCECOSTS;
	public static JsonObject FAIRYSOULS;
	public static JsonObject REFORGESTONES;
	public static JsonObject TROPHYFISH;
	public static JsonObject WEIGHT;
	public static JsonObject RNGSCORE;
	public static JsonObject ABIPHONE;
	public static JsonObject ESSENCESHOPS;
	public static JsonObject SBLEVELS;
	public static JsonObject MUSEUM;
	public static JsonObject BESTIARY;
	public static JsonObject SACKS;
	public static JsonObject HOPPITY;
	public static JsonObject DYES;
	public static JsonObject GEMSTONES;
	public static JsonObject GARDEN;

	private static final ReentrantLock lock = new ReentrantLock();

	@SubscribeEvent
	public void reload(RepositoryReloadEvent event) {
		try {
			lock.lock();

			BONUSES = Utils.getConstant("bonuses", gson);
			DISABLE = Utils.getConstant("disable", gson);
			ENCHANTS = Utils.getConstant("enchants", gson);
			LEVELING = Utils.getConstant("leveling", gson);
			MISC = Utils.getConstant("misc", gson);
			PETNUMS = Utils.getConstant("petnums", gson);
			PETS = Utils.getConstant("pets", gson);
			PARENTS = Utils.getConstant("parents", gson);
			ESSENCECOSTS = Utils.getConstant("essencecosts", gson);
			FAIRYSOULS = Utils.getConstant("fairy_souls", gson);
			REFORGESTONES = Utils.getConstant("reforgestones", gson);
			TROPHYFISH = Utils.getConstant("trophyfish", gson);
			WEIGHT = Utils.getConstant("weight", gson);
			RNGSCORE = Utils.getConstant("rngscore", gson);
			ABIPHONE = Utils.getConstant("abiphone", gson);
			ESSENCESHOPS = Utils.getConstant("essenceshops", gson);
			SBLEVELS = Utils.getConstant("sblevels", gson);
			MUSEUM = Utils.getConstant("museum", gson);
			BESTIARY = Utils.getConstant("bestiary", gson);
			SACKS = Utils.getConstant("sacks", gson);
			HOPPITY = Utils.getConstant("hoppity", gson);
			DYES = Utils.getConstant("dyes", gson);
			GEMSTONES = Utils.getConstant("gemstones", gson);
			GARDEN = Utils.getConstant("garden", gson);

			parseEssenceCosts();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			lock.unlock();
		}
	}

	public void parseEssenceCosts() {
		for (Map.Entry<String, JsonElement> entry : ESSENCECOSTS.entrySet()) {
			NeuRecipe parsed = EssenceUpgrades.parseFromEssenceCostEntry(entry);
			if (parsed != null) {
				NotEnoughUpdates.INSTANCE.manager.registerNeuRecipe(parsed);
			} else {
				System.out.println("NULL for: " + entry);
			}
		}
	}
}