Skip to content

Commit 54103ad

Browse files
committed
ExprName - add support for Ref
1 parent 012001e commit 54103ad

File tree

1 file changed

+24
-4
lines changed
  • src/main/java/com/github/skriptdev/skript/plugin/elements/expressions/entity

1 file changed

+24
-4
lines changed

src/main/java/com/github/skriptdev/skript/plugin/elements/expressions/entity/ExprName.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.github.skriptdev.skript.plugin.elements.expressions.entity;
22

33
import com.github.skriptdev.skript.api.hytale.utils.EntityUtils;
4+
import com.hypixel.hytale.component.Ref;
5+
import com.hypixel.hytale.component.Store;
46
import com.hypixel.hytale.server.core.entity.Entity;
57
import com.hypixel.hytale.server.core.entity.entities.Player;
8+
import com.hypixel.hytale.server.core.entity.nameplate.Nameplate;
69
import com.hypixel.hytale.server.core.universe.PlayerRef;
710
import com.hypixel.hytale.server.core.universe.world.World;
11+
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
812
import io.github.syst3ms.skriptparser.lang.Expression;
913
import io.github.syst3ms.skriptparser.lang.TriggerContext;
1014
import io.github.syst3ms.skriptparser.lang.properties.PropertyExpression;
@@ -20,12 +24,12 @@ public class ExprName extends PropertyExpression<Object, String> {
2024

2125
public static void register(SkriptRegistration registration) {
2226
registration.newPropertyExpression(ExprName.class, String.class,
23-
"[:display] name[s]", "entities/players/playerrefs/worlds")
27+
"[:display] name[s]", "entities/players/playerrefs/refs/worlds")
2428
.name("Name of Object")
2529
.description("Gets the name of an object.",
26-
"Currently supports players, entities, and worlds.",
30+
"Currently supports players, entities, refs, and worlds.",
2731
"Display name refers to the nameplate over an entity/player's head.",
28-
"Display name of Entity/Player can be set. PlayerRef/World do not support setting.")
32+
"Display name of Entity/Ref/Player can be set. PlayerRef/World do not support setting.")
2933
.examples("set {_name} to name of player",
3034
"set {_w} to name of world of player",
3135
"set display name of target entity of player to \"Mr Sheep\"")
@@ -41,12 +45,19 @@ public boolean init(Expression<?> @NotNull [] expressions, int matchedPattern, P
4145
return super.init(expressions, matchedPattern, parseContext);
4246
}
4347

48+
@SuppressWarnings("unchecked")
4449
@Override
4550
public @Nullable String getProperty(Object object) {
4651
return switch (object) {
4752
case PlayerRef playerRef -> playerRef.getUsername();
4853
case Player player -> this.display ? EntityUtils.getName(player) : player.getDisplayName();
4954
case Entity entity -> EntityUtils.getName(entity);
55+
case Ref<?> ref -> {
56+
Ref<EntityStore> reference = (Ref<EntityStore>) ref;
57+
Nameplate component = reference.getStore().getComponent(reference, Nameplate.getComponentType());
58+
if (component != null) yield component.getText();
59+
else yield null;
60+
}
5061
case World world -> world.getName();
5162
default -> null;
5263
};
@@ -59,7 +70,7 @@ public Optional<Class<?>[]> acceptsChange(@NotNull ChangeMode mode) {
5970
return Optional.empty();
6071
}
6172

62-
@SuppressWarnings("ConstantValue")
73+
@SuppressWarnings({"ConstantValue", "unchecked"})
6374
@Override
6475
public void change(@NotNull TriggerContext ctx, @NotNull ChangeMode changeMode, Object @NotNull [] changeWith) {
6576
String name = null;
@@ -71,6 +82,15 @@ public void change(@NotNull TriggerContext ctx, @NotNull ChangeMode changeMode,
7182
for (Object o : getOwner().getArray(ctx)) {
7283
if (o instanceof Entity entity) {
7384
EntityUtils.setNameplateName(entity, name);
85+
} else if (o instanceof Ref<?> ref) {
86+
Ref<EntityStore> reference = (Ref<EntityStore>) ref;
87+
Store<EntityStore> store = reference.getStore();
88+
if (name != null) {
89+
Nameplate component = store.ensureAndGetComponent(reference, Nameplate.getComponentType());
90+
component.setText(name);
91+
} else {
92+
store.tryRemoveComponent(reference, Nameplate.getComponentType());
93+
}
7494
}
7595
}
7696
}

0 commit comments

Comments
 (0)