From 01e1147c7f783207dc4204bcf01f6d8a0c846c0a Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Sat, 6 Apr 2019 21:09:31 +0200 Subject: [PATCH] Improved handling of gamemode default placeholders --- .../GameModePlaceholderReplacer.java | 16 +++++++++ .../api/placeholders/PlaceholderReplacer.java | 1 + .../bentobox/lists/GameModePlaceholders.java | 35 ++++++++++++++----- .../managers/GameModePlaceholderManager.java | 34 ++++++------------ 4 files changed, 54 insertions(+), 32 deletions(-) create mode 100644 src/main/java/world/bentobox/bentobox/api/placeholders/GameModePlaceholderReplacer.java diff --git a/src/main/java/world/bentobox/bentobox/api/placeholders/GameModePlaceholderReplacer.java b/src/main/java/world/bentobox/bentobox/api/placeholders/GameModePlaceholderReplacer.java new file mode 100644 index 000000000..a49872c44 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/api/placeholders/GameModePlaceholderReplacer.java @@ -0,0 +1,16 @@ +package world.bentobox.bentobox.api.placeholders; + +import world.bentobox.bentobox.api.addons.GameModeAddon; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.objects.Island; + +/** + * + * @since 1.5.0 + * @author Poslovitch + */ +@FunctionalInterface +public interface GameModePlaceholderReplacer { + + String onReplace(GameModeAddon addon, User user, Island island); +} diff --git a/src/main/java/world/bentobox/bentobox/api/placeholders/PlaceholderReplacer.java b/src/main/java/world/bentobox/bentobox/api/placeholders/PlaceholderReplacer.java index 54bdfb59d..6725faf73 100644 --- a/src/main/java/world/bentobox/bentobox/api/placeholders/PlaceholderReplacer.java +++ b/src/main/java/world/bentobox/bentobox/api/placeholders/PlaceholderReplacer.java @@ -2,6 +2,7 @@ package world.bentobox.bentobox.api.placeholders; import world.bentobox.bentobox.api.user.User; +@FunctionalInterface public interface PlaceholderReplacer { String onReplace(User user); diff --git a/src/main/java/world/bentobox/bentobox/lists/GameModePlaceholders.java b/src/main/java/world/bentobox/bentobox/lists/GameModePlaceholders.java index 63eaef882..6c7acc0fc 100644 --- a/src/main/java/world/bentobox/bentobox/lists/GameModePlaceholders.java +++ b/src/main/java/world/bentobox/bentobox/lists/GameModePlaceholders.java @@ -1,22 +1,41 @@ package world.bentobox.bentobox.lists; +import world.bentobox.bentobox.api.placeholders.GameModePlaceholderReplacer; +import world.bentobox.bentobox.util.Util; + +import java.text.DateFormat; +import java.time.Instant; +import java.util.Date; + public enum GameModePlaceholders { - WORLD_FRIENDLY_NAME("world-friendlyname"), - ISLAND_DISTANCE("island-distance"), - ISLAND_PROTECTION_RANGE("island-protection-range"), - ISLAND_OWNER("island-owner"), - ISLAND_CREATION_DATE("island-creation-date"), - ISLAND_SPAWNPOINT("island-spawnpoint"), - ISLAND_NAME("island-name"); + WORLD_FRIENDLY_NAME("world-friendlyname", (addon, user, island) -> addon.getWorldSettings().getFriendlyName()), + ISLAND_DISTANCE("island-distance", (addon, user, island) -> DateFormat.getInstance().format(Date.from(Instant.ofEpochMilli(island.getCreatedDate())))), + ISLAND_PROTECTION_RANGE("island-protection-range", (addon, user, island) -> String.valueOf(addon.getWorldSettings().getIslandDistance())), + ISLAND_OWNER("island-owner", (addon, user, island) -> addon.getPlayers().getName(island.getOwner())), + ISLAND_CREATION_DATE("island-creation-date", (addon, user, island) -> String.valueOf(island.getProtectionRange())), + ISLAND_SPAWNPOINT("island-spawnpoint", (addon, user, island) -> Util.xyz(island.getCenter().toVector())), + ISLAND_NAME("island-name", (addon, user, island) -> island.getName() == null ? "" : island.getName()); private String placeholder; + /** + * @since 1.5.0 + */ + private GameModePlaceholderReplacer replacer; - GameModePlaceholders(String placeholder) { + GameModePlaceholders(String placeholder, GameModePlaceholderReplacer replacer) { this.placeholder = placeholder; + this.replacer = replacer; } public String getPlaceholder() { return placeholder; } + + /** + * @since 1.5.0 + */ + public GameModePlaceholderReplacer getReplacer() { + return replacer; + } } diff --git a/src/main/java/world/bentobox/bentobox/managers/GameModePlaceholderManager.java b/src/main/java/world/bentobox/bentobox/managers/GameModePlaceholderManager.java index a220621e6..7c48683f8 100644 --- a/src/main/java/world/bentobox/bentobox/managers/GameModePlaceholderManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/GameModePlaceholderManager.java @@ -6,20 +6,15 @@ import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.lists.GameModePlaceholders; -import world.bentobox.bentobox.util.Util; -import java.text.DateFormat; -import java.time.Instant; import java.util.Arrays; -import java.util.Date; import java.util.EnumMap; import java.util.Map; /** - * * Registers default placeholders for all GameModes. Will not overwrite any that the gamemode addon itself implements. * @author tastybento - * + * @since 1.4.0 */ public class GameModePlaceholderManager { @@ -41,6 +36,11 @@ public class GameModePlaceholderManager { } } +/** + * Registers default placeholders for all GameModes. Will not overwrite any that the gamemode addon itself implements. + * @author tastybento + * @since 1.4.0 + */ class DefaultPlaceholder implements PlaceholderReplacer { private final GameModeAddon addon; private final GameModePlaceholders type; @@ -58,26 +58,12 @@ class DefaultPlaceholder implements PlaceholderReplacer { return ""; } Island island = addon.getIslands().getIsland(addon.getOverWorld(), user); - switch (type) { - case WORLD_FRIENDLY_NAME: - return addon.getWorldSettings().getFriendlyName(); - case ISLAND_CREATION_DATE: - return island == null ? "" : DateFormat.getInstance().format(Date.from(Instant.ofEpochMilli(island.getCreatedDate()))); - case ISLAND_DISTANCE: - return String.valueOf(addon.getWorldSettings().getIslandDistance()); - case ISLAND_NAME: - return island == null ? "" : (island.getName() == null ? "" : island.getName()); - case ISLAND_OWNER: - return island == null ? "" : addon.getPlayers().getName(island.getOwner()); - case ISLAND_PROTECTION_RANGE: - return island == null ? "" : String.valueOf(island.getProtectionRange()); - case ISLAND_SPAWNPOINT: - return island == null ? "" : Util.xyz(island.getCenter().toVector()); - default: - return ""; + if (island == null) { + return ""; } + + return type.getReplacer().onReplace(addon, user, island); } - }