aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/item/enchants/EnchantMatcher.java
blob: 82b3a5977ea8130c2591fa714c4315d8b724c0f8 (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
/*
 * 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 com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.miscgui.GuiEnchantColour;
import io.github.moulberry.notenoughupdates.util.LRUCache;
import lombok.Value;
import net.minecraft.util.EnumChatFormatting;
import net.minecraftforge.fml.common.Loader;

import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

@Value
public class EnchantMatcher {
	public static final String GROUP_ENCHANT_NAME = "enchantName";
	public static final String GROUP_LEVEL = "level";

	private final static String romanNumerals =
		"(I|II|III|IV|V|VI|VII|VIII|IX|X|XI|XII|XIII|XIV|XV|XVI|XVII|XVIII|XIX|XX)";
	private final static Supplier<String> validColors = Suppliers.memoize(() ->
		"0123456789abcdefzZ");

	public static LRUCache<String, Optional<EnchantMatcher>> fromSaveFormatMemoized =
		LRUCache.memoize(
			EnchantMatcher::fromSaveFormat,
			() -> NotEnoughUpdates.INSTANCE.config.hidden.enchantColours.size() + 1
		);

	Pattern patternWithLevels;
	int compareWith;
	String formatting;
	char compareUsing;

	/**
	 * Use {@link #fromSaveFormatMemoized} instead.
	 */
	@Deprecated
	public static Optional<EnchantMatcher> fromSaveFormat(String saveFormat) {
		List<String> colourOps = GuiEnchantColour.splitter.splitToList(saveFormat);
		String enchantName = GuiEnchantColour.getColourOpIndex(colourOps, 0);
		String comparator = GuiEnchantColour.getColourOpIndex(colourOps, 1);
		String comparison = GuiEnchantColour.getColourOpIndex(colourOps, 2);
		String colourCode = GuiEnchantColour.getColourOpIndex(colourOps, 3);
		String modifier = GuiEnchantColour.getColourOpIndex(colourOps, 4);

		int intModifier = GuiEnchantColour.getIntModifier(modifier);

		if (comparator.length() != 1
			|| colourCode.length() != 1
			|| comparison.isEmpty()
			|| enchantName.isEmpty()) return Optional.empty();

		if (!">=<".contains(comparator)) return Optional.empty();
		int compareWith;
		try {
			compareWith = Integer.parseInt(comparison);
		} catch (NumberFormatException e) {
			return Optional.empty();
		}

		if (!validColors.get().contains(colourCode)) return Optional.empty();
		Pattern patternWithLevels;
		try {
			patternWithLevels = Pattern.compile(
				"(§b|§9|§([b9l])§d§l)(?<" + GROUP_ENCHANT_NAME + ">" + enchantName + ") " +
					"(?<" + GROUP_LEVEL + ">\\d+|" + romanNumerals + ")(?:(?:§9)?,|(?: §8(?:,?[0-9]+)*)?$)");
		} catch (PatternSyntaxException e) {
			NotEnoughUpdates.LOGGER.warn("Invalid pattern constructed for enchant matching", e);
			return Optional.empty();
		}

		String formatting = "§" + colourCode;

		if ((intModifier & GuiEnchantColour.BOLD_MODIFIER) != 0) {
			formatting += EnumChatFormatting.BOLD;
		}
		if ((intModifier & GuiEnchantColour.ITALIC_MODIFIER) != 0) {
			formatting += EnumChatFormatting.ITALIC;
		}
		if ((intModifier & GuiEnchantColour.UNDERLINE_MODIFIER) != 0) {
			formatting += EnumChatFormatting.UNDERLINE;
		}
		if ((intModifier & GuiEnchantColour.OBFUSCATED_MODIFIER) != 0) {
			formatting += EnumChatFormatting.OBFUSCATED;
		}
		if ((intModifier & GuiEnchantColour.STRIKETHROUGH_MODIFIER) != 0) {
			formatting += EnumChatFormatting.STRIKETHROUGH;
		}

		return Optional.of(new EnchantMatcher(patternWithLevels, compareWith, formatting, comparator.charAt(0)));
	}

	public boolean doesLevelMatch(int level) {
		switch (compareUsing) {
			case '>':
				return level > compareWith;
			case '=':
				return level == compareWith;
			case '<':
				return level < compareWith;
		}
		return false;
	}

}