-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto adds default placeholders for GameModeAddons
- Loading branch information
1 parent
99e9ad6
commit 449cc75
Showing
7 changed files
with
228 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/main/java/world/bentobox/bentobox/managers/GameModePlaceholderManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ""; | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/test/java/world/bentobox/bentobox/managers/GameModePlaceholderManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |