aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/item/enchants/EnchantStyleCustomizer.java
blob: 82db0b30b294289241f873051f162721e96a477c (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
/*
 * 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.miscfeatures.item.enchants;

import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe;
import io.github.moulberry.notenoughupdates.util.LRUCache;
import io.github.moulberry.notenoughupdates.util.LateBindingChroma;
import io.github.moulberry.notenoughupdates.util.Utils;
import lombok.var;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;

@NEUAutoSubscribe
public class EnchantStyleCustomizer {

	public static EnchantStyleCustomizer INSTANCE = new EnchantStyleCustomizer();

	LRUCache<String, LateBindingChroma> enchantLineCache = LRUCache.memoize(this::replaceEnchantLine, 1000);
	List<String> lastEnchant = new ArrayList<>();

	public LateBindingChroma replaceEnchantLine(String originalLine) {
		var line = originalLine;
		Set<String> alreadyReplacedEnchants = new HashSet<>();
		for (String enchantMatcherStr : NotEnoughUpdates.INSTANCE.config.hidden.enchantColours) {
			var enchantMatcherP = EnchantMatcher.fromSaveFormatMemoized.apply(enchantMatcherStr);
			if (!enchantMatcherP.isPresent()) continue;
			var enchantMatcher = enchantMatcherP.get();
			Matcher matcher;
			var matchIterations = 0;
			var last = 0;
			while ((matcher = enchantMatcher.getPatternWithLevels().matcher(line)).find(last) && matchIterations++ < 5) {
				var enchantName = matcher.group(EnchantMatcher.GROUP_ENCHANT_NAME);
				var levelText = matcher.group(EnchantMatcher.GROUP_LEVEL);
				if (enchantName == null || levelText == null
					|| levelText.isEmpty() || enchantName.isEmpty()) continue;
				String cleanEnchantName = Utils.cleanColour(enchantName);
				if (cleanEnchantName.startsWith(" ")) {
					last = matcher.end();
					continue;
				}

				var level = Utils.parseIntOrRomanNumeral(levelText);
				if (!enchantMatcher.doesLevelMatch(level)) {
					last = matcher.end();
					continue;
				}

				if (alreadyReplacedEnchants.contains(cleanEnchantName)) continue;
				alreadyReplacedEnchants.add(cleanEnchantName);

				var startMatch = matcher.start();
				var endLevel = matcher.end(EnchantMatcher.GROUP_LEVEL);

				var parsed = line.substring(0, startMatch)
					+ enchantMatcher.getFormatting() + enchantName + " " + levelText;
				line = parsed + (endLevel >= line.length() ? "" : line.substring(endLevel));
				last = parsed.length();
			}
		}
		return LateBindingChroma.of(line);
	}

	public void cacheInvalidate() {
		enchantLineCache.clearCache();
	}

	@SubscribeEvent
	public void onItemTooltip(ItemTooltipEvent event) {
		var nbt = event.itemStack.getTagCompound();
		if (nbt == null) return;
		var extraAttributes = nbt.getCompoundTag("ExtraAttributes");
		var enchantments = extraAttributes.getCompoundTag("enchantments");
		var attributes = extraAttributes.getCompoundTag("attributes");
		enchantments.merge(attributes);
		if (enchantments.getKeySet().isEmpty()) return;
		if (!lastEnchant.equals(NotEnoughUpdates.INSTANCE.config.hidden.enchantColours)
			|| !NotEnoughUpdates.INSTANCE.config.misc.cacheItemEnchant) {
			cacheInvalidate();
			lastEnchant = new ArrayList<>(NotEnoughUpdates.INSTANCE.config.hidden.enchantColours);
		}
		var lineIndex = 0;
		for (var iterator = event.toolTip.listIterator(); iterator.hasNext(); ) {
			var nextLine = iterator.next();
			var replacedLine = enchantLineCache.apply(nextLine).render(lineIndex++);
			if (!nextLine.equals(replacedLine)) {
				iterator.set(replacedLine);
			}
		}
	}

}