From 88b260ffb9c4460837fa855e1dbbf9089c97dc67 Mon Sep 17 00:00:00 2001 From: filoghost Date: Sun, 25 Jul 2021 22:17:28 +0200 Subject: [PATCH] Improve placeholder handling --- .../plugin/hologram/tracking/DisplayText.java | 65 +++++++------------ .../hologram/tracking/TextLineTracker.java | 8 +-- .../parsing/StringWithPlaceholders.java | 37 ++++++----- 3 files changed, 48 insertions(+), 62 deletions(-) diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/DisplayText.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/DisplayText.java index 9e80ba60..c5f69691 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/DisplayText.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/DisplayText.java @@ -10,6 +10,7 @@ import me.filoghost.holographicdisplays.plugin.placeholder.parsing.PlaceholderOc import me.filoghost.holographicdisplays.plugin.placeholder.parsing.StringWithPlaceholders; import me.filoghost.holographicdisplays.plugin.placeholder.tracking.PlaceholderTracker; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Objects; @@ -17,24 +18,41 @@ import java.util.Objects; class DisplayText { private final PlaceholderTracker placeholderTracker; - private @Nullable StringWithPlaceholders textWithoutReplacements; - private @Nullable StringWithPlaceholders textWithGlobalReplacements; + private @NotNull StringWithPlaceholders textWithoutReplacements; + private @NotNull StringWithPlaceholders textWithGlobalReplacements; DisplayText(PlaceholderTracker placeholderTracker) { this.placeholderTracker = placeholderTracker; + this.textWithoutReplacements = StringWithPlaceholders.of(null); + this.textWithGlobalReplacements = StringWithPlaceholders.of(null); } - void set(@Nullable String textString) { + void setWithoutReplacements(@Nullable String textString) { textWithoutReplacements = StringWithPlaceholders.of(textString); textWithGlobalReplacements = textWithoutReplacements; } - String get() { - return textWithoutReplacements != null ? textWithoutReplacements.getUnreplacedString() : null; + String getWithoutReplacements() { + return textWithoutReplacements.getUnreplacedString(); + } + + String getWithGlobalReplacements() { + return textWithGlobalReplacements.getUnreplacedString(); + } + + String getWithIndividualReplacements(Player player) { + String textWithIndividualReplacements = textWithGlobalReplacements.replacePlaceholders((PlaceholderOccurrence occurrence) -> + placeholderTracker.updateAndGetIndividualReplacement(occurrence, player)); + + if (PlaceholderAPIHook.isEnabled() && PlaceholderAPIHook.containsPlaceholders(textWithIndividualReplacements)) { + textWithIndividualReplacements = PlaceholderAPIHook.replacePlaceholders(player, textWithIndividualReplacements); + } + + return textWithIndividualReplacements; } boolean updateGlobalReplacements() { - if (textWithoutReplacements == null || !textWithoutReplacements.containsPlaceholders()) { + if (!textWithoutReplacements.containsPlaceholders()) { return false; } @@ -49,42 +67,7 @@ class DisplayText { return true; } - String getWithGlobalReplacements() { - if (containsIndividualPlaceholders()) { - throw new IllegalStateException("contains individual placeholders"); - } - - if (textWithoutReplacements == null || !textWithoutReplacements.containsPlaceholders()) { - return textWithoutReplacements.getUnreplacedString(); - } - - return textWithGlobalReplacements.getUnreplacedString(); - } - - String getWithIndividualReplacements(Player player) { - if (!containsIndividualPlaceholders()) { - throw new IllegalStateException("does not contain individual placeholders"); - } - - if (textWithoutReplacements == null || !textWithoutReplacements.containsPlaceholders()) { - return textWithoutReplacements.getUnreplacedString(); - } - - String text = textWithGlobalReplacements.replacePlaceholders((PlaceholderOccurrence occurrence) -> - placeholderTracker.updateAndGetIndividualReplacement(occurrence, player)); - - if (PlaceholderAPIHook.isEnabled() && PlaceholderAPIHook.containsPlaceholders(text)) { - text = PlaceholderAPIHook.replacePlaceholders(player, text); - } - - return text; - } - boolean containsIndividualPlaceholders() { - if (textWithoutReplacements == null) { - return false; - } - return placeholderTracker.containsIndividualPlaceholders(textWithoutReplacements) || PlaceholderAPIHook.containsPlaceholders(textWithoutReplacements.getUnreplacedString()); } diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/TextLineTracker.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/TextLineTracker.java index 1c652a8d..b22b177f 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/TextLineTracker.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/hologram/tracking/TextLineTracker.java @@ -47,8 +47,8 @@ public class TextLineTracker extends TouchableLineTracker { super.detectChanges(); String displayText = line.getText(); - if (!Objects.equals(this.displayText.get(), displayText)) { - this.displayText.set(displayText); + if (!Objects.equals(this.displayText.getWithoutReplacements(), displayText)) { + this.displayText.setWithoutReplacements(displayText); this.displayTextChanged = true; } @@ -72,7 +72,7 @@ public class TextLineTracker extends TouchableLineTracker { super.addSpawnPackets(packetList); if (!allowPlaceholders) { - packetList.addArmorStandSpawnPackets(armorStandEntityID, locationX, getArmorStandLocationY(), locationZ, displayText.get()); + packetList.addArmorStandSpawnPackets(armorStandEntityID, locationX, getArmorStandLocationY(), locationZ, displayText.getWithoutReplacements()); } else if (displayText.containsIndividualPlaceholders()) { packetList.addArmorStandSpawnPackets(armorStandEntityID, locationX, getArmorStandLocationY(), locationZ, displayText::getWithIndividualReplacements); } else { @@ -93,7 +93,7 @@ public class TextLineTracker extends TouchableLineTracker { if (displayTextChanged) { if (!allowPlaceholders) { - packetList.addArmorStandNameChangePackets(armorStandEntityID, displayText.get()); + packetList.addArmorStandNameChangePackets(armorStandEntityID, displayText.getWithoutReplacements()); } else if (displayText.containsIndividualPlaceholders()) { packetList.addArmorStandNameChangePackets(armorStandEntityID, displayText::getWithIndividualReplacements); } else { diff --git a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/placeholder/parsing/StringWithPlaceholders.java b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/placeholder/parsing/StringWithPlaceholders.java index 2d812b68..0bf72cc7 100644 --- a/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/placeholder/parsing/StringWithPlaceholders.java +++ b/plugin/src/main/java/me/filoghost/holographicdisplays/plugin/placeholder/parsing/StringWithPlaceholders.java @@ -5,38 +5,41 @@ */ package me.filoghost.holographicdisplays.plugin.placeholder.parsing; -import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.function.Predicate; public final class StringWithPlaceholders { + private static final StringWithPlaceholders NULL_INSTANCE = new StringWithPlaceholders(null, null); + private static final char PLACEHOLDER_END_CHAR = '}'; private static final char PLACEHOLDER_START_CHAR = '{'; - private final @NotNull String string; - private final List stringParts; + private final @Nullable String string; + private final @Nullable List stringParts; - @Contract("null -> null; !null -> !null") - public static StringWithPlaceholders of(@Nullable String string) { - return string != null ? new StringWithPlaceholders(string) : null; + public static @NotNull StringWithPlaceholders of(@Nullable String string) { + if (string == null) { + return NULL_INSTANCE; + } + return new StringWithPlaceholders(string); } - public StringWithPlaceholders(@NotNull String string) { - this.string = string; - this.stringParts = splitToParts(string); + private StringWithPlaceholders(@NotNull String string) { + this(string, splitToParts(string)); } - private StringWithPlaceholders(@NotNull String string, @Nullable List stringParts) { + private StringWithPlaceholders(@Nullable String string, @Nullable List stringParts) { this.string = string; this.stringParts = stringParts; } - public String getUnreplacedString() { + public @Nullable String getUnreplacedString() { return string; } @@ -45,7 +48,7 @@ public final class StringWithPlaceholders { } public boolean anyMatch(Predicate filter) { - if (stringParts == null) { + if (!containsPlaceholders()) { return false; } @@ -61,7 +64,7 @@ public final class StringWithPlaceholders { return false; } - public StringWithPlaceholders partiallyReplacePlaceholders(PlaceholderReplaceFunction replaceFunction) { + public @NotNull StringWithPlaceholders partiallyReplacePlaceholders(PlaceholderReplaceFunction replaceFunction) { if (!containsPlaceholders()) { return this; } @@ -104,7 +107,7 @@ public final class StringWithPlaceholders { return new StringWithPlaceholders(fullOutput.toString(), newStringParts); } - public String replacePlaceholders(PlaceholderReplaceFunction replaceFunction) { + public @Nullable String replacePlaceholders(PlaceholderReplaceFunction replaceFunction) { if (!containsPlaceholders()) { return string; } @@ -117,7 +120,7 @@ public final class StringWithPlaceholders { return output.toString(); } - private @Nullable List splitToParts(String string) { + private static @Nullable List splitToParts(@NotNull String string) { int placeholderStartIndex = -1; int lastAppendIndex = 0; List stringParts = null; // Lazy initialization @@ -181,12 +184,12 @@ public final class StringWithPlaceholders { } StringWithPlaceholders other = (StringWithPlaceholders) obj; - return this.string.equals(other.string); + return Objects.equals(this.string, other.string); } @Override public int hashCode() { - return string.hashCode(); + return Objects.hashCode(string); }