#1015 Distinguish player-dependent tags from "simple" tags

This commit is contained in:
ljacqu
2017-01-22 10:43:46 +01:00
parent 811bdee128
commit 367380265e
6 changed files with 142 additions and 50 deletions
@@ -24,6 +24,7 @@ import java.util.List;
import java.util.stream.Collectors;
import static fr.xephi.authme.util.FileUtils.copyFileFromResource;
import static fr.xephi.authme.util.lazytags.TagBuilder.createTag;
/**
* Configuration for the welcome message (welcome.txt).
@@ -48,16 +49,16 @@ public class WelcomeMessageConfiguration implements Reloadable {
/** List of all supported tags for the welcome message. */
private final List<Tag> availableTags = Arrays.asList(
new Tag("&", () -> "\u00a7"),
new Tag("{PLAYER}", pl -> pl.getName()),
new Tag("{ONLINE}", () -> Integer.toString(bukkitService.getOnlinePlayers().size())),
new Tag("{MAXPLAYERS}", () -> Integer.toString(server.getMaxPlayers())),
new Tag("{IP}", pl -> PlayerUtils.getPlayerIp(pl)),
new Tag("{LOGINS}", () -> Integer.toString(playerCache.getLogged())),
new Tag("{WORLD}", pl -> pl.getWorld().getName()),
new Tag("{SERVER}", () -> server.getServerName()),
new Tag("{VERSION}", () -> server.getBukkitVersion()),
new Tag("{COUNTRY}", pl -> geoIpService.getCountryName(PlayerUtils.getPlayerIp(pl))));
createTag("&", () -> "\u00a7"),
createTag("{PLAYER}", pl -> pl.getName()),
createTag("{ONLINE}", () -> Integer.toString(bukkitService.getOnlinePlayers().size())),
createTag("{MAXPLAYERS}", () -> Integer.toString(server.getMaxPlayers())),
createTag("{IP}", pl -> PlayerUtils.getPlayerIp(pl)),
createTag("{LOGINS}", () -> Integer.toString(playerCache.getLogged())),
createTag("{WORLD}", pl -> pl.getWorld().getName()),
createTag("{SERVER}", () -> server.getServerName()),
createTag("{VERSION}", () -> server.getBukkitVersion()),
createTag("{COUNTRY}", pl -> geoIpService.getCountryName(PlayerUtils.getPlayerIp(pl))));
/** Welcome message, by lines. */
private List<String> welcomeMessage;
@@ -0,0 +1,35 @@
package fr.xephi.authme.util.lazytags;
import org.bukkit.entity.Player;
import java.util.function.Function;
/**
* Replaceable tag whose value depends on the player.
*/
public class PlayerTag implements Tag {
private final String name;
private final Function<Player, String> replacementFunction;
/**
* Constructor.
*
* @param name the tag (placeholder) that will be replaced
* @param replacementFunction the function producing the replacement
*/
public PlayerTag(String name, Function<Player, String> replacementFunction) {
this.name = name;
this.replacementFunction = replacementFunction;
}
@Override
public String getName() {
return name;
}
@Override
public String getValue(Player player) {
return replacementFunction.apply(player);
}
}
@@ -0,0 +1,29 @@
package fr.xephi.authme.util.lazytags;
import org.bukkit.entity.Player;
import java.util.function.Supplier;
/**
* Tag to be replaced that does not depend on the player.
*/
public class SimpleTag implements Tag {
private final String name;
private final Supplier<String> replacementFunction;
public SimpleTag(String name, Supplier<String> replacementFunction) {
this.name = name;
this.replacementFunction = replacementFunction;
}
@Override
public String getName() {
return name;
}
@Override
public String getValue(Player player) {
return replacementFunction.get();
}
}
@@ -2,44 +2,15 @@ package fr.xephi.authme.util.lazytags;
import org.bukkit.entity.Player;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Represents a tag in a text that can be replaced with data (which may depend on the Player).
* Represents a tag in a text to be replaced with a value (which may depend on the Player).
*/
public class Tag {
private final String name;
private final Function<Player, String> replacementFunction;
public interface Tag {
/**
* Constructor.
*
* @param name the tag (placeholder) that will be replaced
* @param replacementFunction the function producing the replacement
* @return the tag to replace
*/
public Tag(String name, Function<Player, String> replacementFunction) {
this.name = name;
this.replacementFunction = replacementFunction;
}
/**
* Constructor.
*
* @param name the tag (placeholder) that will be replaced
* @param replacementFunction supplier providing the text to replace the tag with
*/
public Tag(String name, Supplier<String> replacementFunction) {
this(name, p -> replacementFunction.get());
}
/**
* @return the tag
*/
public String getName() {
return name;
}
String getName();
/**
* Returns the value to replace the tag with for the given player.
@@ -47,7 +18,5 @@ public class Tag {
* @param player the player to evaluate the replacement for
* @return the replacement
*/
public String getValue(Player player) {
return replacementFunction.apply(player);
}
String getValue(Player player);
}
@@ -0,0 +1,23 @@
package fr.xephi.authme.util.lazytags;
import org.bukkit.entity.Player;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Utility class for creating tags.
*/
public final class TagBuilder {
private TagBuilder() {
}
public static Tag createTag(String name, Function<Player, String> replacementFunction) {
return new PlayerTag(name, replacementFunction);
}
public static Tag createTag(String name, Supplier<String> replacementFunction) {
return new SimpleTag(name, replacementFunction);
}
}