From 30009bab7bab35bedc44d5b18c73ad48405dec92 Mon Sep 17 00:00:00 2001
From: Patbox <39821509+Patbox@users.noreply.github.com>
Date: Mon, 31 May 2021 19:30:48 +0200
Subject: [PATCH] Some documentation about text tags, fix gradient
---
TEXT_FORMATTING.md | 42 ++++
gradle.properties | 6 +-
.../java/eu/pb4/placeholders/TextParser.java | 8 +
.../placeholders/util/TextParserUtils.java | 193 +++++++++++-------
4 files changed, 169 insertions(+), 80 deletions(-)
create mode 100644 TEXT_FORMATTING.md
diff --git a/TEXT_FORMATTING.md b/TEXT_FORMATTING.md
new file mode 100644
index 0000000..df67d25
--- /dev/null
+++ b/TEXT_FORMATTING.md
@@ -0,0 +1,42 @@
+# About tags/TextParser
+TextParser is a small utility for allowing user to create Text Components in simpler way.
+It works in similar way to HTML, however it's structure has some differences.
+Tags have structure of `` or ``. Most of tags needs to be closed by using ``,
+but there are exceptions from this (which is noted below). Argument can be any string, but it should be
+wrapped with `'` symbol (otherwise it might break way more). Additionally, all `:` within it should be escaped with `\` symbol (`\:`)
+
+# List of formatting tags
+- \ - Changes text color to yellow,
+- \ - Changes text color to dark blue,
+- \ - Changes text color to dark purple,
+- \ - Changes text color to gold,
+- \ - Changes text color to red,
+- \ - Changes text color to aqua,
+- \ - Changes text color to gray,
+- \ - Changes text color to light purple,
+- \ - Changes text color to white,
+- \ - Changes text color to dark gray,
+- \ - Changes text color to green,
+- \ - Changes text color to dark green,
+- \ - Changes text color to blue,
+- \ - Changes text color to dark aqua,
+- \ - Changes text color to dark green,
+- \ - Changes text color to black,
+- \ / \ - Changes text color to selected vanilla color or rgb value with `#RRGGBB` format,
+
+- \ - Makes text strikethrough,
+- \ - Underlines text,
+- \ - Makes text italic,
+- \ - Obfuscates text (matrix effect),
+- \ - Makes text bold,
+
+- \ - Adds click even to text (see vanilla [click events here](https://minecraft.fandom.com/wiki/Raw_JSON_text_format))
+- \ - Adds hover information. For now only `` event is supported. Value uses the same formatting as rest
+- \ - After clicking makes player insert text to their message,
+
+- \ - Changes font to selected one (can be from vanilla or resource pack),
+- \ - Adds value depending on your lang file. Doesn't need closing tag,
+- \ - Adds name of clientside control key (see [values here](https://minecraft.fandom.com/wiki/Controls#Configurable_controls)). Doesn't need closing tag,
+
+- \ / \ - Makes text within it have rainbow colors (supports inner formatting). All arguments are optional (`` is still valid),
+- \ / \ - Creates a gradient, can have multiple main colors,
diff --git a/gradle.properties b/gradle.properties
index 93b831c..3fb4ca9 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -3,12 +3,12 @@ org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://fabricmc.net/use
- minecraft_version=1.17-pre1
- yarn_mappings=1.17-pre1+build.1
+ minecraft_version=1.17-pre2
+ yarn_mappings=1.17-pre2+build.1
loader_version=0.11.3
# Mod Properties
- mod_version = 1.0.0-pre0-1.17-pre1
+ mod_version = 1.0.0-pre1-1.17-pre2
maven_group = eu.pb4
archives_base_name = placeholder-api
diff --git a/src/main/java/eu/pb4/placeholders/TextParser.java b/src/main/java/eu/pb4/placeholders/TextParser.java
index 3e8d0ff..c2c603c 100644
--- a/src/main/java/eu/pb4/placeholders/TextParser.java
+++ b/src/main/java/eu/pb4/placeholders/TextParser.java
@@ -1,5 +1,6 @@
package eu.pb4.placeholders;
+import com.google.common.collect.ImmutableMap;
import eu.pb4.placeholders.util.TextParserUtils;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
@@ -27,6 +28,13 @@ public static void register(String identifier, TextFormatterHandler handler) {
TAGS.put(identifier, handler);
}
+ /**
+ * Returns map of registered tags
+ */
+ public static ImmutableMap getRegisteredTags() {
+ return ImmutableMap.copyOf(TAGS);
+ }
+
@FunctionalInterface
public interface TextFormatterHandler {
int parse(String tag, String data, MutableText text, String input, Map handlers, String endAt);
diff --git a/src/main/java/eu/pb4/placeholders/util/TextParserUtils.java b/src/main/java/eu/pb4/placeholders/util/TextParserUtils.java
index ac5ad81..8a9b2ce 100644
--- a/src/main/java/eu/pb4/placeholders/util/TextParserUtils.java
+++ b/src/main/java/eu/pb4/placeholders/util/TextParserUtils.java
@@ -5,6 +5,7 @@
import net.minecraft.text.*;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
+import net.minecraft.util.math.MathHelper;
import java.util.*;
import java.util.regex.Matcher;
@@ -84,9 +85,8 @@ public static int recursiveParsing(MutableText text, String input, Map";
TextParser.TextFormatterHandler handler = handlers.get(tag);
- currentPos = matcher.end();
-
if (handler != null) {
+ currentPos = matcher.end();
try {
int toIgnore = handler.parse(tag, data, text, input.substring(currentPos), handlers, end);
currentPos += toIgnore;
@@ -137,11 +137,16 @@ public static void register() {
});
}
- TextParser.register("color", (String tag, String data, MutableText text, String input, Map handlers, String endAt) -> {
- MutableText out = new LiteralText("").fillStyle(Style.EMPTY.withColor(TextColor.parse(cleanArgument(data))));
- text.append(out);
- return recursiveParsing(out, input, handlers, endAt);
- });
+ {
+ TextParser.TextFormatterHandler color = (String tag, String data, MutableText text, String input, Map handlers, String endAt) -> {
+ MutableText out = new LiteralText("").fillStyle(Style.EMPTY.withColor(TextColor.parse(cleanArgument(data))));
+ text.append(out);
+ return recursiveParsing(out, input, handlers, endAt);
+ };
+
+ TextParser.register("color", color);
+ TextParser.register("c", color);
+ }
TextParser.register("font", (String tag, String data, MutableText text, String input, Map handlers, String endAt) -> {
MutableText out = new LiteralText("").fillStyle(Style.EMPTY.withFont(Identifier.tryParse(cleanArgument(data))));
@@ -209,88 +214,122 @@ public static void register() {
return recursiveParsing(out, input, handlers, endAt);
});
- TextParser.register("rainbow", (String tag, String data, MutableText text, String input, Map handlers, String endAt) -> {
- MutableText out = new LiteralText("");
- String[] val = data.split(":");
- float freq = 1;
- float saturation = 1;
- float offset = 0;
-
- if (val.length >= 1) {
- try {
- freq = Float.parseFloat(val[0]);
- } catch (Exception e) {
+ {
+ TextParser.TextFormatterHandler rainbow = (String tag, String data, MutableText text, String input, Map handlers, String endAt) -> {
+ MutableText out = new LiteralText("");
+ String[] val = data.split(":");
+ float freq = 1;
+ float saturation = 1;
+ float offset = 0;
+
+ if (val.length >= 1) {
+ try {
+ freq = Float.parseFloat(val[0]);
+ } catch (Exception e) {
+ }
}
- }
- if (val.length >= 2) {
- try {
- saturation = Float.parseFloat(val[1]);
- } catch (Exception e) {
+ if (val.length >= 2) {
+ try {
+ saturation = Float.parseFloat(val[1]);
+ } catch (Exception e) {
+ }
}
- }
- if (val.length >= 3) {
- try {
- offset = Float.parseFloat(val[2]);
- } catch (Exception e) {
+ if (val.length >= 3) {
+ try {
+ offset = Float.parseFloat(val[2]);
+ } catch (Exception e) {
+ }
}
- }
- int toIgnore = recursiveParsing(out, input, handlers, endAt);
- String flatString = GeneralUtils.textToString(out);
+ int toIgnore = recursiveParsing(out, input, handlers, endAt);
+ String flatString = GeneralUtils.textToString(out);
- final float finalFreq = freq;
- final float finalOffset = offset;
- final float finalSaturation = saturation;
+ final float finalFreq = freq;
+ final float finalOffset = offset;
+ final float finalSaturation = saturation;
- text.append(GeneralUtils.toGradient(out, (pos) -> TextColor.fromRgb(GeneralUtils.hvsToRgb(((pos * finalFreq) / (flatString.length() + 1) + finalOffset) % 1, finalSaturation, 1))));
+ text.append(GeneralUtils.toGradient(out, (pos) -> TextColor.fromRgb(GeneralUtils.hvsToRgb(((pos * finalFreq) / (flatString.length() + 1) + finalOffset) % 1, finalSaturation, 1))));
- return toIgnore;
- });
+ return toIgnore;
+ };
- TextParser.register("gradient", (String tag, String data, MutableText text, String input, Map handlers, String endAt) -> {
- MutableText out = new LiteralText("");
- String[] val = data.split(":");
-
- int toIgnore = recursiveParsing(out, input, handlers, endAt);
- String flatString = GeneralUtils.textToString(out);
- List textColors = new ArrayList<>();
- for (String string : val) {
- TextColor color = TextColor.parse(string);
- if (color != null) {
- textColors.add(color);
- }
- }
+ TextParser.register("rainbow", rainbow);
+ TextParser.register("rb", rainbow);
+ }
- final double step = ((double) textColors.size() - 1) / flatString.length();
- final int sectionSize = (textColors.size() - 1) / (flatString.length() + 1);
-
- GeneralUtils.HSV hsv = GeneralUtils.rgbToHsv(textColors.get(0).getRgb());
- AtomicDouble hue = new AtomicDouble(hsv.h());
- AtomicDouble saturation = new AtomicDouble(hsv.s());
- AtomicDouble value = new AtomicDouble(hsv.v());
-
- text.append(GeneralUtils.toGradient(out, (pos) -> {
- GeneralUtils.HSV colorA = GeneralUtils.rgbToHsv(textColors.get(pos * sectionSize).getRgb());
- GeneralUtils.HSV colorB = GeneralUtils.rgbToHsv(textColors.get(pos * sectionSize + 1).getRgb());
- float sym = Math.abs(colorB.h() - colorA.h()) > Math.abs(colorA.h() - colorB.h()) ? -1 : 1;
- float h = colorB.h() - colorA.h();
- float delta = (h + ((Math.abs(h) > 0.5) ? ((h < 0) ? 1 : -1) : 0));
-
- float localHue = (float) hue.get();
- float futureHue = (float) (localHue + delta * step);
- if (futureHue < 0) {
- futureHue += 1;
+ {
+ TextParser.TextFormatterHandler gradient = (String tag, String data, MutableText text, String input, Map handlers, String endAt) -> {
+ MutableText out = new LiteralText("");
+ String[] val = data.split(":");
+
+ int toIgnore = recursiveParsing(out, input, handlers, endAt);
+ String flatString = GeneralUtils.textToString(out);
+ List textColors = new ArrayList<>();
+ for (String string : val) {
+ TextColor color = TextColor.parse(string);
+ if (color != null) {
+ textColors.add(color);
+ }
+ }
+ if (textColors.size() == 0) {
+ textColors.add(TextColor.fromFormatting(Formatting.WHITE));
+ textColors.add(TextColor.fromFormatting(Formatting.WHITE));
+ } else if (textColors.size() == 1) {
+ textColors.add(textColors.get(0));
}
- hue.set(futureHue);
- return TextColor.fromRgb(GeneralUtils.hvsToRgb(
- localHue,
- (float) saturation.getAndAdd((colorB.s() - colorA.s()) * step),
- (float) value.getAndAdd((colorB.v() - colorA.v()) * step)));
- }));
+ final double step = ((double) textColors.size() - 1) / flatString.length();
+ final float sectionSize = ((float) textColors.size() - 1) / (flatString.length() + 1);
- return toIgnore;
- });
+ GeneralUtils.HSV hsv = GeneralUtils.rgbToHsv(textColors.get(0).getRgb());
+ AtomicDouble hue = new AtomicDouble(hsv.h());
+ AtomicDouble saturation = new AtomicDouble(hsv.s());
+ AtomicDouble value = new AtomicDouble(hsv.v());
+
+ text.append(GeneralUtils.toGradient(out, (pos) -> {
+ GeneralUtils.HSV colorA = GeneralUtils.rgbToHsv(textColors.get((int) (pos * sectionSize)).getRgb());
+ GeneralUtils.HSV colorB = GeneralUtils.rgbToHsv(textColors.get((int) (pos * sectionSize) + 1).getRgb());
+
+ float localHue = (float) hue.get();
+ {
+ float h = colorB.h() - colorA.h();
+ float delta = (h + ((Math.abs(h) > 0.50001) ? ((h < 0) ? 1 : -1) : 0));
+
+ float futureHue = (float) (localHue + delta * step);
+ if (futureHue < 0) {
+ futureHue += 1;
+ } else if (futureHue > 1) {
+ futureHue -= 1;
+ }
+ hue.set(futureHue);
+ }
+
+ float localSat = (float) saturation.get();
+ {
+ float s = colorB.s() - colorA.s();
+ float futureSat = MathHelper.clamp((float) (localSat + s * step), 0, 1);
+ saturation.set(futureSat);
+ }
+
+ float localVal = (float) value.get();
+ {
+ float v = colorB.v() - colorA.v();
+ float futureVal = MathHelper.clamp((float) (localVal + v * step), 0, 1);
+ value.set(futureVal);
+ }
+
+ return TextColor.fromRgb(GeneralUtils.hvsToRgb(
+ localHue,
+ localSat,
+ localVal));
+ }));
+
+ return toIgnore;
+ };
+
+ TextParser.register("gradient", gradient);
+ TextParser.register("gr", gradient);
+ }
ESCAPED_CHARS.put("\\\\", "&slsh;");
ESCAPED_CHARS.put("\\<", "<");