9
0
mirror of https://github.com/Xiao-MoMi/Custom-Nameplates.git synced 2025-12-19 15:09:23 +00:00

We need more work

This commit is contained in:
XiaoMoMi
2024-11-18 01:53:08 +08:00
parent a57cfb0842
commit 5c2411e420
6 changed files with 156 additions and 109 deletions

View File

@@ -83,10 +83,10 @@ public abstract class AbstractCNPlayer implements CNPlayer {
}
private String updatePlayerPlaceholder(PlayerPlaceholder placeholder) {
TimeStampData<String> value = getRawValue(placeholder);
TimeStampData<String> 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<String> value = getRawValue(placeholder);
TimeStampData<String> 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<String> value) {
public void setPlayerValue(PlayerPlaceholder placeholder, TimeStampData<String> value) {
cachedValues.put(placeholder.countId(), value);
}
@Override
public boolean setValue(Placeholder placeholder, String value) {
TimeStampData<String> 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<String> value) {
cachedValues.put(placeholder.countId(), value);
}
@Override
public void setRelationalValue(Placeholder placeholder, CNPlayer another, TimeStampData<String> value) {
public void setRelationalValue(RelationalPlaceholder placeholder, CNPlayer another, TimeStampData<String> value) {
WeakHashMap<CNPlayer, TimeStampData<String>> map = cachedRelationalValues.computeIfAbsent(placeholder.countId(), k -> new WeakHashMap<>());
map.put(another, value);
}
// @Override
// public boolean setPlayerValue(PlayerPlaceholder placeholder, String value) {
// TimeStampData<String> 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<CNPlayer, TimeStampData<String>> map = cachedRelationalValues.computeIfAbsent(placeholder.countId(), k -> new WeakHashMap<>());
// TimeStampData<String> 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<CNPlayer, TimeStampData<String>> map = cachedRelationalValues.computeIfAbsent(placeholder.countId(), k -> new WeakHashMap<>());
TimeStampData<String> 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<String> getRawValue(Placeholder placeholder) {
public TimeStampData<String> getRawPlayerValue(PlayerPlaceholder placeholder) {
return cachedValues.get(placeholder.countId());
}
@Nullable
@Override
public @NotNull String getCachedRelationalValue(Placeholder placeholder, CNPlayer another) {
WeakHashMap<CNPlayer, TimeStampData<String>> 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<String> getRawSharedValue(SharedPlaceholder placeholder) {
return cachedValues.get(placeholder.countId());
}
@Nullable
@Override
public TimeStampData<String> getRawRelationalValue(Placeholder placeholder, CNPlayer another) {
public TimeStampData<String> getRawRelationalValue(RelationalPlaceholder placeholder, CNPlayer another) {
WeakHashMap<CNPlayer, TimeStampData<String>> 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;
}

View File

@@ -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<String> 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<String> 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<String> 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<String> getRawRelationalValue(Placeholder placeholder, CNPlayer another);
TimeStampData<String> 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<String> value);
void setPlayerValue(PlayerPlaceholder placeholder, TimeStampData<String> 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<String> 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<String> value);
void setRelationalValue(RelationalPlaceholder placeholder, CNPlayer another, TimeStampData<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(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

View File

@@ -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);
}

View File

@@ -584,10 +584,10 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
List<RelationalPlaceholder> delayedPlaceholdersToUpdate = new ObjectArrayList<>();
for (Placeholder placeholder : player.activePlaceholdersToRefresh()) {
if (placeholder instanceof PlayerPlaceholder playerPlaceholder) {
TimeStampData<String> previous = player.getRawValue(placeholder);
TimeStampData<String> 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<String> previous = player.getRawValue(placeholder);
TimeStampData<String> 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

View File

@@ -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;
}

View File

@@ -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
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