Deprecated GameModePlaceholderManager

GameModePlaceholderManager#registerGameModePlaceholders(...) is now handled by PlaceholdersManager#registerDefaultPlaceholders(...).
This commit is contained in:
Florian CUNY 2019-04-07 20:59:21 +02:00
parent 09f97ffc78
commit 5dd7796be4
4 changed files with 42 additions and 31 deletions

View File

@ -1,7 +1,5 @@
package world.bentobox.bentobox; package world.bentobox.bentobox;
import java.util.Optional;
import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.commons.lang.exception.ExceptionUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.generator.ChunkGenerator; import org.bukkit.generator.ChunkGenerator;
@ -9,7 +7,6 @@ import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.api.configuration.Config; import world.bentobox.bentobox.api.configuration.Config;
import world.bentobox.bentobox.api.events.BentoBoxReadyEvent; import world.bentobox.bentobox.api.events.BentoBoxReadyEvent;
import world.bentobox.bentobox.api.localization.TextVariables; 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.AddonsManager;
import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.FlagsManager; import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.GameModePlaceholderManager;
import world.bentobox.bentobox.managers.HooksManager; import world.bentobox.bentobox.managers.HooksManager;
import world.bentobox.bentobox.managers.IslandDeletionManager; import world.bentobox.bentobox.managers.IslandDeletionManager;
import world.bentobox.bentobox.managers.IslandWorldManager; 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.util.heads.HeadGetter;
import world.bentobox.bentobox.versions.ServerCompatibility; import world.bentobox.bentobox.versions.ServerCompatibility;
import java.util.Optional;
/** /**
* Main BentoBox class * Main BentoBox class
* @author tastybento, Poslovitch * @author tastybento, Poslovitch
@ -154,8 +152,7 @@ public class BentoBox extends JavaPlugin {
addonsManager.enableAddons(); addonsManager.enableAddons();
// Register default gamemode placeholders // Register default gamemode placeholders
GameModePlaceholderManager gmp = new GameModePlaceholderManager(this); addonsManager.getGameModeAddons().forEach(placeholdersManager::registerDefaultPlaceholders);
addonsManager.getGameModeAddons().forEach(gmp::registerGameModePlaceholders);
getServer().getScheduler().runTask(instance, () -> { getServer().getScheduler().runTask(instance, () -> {
// Register Listeners // Register Listeners

View File

@ -9,15 +9,13 @@ import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.lists.GameModePlaceholders; 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. * Registers default placeholders for all GameModes. Will not overwrite any that the gamemode addon itself implements.
* @author tastybento * @author tastybento
* @since 1.4.0 * @since 1.4.0
* @deprecated As of 1.5.0, for removal.
*/ */
@Deprecated
public class GameModePlaceholderManager { public class GameModePlaceholderManager {
private BentoBox plugin; private BentoBox plugin;
@ -26,19 +24,13 @@ public class GameModePlaceholderManager {
this.plugin = plugin; 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) { public void registerGameModePlaceholders(@NonNull GameModeAddon addon) {
Arrays.stream(GameModePlaceholders.values()) plugin.getPlaceholdersManager().registerDefaultPlaceholders(addon);
.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<GameModePlaceholders, String> 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())));
} }
} }

View File

@ -4,9 +4,14 @@ import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon; 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.api.placeholders.PlaceholderReplacer;
import world.bentobox.bentobox.hooks.PlaceholderAPIHook; 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; import java.util.Optional;
/** /**
@ -50,6 +55,26 @@ public class PlaceholdersManager {
getPlaceholderAPIHook().ifPresent(hook -> hook.registerPlaceholder(addon, placeholder, replacer)); 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<GameModePlaceholders, String> 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. * 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. * Note that if the placeholder you are trying to unregister has been registered by an addon, you should use {@link #unregisterPlaceholder(Addon, String)} instead.

View File

@ -20,7 +20,7 @@ import static org.mockito.Mockito.when;
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest( {BentoBox.class} ) @PrepareForTest( {BentoBox.class} )
public class GameModePlaceholderManagerTest { public class PlaceholdersManagerTest {
@Mock @Mock
private BentoBox plugin; private BentoBox plugin;
@ -29,11 +29,8 @@ public class GameModePlaceholderManagerTest {
@Mock @Mock
private PlaceholdersManager pm; private PlaceholdersManager pm;
private GameModePlaceholderManager gpm;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
gpm = new GameModePlaceholderManager(plugin);
// Addon // Addon
@NonNull @NonNull
AddonDescription desc = new AddonDescription.Builder("main", "bskyblock", "1.0").build(); 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 @Test
public void testRegisterGameModePlaceholdersAllDefaults() { public void testRegisterGameModePlaceholdersAllDefaults() {
gpm.registerGameModePlaceholders(addon); pm.registerDefaultPlaceholders(addon);
// 7 registrations for this addon // 7 registrations for this addon
Mockito.verify(pm, Mockito.atLeast(1)).registerPlaceholder(Mockito.anyString(), Mockito.any()); 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 @Test
public void testRegisterGameModePlaceholdersSomePreregistered() { public void testRegisterDefaultPlaceholdersSomePreregistered() {
// Some duplicates // Some duplicates
when(pm.isPlaceholder(Mockito.any(), Mockito.any())).thenReturn(false, true, true, false, false, true, false); 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 // 3 registrations for this addon
Mockito.verify(pm, Mockito.atLeast(1)).registerPlaceholder(Mockito.anyString(), Mockito.any()); Mockito.verify(pm, Mockito.atLeast(1)).registerPlaceholder(Mockito.anyString(), Mockito.any());