From 5dd7796be42352e729d3ba2f0c1bd0688cffe64e Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Sun, 7 Apr 2019 20:59:21 +0200 Subject: [PATCH] Deprecated GameModePlaceholderManager GameModePlaceholderManager#registerGameModePlaceholders(...) is now handled by PlaceholdersManager#registerDefaultPlaceholders(...). --- .../world/bentobox/bentobox/BentoBox.java | 9 +++---- .../managers/GameModePlaceholderManager.java | 24 ++++++------------ .../managers/PlaceholdersManager.java | 25 +++++++++++++++++++ ...Test.java => PlaceholdersManagerTest.java} | 15 +++++------ 4 files changed, 42 insertions(+), 31 deletions(-) rename src/test/java/world/bentobox/bentobox/managers/{GameModePlaceholderManagerTest.java => PlaceholdersManagerTest.java} (73%) diff --git a/src/main/java/world/bentobox/bentobox/BentoBox.java b/src/main/java/world/bentobox/bentobox/BentoBox.java index 6e461c54b..dc0273442 100644 --- a/src/main/java/world/bentobox/bentobox/BentoBox.java +++ b/src/main/java/world/bentobox/bentobox/BentoBox.java @@ -1,7 +1,5 @@ package world.bentobox.bentobox; -import java.util.Optional; - import org.apache.commons.lang.exception.ExceptionUtils; import org.bukkit.Bukkit; import org.bukkit.generator.ChunkGenerator; @@ -9,7 +7,6 @@ import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; - import world.bentobox.bentobox.api.configuration.Config; import world.bentobox.bentobox.api.events.BentoBoxReadyEvent; import world.bentobox.bentobox.api.localization.TextVariables; @@ -30,7 +27,6 @@ import world.bentobox.bentobox.listeners.StandardSpawnProtectionListener; import world.bentobox.bentobox.managers.AddonsManager; import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.FlagsManager; -import world.bentobox.bentobox.managers.GameModePlaceholderManager; import world.bentobox.bentobox.managers.HooksManager; import world.bentobox.bentobox.managers.IslandDeletionManager; import world.bentobox.bentobox.managers.IslandWorldManager; @@ -44,6 +40,8 @@ import world.bentobox.bentobox.managers.WebManager; import world.bentobox.bentobox.util.heads.HeadGetter; import world.bentobox.bentobox.versions.ServerCompatibility; +import java.util.Optional; + /** * Main BentoBox class * @author tastybento, Poslovitch @@ -154,8 +152,7 @@ public class BentoBox extends JavaPlugin { addonsManager.enableAddons(); // Register default gamemode placeholders - GameModePlaceholderManager gmp = new GameModePlaceholderManager(this); - addonsManager.getGameModeAddons().forEach(gmp::registerGameModePlaceholders); + addonsManager.getGameModeAddons().forEach(placeholdersManager::registerDefaultPlaceholders); getServer().getScheduler().runTask(instance, () -> { // Register Listeners diff --git a/src/main/java/world/bentobox/bentobox/managers/GameModePlaceholderManager.java b/src/main/java/world/bentobox/bentobox/managers/GameModePlaceholderManager.java index 23d2efca8..50fd405b1 100644 --- a/src/main/java/world/bentobox/bentobox/managers/GameModePlaceholderManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/GameModePlaceholderManager.java @@ -9,15 +9,13 @@ import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.lists.GameModePlaceholders; -import java.util.Arrays; -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 + * @deprecated As of 1.5.0, for removal. */ +@Deprecated public class GameModePlaceholderManager { private BentoBox plugin; @@ -26,19 +24,13 @@ public class GameModePlaceholderManager { this.plugin = plugin; } + /** + * @since 1.4.0 + * @deprecated As of 1.5.0, for removal. Use {@link PlaceholdersManager#registerDefaultPlaceholders(GameModeAddon)} instead. + */ + @Deprecated public void registerGameModePlaceholders(@NonNull GameModeAddon addon) { - Arrays.stream(GameModePlaceholders.values()) - .filter(placeholder -> !plugin.getPlaceholdersManager().isPlaceholder(addon, placeholder.getPlaceholder())) - .forEach(placeholder -> plugin.getPlaceholdersManager().registerPlaceholder(addon, placeholder.getPlaceholder(), new DefaultPlaceholder(addon, placeholder))); - - // TODO legacy placeholders, do not forget to remove at some point - String prefix = addon.getDescription().getName().toLowerCase(); - Map placeholders = new EnumMap<>(GameModePlaceholders.class); - Arrays.stream(GameModePlaceholders.values()).forEach(placeholder -> placeholders.put(placeholder, prefix + "-" + placeholder.getPlaceholder().replace('_', '-'))); - - // Register placeholders only if they have not already been registered by the addon itself - placeholders.entrySet().stream().filter(en -> !plugin.getPlaceholdersManager().isPlaceholder(addon, en.getValue())) - .forEach(en -> plugin.getPlaceholdersManager().registerPlaceholder(en.getValue(), new DefaultPlaceholder(addon, en.getKey()))); + plugin.getPlaceholdersManager().registerDefaultPlaceholders(addon); } } diff --git a/src/main/java/world/bentobox/bentobox/managers/PlaceholdersManager.java b/src/main/java/world/bentobox/bentobox/managers/PlaceholdersManager.java index ffff57bb3..921718dcd 100644 --- a/src/main/java/world/bentobox/bentobox/managers/PlaceholdersManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/PlaceholdersManager.java @@ -4,9 +4,14 @@ import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.addons.Addon; +import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer; import world.bentobox.bentobox.hooks.PlaceholderAPIHook; +import world.bentobox.bentobox.lists.GameModePlaceholders; +import java.util.Arrays; +import java.util.EnumMap; +import java.util.Map; import java.util.Optional; /** @@ -50,6 +55,26 @@ public class PlaceholdersManager { getPlaceholderAPIHook().ifPresent(hook -> hook.registerPlaceholder(addon, placeholder, replacer)); } + /** + * Registers default placeholders for this gamemode addon. + * @param addon the gamemode addon to register the default placeholders too. + * @since 1.5.0 + */ + public void registerDefaultPlaceholders(@NonNull GameModeAddon addon) { + Arrays.stream(GameModePlaceholders.values()) + .filter(placeholder -> !isPlaceholder(addon, placeholder.getPlaceholder())) + .forEach(placeholder -> registerPlaceholder(addon, placeholder.getPlaceholder(), new DefaultPlaceholder(addon, placeholder))); + + // TODO legacy placeholders, do not forget to remove at some point + String prefix = addon.getDescription().getName().toLowerCase(); + Map placeholders = new EnumMap<>(GameModePlaceholders.class); + Arrays.stream(GameModePlaceholders.values()).forEach(placeholder -> placeholders.put(placeholder, prefix + "-" + placeholder.getPlaceholder().replace('_', '-'))); + + // Register placeholders only if they have not already been registered by the addon itself + placeholders.entrySet().stream().filter(en -> !isPlaceholder(addon, en.getValue())) + .forEach(en -> registerPlaceholder(en.getValue(), new DefaultPlaceholder(addon, en.getKey()))); + } + /** * Unregisters this placeholder on the behalf of BentoBox. * Note that if the placeholder you are trying to unregister has been registered by an addon, you should use {@link #unregisterPlaceholder(Addon, String)} instead. diff --git a/src/test/java/world/bentobox/bentobox/managers/GameModePlaceholderManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/PlaceholdersManagerTest.java similarity index 73% rename from src/test/java/world/bentobox/bentobox/managers/GameModePlaceholderManagerTest.java rename to src/test/java/world/bentobox/bentobox/managers/PlaceholdersManagerTest.java index a59ccdb97..fbdc60bdb 100644 --- a/src/test/java/world/bentobox/bentobox/managers/GameModePlaceholderManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/PlaceholdersManagerTest.java @@ -20,7 +20,7 @@ import static org.mockito.Mockito.when; */ @RunWith(PowerMockRunner.class) @PrepareForTest( {BentoBox.class} ) -public class GameModePlaceholderManagerTest { +public class PlaceholdersManagerTest { @Mock private BentoBox plugin; @@ -29,11 +29,8 @@ public class GameModePlaceholderManagerTest { @Mock private PlaceholdersManager pm; - private GameModePlaceholderManager gpm; - @Before public void setUp() throws Exception { - gpm = new GameModePlaceholderManager(plugin); // Addon @NonNull AddonDescription desc = new AddonDescription.Builder("main", "bskyblock", "1.0").build(); @@ -45,24 +42,24 @@ public class GameModePlaceholderManagerTest { } /** - * Test method for {@link world.bentobox.bentobox.managers.GameModePlaceholderManager#registerGameModePlaceholders(world.bentobox.bentobox.api.addons.GameModeAddon)}. + * Test method for {@link world.bentobox.bentobox.managers.PlaceholdersManager#registerDefaultPlaceholders(GameModeAddon)}. */ @Test public void testRegisterGameModePlaceholdersAllDefaults() { - gpm.registerGameModePlaceholders(addon); + pm.registerDefaultPlaceholders(addon); // 7 registrations for this addon Mockito.verify(pm, Mockito.atLeast(1)).registerPlaceholder(Mockito.anyString(), Mockito.any()); } /** - * Test method for {@link world.bentobox.bentobox.managers.GameModePlaceholderManager#registerGameModePlaceholders(world.bentobox.bentobox.api.addons.GameModeAddon)}. + * Test method for {@link world.bentobox.bentobox.managers.PlaceholdersManager#registerDefaultPlaceholders(GameModeAddon)}. */ @Test - public void testRegisterGameModePlaceholdersSomePreregistered() { + public void testRegisterDefaultPlaceholdersSomePreregistered() { // Some duplicates when(pm.isPlaceholder(Mockito.any(), Mockito.any())).thenReturn(false, true, true, false, false, true, false); - gpm.registerGameModePlaceholders(addon); + pm.registerDefaultPlaceholders(addon); // 3 registrations for this addon Mockito.verify(pm, Mockito.atLeast(1)).registerPlaceholder(Mockito.anyString(), Mockito.any());