#1015 Distinguish player-dependent tags from "simple" tags
This commit is contained in:
parent
811bdee128
commit
367380265e
@ -24,6 +24,7 @@ import java.util.List;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static fr.xephi.authme.util.FileUtils.copyFileFromResource;
|
import static fr.xephi.authme.util.FileUtils.copyFileFromResource;
|
||||||
|
import static fr.xephi.authme.util.lazytags.TagBuilder.createTag;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration for the welcome message (welcome.txt).
|
* 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. */
|
/** List of all supported tags for the welcome message. */
|
||||||
private final List<Tag> availableTags = Arrays.asList(
|
private final List<Tag> availableTags = Arrays.asList(
|
||||||
new Tag("&", () -> "\u00a7"),
|
createTag("&", () -> "\u00a7"),
|
||||||
new Tag("{PLAYER}", pl -> pl.getName()),
|
createTag("{PLAYER}", pl -> pl.getName()),
|
||||||
new Tag("{ONLINE}", () -> Integer.toString(bukkitService.getOnlinePlayers().size())),
|
createTag("{ONLINE}", () -> Integer.toString(bukkitService.getOnlinePlayers().size())),
|
||||||
new Tag("{MAXPLAYERS}", () -> Integer.toString(server.getMaxPlayers())),
|
createTag("{MAXPLAYERS}", () -> Integer.toString(server.getMaxPlayers())),
|
||||||
new Tag("{IP}", pl -> PlayerUtils.getPlayerIp(pl)),
|
createTag("{IP}", pl -> PlayerUtils.getPlayerIp(pl)),
|
||||||
new Tag("{LOGINS}", () -> Integer.toString(playerCache.getLogged())),
|
createTag("{LOGINS}", () -> Integer.toString(playerCache.getLogged())),
|
||||||
new Tag("{WORLD}", pl -> pl.getWorld().getName()),
|
createTag("{WORLD}", pl -> pl.getWorld().getName()),
|
||||||
new Tag("{SERVER}", () -> server.getServerName()),
|
createTag("{SERVER}", () -> server.getServerName()),
|
||||||
new Tag("{VERSION}", () -> server.getBukkitVersion()),
|
createTag("{VERSION}", () -> server.getBukkitVersion()),
|
||||||
new Tag("{COUNTRY}", pl -> geoIpService.getCountryName(PlayerUtils.getPlayerIp(pl))));
|
createTag("{COUNTRY}", pl -> geoIpService.getCountryName(PlayerUtils.getPlayerIp(pl))));
|
||||||
|
|
||||||
/** Welcome message, by lines. */
|
/** Welcome message, by lines. */
|
||||||
private List<String> welcomeMessage;
|
private List<String> welcomeMessage;
|
||||||
|
|||||||
35
src/main/java/fr/xephi/authme/util/lazytags/PlayerTag.java
Normal file
35
src/main/java/fr/xephi/authme/util/lazytags/PlayerTag.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
29
src/main/java/fr/xephi/authme/util/lazytags/SimpleTag.java
Normal file
29
src/main/java/fr/xephi/authme/util/lazytags/SimpleTag.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,44 +2,15 @@ package fr.xephi.authme.util.lazytags;
|
|||||||
|
|
||||||
import org.bukkit.entity.Player;
|
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 {
|
public interface Tag {
|
||||||
|
|
||||||
private final String name;
|
|
||||||
private final Function<Player, String> replacementFunction;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* @return the tag to replace
|
||||||
*
|
|
||||||
* @param name the tag (placeholder) that will be replaced
|
|
||||||
* @param replacementFunction the function producing the replacement
|
|
||||||
*/
|
*/
|
||||||
public Tag(String name, Function<Player, String> replacementFunction) {
|
String getName();
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the value to replace the tag with for the given player.
|
* 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
|
* @param player the player to evaluate the replacement for
|
||||||
* @return the replacement
|
* @return the replacement
|
||||||
*/
|
*/
|
||||||
public String getValue(Player player) {
|
String getValue(Player player);
|
||||||
return replacementFunction.apply(player);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/main/java/fr/xephi/authme/util/lazytags/TagBuilder.java
Normal file
23
src/main/java/fr/xephi/authme/util/lazytags/TagBuilder.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,6 +9,7 @@ import fr.xephi.authme.initialization.DataFolder;
|
|||||||
import fr.xephi.authme.service.BukkitService;
|
import fr.xephi.authme.service.BukkitService;
|
||||||
import fr.xephi.authme.service.GeoIpService;
|
import fr.xephi.authme.service.GeoIpService;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
|
import org.bukkit.World;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -19,6 +20,7 @@ import org.mockito.Mock;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.contains;
|
import static org.hamcrest.Matchers.contains;
|
||||||
@ -66,9 +68,8 @@ public class WelcomeMessageConfigurationTest {
|
|||||||
public void shouldLoadWelcomeMessage() throws IOException {
|
public void shouldLoadWelcomeMessage() throws IOException {
|
||||||
// given
|
// given
|
||||||
String welcomeMessage = "This is my welcome message for testing\nBye!";
|
String welcomeMessage = "This is my welcome message for testing\nBye!";
|
||||||
Files.write(welcomeFile.toPath(), welcomeMessage.getBytes());
|
setWelcomeMessageAndReload(welcomeMessage);
|
||||||
Player player = mock(Player.class);
|
Player player = mock(Player.class);
|
||||||
welcomeMessageConfiguration.reload();
|
|
||||||
|
|
||||||
// when
|
// when
|
||||||
List<String> result = welcomeMessageConfiguration.getWelcomeMessage(player);
|
List<String> result = welcomeMessageConfiguration.getWelcomeMessage(player);
|
||||||
@ -83,8 +84,7 @@ public class WelcomeMessageConfigurationTest {
|
|||||||
public void shouldReplaceNameAndIpAndCountry() throws IOException {
|
public void shouldReplaceNameAndIpAndCountry() throws IOException {
|
||||||
// given
|
// given
|
||||||
String welcomeMessage = "Hello {PLAYER}, your IP is {IP}\nYour country is {COUNTRY}.\nWelcome to {SERVER}!";
|
String welcomeMessage = "Hello {PLAYER}, your IP is {IP}\nYour country is {COUNTRY}.\nWelcome to {SERVER}!";
|
||||||
Files.write(welcomeFile.toPath(), welcomeMessage.getBytes());
|
setWelcomeMessageAndReload(welcomeMessage);
|
||||||
welcomeMessageConfiguration.reload();
|
|
||||||
|
|
||||||
Player player = mock(Player.class);
|
Player player = mock(Player.class);
|
||||||
given(player.getName()).willReturn("Bobby");
|
given(player.getName()).willReturn("Bobby");
|
||||||
@ -103,4 +103,39 @@ public class WelcomeMessageConfigurationTest {
|
|||||||
verify(server, only()).getServerName();
|
verify(server, only()).getServerName();
|
||||||
verifyZeroInteractions(playerCache);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user