Skip to content

Commit be1d107

Browse files
committed
Add clear tag, shortcut for close tag, some placeholders
1 parent 21ad2d1 commit be1d107

11 files changed

Lines changed: 193 additions & 12 deletions

File tree

.github/workflows/release.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ jobs:
3535
MAVEN_URL: ${{ secrets.MAVEN_URL }}
3636
MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
3737
MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}
38-
38+
MODRINTH: ${{ secrets.MODRINTH }}
39+
CHANGELOG: ${{ github.event.release.body }}
3940
- name: Upload GitHub release
4041
uses: AButler/upload-release-assets@v2.0
4142
with:

build.gradle

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
id 'fabric-loom' version '0.12-SNAPSHOT'
33
id 'maven-publish'
4+
id "com.modrinth.minotaur" version "2.+"
45
}
56

67
sourceCompatibility = JavaVersion.VERSION_17
@@ -133,3 +134,20 @@ publishing {
133134
}
134135
}
135136
}
137+
138+
if (System.getenv("MODRINTH")) {
139+
modrinth {
140+
token = System.getenv("MODRINTH")
141+
projectId = 'eXts2L7r'// The ID of your modrinth project, slugs will not work.
142+
versionNumber = "" + version // The version of the mod to upload.
143+
versionType = "release"
144+
uploadFile = remapJar // This links to a task that builds your mod jar and sets "uploadFile" to the mod jar.
145+
gameVersions = [((String) project.minecraft_version)]
146+
changelog = System.getenv("CHANGELOG")
147+
loaders = ["fabric", "quilt"]
148+
}
149+
150+
remapJar {
151+
finalizedBy project.tasks.modrinth
152+
}
153+
}

docs/user/default-placeholders.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Prior to 1.19, arguments were separated with a slash (`/`) instead of space.
7474
displayed in gigabytes)
7575
- `%server:online%` - The number of online players.
7676
- `%server:max_players%` - The maximum player count.
77+
- `%server:brand%` (2.1.3+) - Returns server's brand ("fabric"/"quilt"/etc).
78+
- `%server:mod_count%` (2.1.3+) - Returns amount of installed mods.
7779
- `%server:mod_version [modid]%` - Returns version of the specified mod.
7880
- `%server:mod_name [modid]%` - Returns name of the specified mod.
7981
- `%server:mod_description [modid]%` - Returns description of the specified mod.

docs/user/text-format.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Formatting is build on concept of tags.
1717
<lang:'chat.type.team.sent':'<hover\:\'<lang\:chat.type.team.hover>\'><suggest_command\:\'/teammsg \'>${team}':'${displayName}':'${message}'>
1818
```
1919

20-
Most of them come in pairs of a starting (`#!xml <tag>`) and closing one (`#!xml </tag>`).
20+
Most of them come in pairs of a starting (`#!xml <tag>`) and closing one (`#!xml </tag>` for direct or `#!xml <‍/‍>` (2.1.3+) for automatic).
2121
While closing ones are technically optional, without them formatting will continue until end of
2222
an input text or special `#!xml <reset>` tag. Some tags support arguments, which can be passed by adding `:`
2323
after tag name in starting one (for example `#!xml <color:#FF3333> </color>`). Arguments containing symbols like
@@ -174,5 +174,23 @@ There 2 types of gradients:
174174

175175
`#!xml <reset>` and `#!xml <r>` are special, self-contained tags which close all previous formatting. They are
176176
useful in cases, where you want to close multiple formatting tags quickly
177-
178177

178+
### Clear (2.1.3+)
179+
180+
!!! note inline end
181+
182+
This tag should be closed.
183+
184+
This tag allows you to clear any formatting within this tag, without leaving any visible tags. It also
185+
works with placeholders, which gives a bit more flexibility.
186+
187+
This tag can work without arguments making it clear all formatting or with them limiting clearing to selected types.
188+
189+
Examples:
190+
191+
- `#!xml <clear>` - Removes all formatting, leaving only text.
192+
- `#!xml <clear:hover>` - Removes all hovers.
193+
- `#!xml <clear:hover:color>` - Removes all hovers and colors.
194+
195+
Supported arguments: `color`, `bold`, `italic`, `strikethrough`, `underline`, `hover`, `click`,
196+
`insertion`, `font`, `all`.

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ org.gradle.jvmargs=-Xmx1G
1111
fabric_version=0.83.0+1.20.1
1212

1313
# Mod Properties
14-
mod_version = 2.1.2+1.20.1
14+
mod_version = 2.1.3+1.20.1
1515
maven_group = eu.pb4
1616
archives_base_name = placeholder-api
1717

src/main/java/eu/pb4/placeholders/api/node/parent/ParentNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public final Text toText(ParserContext context, boolean removeBackslashes) {
4343
return out;
4444
}
4545

46-
return ((MutableText) this.applyFormatting(out.copy(), context)).fillStyle(out.getStyle());
46+
return ((MutableText) this.applyFormatting(out.copy(), context));
4747
} else {
4848
MutableText base = compact ? null : Text.empty();
4949

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package eu.pb4.placeholders.api.node.parent;
2+
3+
import eu.pb4.placeholders.api.ParserContext;
4+
import eu.pb4.placeholders.api.node.TextNode;
5+
import eu.pb4.placeholders.impl.GeneralUtils;
6+
import net.minecraft.text.MutableText;
7+
import net.minecraft.text.Style;
8+
import net.minecraft.text.Text;
9+
10+
import java.util.Arrays;
11+
import java.util.function.Function;
12+
13+
public final class TransformNode extends ParentNode {
14+
private final Function<MutableText, Text> transform;
15+
16+
public TransformNode(TextNode[] nodes, Function<MutableText, Text> transform) {
17+
super(nodes);
18+
this.transform = transform;
19+
}
20+
21+
public static TransformNode deepStyle(Function<Style, Style> styleFunction, TextNode... nodes) {
22+
return new TransformNode(nodes, new GeneralUtils.MutableTransformer(styleFunction));
23+
}
24+
25+
@Override
26+
protected Text applyFormatting(MutableText out, ParserContext context) {
27+
return this.transform.apply(out);
28+
}
29+
30+
@Override
31+
public ParentTextNode copyWith(TextNode[] children) {
32+
return new TransformNode(children, this.transform);
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return "TransformNode{" +
38+
"transform=" + transform +
39+
", children=" + Arrays.toString(children) +
40+
'}';
41+
}
42+
}

src/main/java/eu/pb4/placeholders/impl/GeneralUtils.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.slf4j.LoggerFactory;
1414

1515
import java.util.ArrayList;
16+
import java.util.function.Function;
1617

1718

1819
@ApiStatus.Internal
@@ -162,6 +163,12 @@ public static HSV rgbToHsv(int rgb) {
162163
return new HSV(h, s, cmax);
163164
}
164165

166+
public static Text deepTransform(Text input) {
167+
var output = cloneText(input);
168+
removeHoverAndClick(output);
169+
return output;
170+
}
171+
165172
public static Text removeHoverAndClick(Text input) {
166173
var output = cloneText(input);
167174
removeHoverAndClick(output);
@@ -214,6 +221,32 @@ public static MutableText cloneText(Text input) {
214221
return baseText;
215222
}
216223

224+
public static MutableText cloneTransformText(Text input, Function<MutableText, MutableText> transform) {
225+
MutableText baseText;
226+
if (input.getContent() instanceof TranslatableTextContent translatable) {
227+
var obj = new ArrayList<>();
228+
229+
for (var arg : translatable.getArgs()) {
230+
if (arg instanceof Text argText) {
231+
obj.add(cloneTransformText(argText, transform));
232+
} else {
233+
obj.add(arg);
234+
}
235+
}
236+
237+
baseText = Text.translatable(translatable.getKey(), obj.toArray());
238+
} else {
239+
baseText = input.copyContentOnly();
240+
}
241+
242+
for (var sibling : input.getSiblings()) {
243+
baseText.append(cloneTransformText(sibling, transform));
244+
}
245+
246+
baseText.setStyle(input.getStyle());
247+
return transform.apply(baseText);
248+
}
249+
217250
public static Text getItemText(ItemStack stack, boolean rarity) {
218251
if (!stack.isEmpty()) {
219252
MutableText mutableText = Text.empty().append(stack.getName());
@@ -314,4 +347,17 @@ public record TextLengthPair(MutableText text, int length) {
314347

315348
public record Pair<L, R>(L left, R right) {
316349
}
350+
351+
public record MutableTransformer(Function<Style, Style> textMutableTextFunction) implements Function<MutableText, Text> {
352+
public static final MutableTransformer CLEAR = new MutableTransformer(x -> Style.EMPTY);
353+
354+
@Override
355+
public Text apply(MutableText text) {
356+
return GeneralUtils.cloneTransformText(text, this::transformStyle);
357+
}
358+
359+
private MutableText transformStyle(MutableText mutableText) {
360+
return mutableText.setStyle(textMutableTextFunction.apply(mutableText.getStyle()));
361+
}
362+
}
317363
}

src/main/java/eu/pb4/placeholders/impl/placeholder/builtin/ServerPlaceholders.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ public static void register() {
121121
return PlaceholderResult.invalid("Invalid argument");
122122
});
123123

124+
Placeholders.register(new Identifier("server", "brand"), (ctx, arg) -> {
125+
return PlaceholderResult.value(Text.literal(ctx.server().getServerModName()));
126+
});
127+
128+
Placeholders.register(new Identifier("server", "mod_count"), (ctx, arg) -> {
129+
return PlaceholderResult.value(Text.literal("" + FabricLoader.getInstance().getAllMods().size()));
130+
});
131+
124132
Placeholders.register(new Identifier("server", "mod_description"), (ctx, arg) -> {
125133
if (arg != null) {
126134
var container = FabricLoader.getInstance().getModContainer(arg);

src/main/java/eu/pb4/placeholders/impl/textparser/TextParserImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static TextParserV1.NodeList recursiveParsing(String input, TextParserV1.
8888
var text = new ArrayList<TextNode>();
8989

9090
Matcher matcher = STARTING_PATTERN.matcher(input);
91-
Matcher matcherEnd = endAt != null ? Pattern.compile(endAt).matcher(input) : null;
91+
Matcher matcherEnd = endAt != null ? Pattern.compile("(" + endAt + ")|(</>)").matcher(input) : null;
9292
int currentPos = 0;
9393
int offset = 0;
9494
boolean hasEndTag = endAt != null && matcherEnd.find();
@@ -112,7 +112,7 @@ public static TextParserV1.NodeList recursiveParsing(String input, TextParserV1.
112112
currentEnd = matcher.start();
113113
if (currentPos < currentEnd) {
114114
String restOfText = restoreOriginalEscaping(input.substring(currentPos, currentEnd));
115-
if (restOfText.length() != 0) {
115+
if (!restOfText.isEmpty()) {
116116
text.add(new LiteralNode(restOfText));
117117
}
118118
}
@@ -121,7 +121,7 @@ public static TextParserV1.NodeList recursiveParsing(String input, TextParserV1.
121121
} else {
122122
String betweenText = input.substring(currentPos, matcher.start());
123123

124-
if (betweenText.length() != 0) {
124+
if (!betweenText.isEmpty()) {
125125
text.add(new LiteralNode(restoreOriginalEscaping(betweenText)));
126126
}
127127
currentPos = matcher.end();
@@ -139,7 +139,7 @@ public static TextParserV1.NodeList recursiveParsing(String input, TextParserV1.
139139
if (handler != null) {
140140
String betweenText = input.substring(currentPos, matcher.start());
141141

142-
if (betweenText.length() != 0) {
142+
if (!betweenText.isEmpty()) {
143143
text.add(new LiteralNode(restoreOriginalEscaping(betweenText)));
144144

145145
}
@@ -175,13 +175,13 @@ public static TextParserV1.NodeList recursiveParsing(String input, TextParserV1.
175175

176176
if (currentPos < currentEnd) {
177177
String restOfText = restoreOriginalEscaping(input.substring(currentPos, currentEnd));
178-
if (restOfText.length() != 0) {
178+
if (!restOfText.isEmpty()) {
179179
text.add(new LiteralNode(restOfText));
180180
}
181181
}
182182

183183
if (hasEndTag) {
184-
currentEnd += endAt.length();
184+
currentEnd += matcherEnd.group().length();
185185
} else {
186186
currentEnd = input.length();
187187
}
@@ -212,7 +212,7 @@ public static String convertToString(Text text) {
212212
}
213213
}
214214

215-
if (stringList.size() > 0) {
215+
if (!stringList.isEmpty()) {
216216
stringList.add(0, "");
217217
}
218218

0 commit comments

Comments
 (0)