mirror of
https://github.com/filoghost/HolographicDisplays.git
synced 2025-01-20 06:41:20 +01:00
Don't apply display format inside placeholders
This commit is contained in:
parent
d251998cdd
commit
de3366a8a2
@ -12,6 +12,7 @@ import me.filoghost.holographicdisplays.plugin.hologram.internal.InternalHologra
|
||||
import me.filoghost.holographicdisplays.plugin.hologram.internal.InternalHologramLine;
|
||||
import me.filoghost.holographicdisplays.plugin.lib.nbt.parser.MojangsonParseException;
|
||||
import me.filoghost.holographicdisplays.plugin.lib.nbt.parser.MojangsonParser;
|
||||
import me.filoghost.holographicdisplays.plugin.placeholder.parsing.StringWithPlaceholders;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@ -29,7 +30,8 @@ public class HologramLineParser {
|
||||
hologramLine = hologram.createItemLine(icon, serializedLine);
|
||||
|
||||
} else {
|
||||
String displayText = DisplayFormat.apply(serializedLine);
|
||||
// Don't apply display format inside placeholders
|
||||
String displayText = StringWithPlaceholders.of(serializedLine).replaceLiteralParts(DisplayFormat::apply);
|
||||
hologramLine = hologram.createTextLine(displayText, serializedLine);
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public final class StringWithPlaceholders {
|
||||
@ -27,11 +28,7 @@ public final class StringWithPlaceholders {
|
||||
if (string == null) {
|
||||
return NULL_INSTANCE;
|
||||
}
|
||||
return new StringWithPlaceholders(string);
|
||||
}
|
||||
|
||||
private StringWithPlaceholders(@NotNull String string) {
|
||||
this(string, splitToParts(string));
|
||||
return new StringWithPlaceholders(string, splitToParts(string));
|
||||
}
|
||||
|
||||
private StringWithPlaceholders(@Nullable String string, @Nullable List<StringPart> stringParts) {
|
||||
@ -91,7 +88,7 @@ public final class StringWithPlaceholders {
|
||||
output.setLength(0);
|
||||
}
|
||||
newStringParts.add(placeholderStringPart);
|
||||
fullOutput.append(placeholderStringPart.nonReplacedString);
|
||||
fullOutput.append(placeholderStringPart.unreplacedString);
|
||||
}
|
||||
} else {
|
||||
LiteralStringPart literalStringPart = (LiteralStringPart) part;
|
||||
@ -120,6 +117,26 @@ public final class StringWithPlaceholders {
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
public String replaceLiteralParts(Function<String, String> transformFunction) {
|
||||
if (!containsPlaceholders() || string == null) {
|
||||
return transformFunction.apply(string);
|
||||
}
|
||||
|
||||
StringBuilder stringOutput = new StringBuilder(string.length());
|
||||
|
||||
for (StringPart part : stringParts) {
|
||||
if (part instanceof LiteralStringPart) {
|
||||
LiteralStringPart literalStringPart = (LiteralStringPart) part;
|
||||
String transformedPart = transformFunction.apply(literalStringPart.literalString);
|
||||
stringOutput.append(transformedPart);
|
||||
} else {
|
||||
stringOutput.append(((PlaceholderStringPart) part).unreplacedString);
|
||||
}
|
||||
}
|
||||
|
||||
return stringOutput.toString();
|
||||
}
|
||||
|
||||
private static @Nullable List<StringPart> splitToParts(@NotNull String string) {
|
||||
int placeholderStartIndex = -1;
|
||||
int lastAppendIndex = 0;
|
||||
@ -219,11 +236,11 @@ public final class StringWithPlaceholders {
|
||||
private static class PlaceholderStringPart implements StringPart {
|
||||
|
||||
private final PlaceholderOccurrence placeholderOccurrence;
|
||||
private final String nonReplacedString;
|
||||
private final String unreplacedString;
|
||||
|
||||
PlaceholderStringPart(PlaceholderOccurrence placeholderOccurrence, String nonReplacedString) {
|
||||
PlaceholderStringPart(PlaceholderOccurrence placeholderOccurrence, String unreplacedString) {
|
||||
this.placeholderOccurrence = placeholderOccurrence;
|
||||
this.nonReplacedString = nonReplacedString;
|
||||
this.unreplacedString = unreplacedString;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -232,8 +249,8 @@ public final class StringWithPlaceholders {
|
||||
if (replacement != null) {
|
||||
return replacement;
|
||||
} else {
|
||||
// If no replacement is provided, leave the unparsed placeholder string
|
||||
return nonReplacedString;
|
||||
// If no replacement is provided, leave the unreplaced placeholder string
|
||||
return unreplacedString;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,6 +52,22 @@ class StringWithPlaceholdersTest {
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "[{index}] {0} -> {1}")
|
||||
@MethodSource("replaceLiteralPartsTestArguments")
|
||||
void replaceLiteralParts(String input, String expectedOutput) {
|
||||
StringWithPlaceholders s = StringWithPlaceholders.of(input);
|
||||
assertThat(s.replaceLiteralParts(literalPart -> "_")).isEqualTo(expectedOutput);
|
||||
}
|
||||
|
||||
static Stream<Arguments> replaceLiteralPartsTestArguments() {
|
||||
return Stream.of(
|
||||
Arguments.of(" {p} ", "_{p}_"),
|
||||
Arguments.of("{p} {p} abc {p}", "{p}_{p}_{p}"),
|
||||
Arguments.of("{p}{p}", "{p}{p}"),
|
||||
Arguments.of(" {{p}} ", "_{p}_")
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void skipReplacing() {
|
||||
String input = "{p} a {p} b {p}";
|
||||
|
Loading…
Reference in New Issue
Block a user