From 8f236afae9f3187012d0dc0a14c33db2a81dd729 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Tue, 18 Dec 2018 17:36:53 +1000 Subject: [PATCH] Added a display name to Actors --- .../java/com/sk89q/worldedit/bukkit/BukkitPlayer.java | 5 +++++ .../com/sk89q/worldedit/extension/platform/Actor.java | 9 +++++++++ .../sk89q/worldedit/extension/platform/PlayerProxy.java | 5 +++++ .../java/com/sk89q/worldedit/sponge/SpongePlayer.java | 6 +++++- 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java index e08e6b6dc..0ec373f63 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java @@ -83,6 +83,11 @@ public class BukkitPlayer extends AbstractPlayerActor { return player.getName(); } + @Override + public String getDisplayName() { + return player.getDisplayName(); + } + @Override public void giveItem(BaseItemStack itemStack) { player.getInventory().addItem(BukkitAdapter.adapt(itemStack)); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Actor.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Actor.java index 43f665423..cf64b5974 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Actor.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/Actor.java @@ -38,6 +38,15 @@ public interface Actor extends Identifiable, SessionOwner, Subject { */ String getName(); + /** + * Gets the display name of the actor. This can be a nickname, and is not guaranteed to be unique. + * + * @return The display name + */ + default String getDisplayName() { + return getName(); + } + /** * Print a message. * diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlayerProxy.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlayerProxy.java index be748c2c7..7d8a1b3da 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlayerProxy.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlayerProxy.java @@ -82,6 +82,11 @@ class PlayerProxy extends AbstractPlayerActor { return basePlayer.getName(); } + @Override + public String getDisplayName() { + return basePlayer.getDisplayName(); + } + @Override public BaseEntity getState() { throw new UnsupportedOperationException("Can't getState() on a player"); diff --git a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePlayer.java b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePlayer.java index fba1c81fc..08f7b47c3 100644 --- a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePlayer.java +++ b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePlayer.java @@ -35,7 +35,6 @@ import com.sk89q.worldedit.world.block.BlockStateHolder; import com.sk89q.worldedit.world.gamemode.GameMode; import com.sk89q.worldedit.world.gamemode.GameModes; import com.sk89q.worldedit.world.item.ItemTypes; - import org.spongepowered.api.Sponge; import org.spongepowered.api.data.type.HandTypes; import org.spongepowered.api.entity.living.player.Player; @@ -79,6 +78,11 @@ public class SpongePlayer extends AbstractPlayerActor { return this.player.getName(); } + @Override + public String getDisplayName() { + return player.getDisplayNameData().displayName().getDirect().map(TextSerializers.LEGACY_FORMATTING_CODE::serialize).orElse(getName()); + } + @Override public BaseEntity getState() { throw new UnsupportedOperationException("Cannot create a state from this object");