Skip to content

Commit

Permalink
Auto adds default placeholders for GameModeAddons
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Mar 22, 2019
1 parent 99e9ad6 commit 449cc75
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/main/java/world/bentobox/bentobox/BentoBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -154,6 +155,10 @@ public void onEnable(){
addonsManager.loadAddons();
// Enable addons
addonsManager.enableAddons();

// Register default gamemode placeholders
GameModePlaceholderManager gmp = new GameModePlaceholderManager(this);
addonsManager.getGameModeAddons().forEach(gmp::registerGameModePlaceholders);

getServer().getScheduler().runTask(instance, () -> {
// Register Listeners
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,14 @@ public String onPlaceholderRequest(Player p, String placeholder) {
}
return null;
}

/**
* Checks if a placeholder with this name is already registered
* @param placeholder - name of placeholder
* @return <tt>true</tt> if a placeholder with this name is already registered
* @since 1.4.0
*/
public boolean isPlaceholder(@NonNull String placeholder) {
return placeholders.containsKey(placeholder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,15 @@ public void unregisterPlaceholder(@NonNull Addon addon, @NonNull String placehol
public void registerAddonPlaceholder(Addon addon, String placeholder, PlaceholderReplacer replacer) {
registerPlaceholder(addon, placeholder, replacer);
}

/**
* Checks if a placeholder with this name is already registered
* @param addon the addon, not null
* @param placeholder - name of placeholder
* @return <tt>true</tt> if a placeholder with this name is already registered
* @since 1.4.0
*/
public boolean isPlaceholder(@NonNull Addon addon, @NonNull String placeholder) {
return addonsExpansions.containsKey(addon) ? addonsExpansions.get(addon).isPlaceholder(placeholder) : false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package world.bentobox.bentobox.managers;

import java.text.DateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.EnumMap;
import java.util.Map;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
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.managers.GameModePlaceholderManager.Placeholders;
import world.bentobox.bentobox.util.Util;

/**
*
* Registers default placeholders for all GameModes. Will not overwrite any that the gamemode addon itself implements.
* @author tastybento
*
*/
public class GameModePlaceholderManager {

enum Placeholders {
FRIENDLY_NAME,
ISLAND_DISTANCE,
ISLAND_PROTECTION_RANGE,
ISLAND_OWNER,
ISLAND_CREATION_DATE,
ISLAND_SPAWNPOINT,
ISLAND_NAME
}

private BentoBox plugin;

public GameModePlaceholderManager(BentoBox plugin) {
super();
this.plugin = plugin;
}

public void registerGameModePlaceholders(GameModeAddon addon) {
String prefix = addon.getDescription().getName().toLowerCase();
Map<Placeholders, String> placeholders = new EnumMap<>(Placeholders.class);
placeholders.put(Placeholders.FRIENDLY_NAME, prefix + "-world-friendlyname");
placeholders.put(Placeholders.ISLAND_DISTANCE, prefix + "-island-distance");
placeholders.put(Placeholders.ISLAND_PROTECTION_RANGE, prefix + "-island-protection-range");
placeholders.put(Placeholders.ISLAND_OWNER, prefix + "-island-owner");
placeholders.put(Placeholders.ISLAND_CREATION_DATE, prefix + "-island-creation-date");
placeholders.put(Placeholders.ISLAND_SPAWNPOINT, prefix + "-island-spawnpoint");
placeholders.put(Placeholders.ISLAND_NAME, prefix + "-island-name");

// 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())));
}
}

class DefaultPlaceholder implements PlaceholderReplacer {
private final GameModeAddon addon;
private final GameModePlaceholderManager.Placeholders type;
public DefaultPlaceholder(GameModeAddon addon, Placeholders type) {
super();
this.addon = addon;
this.type = type;
}
/* (non-Javadoc)
* @see world.bentobox.bentobox.api.placeholders.PlaceholderReplacer#onReplace(world.bentobox.bentobox.api.user.User)
*/
@Override
public String onReplace(User user) {
if (user == null) {
return "";
}
Island island = addon.getIslands().getIsland(addon.getOverWorld(), user);
switch (type) {
case 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 "";
}
}

}



Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package world.bentobox.bentobox.managers;

import com.mongodb.lang.NonNull;
import com.mongodb.lang.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer;
import world.bentobox.bentobox.hooks.PlaceholderAPIHook;

import java.util.Optional;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

/**
* Manages placeholder integration.
*
Expand Down Expand Up @@ -85,4 +86,15 @@ public void unregisterPlaceholder(@Nullable Addon addon, @NonNull String placeho
private Optional<PlaceholderAPIHook> getPlaceholderAPIHook() {
return plugin.getHooks().getHook("PlaceholderAPI").map(hook -> (PlaceholderAPIHook) hook);
}

/**
* Checks if a placeholder with this name is already registered
* @param addon the addon, not null
* @param placeholder - name of placeholder
* @return <tt>true</tt> if a placeholder with this name is already registered
* @since 1.4.0
*/
public boolean isPlaceholder(@NonNull Addon addon, @NonNull String placeholder) {
return addon == null ? false : getPlaceholderAPIHook().map(h -> h.isPlaceholder(addon, placeholder)).orElse(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@PrepareForTest( { BentoBox.class} )
public class AddonsManagerTest {

private static BentoBox plugin;
private BentoBox plugin;

@Before
public void setup() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
*
*/
package world.bentobox.bentobox.managers;

import static org.mockito.Mockito.when;

import org.eclipse.jdt.annotation.NonNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.AddonDescription;
import world.bentobox.bentobox.api.addons.GameModeAddon;

/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( {BentoBox.class} )
public class GameModePlaceholderManagerTest {

@Mock
private BentoBox plugin;
@Mock
private GameModeAddon addon;
@Mock
private PlaceholdersManager pm;



private GameModePlaceholderManager gpm;

/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
gpm = new GameModePlaceholderManager(plugin);
// Addon
@NonNull
AddonDescription desc = new AddonDescription.Builder("main", "bskyblock", "1.0").build();
when(addon.getDescription()).thenReturn(desc);

when(plugin.getPlaceholdersManager()).thenReturn(pm);
// No placeholders registered yet
when(pm.isPlaceholder(Mockito.any(), Mockito.any())).thenReturn(false);
}

/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}

/**
* Test method for {@link world.bentobox.bentobox.managers.GameModePlaceholderManager#registerGameModePlaceholders(world.bentobox.bentobox.api.addons.GameModeAddon)}.
*/
@Test
public void testRegisterGameModePlaceholdersAllDefaults() {
gpm.registerGameModePlaceholders(addon);
// 7 registrations for this addon
Mockito.verify(pm, Mockito.times(7)).registerPlaceholder(Mockito.anyString(), Mockito.any());
}

/**
* Test method for {@link world.bentobox.bentobox.managers.GameModePlaceholderManager#registerGameModePlaceholders(world.bentobox.bentobox.api.addons.GameModeAddon)}.
*/
@Test
public void testRegisterGameModePlaceholdersSomePreregistered() {
// Some duplicates
when(pm.isPlaceholder(Mockito.any(), Mockito.any())).thenReturn(false, true, true, false, false, true, false);

gpm.registerGameModePlaceholders(addon);

// 3 registrations for this addon
Mockito.verify(pm, Mockito.times(4)).registerPlaceholder(Mockito.anyString(), Mockito.any());
}

}

0 comments on commit 449cc75

Please sign in to comment.