#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
@@ -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);
}
}