aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/miscgui/minionhelper/loaders/repo/MinionHelperRepoMinionLoader.java
blob: 8d2aaaf44dac6768c0c1d634181fd48beed321b2 (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
/*
 * Copyright (C) 2022 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.miscgui.minionhelper.loaders.repo;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.core.util.StringUtils;
import io.github.moulberry.notenoughupdates.miscgui.minionhelper.Minion;
import io.github.moulberry.notenoughupdates.miscgui.minionhelper.MinionHelperManager;
import io.github.moulberry.notenoughupdates.miscgui.minionhelper.requirements.CollectionRequirement;
import io.github.moulberry.notenoughupdates.miscgui.minionhelper.requirements.ReputationRequirement;
import io.github.moulberry.notenoughupdates.miscgui.minionhelper.requirements.SlayerRequirement;
import io.github.moulberry.notenoughupdates.miscgui.minionhelper.sources.CraftingSource;
import io.github.moulberry.notenoughupdates.util.Utils;

import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Pattern;

public class MinionHelperRepoMinionLoader {

	private final MinionHelperRepoLoader repoLoader;
	private final MinionHelperManager manager;

	public MinionHelperRepoMinionLoader(MinionHelperRepoLoader repoLoader, MinionHelperManager manager) {
		this.repoLoader = repoLoader;
		this.manager = manager;
	}

	void loadMinionData() {
		TreeMap<String, JsonObject> itemInformation = NotEnoughUpdates.INSTANCE.manager.getItemInformation();

		for (Map.Entry<String, Minion> entry : manager.getAllMinions().entrySet()) {
			String internalName = entry.getKey();
			if (!itemInformation.containsKey(internalName)) continue;
			Minion minion = entry.getValue();

			JsonObject jsonObject = itemInformation.get(internalName);
			if (jsonObject.has("displayname")) {
				String displayName = jsonObject.get("displayname").getAsString();
				displayName = StringUtils.cleanColour(displayName);
				displayName = StringUtils.removeLastWord(displayName, " ");
				minion.setDisplayName(displayName);
			}

			if (jsonObject.has("recipe")) {
				loadRecipes(minion, jsonObject);
			}

			loadRequirements(minion, jsonObject);
		}
	}

	private void loadRequirements(Minion minion, JsonObject jsonObject) {
		for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
			String name = entry.getKey();
			if (name.endsWith("_req") || name.equals("crafttext")) {
				String value = entry.getValue().getAsString();

				try {
					switch (name) {
						case "reputation_req": {
							String[] split = value.split(":");
							String reputationType = split[0];
							int reputation = Integer.parseInt(split[1]);
							minion.getRequirements().add(new ReputationRequirement(reputationType, reputation));
							break;
						}
						case "crafttext": {
							if (minion.getTier() != 1) break;
							if (value.isEmpty()) break;

							String rawCollection = value.split(Pattern.quote(": "))[1];
							String cleanCollection = StringUtils.removeLastWord(rawCollection, " ");
							String rawTier = rawCollection.substring(cleanCollection.length() + 1);
							int tier = Utils.parseRomanNumeral(rawTier);
							minion.getRequirements().add(new CollectionRequirement(cleanCollection, tier));
							break;
						}
						case "slayer_req": {
							String[] split = value.split("_");
							String slayerType = split[0].toLowerCase(Locale.ROOT);
							int tier = Integer.parseInt(split[1]);
							minion.getRequirements().add(new SlayerRequirement(slayerType, tier));
							break;
						}
					}
				} catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
					repoLoader.errorWhileLoading = true;
					if (NotEnoughUpdates.INSTANCE.config.hidden.dev) {
						Utils.addChatMessage(
							"§c[NEU] Error in MinionHelperRepoLoader while loading repo entry " + minion.getDisplayName() + " " +
								minion.getTier() + ": " +
								e.getClass().getSimpleName() + ": " + e.getMessage());
					}
					e.printStackTrace();
				}
			}
		}
	}

	private void loadRecipes(Minion minion, JsonObject jsonObject) {
		JsonObject recipes = jsonObject.get("recipe").getAsJsonObject();
		RecipeBuilder builder = new RecipeBuilder(manager);
		for (Map.Entry<String, JsonElement> entry : recipes.entrySet()) {
			String rawString = entry.getValue().getAsString();

			builder.addLine(minion, rawString);
		}

		minion.setMinionSource(new CraftingSource(builder.getItems()));
		minion.setParent(builder.getParent());
	}
}