From 5c2411e420511a2499a884a15828f443f53e5cd8 Mon Sep 17 00:00:00 2001 From: XiaoMoMi Date: Mon, 18 Nov 2024 01:53:08 +0800 Subject: [PATCH] We need more work --- .../api/AbstractCNPlayer.java | 138 ++++++++++-------- .../customnameplates/api/CNPlayer.java | 70 ++++++--- .../api/feature/PreParsedDynamicText.java | 14 +- .../placeholder/PlaceholderManagerImpl.java | 8 +- .../compatibility/NameplatesExpansion.java | 26 ++-- gradle.properties | 9 +- 6 files changed, 156 insertions(+), 109 deletions(-) diff --git a/api/src/main/java/net/momirealms/customnameplates/api/AbstractCNPlayer.java b/api/src/main/java/net/momirealms/customnameplates/api/AbstractCNPlayer.java index a72c4f1..214291c 100644 --- a/api/src/main/java/net/momirealms/customnameplates/api/AbstractCNPlayer.java +++ b/api/src/main/java/net/momirealms/customnameplates/api/AbstractCNPlayer.java @@ -83,10 +83,10 @@ public abstract class AbstractCNPlayer implements CNPlayer { } private String updatePlayerPlaceholder(PlayerPlaceholder placeholder) { - TimeStampData value = getRawValue(placeholder); + TimeStampData value = getRawPlayerValue(placeholder); if (value == null) { value = new TimeStampData<>(placeholder.request(this), MainTask.getTicks(), true); - setValue(placeholder, value); + setPlayerValue(placeholder, value); return value.data(); } if (value.ticks() != MainTask.getTicks()) { @@ -119,7 +119,7 @@ public abstract class AbstractCNPlayer implements CNPlayer { } private String updateSharedPlaceholder(SharedPlaceholder placeholder) { - TimeStampData value = getRawValue(placeholder); + TimeStampData value = getRawSharedValue(placeholder); if (value == null) { String latest; if (MainTask.hasRequested(placeholder.countId())) { @@ -128,7 +128,7 @@ public abstract class AbstractCNPlayer implements CNPlayer { latest = placeholder.request(); } value = new TimeStampData<>(latest, MainTask.getTicks(), true); - setValue(placeholder, value); + setSharedValue(placeholder, value); return value.data(); } if (value.ticks() != MainTask.getTicks()) { @@ -266,86 +266,98 @@ public abstract class AbstractCNPlayer implements CNPlayer { } @Override - public void setValue(Placeholder placeholder, TimeStampData value) { + public void setPlayerValue(PlayerPlaceholder placeholder, TimeStampData value) { cachedValues.put(placeholder.countId(), value); } @Override - public boolean setValue(Placeholder placeholder, String value) { - TimeStampData previous = cachedValues.get(placeholder.countId()); - int currentTicks = MainTask.getTicks(); - boolean changed = false; - if (previous != null) { - if (previous.ticks() == currentTicks) { - return false; - } - String data = previous.data(); - if (!data.equals(value)) { - changed = true; - previous.data(value); - previous.updateTicks(true); - } - } else { - changed= true; - previous = new TimeStampData<>(value, currentTicks, true); - cachedValues.put(placeholder.countId(), previous); - } - return changed; + public void setSharedValue(SharedPlaceholder placeholder, TimeStampData value) { + cachedValues.put(placeholder.countId(), value); } @Override - public void setRelationalValue(Placeholder placeholder, CNPlayer another, TimeStampData value) { + public void setRelationalValue(RelationalPlaceholder placeholder, CNPlayer another, TimeStampData value) { WeakHashMap> map = cachedRelationalValues.computeIfAbsent(placeholder.countId(), k -> new WeakHashMap<>()); map.put(another, value); } +// @Override +// public boolean setPlayerValue(PlayerPlaceholder placeholder, String value) { +// TimeStampData previous = cachedValues.get(placeholder.countId()); +// int currentTicks = MainTask.getTicks(); +// boolean changed = false; +// if (previous != null) { +// if (previous.ticks() == currentTicks) { +// return false; +// } +// String data = previous.data(); +// if (!data.equals(value)) { +// changed = true; +// previous.data(value); +// previous.updateTicks(true); +// } +// } else { +// changed= true; +// previous = new TimeStampData<>(value, currentTicks, true); +// cachedValues.put(placeholder.countId(), previous); +// } +// return changed; +// } + +// @Override +// public boolean setRelationalValue(RelationalPlaceholder placeholder, CNPlayer another, String value) { +// WeakHashMap> map = cachedRelationalValues.computeIfAbsent(placeholder.countId(), k -> new WeakHashMap<>()); +// TimeStampData previous = map.get(another); +// int currentTicks = MainTask.getTicks(); +// boolean changed = false; +// if (previous != null) { +// if (previous.ticks() == currentTicks) { +// return false; +// } +// String data = previous.data(); +// if (!data.equals(value)) { +// changed = true; +// previous.data(value); +// previous.updateTicks(true); +// } +// } else { +// changed= true; +// previous = new TimeStampData<>(value, currentTicks, true); +// map.put(another, previous); +// } +// return changed; +// } + @Override - public boolean setRelationalValue(Placeholder placeholder, CNPlayer another, String value) { - WeakHashMap> map = cachedRelationalValues.computeIfAbsent(placeholder.countId(), k -> new WeakHashMap<>()); - TimeStampData previous = map.get(another); - int currentTicks = MainTask.getTicks(); - boolean changed = false; - if (previous != null) { - if (previous.ticks() == currentTicks) { - return false; - } - String data = previous.data(); - if (!data.equals(value)) { - changed = true; - previous.data(value); - previous.updateTicks(true); - } - } else { - changed= true; - previous = new TimeStampData<>(value, currentTicks, true); - map.put(another, previous); - } - return changed; + public @NotNull String getCachedSharedValue(SharedPlaceholder placeholder) { + return updateSharedPlaceholder(placeholder); } @Override - public @NotNull String getCachedValue(Placeholder placeholder) { - return Optional.ofNullable(cachedValues.get(placeholder.countId())).map(TimeStampData::data).orElse(placeholder.id()); + public @NotNull String getCachedPlayerValue(PlayerPlaceholder placeholder) { + return updatePlayerPlaceholder(placeholder); + } + + @Override + public @NotNull String getCachedRelationalValue(RelationalPlaceholder placeholder, CNPlayer another) { + return updateRelationalPlaceholder(placeholder, another); } @Nullable @Override - public TimeStampData getRawValue(Placeholder placeholder) { + public TimeStampData getRawPlayerValue(PlayerPlaceholder placeholder) { return cachedValues.get(placeholder.countId()); } + @Nullable @Override - public @NotNull String getCachedRelationalValue(Placeholder placeholder, CNPlayer another) { - WeakHashMap> map = cachedRelationalValues.get(placeholder.countId()); - if (map == null) { - return placeholder.id(); - } - return Optional.ofNullable(map.get(another)).map(TimeStampData::data).orElse(placeholder.id()); + public TimeStampData getRawSharedValue(SharedPlaceholder placeholder) { + return cachedValues.get(placeholder.countId()); } @Nullable @Override - public TimeStampData getRawRelationalValue(Placeholder placeholder, CNPlayer another) { + public TimeStampData getRawRelationalValue(RelationalPlaceholder placeholder, CNPlayer another) { WeakHashMap> map = cachedRelationalValues.get(placeholder.countId()); if (map == null) { return null; @@ -431,12 +443,12 @@ public abstract class AbstractCNPlayer implements CNPlayer { } tracker = new Tracker(another); trackers.put(another, tracker); - for (Placeholder placeholder : activePlaceholders()) { - if (placeholder instanceof RelationalPlaceholder relationalPlaceholder) { - String value = relationalPlaceholder.request(this, another); - setRelationalValue(placeholder, another, value); - } - } +// for (Placeholder placeholder : activePlaceholders()) { +// if (placeholder instanceof RelationalPlaceholder relationalPlaceholder) { +// String value = relationalPlaceholder.request(this, another); +// setRelationalValue(relationalPlaceholder, another, value); +// } +// } return tracker; } diff --git a/api/src/main/java/net/momirealms/customnameplates/api/CNPlayer.java b/api/src/main/java/net/momirealms/customnameplates/api/CNPlayer.java index ad2171d..7038ac8 100644 --- a/api/src/main/java/net/momirealms/customnameplates/api/CNPlayer.java +++ b/api/src/main/java/net/momirealms/customnameplates/api/CNPlayer.java @@ -23,6 +23,8 @@ import net.momirealms.customnameplates.api.feature.TimeStampData; import net.momirealms.customnameplates.api.feature.tag.TeamView; import net.momirealms.customnameplates.api.network.Tracker; import net.momirealms.customnameplates.api.placeholder.Placeholder; +import net.momirealms.customnameplates.api.placeholder.PlayerPlaceholder; +import net.momirealms.customnameplates.api.placeholder.RelationalPlaceholder; import net.momirealms.customnameplates.api.placeholder.SharedPlaceholder; import net.momirealms.customnameplates.api.requirement.Requirement; import net.momirealms.customnameplates.api.util.Vector3; @@ -201,16 +203,16 @@ public interface CNPlayer { * @return the cached data as a string */ @NotNull - String getCachedValue(Placeholder placeholder); + String getCachedSharedValue(SharedPlaceholder placeholder); /** - * Retrieves the cached {@link TimeStampData} for a given placeholder. + * Retrieves the cached data for a given placeholder. * * @param placeholder the placeholder to retrieve data for - * @return the cached TickStampData, or null if none exists + * @return the cached data as a string */ - @Nullable - TimeStampData getRawValue(Placeholder placeholder); + @NotNull + String getCachedPlayerValue(PlayerPlaceholder placeholder); /** * Retrieves the cached relational data between this player and another for a given placeholder. @@ -220,7 +222,25 @@ public interface CNPlayer { * @return the relational data as a string */ @NotNull - String getCachedRelationalValue(Placeholder placeholder, CNPlayer another); + String getCachedRelationalValue(RelationalPlaceholder placeholder, CNPlayer another); + + /** + * Retrieves the cached {@link TimeStampData} for a given placeholder. + * + * @param placeholder the placeholder to retrieve data for + * @return the cached TickStampData, or null if none exists + */ + @Nullable + TimeStampData getRawPlayerValue(PlayerPlaceholder placeholder); + + /** + * Retrieves the cached {@link TimeStampData} for a given placeholder. + * + * @param placeholder the placeholder to retrieve data for + * @return the cached TickStampData, or null if none exists + */ + @Nullable + TimeStampData getRawSharedValue(SharedPlaceholder placeholder); /** * Retrieves the cached relational {@link TimeStampData} for a given placeholder. @@ -230,7 +250,7 @@ public interface CNPlayer { * @return the cached relational TickStampData, or null if none exists */ @Nullable - TimeStampData getRawRelationalValue(Placeholder placeholder, CNPlayer another); + TimeStampData getRawRelationalValue(RelationalPlaceholder placeholder, CNPlayer another); /** * Caches the specified {@link TimeStampData} for the given placeholder. @@ -238,16 +258,15 @@ public interface CNPlayer { * @param placeholder the placeholder to cache * @param value the value to cache */ - void setValue(Placeholder placeholder, TimeStampData value); + void setPlayerValue(PlayerPlaceholder placeholder, TimeStampData value); /** - * Caches the specified value for the given placeholder. + * Caches the specified {@link TimeStampData} for the given placeholder. * * @param placeholder the placeholder to cache * @param value the value to cache - * @return true if the value was changed, false otherwise */ - boolean setValue(Placeholder placeholder, String value); + void setSharedValue(SharedPlaceholder placeholder, TimeStampData value); /** * Caches the specified relational {@link TimeStampData} for a given placeholder and player. @@ -256,17 +275,26 @@ public interface CNPlayer { * @param another the other player * @param value the value to cache */ - void setRelationalValue(Placeholder placeholder, CNPlayer another, TimeStampData value); + void setRelationalValue(RelationalPlaceholder placeholder, CNPlayer another, TimeStampData value); - /** - * Caches the specified relational value for a given placeholder and player. - * - * @param placeholder the relational placeholder - * @param another the other player - * @param value the value to cache - * @return true if the value was changed, false otherwise - */ - boolean setRelationalValue(Placeholder placeholder, CNPlayer another, String value); +// /** +// * Caches the specified value for the given placeholder. +// * +// * @param placeholder the placeholder to cache +// * @param value the value to cache +// * @return true if the value was changed, false otherwise +// */ +// boolean setPlayerValue(PlayerPlaceholder placeholder, String value); + +// /** +// * Caches the specified relational value for a given placeholder and player. +// * +// * @param placeholder the relational placeholder +// * @param another the other player +// * @param value the value to cache +// * @return true if the value was changed, false otherwise +// */ +// boolean setRelationalValue(RelationalPlaceholder placeholder, CNPlayer another, String value); /** * Adds a feature to the player diff --git a/api/src/main/java/net/momirealms/customnameplates/api/feature/PreParsedDynamicText.java b/api/src/main/java/net/momirealms/customnameplates/api/feature/PreParsedDynamicText.java index 54b605b..e45fc15 100644 --- a/api/src/main/java/net/momirealms/customnameplates/api/feature/PreParsedDynamicText.java +++ b/api/src/main/java/net/momirealms/customnameplates/api/feature/PreParsedDynamicText.java @@ -55,18 +55,24 @@ public class PreParsedDynamicText { for (String id : detectedPlaceholders) { Placeholder placeholder = manager.getPlaceholder(id); placeholders.add(placeholder); - if (placeholder instanceof RelationalPlaceholder) { - convertor.add((owner) -> (viewer) -> owner.getCachedRelationalValue(placeholder, viewer)); + if (placeholder instanceof RelationalPlaceholder relationalPlaceholder) { + convertor.add((owner) -> (viewer) -> owner.getCachedRelationalValue(relationalPlaceholder, viewer)); } else if (placeholder instanceof PlayerPlaceholder playerPlaceholder) { convertor.add((owner) -> (viewer) -> { if (owner != null) { - return owner.getCachedValue(placeholder); + return owner.getCachedPlayerValue(playerPlaceholder); } else { return playerPlaceholder.request(null); } }); } else if (placeholder instanceof SharedPlaceholder sharedPlaceholder) { - convertor.add((owner) -> (viewer) -> sharedPlaceholder.getLatestValue()); + convertor.add((owner) -> (viewer) -> { + if (owner != null) { + return owner.getCachedSharedValue(sharedPlaceholder); + } else { + return sharedPlaceholder.request(); + } + }); } else { convertor.add((owner) -> (viewer) -> id); } diff --git a/backend/src/main/java/net/momirealms/customnameplates/backend/placeholder/PlaceholderManagerImpl.java b/backend/src/main/java/net/momirealms/customnameplates/backend/placeholder/PlaceholderManagerImpl.java index 5a45b0b..9626454 100644 --- a/backend/src/main/java/net/momirealms/customnameplates/backend/placeholder/PlaceholderManagerImpl.java +++ b/backend/src/main/java/net/momirealms/customnameplates/backend/placeholder/PlaceholderManagerImpl.java @@ -584,10 +584,10 @@ public class PlaceholderManagerImpl implements PlaceholderManager { List delayedPlaceholdersToUpdate = new ObjectArrayList<>(); for (Placeholder placeholder : player.activePlaceholdersToRefresh()) { if (placeholder instanceof PlayerPlaceholder playerPlaceholder) { - TimeStampData previous = player.getRawValue(placeholder); + TimeStampData previous = player.getRawPlayerValue(playerPlaceholder); if (previous == null) { String value = playerPlaceholder.request(player); - player.setValue(placeholder, new TimeStampData<>(value, MainTask.getTicks(), true)); + player.setPlayerValue(playerPlaceholder, new TimeStampData<>(value, MainTask.getTicks(), true)); featuresToNotifyUpdates.addAll(player.activeFeatures(placeholder)); } else { if (previous.ticks() > MainTask.getTicks() - getRefreshInterval(placeholder.countId())) { @@ -609,7 +609,7 @@ public class PlaceholderManagerImpl implements PlaceholderManager { } else if (placeholder instanceof RelationalPlaceholder relationalPlaceholder) { delayedPlaceholdersToUpdate.add(relationalPlaceholder); } else if (placeholder instanceof SharedPlaceholder sharedPlaceholder) { - TimeStampData previous = player.getRawValue(placeholder); + TimeStampData previous = player.getRawSharedValue(sharedPlaceholder); if (previous == null) { String value; // if the shared placeholder has been updated by other players @@ -618,7 +618,7 @@ public class PlaceholderManagerImpl implements PlaceholderManager { } else { value = sharedPlaceholder.request(); } - player.setValue(placeholder, new TimeStampData<>(value, MainTask.getTicks(), true)); + player.setSharedValue(sharedPlaceholder, new TimeStampData<>(value, MainTask.getTicks(), true)); featuresToNotifyUpdates.addAll(player.activeFeatures(placeholder)); } else { // The placeholder has been refreshed by other codes diff --git a/compatibility/src/main/java/net/momirealms/customnameplates/bukkit/compatibility/NameplatesExpansion.java b/compatibility/src/main/java/net/momirealms/customnameplates/bukkit/compatibility/NameplatesExpansion.java index 6bbd274..51d3d6e 100644 --- a/compatibility/src/main/java/net/momirealms/customnameplates/bukkit/compatibility/NameplatesExpansion.java +++ b/compatibility/src/main/java/net/momirealms/customnameplates/bukkit/compatibility/NameplatesExpansion.java @@ -21,18 +21,15 @@ import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.Relational; import net.momirealms.customnameplates.api.CNPlayer; import net.momirealms.customnameplates.api.CustomNameplates; -import net.momirealms.customnameplates.api.feature.TimeStampData; import net.momirealms.customnameplates.api.placeholder.Placeholder; import net.momirealms.customnameplates.api.placeholder.PlayerPlaceholder; +import net.momirealms.customnameplates.api.placeholder.RelationalPlaceholder; +import net.momirealms.customnameplates.api.placeholder.SharedPlaceholder; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Collections; -import java.util.Optional; -import java.util.Set; - public class NameplatesExpansion extends PlaceholderExpansion implements Relational { private final CustomNameplates plugin; @@ -64,14 +61,10 @@ public class NameplatesExpansion extends PlaceholderExpansion implements Relatio @Override public @Nullable String onRequest(OfflinePlayer player, @NotNull String params) { Placeholder placeholder = plugin.getPlaceholderManager().getRegisteredPlaceholder("%np_" + params + "%"); + CNPlayer cnPlayer = player == null ? null : plugin.getPlayer(player.getUniqueId()); if (placeholder instanceof PlayerPlaceholder playerPlaceholder) { - CNPlayer cnPlayer = player == null ? null : plugin.getPlayer(player.getUniqueId()); - if (placeholder.children().isEmpty()) { - return playerPlaceholder.request(cnPlayer); - } if (cnPlayer != null) { - cnPlayer.forceUpdatePlaceholders(Set.of(placeholder), Collections.emptySet()); - return cnPlayer.getCachedValue(placeholder); + return cnPlayer.getCachedPlayerValue(playerPlaceholder); } else { try { return playerPlaceholder.request(null); @@ -79,6 +72,12 @@ public class NameplatesExpansion extends PlaceholderExpansion implements Relatio plugin.getPluginLogger().warn("%nameplates_" + params + "% contains a placeholder that requires a player as the parameter"); } } + } else if (placeholder instanceof SharedPlaceholder sharedPlaceholder) { + if (cnPlayer != null) { + return cnPlayer.getCachedSharedValue(sharedPlaceholder); + } else { + return sharedPlaceholder.request(); + } } return null; } @@ -91,9 +90,8 @@ public class NameplatesExpansion extends PlaceholderExpansion implements Relatio return null; } Placeholder placeholder = plugin.getPlaceholderManager().getRegisteredPlaceholder("%rel_np_" + params + "%"); - if (placeholder != null) { - cnPlayer1.forceUpdatePlaceholders(Set.of(placeholder), Set.of(cnPlayer2)); - return Optional.ofNullable(cnPlayer1.getRawRelationalValue(placeholder, cnPlayer2)).map(TimeStampData::data).orElse(placeholder.id()); + if (placeholder instanceof RelationalPlaceholder relationalPlaceholder) { + return cnPlayer1.getCachedRelationalValue(relationalPlaceholder, cnPlayer2); } return null; } diff --git a/gradle.properties b/gradle.properties index 300f989..75d43f5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,5 @@ # Project settings # Rule: [major update].[feature update].[bug fix] -project_version=3.0.11 config_version=32 project_group=net.momirealms @@ -51,5 +50,9 @@ fastutil_version=8.5.15 #systemProp.socks.proxyPort=7890 #systemProp.http.proxyHost=127.0.0.1 #systemProp.http.proxyPort=7890 -#systemProp.https.proxyHost=127.0.0.1 -#systemProp.https.proxyPort=7890 \ No newline at end of file +systemProp.socks.proxyHost=127.0.0.1 +systemProp.socks.proxyPort=7890 +systemProp.http.proxyHost=127.0.0.1 +systemProp.http.proxyPort=7890 +systemProp.https.proxyHost=127.0.0.1 +systemProp.https.proxyPort=7890 \ No newline at end of file