Skip to content

Commit

Permalink
Add clear tag, shortcut for close tag, some placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Sep 8, 2023
1 parent 21ad2d1 commit be1d107
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ jobs:
MAVEN_URL: ${{ secrets.MAVEN_URL }}
MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}

MODRINTH: ${{ secrets.MODRINTH }}
CHANGELOG: ${{ github.event.release.body }}
- name: Upload GitHub release
uses: AButler/[email protected]
with:
Expand Down
18 changes: 18 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'fabric-loom' version '0.12-SNAPSHOT'
id 'maven-publish'
id "com.modrinth.minotaur" version "2.+"
}

sourceCompatibility = JavaVersion.VERSION_17
Expand Down Expand Up @@ -133,3 +134,20 @@ publishing {
}
}
}

if (System.getenv("MODRINTH")) {
modrinth {
token = System.getenv("MODRINTH")
projectId = 'eXts2L7r'// The ID of your modrinth project, slugs will not work.
versionNumber = "" + version // The version of the mod to upload.
versionType = "release"
uploadFile = remapJar // This links to a task that builds your mod jar and sets "uploadFile" to the mod jar.
gameVersions = [((String) project.minecraft_version)]
changelog = System.getenv("CHANGELOG")
loaders = ["fabric", "quilt"]
}

remapJar {
finalizedBy project.tasks.modrinth
}
}
2 changes: 2 additions & 0 deletions docs/user/default-placeholders.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Prior to 1.19, arguments were separated with a slash (`/`) instead of space.
displayed in gigabytes)
- `%server:online%` - The number of online players.
- `%server:max_players%` - The maximum player count.
- `%server:brand%` (2.1.3+) - Returns server's brand ("fabric"/"quilt"/etc).
- `%server:mod_count%` (2.1.3+) - Returns amount of installed mods.
- `%server:mod_version [modid]%` - Returns version of the specified mod.
- `%server:mod_name [modid]%` - Returns name of the specified mod.
- `%server:mod_description [modid]%` - Returns description of the specified mod.
Expand Down
22 changes: 20 additions & 2 deletions docs/user/text-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Formatting is build on concept of tags.
<lang:'chat.type.team.sent':'<hover\:\'<lang\:chat.type.team.hover>\'><suggest_command\:\'/teammsg \'>${team}':'${displayName}':'${message}'>
```

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

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


### Clear (2.1.3+)

!!! note inline end

This tag should be closed.

This tag allows you to clear any formatting within this tag, without leaving any visible tags. It also
works with placeholders, which gives a bit more flexibility.

This tag can work without arguments making it clear all formatting or with them limiting clearing to selected types.

Examples:

- `#!xml <clear>` - Removes all formatting, leaving only text.
- `#!xml <clear:hover>` - Removes all hovers.
- `#!xml <clear:hover:color>` - Removes all hovers and colors.

Supported arguments: `color`, `bold`, `italic`, `strikethrough`, `underline`, `hover`, `click`,
`insertion`, `font`, `all`.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ org.gradle.jvmargs=-Xmx1G
fabric_version=0.83.0+1.20.1

# Mod Properties
mod_version = 2.1.2+1.20.1
mod_version = 2.1.3+1.20.1
maven_group = eu.pb4
archives_base_name = placeholder-api

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final Text toText(ParserContext context, boolean removeBackslashes) {
return out;
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package eu.pb4.placeholders.api.node.parent;

import eu.pb4.placeholders.api.ParserContext;
import eu.pb4.placeholders.api.node.TextNode;
import eu.pb4.placeholders.impl.GeneralUtils;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;

import java.util.Arrays;
import java.util.function.Function;

public final class TransformNode extends ParentNode {
private final Function<MutableText, Text> transform;

public TransformNode(TextNode[] nodes, Function<MutableText, Text> transform) {
super(nodes);
this.transform = transform;
}

public static TransformNode deepStyle(Function<Style, Style> styleFunction, TextNode... nodes) {
return new TransformNode(nodes, new GeneralUtils.MutableTransformer(styleFunction));
}

@Override
protected Text applyFormatting(MutableText out, ParserContext context) {
return this.transform.apply(out);
}

@Override
public ParentTextNode copyWith(TextNode[] children) {
return new TransformNode(children, this.transform);
}

@Override
public String toString() {
return "TransformNode{" +
"transform=" + transform +
", children=" + Arrays.toString(children) +
'}';
}
}
46 changes: 46 additions & 0 deletions src/main/java/eu/pb4/placeholders/impl/GeneralUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.function.Function;


@ApiStatus.Internal
Expand Down Expand Up @@ -162,6 +163,12 @@ public static HSV rgbToHsv(int rgb) {
return new HSV(h, s, cmax);
}

public static Text deepTransform(Text input) {
var output = cloneText(input);
removeHoverAndClick(output);
return output;
}

public static Text removeHoverAndClick(Text input) {
var output = cloneText(input);
removeHoverAndClick(output);
Expand Down Expand Up @@ -214,6 +221,32 @@ public static MutableText cloneText(Text input) {
return baseText;
}

public static MutableText cloneTransformText(Text input, Function<MutableText, MutableText> transform) {
MutableText baseText;
if (input.getContent() instanceof TranslatableTextContent translatable) {
var obj = new ArrayList<>();

for (var arg : translatable.getArgs()) {
if (arg instanceof Text argText) {
obj.add(cloneTransformText(argText, transform));
} else {
obj.add(arg);
}
}

baseText = Text.translatable(translatable.getKey(), obj.toArray());
} else {
baseText = input.copyContentOnly();
}

for (var sibling : input.getSiblings()) {
baseText.append(cloneTransformText(sibling, transform));
}

baseText.setStyle(input.getStyle());
return transform.apply(baseText);
}

public static Text getItemText(ItemStack stack, boolean rarity) {
if (!stack.isEmpty()) {
MutableText mutableText = Text.empty().append(stack.getName());
Expand Down Expand Up @@ -314,4 +347,17 @@ public record TextLengthPair(MutableText text, int length) {

public record Pair<L, R>(L left, R right) {
}

public record MutableTransformer(Function<Style, Style> textMutableTextFunction) implements Function<MutableText, Text> {
public static final MutableTransformer CLEAR = new MutableTransformer(x -> Style.EMPTY);

@Override
public Text apply(MutableText text) {
return GeneralUtils.cloneTransformText(text, this::transformStyle);
}

private MutableText transformStyle(MutableText mutableText) {
return mutableText.setStyle(textMutableTextFunction.apply(mutableText.getStyle()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ public static void register() {
return PlaceholderResult.invalid("Invalid argument");
});

Placeholders.register(new Identifier("server", "brand"), (ctx, arg) -> {
return PlaceholderResult.value(Text.literal(ctx.server().getServerModName()));
});

Placeholders.register(new Identifier("server", "mod_count"), (ctx, arg) -> {
return PlaceholderResult.value(Text.literal("" + FabricLoader.getInstance().getAllMods().size()));
});

Placeholders.register(new Identifier("server", "mod_description"), (ctx, arg) -> {
if (arg != null) {
var container = FabricLoader.getInstance().getModContainer(arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static TextParserV1.NodeList recursiveParsing(String input, TextParserV1.
var text = new ArrayList<TextNode>();

Matcher matcher = STARTING_PATTERN.matcher(input);
Matcher matcherEnd = endAt != null ? Pattern.compile(endAt).matcher(input) : null;
Matcher matcherEnd = endAt != null ? Pattern.compile("(" + endAt + ")|(</>)").matcher(input) : null;
int currentPos = 0;
int offset = 0;
boolean hasEndTag = endAt != null && matcherEnd.find();
Expand All @@ -112,7 +112,7 @@ public static TextParserV1.NodeList recursiveParsing(String input, TextParserV1.
currentEnd = matcher.start();
if (currentPos < currentEnd) {
String restOfText = restoreOriginalEscaping(input.substring(currentPos, currentEnd));
if (restOfText.length() != 0) {
if (!restOfText.isEmpty()) {
text.add(new LiteralNode(restOfText));
}
}
Expand All @@ -121,7 +121,7 @@ public static TextParserV1.NodeList recursiveParsing(String input, TextParserV1.
} else {
String betweenText = input.substring(currentPos, matcher.start());

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

if (betweenText.length() != 0) {
if (!betweenText.isEmpty()) {
text.add(new LiteralNode(restoreOriginalEscaping(betweenText)));

}
Expand Down Expand Up @@ -175,13 +175,13 @@ public static TextParserV1.NodeList recursiveParsing(String input, TextParserV1.

if (currentPos < currentEnd) {
String restOfText = restoreOriginalEscaping(input.substring(currentPos, currentEnd));
if (restOfText.length() != 0) {
if (!restOfText.isEmpty()) {
text.add(new LiteralNode(restOfText));
}
}

if (hasEndTag) {
currentEnd += endAt.length();
currentEnd += matcherEnd.group().length();
} else {
currentEnd = input.length();
}
Expand Down Expand Up @@ -212,7 +212,7 @@ public static String convertToString(Text text) {
}
}

if (stringList.size() > 0) {
if (!stringList.isEmpty()) {
stringList.add(0, "");
}

Expand Down
46 changes: 46 additions & 0 deletions src/main/java/eu/pb4/placeholders/impl/textparser/TextTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.jetbrains.annotations.ApiStatus;

import java.util.*;
import java.util.function.Function;

import static eu.pb4.placeholders.impl.textparser.TextParserImpl.*;

Expand Down Expand Up @@ -484,6 +485,23 @@ public static void register() {
);
}

{
TextParserV1.registerDefault(
TextParserV1.TextTag.of(
"clear",
"special",
false,
(tag, data, input, handlers, endAt) -> {
String[] val = data.isEmpty() ? new String[0] : data.split(":");

var out = recursiveParsing(input, handlers, endAt);
return out.value(new TransformNode(out.nodes(), getTransform(val)));

}
)
);
}

{
TextParserV1.registerDefault(
TextParserV1.TextTag.of(
Expand Down Expand Up @@ -564,6 +582,32 @@ public static void register() {
}
}

private static Function<MutableText, Text> getTransform(String[] val) {
if (val.length == 0) {
return GeneralUtils.MutableTransformer.CLEAR;
}

Function<Style, Style> func = (x) -> x;

for (var arg : val) {
func = func.andThen(switch (arg) {
case "hover" -> x -> x.withHoverEvent(null);
case "click" -> x -> x.withClickEvent(null);
case "color" -> x -> x.withColor((TextColor) null);
case "insertion" -> x -> x.withInsertion(null);
case "font" -> x -> x.withFont(null);
case "bold" -> x -> x.withBold(null);
case "italic" -> x -> x.withItalic(null);
case "underline" -> x -> x.withUnderline(null);
case "strikethrough" -> x -> x.withStrikethrough(null);
case "all" -> x -> Style.EMPTY;
default -> x -> x;
});
};

return new GeneralUtils.MutableTransformer(func);
}

private static boolean isntFalse(String arg) {
return arg.isEmpty() || !arg.equals("false");
}
Expand All @@ -582,6 +626,8 @@ private static TextParserV1.TagNodeBuilder bool(BooleanTag wrapper) {
};
}



interface Wrapper {
TextNode wrap(TextNode[] nodes, String arg);
}
Expand Down

0 comments on commit be1d107

Please sign in to comment.