9
0
mirror of https://github.com/Xiao-MoMi/Custom-Nameplates.git synced 2026-01-06 15:42:00 +00:00
This commit is contained in:
XiaoMoMi
2024-10-07 22:08:37 +08:00
parent f6de779090
commit cad0e07411
26 changed files with 426 additions and 328 deletions

View File

@@ -127,7 +127,7 @@ public class ActionBarSender implements Feature {
currentActionBar = carouselText.preParsedDynamicText().fastCreate(owner);
if (carouselText.updateOnDisplay()) {
owner.forceUpdate(currentActionBar.placeholders(), Collections.emptySet());
owner.forceUpdatePlaceholders(currentActionBar.placeholders(), Collections.emptySet());
}
textChangeFlag = true;
}

View File

@@ -126,7 +126,7 @@ public class BossBarSender implements Feature, BossBar {
currentBossBar = carouselText.preParsedDynamicText().fastCreate(owner);
if (carouselText.updateOnDisplay()) {
owner.forceUpdate(currentBossBar.placeholders(), Collections.emptySet());
owner.forceUpdatePlaceholders(currentBossBar.placeholders(), Collections.emptySet());
}
refresh();
@@ -183,7 +183,7 @@ public class BossBarSender implements Feature, BossBar {
}
public void sendLatestBossBarName() {
if (latestContent != null) {
if (latestContent != null && isShown()) {
Object packet = CustomNameplates.getInstance().getPlatform().updateBossBarNamePacket(uuid, AdventureHelper.miniMessageToMinecraftComponent(latestContent));
CustomNameplates.getInstance().getPacketSender().sendPacket(owner, packet);
}

View File

@@ -104,7 +104,7 @@ public class BubbleTag extends AbstractTag {
}
@Override
public void onOpacityChange(CNPlayer viewer, boolean dark) {
public void darkTag(CNPlayer viewer, boolean dark) {
}
@Override
@@ -306,4 +306,9 @@ public class BubbleTag extends AbstractTag {
public boolean affectedByScaling() {
return true;
}
@Override
public boolean affectedBySpectator() {
return false;
}
}

View File

@@ -63,7 +63,9 @@ public class NameTag extends AbstractTag implements RelationalFeature {
owner.position().add(0,(1.8 + (affectedByCrouching() && tracker.isCrouching() && !owner.isFlying() ? -0.3 : 0) + renderer.hatOffset()) * (affectedByScaling() ? tracker.getScale() : 1),0),
0f, 0f, 0d,
0, 0, 0,
component, config.backgroundColor(), opacity(), config.hasShadow(), config.isSeeThrough(), config.useDefaultBackgroundColor(),
component, config.backgroundColor(),
(owner.isSpectator() && affectedBySpectator()) || (owner.isCrouching() && affectedByCrouching()) ? 64 : opacity(),
config.hasShadow(), config.isSeeThrough(), config.useDefaultBackgroundColor(),
config.alignment(), config.viewRange(), config.shadowRadius(), config.shadowStrength(),
(affectedByScaling() ? scale(viewer).multiply(tracker.getScale()) : scale(viewer)),
(affectedByScaling() ? translation(viewer).multiply(tracker.getScale()) : translation(viewer)),
@@ -101,7 +103,7 @@ public class NameTag extends AbstractTag implements RelationalFeature {
currentText = carouselText.preParsedDynamicText().fastCreate(owner);
if (carouselText.updateOnDisplay()) {
owner.forceUpdate(currentText.placeholders(), owner.nearbyPlayers());
owner.forceUpdatePlaceholders(currentText.placeholders(), owner.nearbyPlayers());
}
refresh();
@@ -194,7 +196,7 @@ public class NameTag extends AbstractTag implements RelationalFeature {
@Override
public byte opacity() {
return owner.isSpectator() || (owner.isCrouching() && affectedByCrouching()) ? 64 : config.opacity();
return config.opacity();
}
@Override
@@ -204,11 +206,16 @@ public class NameTag extends AbstractTag implements RelationalFeature {
@Override
public boolean affectedByCrouching() {
return config.affectedByScaling();
return config.affectedByCrouching();
}
@Override
public boolean affectedByScaling() {
return affectedByCrouching();
return config.affectedByScaling();
}
@Override
public boolean affectedBySpectator() {
return config.affectedBySpectator();
}
}

View File

@@ -261,7 +261,23 @@ public class TagRendererImpl implements TagRenderer {
if (display.affectedByCrouching()) {
if (display.isShown()) {
if (display.isShown(another)) {
display.onOpacityChange(another, isCrouching || tracker.isSpectator());
display.darkTag(another, isCrouching || (display.affectedBySpectator() && tracker.isSpectator()));
}
}
}
}
}
public void handleGameModeChange(CNPlayer another, boolean isSpectator) {
Tracker tracker = owner.getTracker(another);
// can be null
if (tracker == null) return;
tracker.setSpectator(isSpectator);
for (Tag display : this.tags) {
if (display.affectedBySpectator()) {
if (display.isShown()) {
if (display.isShown(another)) {
display.darkTag(another, isSpectator || (display.affectedByCrouching() && tracker.isCrouching()));
}
}
}
@@ -283,18 +299,4 @@ public class TagRendererImpl implements TagRenderer {
}
}
}
public void handleGameModeChange(CNPlayer another, boolean isSpectator) {
Tracker tracker = owner.getTracker(another);
// can be null
if (tracker == null) return;
tracker.setSpectator(isSpectator);
for (Tag display : this.tags) {
if (display.isShown()) {
if (display.isShown(another)) {
display.onOpacityChange(another, isSpectator || tracker.isCrouching());
}
}
}
}
}

View File

@@ -217,6 +217,7 @@ public class UnlimitedTagManagerImpl implements UnlimitedTagManager, JoinQuitLis
.useDefaultBackgroundColor(section.getBoolean("use-default-background-color", false))
.backgroundColor(ConfigUtils.argb(section.getString("background-color", "64,0,0,0")))
.affectedByCrouching(section.getBoolean("affected-by-crouching", true))
.affectedBySpectator(section.getBoolean("affected-by-spectator", true))
.affectedByScaling(section.getBoolean("affected-by-scale-attribute", true))
.carouselText(
section.contains("text") ?

View File

@@ -23,7 +23,8 @@ import net.momirealms.customnameplates.api.CNPlayer;
import net.momirealms.customnameplates.api.ConfigManager;
import net.momirealms.customnameplates.api.CustomNameplates;
import net.momirealms.customnameplates.api.MainTask;
import net.momirealms.customnameplates.api.feature.*;
import net.momirealms.customnameplates.api.feature.OffsetFont;
import net.momirealms.customnameplates.api.feature.PreParsedDynamicText;
import net.momirealms.customnameplates.api.feature.background.Background;
import net.momirealms.customnameplates.api.feature.bubble.Bubble;
import net.momirealms.customnameplates.api.feature.bubble.BubbleConfig;
@@ -55,7 +56,6 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
private final HashMap<String, Integer> refreshIntervals = new HashMap<>();
private final Map<String, Placeholder> registeredPlaceholders = new HashMap<>();
private final HashMap<Placeholder, List<PreParsedDynamicText>> childrenText = new HashMap<>();
private final List<PreParsedDynamicText> delayedInitTexts = new ArrayList<>();
private final HashMap<String, Placeholder> nestedPlaceholders = new HashMap<>();
public PlaceholderManagerImpl(CustomNameplates plugin) {
@@ -84,23 +84,20 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
Placeholder placeholder = entry.getKey();
for (PreParsedDynamicText preParsedText : entry.getValue()) {
preParsedText.init();
placeholder.addChildren(preParsedText.placeholders());
for (Placeholder child : preParsedText.placeholders()) {
placeholder.addChild(child);
child.addParent(placeholder);
}
}
this.nestedPlaceholders.put(placeholder.id().replace("%np_", "%nameplates_").replace("%rel_np_", "%rel_nameplates_"), placeholder);
}
this.childrenText.clear();
for (PreParsedDynamicText preParsedText : this.delayedInitTexts) {
preParsedText.init();
}
this.delayedInitTexts.clear();
}
@Override
public void unload() {
this.refreshIntervals.clear();
this.registeredPlaceholders.clear();
this.delayedInitTexts.clear();
this.childrenText.clear();
this.nestedPlaceholders.clear();
PlaceholderCounter.reset();
@@ -320,7 +317,7 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
PreParsedDynamicText defaultValue = new PreParsedDynamicText(inner.getString("default", ""));
ArrayList<PreParsedDynamicText> list = new ArrayList<>();
list.add(placeholderToSwitch);
this.delayedInitTexts.add(defaultValue);
list.add(defaultValue);
Map<String, PreParsedDynamicText> valueMap = new HashMap<>();
Section results = inner.getSection("case");
if (results != null) {
@@ -328,7 +325,7 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
if (strEntry.getValue() instanceof String string) {
PreParsedDynamicText preParsedDynamicText = new PreParsedDynamicText(string);
valueMap.put(strEntry.getKey(), preParsedDynamicText);
this.delayedInitTexts.add(preParsedDynamicText);
list.add(placeholderToSwitch);
}
}
}
@@ -340,13 +337,13 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
Placeholder placeholder2 = registerPlayerPlaceholder("%np_switch_" + id + "%", (player) -> {
String value = placeholderToSwitch.fastCreate(player).render(player);
PreParsedDynamicText text = valueMap.getOrDefault(value, defaultValue);
player.forceUpdate(text.placeholders(), Collections.emptySet());
player.forceUpdatePlaceholders(text.placeholders(), Collections.emptySet());
return text.fastCreate(player).render(player);
});
Placeholder placeholder3 = registerRelationalPlaceholder("%rel_np_switch_" + id + "%", (p1, p2) -> {
String value = placeholderToSwitch.fastCreate(p1).render(p2);
PreParsedDynamicText text = valueMap.getOrDefault(value, defaultValue);
p1.forceUpdate(text.placeholders(), Set.of(p2));
p1.forceUpdatePlaceholders(text.placeholders(), Set.of(p2));
return text.fastCreate(p1).render(p2);
});
childrenText.put(placeholder1, list);
@@ -474,6 +471,7 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
for (Map.Entry<String, Object> entry : section.getStringRouteMappedValues(false).entrySet()) {
String id = entry.getKey();
if (entry.getValue() instanceof Section placeholderSection) {
List<PreParsedDynamicText> list = new ArrayList<>();
ArrayList<Pair<PreParsedDynamicText, Requirement[]>> orderedTexts = new ArrayList<>();
for (Map.Entry<String, Object> conditionEntry : placeholderSection.getStringRouteMappedValues(false).entrySet()) {
if (conditionEntry.getValue() instanceof Section inner) {
@@ -481,10 +479,10 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
Requirement[] requirements = plugin.getRequirementManager().parseRequirements(inner.getSection("conditions"));
PreParsedDynamicText preParsedDynamicText = new PreParsedDynamicText(text);
orderedTexts.add(Pair.of(preParsedDynamicText, requirements));
this.delayedInitTexts.add(preParsedDynamicText);
list.add(preParsedDynamicText);
}
}
this.registerSharedPlaceholder("%shared_np_conditional_" + id + "%", () -> {
Placeholder placeholder1 = this.registerSharedPlaceholder("%shared_np_conditional_" + id + "%", () -> {
outer:
for (Pair<PreParsedDynamicText, Requirement[]> orderedText : orderedTexts) {
for (Requirement requirement : orderedText.right()) {
@@ -496,28 +494,31 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
}
return "";
});
this.registerPlayerPlaceholder("%np_conditional_" + id + "%", (player) -> {
Placeholder placeholder2 = this.registerPlayerPlaceholder("%np_conditional_" + id + "%", (player) -> {
for (Pair<PreParsedDynamicText, Requirement[]> orderedText : orderedTexts) {
if (!player.isMet(orderedText.right())) {
continue;
}
PreParsedDynamicText text = orderedText.left();
player.forceUpdate(text.placeholders(), Collections.emptySet());
player.forceUpdatePlaceholders(text.placeholders(), Collections.emptySet());
return text.fastCreate(player).render(player);
}
return "";
});
this.registerRelationalPlaceholder("%rel_np_conditional_" + id + "%", (p1, p2) -> {
Placeholder placeholder3 = this.registerRelationalPlaceholder("%rel_np_conditional_" + id + "%", (p1, p2) -> {
for (Pair<PreParsedDynamicText, Requirement[]> orderedText : orderedTexts) {
if (!p1.isMet(p2, orderedText.right())) {
continue;
}
PreParsedDynamicText text = orderedText.left();
p1.forceUpdate(text.placeholders(), Set.of(p2));
p1.forceUpdatePlaceholders(text.placeholders(), Set.of(p2));
return text.fastCreate(p1).render(p2);
}
return "";
});
childrenText.put(placeholder1, list);
childrenText.put(placeholder2, list);
childrenText.put(placeholder3, list);
}
}
}
@@ -526,133 +527,7 @@ public class PlaceholderManagerImpl implements PlaceholderManager {
public void refreshPlaceholders() {
for (CNPlayer player : plugin.getOnlinePlayers()) {
if (!player.isOnline()) continue;
Set<Feature> featuresToNotifyUpdates = new HashSet<>();
Map<Feature, List<CNPlayer>> relationalFeaturesToNotifyUpdates = new HashMap<>();
List<Placeholder> placeholdersToUpdate = player.activePlaceholdersToRefresh();
List<RelationalPlaceholder> delayedPlaceholdersToUpdate = new ArrayList<>();
for (Placeholder placeholder : placeholdersToUpdate) {
if (placeholder instanceof PlayerPlaceholder playerPlaceholder) {
TickStampData<String> previous = player.getValue(placeholder);
if (previous == null) {
String value = playerPlaceholder.request(player);
player.setValue(placeholder, new TickStampData<>(value, MainTask.getTicks(), true));
featuresToNotifyUpdates.addAll(player.activeFeatures(placeholder));
} else {
if (previous.ticks() == MainTask.getTicks()) {
if (previous.hasValueChanged()) {
featuresToNotifyUpdates.addAll(player.activeFeatures(placeholder));
}
continue;
}
String value = playerPlaceholder.request(player);
if (!previous.data().equals(value)) {
previous.data(value);
previous.updateTicks(true);
featuresToNotifyUpdates.addAll(player.activeFeatures(placeholder));
} else {
previous.updateTicks(false);
}
}
} else if (placeholder instanceof RelationalPlaceholder relationalPlaceholder) {
delayedPlaceholdersToUpdate.add(relationalPlaceholder);
} else if (placeholder instanceof SharedPlaceholder sharedPlaceholder) {
TickStampData<String> previous = player.getValue(placeholder);
if (previous == null) {
String value;
// if the shared placeholder has been updated by other players
if (MainTask.hasRequested(sharedPlaceholder.countId())) {
value = sharedPlaceholder.getLatestValue();
} else {
value = sharedPlaceholder.request();
}
player.setValue(placeholder, new TickStampData<>(value, MainTask.getTicks(), true));
featuresToNotifyUpdates.addAll(player.activeFeatures(placeholder));
} else {
// The placeholder has been refreshed by other codes
if (previous.ticks() == MainTask.getTicks()) {
if (previous.hasValueChanged()) {
featuresToNotifyUpdates.addAll(player.activeFeatures(placeholder));
}
continue;
}
String value;
// if the shared placeholder has been updated by other players
if (MainTask.hasRequested(sharedPlaceholder.countId())) {
value = sharedPlaceholder.getLatestValue();
} else {
value = sharedPlaceholder.request();
}
if (!previous.data().equals(value)) {
previous.data(value);
previous.updateTicks(true);
featuresToNotifyUpdates.addAll(player.activeFeatures(placeholder));
} else {
previous.updateTicks(false);
}
}
}
}
for (RelationalPlaceholder placeholder : delayedPlaceholdersToUpdate) {
for (CNPlayer nearby : player.nearbyPlayers()) {
TickStampData<String> previous = player.getRelationalValue(placeholder, nearby);
if (previous == null) {
String value = placeholder.request(player, nearby);
player.setRelationalValue(placeholder, nearby, new TickStampData<>(value, MainTask.getTicks(), true));
for (Feature feature : player.activeFeatures(placeholder)) {
// Filter features that will not be updated for all players
if (!featuresToNotifyUpdates.contains(feature)) {
List<CNPlayer> players = relationalFeaturesToNotifyUpdates.computeIfAbsent(feature, k -> new ArrayList<>());
players.add(nearby);
}
}
} else {
if (previous.ticks() == MainTask.getTicks()) {
if (previous.hasValueChanged()) {
for (Feature feature : player.activeFeatures(placeholder)) {
// Filter features that will not be updated for all players
if (!featuresToNotifyUpdates.contains(feature)) {
List<CNPlayer> players = relationalFeaturesToNotifyUpdates.computeIfAbsent(feature, k -> new ArrayList<>());
players.add(nearby);
}
}
}
continue;
}
String value = placeholder.request(player, nearby);
if (!previous.data().equals(value)) {
previous.data(value);
previous.updateTicks(true);
for (Feature feature : player.activeFeatures(placeholder)) {
// Filter features that will not be updated for all players
if (!featuresToNotifyUpdates.contains(feature)) {
List<CNPlayer> players = relationalFeaturesToNotifyUpdates.computeIfAbsent(feature, k -> new ArrayList<>());
players.add(nearby);
}
}
} else {
previous.updateTicks(false);
}
}
}
}
// Switch to another thread for updating
plugin.getScheduler().async().execute(() -> {
// Async task takes time and the player might have been offline
if (!player.isOnline()) return;
for (Feature feature : featuresToNotifyUpdates) {
feature.notifyPlaceholderUpdates(player, false);
}
for (Map.Entry<Feature, List<CNPlayer>> innerEntry : relationalFeaturesToNotifyUpdates.entrySet()) {
Feature feature = innerEntry.getKey();
if (feature instanceof RelationalFeature relationalFeature) {
for (CNPlayer other : innerEntry.getValue()) {
relationalFeature.notifyPlaceholderUpdates(player, other, false);
}
}
}
});
player.updateAndNotifyChanges(player.activePlaceholdersToRefresh());
}
}

View File

@@ -37,7 +37,7 @@ public abstract class PlaceholderRequirement<T> extends AbstractRequirement {
@Override
public boolean isSatisfied(CNPlayer p1, CNPlayer p2) {
p1.forceUpdate(text.placeholders(), Set.of(p2));
p1.forceUpdatePlaceholders(text.placeholders(), Set.of(p2));
String a1 = text.fastCreate(p1).render(p2);
return checkArgument(a1, any);
}

View File

@@ -37,8 +37,8 @@ public abstract class PlaceholdersRequirement extends AbstractRequirement {
@Override
public boolean isSatisfied(CNPlayer p1, CNPlayer p2) {
p1.forceUpdate(t1.placeholders(), Set.of(p2));
p1.forceUpdate(t2.placeholders(), Set.of(p2));
p1.forceUpdatePlaceholders(t1.placeholders(), Set.of(p2));
p1.forceUpdatePlaceholders(t2.placeholders(), Set.of(p2));
String a1 = t1.fastCreate(p1).render(p2);
String a2 = t2.fastCreate(p1).render(p2);
return checkArgument(a1, a2);

View File

@@ -33,6 +33,7 @@ unlimited:
self-disguised: false
affected-by-crouching: true
affected-by-scale-attribute: true
affected-by-spectator: true
line-width: 1024
background-color: 0,0,0,0
tag_2:
@@ -48,6 +49,7 @@ unlimited:
self-disguised: false
affected-by-crouching: true
affected-by-scale-attribute: true
affected-by-spectator: true
line-width: 1024
background-color: 0,0,0,0
tag_3:
@@ -63,5 +65,6 @@ unlimited:
self-disguised: false
affected-by-crouching: true
affected-by-scale-attribute: true
affected-by-spectator: true
line-width: 1024
background-color: 64,0,0,0