mirror of
https://github.com/filoghost/HolographicDisplays.git
synced 2025-02-07 23:51:33 +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.hologram.internal.InternalHologramLine;
|
||||||
import me.filoghost.holographicdisplays.plugin.lib.nbt.parser.MojangsonParseException;
|
import me.filoghost.holographicdisplays.plugin.lib.nbt.parser.MojangsonParseException;
|
||||||
import me.filoghost.holographicdisplays.plugin.lib.nbt.parser.MojangsonParser;
|
import me.filoghost.holographicdisplays.plugin.lib.nbt.parser.MojangsonParser;
|
||||||
|
import me.filoghost.holographicdisplays.plugin.placeholder.parsing.StringWithPlaceholders;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
@ -29,7 +30,8 @@ public class HologramLineParser {
|
|||||||
hologramLine = hologram.createItemLine(icon, serializedLine);
|
hologramLine = hologram.createItemLine(icon, serializedLine);
|
||||||
|
|
||||||
} else {
|
} 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);
|
hologramLine = hologram.createTextLine(displayText, serializedLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public final class StringWithPlaceholders {
|
public final class StringWithPlaceholders {
|
||||||
@ -27,11 +28,7 @@ public final class StringWithPlaceholders {
|
|||||||
if (string == null) {
|
if (string == null) {
|
||||||
return NULL_INSTANCE;
|
return NULL_INSTANCE;
|
||||||
}
|
}
|
||||||
return new StringWithPlaceholders(string);
|
return new StringWithPlaceholders(string, splitToParts(string));
|
||||||
}
|
|
||||||
|
|
||||||
private StringWithPlaceholders(@NotNull String string) {
|
|
||||||
this(string, splitToParts(string));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private StringWithPlaceholders(@Nullable String string, @Nullable List<StringPart> stringParts) {
|
private StringWithPlaceholders(@Nullable String string, @Nullable List<StringPart> stringParts) {
|
||||||
@ -91,7 +88,7 @@ public final class StringWithPlaceholders {
|
|||||||
output.setLength(0);
|
output.setLength(0);
|
||||||
}
|
}
|
||||||
newStringParts.add(placeholderStringPart);
|
newStringParts.add(placeholderStringPart);
|
||||||
fullOutput.append(placeholderStringPart.nonReplacedString);
|
fullOutput.append(placeholderStringPart.unreplacedString);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
LiteralStringPart literalStringPart = (LiteralStringPart) part;
|
LiteralStringPart literalStringPart = (LiteralStringPart) part;
|
||||||
@ -120,6 +117,26 @@ public final class StringWithPlaceholders {
|
|||||||
return output.toString();
|
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) {
|
private static @Nullable List<StringPart> splitToParts(@NotNull String string) {
|
||||||
int placeholderStartIndex = -1;
|
int placeholderStartIndex = -1;
|
||||||
int lastAppendIndex = 0;
|
int lastAppendIndex = 0;
|
||||||
@ -219,11 +236,11 @@ public final class StringWithPlaceholders {
|
|||||||
private static class PlaceholderStringPart implements StringPart {
|
private static class PlaceholderStringPart implements StringPart {
|
||||||
|
|
||||||
private final PlaceholderOccurrence placeholderOccurrence;
|
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.placeholderOccurrence = placeholderOccurrence;
|
||||||
this.nonReplacedString = nonReplacedString;
|
this.unreplacedString = unreplacedString;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -232,8 +249,8 @@ public final class StringWithPlaceholders {
|
|||||||
if (replacement != null) {
|
if (replacement != null) {
|
||||||
return replacement;
|
return replacement;
|
||||||
} else {
|
} else {
|
||||||
// If no replacement is provided, leave the unparsed placeholder string
|
// If no replacement is provided, leave the unreplaced placeholder string
|
||||||
return nonReplacedString;
|
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
|
@Test
|
||||||
void skipReplacing() {
|
void skipReplacing() {
|
||||||
String input = "{p} a {p} b {p}";
|
String input = "{p} a {p} b {p}";
|
||||||
|
Loading…
Reference in New Issue
Block a user