#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

View File

@ -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;

View File

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

View File

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

View File

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

View File

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

View File

@ -9,6 +9,7 @@ import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.GeoIpService;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.junit.Rule;
import org.junit.Test;
@ -19,6 +20,7 @@ import org.mockito.Mock;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.Matchers.contains;
@ -66,9 +68,8 @@ public class WelcomeMessageConfigurationTest {
public void shouldLoadWelcomeMessage() throws IOException {
// given
String welcomeMessage = "This is my welcome message for testing\nBye!";
Files.write(welcomeFile.toPath(), welcomeMessage.getBytes());
setWelcomeMessageAndReload(welcomeMessage);
Player player = mock(Player.class);
welcomeMessageConfiguration.reload();
// when
List<String> result = welcomeMessageConfiguration.getWelcomeMessage(player);
@ -83,8 +84,7 @@ public class WelcomeMessageConfigurationTest {
public void shouldReplaceNameAndIpAndCountry() throws IOException {
// given
String welcomeMessage = "Hello {PLAYER}, your IP is {IP}\nYour country is {COUNTRY}.\nWelcome to {SERVER}!";
Files.write(welcomeFile.toPath(), welcomeMessage.getBytes());
welcomeMessageConfiguration.reload();
setWelcomeMessageAndReload(welcomeMessage);
Player player = mock(Player.class);
given(player.getName()).willReturn("Bobby");
@ -103,4 +103,39 @@ public class WelcomeMessageConfigurationTest {
verify(server, only()).getServerName();
verifyZeroInteractions(playerCache);
}
@Test
public void shouldApplyOtherReplacements() throws IOException {
// given
String welcomeMessage = "{ONLINE}/{MAXPLAYERS} online\n{LOGINS} logged in\nYour world is {WORLD}\nServer: {VERSION}";
setWelcomeMessageAndReload(welcomeMessage);
given(bukkitService.getOnlinePlayers()).willReturn((List) Arrays.asList(mock(Player.class), mock(Player.class)));
given(server.getMaxPlayers()).willReturn(20);
given(playerCache.getLogged()).willReturn(1);
given(server.getBukkitVersion()).willReturn("Bukkit-456.77.8");
World world = mock(World.class);
given(world.getName()).willReturn("Hub");
Player player = mock(Player.class);
given(player.getWorld()).willReturn(world);
// when
List<String> result = welcomeMessageConfiguration.getWelcomeMessage(player);
// then
assertThat(result, hasSize(4));
assertThat(result.get(0), equalTo("2/20 online"));
assertThat(result.get(1), equalTo("1 logged in"));
assertThat(result.get(2), equalTo("Your world is Hub"));
assertThat(result.get(3), equalTo("Server: Bukkit-456.77.8"));
}
private void setWelcomeMessageAndReload(String welcomeMessage) {
try {
Files.write(welcomeFile.toPath(), welcomeMessage.getBytes());
} catch (IOException e) {
throw new IllegalStateException("Could not write to '" + welcomeFile + "'", e);
}
welcomeMessageConfiguration.reload();
}
}