From 115f434a303417019d05723174ff4a5777cb2b2d Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Sat, 14 Sep 2019 21:51:28 +0200 Subject: [PATCH] Added parsing placeholders in entities' name when pasting blueprints Implements https://github.com/BentoBoxWorld/BentoBox/issues/898 Also adds a few nullability annotations --- .../bentobox/bentobox/api/user/User.java | 2 +- .../bentobox/blueprints/BlueprintPaster.java | 24 ++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/user/User.java b/src/main/java/world/bentobox/bentobox/api/user/User.java index c504c6036..942d923ff 100644 --- a/src/main/java/world/bentobox/bentobox/api/user/User.java +++ b/src/main/java/world/bentobox/bentobox/api/user/User.java @@ -87,7 +87,7 @@ public class User { * @return user - user */ @Nullable - public static User getInstance(UUID uuid) { + public static User getInstance(@Nullable UUID uuid) { if (uuid == null) { return null; } diff --git a/src/main/java/world/bentobox/bentobox/blueprints/BlueprintPaster.java b/src/main/java/world/bentobox/bentobox/blueprints/BlueprintPaster.java index eff50be9a..5a2ae450b 100644 --- a/src/main/java/world/bentobox/bentobox/blueprints/BlueprintPaster.java +++ b/src/main/java/world/bentobox/bentobox/blueprints/BlueprintPaster.java @@ -23,6 +23,7 @@ import org.bukkit.entity.Ageable; import org.bukkit.entity.ChestedHorse; import org.bukkit.entity.Horse; import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; import org.bukkit.entity.Tameable; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; @@ -135,7 +136,7 @@ public class BlueprintPaster { count++; } while (pasteState.equals(PasteState.ENTITIES) && count < pasteSpeed && it3.hasNext()) { - pasteEntity(world, loc, it3.next()); + pasteEntity(world, island, loc, it3.next()); count++; } // STATE SHIFT @@ -211,11 +212,11 @@ public class BlueprintPaster { return blockData; } - private void pasteEntity(World world, Location location, Entry> entry) { + private void pasteEntity(World world, Island island, Location location, Entry> entry) { int x = location.getBlockX() + entry.getKey().getBlockX(); int y = location.getBlockY() + entry.getKey().getBlockY(); int z = location.getBlockZ() + entry.getKey().getBlockZ(); - setEntity(new Location(world, x, y, z), entry.getValue()); + setEntity(island, new Location(world, x, y, z), entry.getValue()); } /** @@ -255,16 +256,28 @@ public class BlueprintPaster { /** * Sets any entity that is in this location + * @param island - Island * @param location - location * @param list - list of entities to paste */ - private void setEntity(Location location, List list) { + private void setEntity(@Nullable Island island, Location location, List list) { list.stream().filter(k -> k.getType() != null).forEach(k -> { // Center, and just a bit high Location center = location.add(new Vector(0.5, 0.5, 0.5)); LivingEntity e = (LivingEntity)location.getWorld().spawnEntity(center, k.getType()); if (k.getCustomName() != null) { - e.setCustomName(k.getCustomName()); + String customName = k.getCustomName(); + + if (island != null) { + // Parse any placeholders in the entity's name, if the owner's connected (he should) + Player owner = User.getInstance(island.getOwner()).getPlayer(); + if (owner != null) { + customName = plugin.getPlaceholdersManager().replacePlaceholders(owner, customName); + } + } + + // Actually set the custom name + e.setCustomName(customName); } if (e instanceof Colorable && k.getColor() != null) { ((Colorable) e).setColor(k.getColor()); @@ -298,7 +311,6 @@ public class BlueprintPaster { /** * Tracks the minimum and maximum block positions - * @param world - world * @param l - location of block pasted */ private void updatePos(Location l) {