|
5 | 5 | import net.minecraft.text.*; |
6 | 6 | import net.minecraft.util.Formatting; |
7 | 7 | import net.minecraft.util.Identifier; |
| 8 | +import net.minecraft.util.math.MathHelper; |
8 | 9 |
|
9 | 10 | import java.util.*; |
10 | 11 | import java.util.regex.Matcher; |
@@ -84,9 +85,8 @@ public static int recursiveParsing(MutableText text, String input, Map<String, T |
84 | 85 | String end = "</" + tag + ">"; |
85 | 86 |
|
86 | 87 | TextParser.TextFormatterHandler handler = handlers.get(tag); |
87 | | - currentPos = matcher.end(); |
88 | | - |
89 | 88 | if (handler != null) { |
| 89 | + currentPos = matcher.end(); |
90 | 90 | try { |
91 | 91 | int toIgnore = handler.parse(tag, data, text, input.substring(currentPos), handlers, end); |
92 | 92 | currentPos += toIgnore; |
@@ -137,11 +137,16 @@ public static void register() { |
137 | 137 | }); |
138 | 138 | } |
139 | 139 |
|
140 | | - TextParser.register("color", (String tag, String data, MutableText text, String input, Map<String, TextParser.TextFormatterHandler> handlers, String endAt) -> { |
141 | | - MutableText out = new LiteralText("").fillStyle(Style.EMPTY.withColor(TextColor.parse(cleanArgument(data)))); |
142 | | - text.append(out); |
143 | | - return recursiveParsing(out, input, handlers, endAt); |
144 | | - }); |
| 140 | + { |
| 141 | + TextParser.TextFormatterHandler color = (String tag, String data, MutableText text, String input, Map<String, TextParser.TextFormatterHandler> handlers, String endAt) -> { |
| 142 | + MutableText out = new LiteralText("").fillStyle(Style.EMPTY.withColor(TextColor.parse(cleanArgument(data)))); |
| 143 | + text.append(out); |
| 144 | + return recursiveParsing(out, input, handlers, endAt); |
| 145 | + }; |
| 146 | + |
| 147 | + TextParser.register("color", color); |
| 148 | + TextParser.register("c", color); |
| 149 | + } |
145 | 150 |
|
146 | 151 | TextParser.register("font", (String tag, String data, MutableText text, String input, Map<String, TextParser.TextFormatterHandler> handlers, String endAt) -> { |
147 | 152 | MutableText out = new LiteralText("").fillStyle(Style.EMPTY.withFont(Identifier.tryParse(cleanArgument(data)))); |
@@ -209,88 +214,122 @@ public static void register() { |
209 | 214 | return recursiveParsing(out, input, handlers, endAt); |
210 | 215 | }); |
211 | 216 |
|
212 | | - TextParser.register("rainbow", (String tag, String data, MutableText text, String input, Map<String, TextParser.TextFormatterHandler> handlers, String endAt) -> { |
213 | | - MutableText out = new LiteralText(""); |
214 | | - String[] val = data.split(":"); |
215 | | - float freq = 1; |
216 | | - float saturation = 1; |
217 | | - float offset = 0; |
218 | | - |
219 | | - if (val.length >= 1) { |
220 | | - try { |
221 | | - freq = Float.parseFloat(val[0]); |
222 | | - } catch (Exception e) { |
| 217 | + { |
| 218 | + TextParser.TextFormatterHandler rainbow = (String tag, String data, MutableText text, String input, Map<String, TextParser.TextFormatterHandler> handlers, String endAt) -> { |
| 219 | + MutableText out = new LiteralText(""); |
| 220 | + String[] val = data.split(":"); |
| 221 | + float freq = 1; |
| 222 | + float saturation = 1; |
| 223 | + float offset = 0; |
| 224 | + |
| 225 | + if (val.length >= 1) { |
| 226 | + try { |
| 227 | + freq = Float.parseFloat(val[0]); |
| 228 | + } catch (Exception e) { |
| 229 | + } |
223 | 230 | } |
224 | | - } |
225 | | - if (val.length >= 2) { |
226 | | - try { |
227 | | - saturation = Float.parseFloat(val[1]); |
228 | | - } catch (Exception e) { |
| 231 | + if (val.length >= 2) { |
| 232 | + try { |
| 233 | + saturation = Float.parseFloat(val[1]); |
| 234 | + } catch (Exception e) { |
| 235 | + } |
229 | 236 | } |
230 | | - } |
231 | | - if (val.length >= 3) { |
232 | | - try { |
233 | | - offset = Float.parseFloat(val[2]); |
234 | | - } catch (Exception e) { |
| 237 | + if (val.length >= 3) { |
| 238 | + try { |
| 239 | + offset = Float.parseFloat(val[2]); |
| 240 | + } catch (Exception e) { |
| 241 | + } |
235 | 242 | } |
236 | | - } |
237 | 243 |
|
238 | | - int toIgnore = recursiveParsing(out, input, handlers, endAt); |
239 | | - String flatString = GeneralUtils.textToString(out); |
| 244 | + int toIgnore = recursiveParsing(out, input, handlers, endAt); |
| 245 | + String flatString = GeneralUtils.textToString(out); |
240 | 246 |
|
241 | | - final float finalFreq = freq; |
242 | | - final float finalOffset = offset; |
243 | | - final float finalSaturation = saturation; |
| 247 | + final float finalFreq = freq; |
| 248 | + final float finalOffset = offset; |
| 249 | + final float finalSaturation = saturation; |
244 | 250 |
|
245 | | - text.append(GeneralUtils.toGradient(out, (pos) -> TextColor.fromRgb(GeneralUtils.hvsToRgb(((pos * finalFreq) / (flatString.length() + 1) + finalOffset) % 1, finalSaturation, 1)))); |
| 251 | + text.append(GeneralUtils.toGradient(out, (pos) -> TextColor.fromRgb(GeneralUtils.hvsToRgb(((pos * finalFreq) / (flatString.length() + 1) + finalOffset) % 1, finalSaturation, 1)))); |
246 | 252 |
|
247 | | - return toIgnore; |
248 | | - }); |
| 253 | + return toIgnore; |
| 254 | + }; |
249 | 255 |
|
250 | | - TextParser.register("gradient", (String tag, String data, MutableText text, String input, Map<String, TextParser.TextFormatterHandler> handlers, String endAt) -> { |
251 | | - MutableText out = new LiteralText(""); |
252 | | - String[] val = data.split(":"); |
253 | | - |
254 | | - int toIgnore = recursiveParsing(out, input, handlers, endAt); |
255 | | - String flatString = GeneralUtils.textToString(out); |
256 | | - List<TextColor> textColors = new ArrayList<>(); |
257 | | - for (String string : val) { |
258 | | - TextColor color = TextColor.parse(string); |
259 | | - if (color != null) { |
260 | | - textColors.add(color); |
261 | | - } |
262 | | - } |
| 256 | + TextParser.register("rainbow", rainbow); |
| 257 | + TextParser.register("rb", rainbow); |
| 258 | + } |
263 | 259 |
|
264 | | - final double step = ((double) textColors.size() - 1) / flatString.length(); |
265 | | - final int sectionSize = (textColors.size() - 1) / (flatString.length() + 1); |
266 | | - |
267 | | - GeneralUtils.HSV hsv = GeneralUtils.rgbToHsv(textColors.get(0).getRgb()); |
268 | | - AtomicDouble hue = new AtomicDouble(hsv.h()); |
269 | | - AtomicDouble saturation = new AtomicDouble(hsv.s()); |
270 | | - AtomicDouble value = new AtomicDouble(hsv.v()); |
271 | | - |
272 | | - text.append(GeneralUtils.toGradient(out, (pos) -> { |
273 | | - GeneralUtils.HSV colorA = GeneralUtils.rgbToHsv(textColors.get(pos * sectionSize).getRgb()); |
274 | | - GeneralUtils.HSV colorB = GeneralUtils.rgbToHsv(textColors.get(pos * sectionSize + 1).getRgb()); |
275 | | - float sym = Math.abs(colorB.h() - colorA.h()) > Math.abs(colorA.h() - colorB.h()) ? -1 : 1; |
276 | | - float h = colorB.h() - colorA.h(); |
277 | | - float delta = (h + ((Math.abs(h) > 0.5) ? ((h < 0) ? 1 : -1) : 0)); |
278 | | - |
279 | | - float localHue = (float) hue.get(); |
280 | | - float futureHue = (float) (localHue + delta * step); |
281 | | - if (futureHue < 0) { |
282 | | - futureHue += 1; |
| 260 | + { |
| 261 | + TextParser.TextFormatterHandler gradient = (String tag, String data, MutableText text, String input, Map<String, TextParser.TextFormatterHandler> handlers, String endAt) -> { |
| 262 | + MutableText out = new LiteralText(""); |
| 263 | + String[] val = data.split(":"); |
| 264 | + |
| 265 | + int toIgnore = recursiveParsing(out, input, handlers, endAt); |
| 266 | + String flatString = GeneralUtils.textToString(out); |
| 267 | + List<TextColor> textColors = new ArrayList<>(); |
| 268 | + for (String string : val) { |
| 269 | + TextColor color = TextColor.parse(string); |
| 270 | + if (color != null) { |
| 271 | + textColors.add(color); |
| 272 | + } |
| 273 | + } |
| 274 | + if (textColors.size() == 0) { |
| 275 | + textColors.add(TextColor.fromFormatting(Formatting.WHITE)); |
| 276 | + textColors.add(TextColor.fromFormatting(Formatting.WHITE)); |
| 277 | + } else if (textColors.size() == 1) { |
| 278 | + textColors.add(textColors.get(0)); |
283 | 279 | } |
284 | | - hue.set(futureHue); |
285 | 280 |
|
286 | | - return TextColor.fromRgb(GeneralUtils.hvsToRgb( |
287 | | - localHue, |
288 | | - (float) saturation.getAndAdd((colorB.s() - colorA.s()) * step), |
289 | | - (float) value.getAndAdd((colorB.v() - colorA.v()) * step))); |
290 | | - })); |
| 281 | + final double step = ((double) textColors.size() - 1) / flatString.length(); |
| 282 | + final float sectionSize = ((float) textColors.size() - 1) / (flatString.length() + 1); |
291 | 283 |
|
292 | | - return toIgnore; |
293 | | - }); |
| 284 | + GeneralUtils.HSV hsv = GeneralUtils.rgbToHsv(textColors.get(0).getRgb()); |
| 285 | + AtomicDouble hue = new AtomicDouble(hsv.h()); |
| 286 | + AtomicDouble saturation = new AtomicDouble(hsv.s()); |
| 287 | + AtomicDouble value = new AtomicDouble(hsv.v()); |
| 288 | + |
| 289 | + text.append(GeneralUtils.toGradient(out, (pos) -> { |
| 290 | + GeneralUtils.HSV colorA = GeneralUtils.rgbToHsv(textColors.get((int) (pos * sectionSize)).getRgb()); |
| 291 | + GeneralUtils.HSV colorB = GeneralUtils.rgbToHsv(textColors.get((int) (pos * sectionSize) + 1).getRgb()); |
| 292 | + |
| 293 | + float localHue = (float) hue.get(); |
| 294 | + { |
| 295 | + float h = colorB.h() - colorA.h(); |
| 296 | + float delta = (h + ((Math.abs(h) > 0.50001) ? ((h < 0) ? 1 : -1) : 0)); |
| 297 | + |
| 298 | + float futureHue = (float) (localHue + delta * step); |
| 299 | + if (futureHue < 0) { |
| 300 | + futureHue += 1; |
| 301 | + } else if (futureHue > 1) { |
| 302 | + futureHue -= 1; |
| 303 | + } |
| 304 | + hue.set(futureHue); |
| 305 | + } |
| 306 | + |
| 307 | + float localSat = (float) saturation.get(); |
| 308 | + { |
| 309 | + float s = colorB.s() - colorA.s(); |
| 310 | + float futureSat = MathHelper.clamp((float) (localSat + s * step), 0, 1); |
| 311 | + saturation.set(futureSat); |
| 312 | + } |
| 313 | + |
| 314 | + float localVal = (float) value.get(); |
| 315 | + { |
| 316 | + float v = colorB.v() - colorA.v(); |
| 317 | + float futureVal = MathHelper.clamp((float) (localVal + v * step), 0, 1); |
| 318 | + value.set(futureVal); |
| 319 | + } |
| 320 | + |
| 321 | + return TextColor.fromRgb(GeneralUtils.hvsToRgb( |
| 322 | + localHue, |
| 323 | + localSat, |
| 324 | + localVal)); |
| 325 | + })); |
| 326 | + |
| 327 | + return toIgnore; |
| 328 | + }; |
| 329 | + |
| 330 | + TextParser.register("gradient", gradient); |
| 331 | + TextParser.register("gr", gradient); |
| 332 | + } |
294 | 333 |
|
295 | 334 | ESCAPED_CHARS.put("\\\\", "&slsh;"); |
296 | 335 | ESCAPED_CHARS.put("\\<", "<"); |
|
0 commit comments