* #1026 Add more tags for forced commands (lazily replaced) - Extract lazy replacement of tags to its own class - Implement wrapper to replace a String property within an object - Use wrapper in command manager and add new tags * Make argument type generic in lazy tags util
This commit is contained in:
@@ -8,6 +8,7 @@ import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.service.GeoIpService;
|
||||
import fr.xephi.authme.util.PlayerUtils;
|
||||
import fr.xephi.authme.util.lazytags.Tag;
|
||||
import fr.xephi.authme.util.lazytags.TagReplacer;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@@ -19,9 +20,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
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;
|
||||
@@ -48,7 +47,7 @@ public class WelcomeMessageConfiguration implements Reloadable {
|
||||
private PlayerCache playerCache;
|
||||
|
||||
/** List of all supported tags for the welcome message. */
|
||||
private final List<Tag> availableTags = Arrays.asList(
|
||||
private final List<Tag<Player>> availableTags = Arrays.asList(
|
||||
createTag("&", () -> "\u00a7"),
|
||||
createTag("{PLAYER}", pl -> pl.getName()),
|
||||
createTag("{ONLINE}", () -> Integer.toString(bukkitService.getOnlinePlayers().size())),
|
||||
@@ -60,16 +59,13 @@ public class WelcomeMessageConfiguration implements Reloadable {
|
||||
createTag("{VERSION}", () -> server.getBukkitVersion()),
|
||||
createTag("{COUNTRY}", pl -> geoIpService.getCountryName(PlayerUtils.getPlayerIp(pl))));
|
||||
|
||||
/** Welcome message, by lines. */
|
||||
private List<String> welcomeMessage;
|
||||
/** Tags used in the welcome message. */
|
||||
private List<Tag> usedTags;
|
||||
private TagReplacer<Player> messageSupplier;
|
||||
|
||||
@PostConstruct
|
||||
@Override
|
||||
public void reload() {
|
||||
welcomeMessage = readWelcomeFile();
|
||||
usedTags = determineUsedTags(welcomeMessage);
|
||||
List<String> welcomeMessage = readWelcomeFile();
|
||||
messageSupplier = TagReplacer.newReplacer(availableTags, welcomeMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,22 +75,7 @@ public class WelcomeMessageConfiguration implements Reloadable {
|
||||
* @return the welcome message
|
||||
*/
|
||||
public List<String> getWelcomeMessage(Player player) {
|
||||
// Note ljacqu 20170121: Using a Map might seem more natural here but we avoid doing so for performance
|
||||
// Although the performance gain here is probably minimal...
|
||||
List<TagValue> tagValues = new LinkedList<>();
|
||||
for (Tag tag : usedTags) {
|
||||
tagValues.add(new TagValue(tag.getName(), tag.getValue(player)));
|
||||
}
|
||||
|
||||
List<String> adaptedMessages = new LinkedList<>();
|
||||
for (String line : welcomeMessage) {
|
||||
String adaptedLine = line;
|
||||
for (TagValue tagValue : tagValues) {
|
||||
adaptedLine = adaptedLine.replace(tagValue.tag, tagValue.value);
|
||||
}
|
||||
adaptedMessages.add(adaptedLine);
|
||||
}
|
||||
return adaptedMessages;
|
||||
return messageSupplier.getAdaptedMessages(player);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,32 +94,4 @@ public class WelcomeMessageConfiguration implements Reloadable {
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines which tags are used in the message.
|
||||
*
|
||||
* @param welcomeMessage the lines of the welcome message
|
||||
* @return the tags
|
||||
*/
|
||||
private List<Tag> determineUsedTags(List<String> welcomeMessage) {
|
||||
return availableTags.stream()
|
||||
.filter(tag -> welcomeMessage.stream().anyMatch(msg -> msg.contains(tag.getName())))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static final class TagValue {
|
||||
|
||||
private final String tag;
|
||||
private final String value;
|
||||
|
||||
TagValue(String tag, String value) {
|
||||
this.tag = tag;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TagValue[tag='" + tag + "', value='" + value + "']";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,21 @@ import ch.jalu.configme.resource.YamlFileResource;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.initialization.Reloadable;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.service.GeoIpService;
|
||||
import fr.xephi.authme.util.FileUtils;
|
||||
import fr.xephi.authme.util.PlayerUtils;
|
||||
import fr.xephi.authme.util.lazytags.Tag;
|
||||
import fr.xephi.authme.util.lazytags.WrappedTagReplacer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static fr.xephi.authme.util.lazytags.TagBuilder.createTag;
|
||||
|
||||
/**
|
||||
* Manages configurable commands to be run when various events occur.
|
||||
*/
|
||||
@@ -19,15 +27,20 @@ public class CommandManager implements Reloadable {
|
||||
|
||||
private final File dataFolder;
|
||||
private final BukkitService bukkitService;
|
||||
private final GeoIpService geoIpService;
|
||||
private final CommandMigrationService commandMigrationService;
|
||||
private final List<Tag<Player>> availableTags = buildAvailableTags();
|
||||
|
||||
private CommandConfig commandConfig;
|
||||
private WrappedTagReplacer<Command, Player> onJoinCommands;
|
||||
private WrappedTagReplacer<Command, Player> onLoginCommands;
|
||||
private WrappedTagReplacer<Command, Player> onRegisterCommands;
|
||||
|
||||
@Inject
|
||||
CommandManager(@DataFolder File dataFolder, BukkitService bukkitService,
|
||||
CommandManager(@DataFolder File dataFolder, BukkitService bukkitService, GeoIpService geoIpService,
|
||||
CommandMigrationService commandMigrationService) {
|
||||
this.dataFolder = dataFolder;
|
||||
this.bukkitService = bukkitService;
|
||||
this.geoIpService = geoIpService;
|
||||
this.commandMigrationService = commandMigrationService;
|
||||
reload();
|
||||
}
|
||||
@@ -38,7 +51,7 @@ public class CommandManager implements Reloadable {
|
||||
* @param player the joining player
|
||||
*/
|
||||
public void runCommandsOnJoin(Player player) {
|
||||
executeCommands(player, commandConfig.getOnJoin());
|
||||
executeCommands(player, onJoinCommands.getAdaptedItems(player));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,7 +60,7 @@ public class CommandManager implements Reloadable {
|
||||
* @param player the player who has registered
|
||||
*/
|
||||
public void runCommandsOnRegister(Player player) {
|
||||
executeCommands(player, commandConfig.getOnRegister());
|
||||
executeCommands(player, onRegisterCommands.getAdaptedItems(player));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,11 +69,11 @@ public class CommandManager implements Reloadable {
|
||||
* @param player the player that logged in
|
||||
*/
|
||||
public void runCommandsOnLogin(Player player) {
|
||||
executeCommands(player, commandConfig.getOnLogin());
|
||||
executeCommands(player, onLoginCommands.getAdaptedItems(player));
|
||||
}
|
||||
|
||||
private void executeCommands(Player player, Map<String, Command> commands) {
|
||||
for (Command command : commands.values()) {
|
||||
private void executeCommands(Player player, List<Command> commands) {
|
||||
for (Command command : commands) {
|
||||
final String execution = command.getCommand().replace("%p", player.getName());
|
||||
if (Executor.CONSOLE.equals(command.getExecutor())) {
|
||||
bukkitService.dispatchConsoleCommand(execution);
|
||||
@@ -77,8 +90,22 @@ public class CommandManager implements Reloadable {
|
||||
|
||||
SettingsManager settingsManager = new SettingsManager(
|
||||
new YamlFileResource(file), commandMigrationService, CommandSettingsHolder.class);
|
||||
commandConfig = settingsManager.getProperty(CommandSettingsHolder.COMMANDS);
|
||||
CommandConfig commandConfig = settingsManager.getProperty(CommandSettingsHolder.COMMANDS);
|
||||
onJoinCommands = newReplacer(commandConfig.getOnJoin());
|
||||
onLoginCommands = newReplacer(commandConfig.getOnLogin());
|
||||
onRegisterCommands = newReplacer(commandConfig.getOnRegister());
|
||||
}
|
||||
|
||||
private WrappedTagReplacer<Command, Player> newReplacer(Map<String, Command> commands) {
|
||||
return new WrappedTagReplacer<>(availableTags, commands.values(), Command::getCommand,
|
||||
(cmd, text) -> new Command(text, cmd.getExecutor()));
|
||||
}
|
||||
|
||||
private List<Tag<Player>> buildAvailableTags() {
|
||||
return Arrays.asList(
|
||||
createTag("%p", pl -> pl.getName()),
|
||||
createTag("%nick", pl -> pl.getDisplayName()),
|
||||
createTag("%ip", pl -> PlayerUtils.getPlayerIp(pl)),
|
||||
createTag("%country", pl -> geoIpService.getCountryName(PlayerUtils.getPlayerIp(pl))));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user