Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto adds default placeholders for GameModeAddons #616

Merged
merged 3 commits into from
Mar 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,22 @@
package world.bentobox.bentobox.lists;

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");

private String placeholder;

GameModePlaceholders(String placeholder) {
this.placeholder = placeholder;
}

public String getPlaceholder() {
return placeholder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package world.bentobox.bentobox.managers;

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.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
*
*/
public class GameModePlaceholderManager {

private BentoBox plugin;

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

public void registerGameModePlaceholders(GameModeAddon addon) {
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()));

// 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 GameModePlaceholders type;
public DefaultPlaceholder(GameModeAddon addon, GameModePlaceholders 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 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 "";
}
}

}



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

}