aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/util/LateBindingChroma.java
blob: ace3afff8730b98ff3f84153dada331f3b0564ad (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
/*
 * 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 lombok.EqualsAndHashCode;
import lombok.Value;
import lombok.var;
import net.minecraft.client.Minecraft;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public abstract class LateBindingChroma {
	private static final Pattern FORMATTING_CODE = Pattern.compile("§(.)");

	public static LateBindingChroma of(String raw) {
		if (!raw.contains("§z"))
			return new LateBindingChroma.WithoutChroma(raw);
		var matcher = FORMATTING_CODE.matcher(raw);
		var chunks = new ArrayList<Chunk>();
		var sb = new StringBuffer();
		var color = "";
		var extraFormatting = "";
		while (matcher.find()) {
			sb.setLength(0);
			matcher.appendReplacement(sb, "");
			chunks.add(new Chunk(color, extraFormatting, sb.toString()));
			var c = matcher.group(1);
			if ("lnomk".contains(c)) {
				extraFormatting += "§" + c;
			} else {
				color = c;
				extraFormatting = "";
			}
		}
		sb.setLength(0);
		matcher.appendTail(sb);
		chunks.add(new Chunk(color, extraFormatting, sb.toString()));
		chunks.removeIf(it -> it.text.isEmpty());
		return new LateBindingChroma.WithChroma(chunks);
	}

	public abstract String render(float chromaOffset);

	@Value
	@EqualsAndHashCode(callSuper = false)
	static class WithoutChroma extends LateBindingChroma {
		String content;

		@Override
		public String render(float chromaOffset) {
			return content;
		}
	}

	@Value
	@EqualsAndHashCode(callSuper = false)
	static class WithChroma extends LateBindingChroma {
		List<Chunk> chunks;

		@Override
		public String render(float offset) {
			var sb = new StringBuilder();
			for (Chunk chunk : chunks) {
				String text;
				if (chunk.color.equals("z")) {
					text = Utils.chromaString(chunk.text, offset, chunk.extraFormatting);
				} else {
					text = (chunk.color.isEmpty() ? "" : ("§" + chunk.color)) + chunk.extraFormatting + chunk.text;
				}
				sb.append(text);
				offset += Minecraft.getMinecraft().fontRendererObj.getStringWidth(text);
			}
			return sb.toString();
		}
	}

	@Value
	public static class Chunk {
		String color;
		String extraFormatting;
		String text;
	}

}