diff --git a/src/main/java/fr/xephi/authme/settings/WelcomeMessageConfiguration.java b/src/main/java/fr/xephi/authme/settings/WelcomeMessageConfiguration.java index b2cc9812..8fcb2163 100644 --- a/src/main/java/fr/xephi/authme/settings/WelcomeMessageConfiguration.java +++ b/src/main/java/fr/xephi/authme/settings/WelcomeMessageConfiguration.java @@ -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 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 welcomeMessage; diff --git a/src/main/java/fr/xephi/authme/util/lazytags/PlayerTag.java b/src/main/java/fr/xephi/authme/util/lazytags/PlayerTag.java new file mode 100644 index 00000000..3c476237 --- /dev/null +++ b/src/main/java/fr/xephi/authme/util/lazytags/PlayerTag.java @@ -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 replacementFunction; + + /** + * Constructor. + * + * @param name the tag (placeholder) that will be replaced + * @param replacementFunction the function producing the replacement + */ + public PlayerTag(String name, Function replacementFunction) { + this.name = name; + this.replacementFunction = replacementFunction; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getValue(Player player) { + return replacementFunction.apply(player); + } +} diff --git a/src/main/java/fr/xephi/authme/util/lazytags/SimpleTag.java b/src/main/java/fr/xephi/authme/util/lazytags/SimpleTag.java new file mode 100644 index 00000000..08ece1c3 --- /dev/null +++ b/src/main/java/fr/xephi/authme/util/lazytags/SimpleTag.java @@ -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 replacementFunction; + + public SimpleTag(String name, Supplier replacementFunction) { + this.name = name; + this.replacementFunction = replacementFunction; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getValue(Player player) { + return replacementFunction.get(); + } +} diff --git a/src/main/java/fr/xephi/authme/util/lazytags/Tag.java b/src/main/java/fr/xephi/authme/util/lazytags/Tag.java index 4489f7a7..5129777d 100644 --- a/src/main/java/fr/xephi/authme/util/lazytags/Tag.java +++ b/src/main/java/fr/xephi/authme/util/lazytags/Tag.java @@ -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 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 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 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); } diff --git a/src/main/java/fr/xephi/authme/util/lazytags/TagBuilder.java b/src/main/java/fr/xephi/authme/util/lazytags/TagBuilder.java new file mode 100644 index 00000000..dc8eaef9 --- /dev/null +++ b/src/main/java/fr/xephi/authme/util/lazytags/TagBuilder.java @@ -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 replacementFunction) { + return new PlayerTag(name, replacementFunction); + } + + public static Tag createTag(String name, Supplier replacementFunction) { + return new SimpleTag(name, replacementFunction); + } +} diff --git a/src/test/java/fr/xephi/authme/settings/WelcomeMessageConfigurationTest.java b/src/test/java/fr/xephi/authme/settings/WelcomeMessageConfigurationTest.java index 010761a0..37a3386a 100644 --- a/src/test/java/fr/xephi/authme/settings/WelcomeMessageConfigurationTest.java +++ b/src/test/java/fr/xephi/authme/settings/WelcomeMessageConfigurationTest.java @@ -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 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 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(); + } }