aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/util/ItemUtils.java
blob: 8b81d1b427201a5079b1d7f6211b1898cb1e949c (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * 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.util;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.listener.ItemTooltipListener;
import io.github.moulberry.notenoughupdates.miscfeatures.PetInfoOverlay;
import io.github.moulberry.notenoughupdates.profileviewer.GuiProfileViewer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTException;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.MathHelper;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.BiFunction;

public class ItemUtils {

	public static ItemStack getCoinItemStack(long coinAmount) {
		String uuid = "2070f6cb-f5db-367a-acd0-64d39a7e5d1b";
		String texture =
			"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNTM4MDcxNzIxY2M1YjRjZDQwNmNlNDMxYTEzZjg2MDgzYTg5NzNlMTA2NGQyZjg4OTc4Njk5MzBlZTZlNTIzNyJ9fX0=";
		if (coinAmount >= 100000) {
			uuid = "94fa2455-2881-31fe-bb4e-e3e24d58dbe3";
			texture =
				"eyJ0aW1lc3RhbXAiOjE2MzU5NTczOTM4MDMsInByb2ZpbGVJZCI6ImJiN2NjYTcxMDQzNDQ0MTI4ZDMwODllMTNiZGZhYjU5IiwicHJvZmlsZU5hbWUiOiJsYXVyZW5jaW8zMDMiLCJzaWduYXR1cmVSZXF1aXJlZCI6dHJ1ZSwidGV4dHVyZXMiOnsiU0tJTiI6eyJ1cmwiOiJodHRwOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlL2M5Yjc3OTk5ZmVkM2EyNzU4YmZlYWYwNzkzZTUyMjgzODE3YmVhNjQwNDRiZjQzZWYyOTQzM2Y5NTRiYjUyZjYiLCJtZXRhZGF0YSI6eyJtb2RlbCI6InNsaW0ifX19fQo=";
		}
		if (coinAmount >= 10000000) {
			uuid = "0af8df1f-098c-3b72-ac6b-65d65fd0b668";
			texture =
				"ewogICJ0aW1lc3RhbXAiIDogMTYzNTk1NzQ4ODQxNywKICAicHJvZmlsZUlkIiA6ICJmNThkZWJkNTlmNTA0MjIyOGY2MDIyMjExZDRjMTQwYyIsCiAgInByb2ZpbGVOYW1lIiA6ICJ1bnZlbnRpdmV0YWxlbnQiLAogICJzaWduYXR1cmVSZXF1aXJlZCIgOiB0cnVlLAogICJ0ZXh0dXJlcyIgOiB7CiAgICAiU0tJTiIgOiB7CiAgICAgICJ1cmwiIDogImh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvN2I5NTFmZWQ2YTdiMmNiYzIwMzY5MTZkZWM3YTQ2YzRhNTY0ODE1NjRkMTRmOTQ1YjZlYmMwMzM4Mjc2NmQzYiIsCiAgICAgICJtZXRhZGF0YSIgOiB7CiAgICAgICAgIm1vZGVsIiA6ICJzbGltIgogICAgICB9CiAgICB9CiAgfQp9";
		}
		ItemStack skull = Utils.createSkull(
			"\u00A7r\u00A76" + NumberFormat.getInstance(Locale.US).format(coinAmount) + " Coins",
			uuid,
			texture
		);
		NBTTagCompound extraAttributes = skull.getTagCompound().getCompoundTag("ExtraAttributes");
		extraAttributes.setString("id", "SKYBLOCK_COIN");
		skull.getTagCompound().setTag("ExtraAttributes", extraAttributes);
		return skull;
	}

	public static ItemStack createQuestionMarkSkull(String label) {
		return Utils.createSkull(
			label,
			"00000000-0000-0000-0000-000000000000",
			"bc8ea1f51f253ff5142ca11ae45193a4ad8c3ab5e9c6eec8ba7a4fcb7bac40"
		);
	}

	public static NBTTagCompound getOrCreateTag(ItemStack is) {
		if (is.hasTagCompound()) return is.getTagCompound();
		NBTTagCompound nbtTagCompound = new NBTTagCompound();
		is.setTagCompound(nbtTagCompound);
		return nbtTagCompound;
	}

	public static void appendLore(ItemStack is, List<String> moreLore) {
		NBTTagCompound tagCompound = is.getTagCompound();
		if (tagCompound == null) {
			tagCompound = new NBTTagCompound();
		}
		NBTTagCompound display = tagCompound.getCompoundTag("display");
		NBTTagList lore = display.getTagList("Lore", 8);
		for (String s : moreLore) {
			lore.appendTag(new NBTTagString(s));
		}
		display.setTag("Lore", lore);
		tagCompound.setTag("display", display);
		is.setTagCompound(tagCompound);
	}

	public static List<String> getLore(ItemStack is) {
		return getLore(is.getTagCompound());
	}

	public static List<String> getLore(NBTTagCompound tagCompound) {
		if (tagCompound == null) {
			return Collections.emptyList();
		}
		NBTTagList tagList = tagCompound.getCompoundTag("display").getTagList("Lore", 8);
		List<String> list = new ArrayList<>();
		for (int i = 0; i < tagList.tagCount(); i++) {
			list.add(tagList.getStringTagAt(i));
		}
		return list;
	}

	public static String getDisplayName(NBTTagCompound compound) {
		if (compound == null) return null;
		String string = compound.getCompoundTag("display").getString("Name");
		if (string == null || string.isEmpty())
			return null;
		return string;
	}

	public static String fixEnchantId(String enchId, boolean useId) {
		if (Constants.ENCHANTS != null && Constants.ENCHANTS.has("enchant_mapping_id") &&
			Constants.ENCHANTS.has("enchant_mapping_item")) {
			JsonArray mappingFrom = Constants.ENCHANTS.getAsJsonArray("enchant_mapping_" + (useId ? "id" : "item"));
			JsonArray mappingTo = Constants.ENCHANTS.getAsJsonArray("enchant_mapping_" + (useId ? "item" : "id"));

			for (int i = 0; i < mappingFrom.size(); i++) {
				if (mappingFrom.get(i).getAsString().equals(enchId)) {
					return mappingTo.get(i).getAsString();
				}
			}

		}
		return enchId;
	}

	/**
	 * Mutates baseValues
	 */
	public static <T> void modifyReplacement(
		Map<String, String> baseValues,
		Map<String, T> modifiers,
		BiFunction<String, T, String> mapper
	) {
		if (modifiers == null || baseValues == null) return;
		for (Map.Entry<String, T> modifier : modifiers.entrySet()) {
			String baseValue = baseValues.get(modifier.getKey());
			if (baseValue == null) continue;
			try {
				baseValues.put(modifier.getKey(), mapper.apply(baseValue, modifier.getValue()));
			} catch (Exception e) {
				System.out.println("Exception during replacement mapping: ");
				e.printStackTrace();
			}
		}
	}

	public static String applyReplacements(Map<String, String> replacements, String text) {
		for (Map.Entry<String, String> replacement : replacements.entrySet()) {
			String search = "{" + replacement.getKey() + "}";
			text = text.replace(search, replacement.getValue());
		}
		return text;
	}

	public static ItemStack createPetItemstackFromPetInfo(PetInfoOverlay.Pet currentPet) {
		String petname = currentPet.petType;
		String tier = Utils.getRarityFromInt(currentPet.rarity.petId).toUpperCase();
		String heldItem = currentPet.petItem;
		String skin = currentPet.skin;
		JsonObject heldItemJson = heldItem == null ? null : NotEnoughUpdates.INSTANCE.manager.getItemInformation().get(
			heldItem);
		String petId = currentPet.getPetId(false);
		float exp = currentPet.petLevel.totalXp;

		GuiProfileViewer.PetLevel levelObj = GuiProfileViewer.getPetLevel(petname, tier, exp);

		float level = levelObj.level;

		ItemStack petItemstack = NotEnoughUpdates.INSTANCE.manager
			.createItemResolutionQuery()
			.withKnownInternalName(petId)
			.resolveToItemStack(false);
		if (petItemstack == null) {
			petItemstack = ItemUtils.createQuestionMarkSkull(EnumChatFormatting.RED + "Unknown Pet");
			appendLore(petItemstack, Arrays.asList(
				"§cThis pet is not saved in the repository",
				"",
				"§cIf you expected it to be there please send a message in",
				"§c§l#neu-support §r§con §ldiscord.gg/moulberry"
			));
		}
		Map<String,