Improved handling of gamemode default placeholders

This commit is contained in:
Florian CUNY 2019-04-06 21:09:31 +02:00
parent 5ff2bc1d6a
commit 01e1147c7f
4 changed files with 54 additions and 32 deletions

View File

@ -0,0 +1,16 @@
package world.bentobox.bentobox.api.placeholders;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
/**
*
* @since 1.5.0
* @author Poslovitch
*/
@FunctionalInterface
public interface GameModePlaceholderReplacer {
String onReplace(GameModeAddon addon, User user, Island island);
}

View File

@ -2,6 +2,7 @@ package world.bentobox.bentobox.api.placeholders;
import world.bentobox.bentobox.api.user.User;
@FunctionalInterface
public interface PlaceholderReplacer {
String onReplace(User user);

View File

@ -1,22 +1,41 @@
package world.bentobox.bentobox.lists;
import world.bentobox.bentobox.api.placeholders.GameModePlaceholderReplacer;
import world.bentobox.bentobox.util.Util;
import java.text.DateFormat;
import java.time.Instant;
import java.util.Date;
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");
WORLD_FRIENDLY_NAME("world-friendlyname", (addon, user, island) -> addon.getWorldSettings().getFriendlyName()),
ISLAND_DISTANCE("island-distance", (addon, user, island) -> DateFormat.getInstance().format(Date.from(Instant.ofEpochMilli(island.getCreatedDate())))),
ISLAND_PROTECTION_RANGE("island-protection-range", (addon, user, island) -> String.valueOf(addon.getWorldSettings().getIslandDistance())),
ISLAND_OWNER("island-owner", (addon, user, island) -> addon.getPlayers().getName(island.getOwner())),
ISLAND_CREATION_DATE("island-creation-date", (addon, user, island) -> String.valueOf(island.getProtectionRange())),
ISLAND_SPAWNPOINT("island-spawnpoint", (addon, user, island) -> Util.xyz(island.getCenter().toVector())),
ISLAND_NAME("island-name", (addon, user, island) -> island.getName() == null ? "" : island.getName());
private String placeholder;
/**
* @since 1.5.0
*/
private GameModePlaceholderReplacer replacer;
GameModePlaceholders(String placeholder) {
GameModePlaceholders(String placeholder, GameModePlaceholderReplacer replacer) {
this.placeholder = placeholder;
this.replacer = replacer;
}
public String getPlaceholder() {
return placeholder;
}
/**
* @since 1.5.0
*/
public GameModePlaceholderReplacer getReplacer() {
return replacer;
}
}

View File

@ -6,20 +6,15 @@ 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
*
* @since 1.4.0
*/
public class GameModePlaceholderManager {
@ -41,6 +36,11 @@ public class GameModePlaceholderManager {
}
}
/**
* Registers default placeholders for all GameModes. Will not overwrite any that the gamemode addon itself implements.
* @author tastybento
* @since 1.4.0
*/
class DefaultPlaceholder implements PlaceholderReplacer {
private final GameModeAddon addon;
private final GameModePlaceholders type;
@ -58,26 +58,12 @@ class DefaultPlaceholder implements PlaceholderReplacer {
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 "";
if (island == null) {
return "";
}
return type.getReplacer().onReplace(addon, user, island);
}
}