Merge branch 'global-replacements'

This commit is contained in:
ljacqu
2018-03-10 16:59:56 +01:00
48 changed files with 298 additions and 70 deletions
@@ -74,7 +74,7 @@ public class RegisterAdminCommand implements ExecutableCommand {
final Player player = bukkitService.getPlayerExact(playerName);
if (player != null) {
bukkitService.scheduleSyncTaskFromOptionallyAsyncTask(() ->
player.kickPlayer(commonService.retrieveSingleMessage(MessageKey.KICK_FOR_ADMIN_REGISTER)));
player.kickPlayer(commonService.retrieveSingleMessage(player, MessageKey.KICK_FOR_ADMIN_REGISTER)));
}
});
}
@@ -97,7 +97,7 @@ public class TempbanManager implements SettingsDependent, HasCleanup {
if (isEnabled) {
final String name = player.getName();
final String ip = PlayerUtils.getPlayerIp(player);
final String reason = messages.retrieveSingle(MessageKey.TEMPBAN_MAX_LOGINS);
final String reason = messages.retrieveSingle(player, MessageKey.TEMPBAN_MAX_LOGINS);
final Date expires = new Date();
long newTime = expires.getTime() + (length * MILLIS_PER_MINUTE);
@@ -49,9 +49,9 @@ class LimboPlayerTaskManager {
*/
void registerMessageTask(Player player, LimboPlayer limbo, boolean isRegistered) {
int interval = settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL);
MessageResult messageResult = getMessageKey(player.getName(), isRegistered);
MessageResult result = getMessageKey(player.getName(), isRegistered);
if (interval > 0) {
String[] joinMessage = messages.retrieveSingle(messageResult.messageKey, messageResult.args).split("\n");
String[] joinMessage = messages.retrieveSingle(player, result.messageKey, result.args).split("\n");
MessageTask messageTask = new MessageTask(player, joinMessage);
bukkitService.runTaskTimer(messageTask, 2 * TICKS_PER_SECOND, interval * TICKS_PER_SECOND);
limbo.setMessageTask(messageTask);
@@ -67,7 +67,7 @@ class LimboPlayerTaskManager {
void registerTimeoutTask(Player player, LimboPlayer limbo) {
final int timeout = settings.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
if (timeout > 0) {
String message = messages.retrieveSingle(MessageKey.LOGIN_TIMEOUT_ERROR);
String message = messages.retrieveSingle(player, MessageKey.LOGIN_TIMEOUT_ERROR);
BukkitTask task = bukkitService.runTaskLater(new TimeoutTask(player, message, playerCache), timeout);
limbo.setTimeoutTask(task);
}
@@ -123,7 +123,7 @@ public class OnJoinVerifier implements Reloadable {
return false;
} else if (!permissionsManager.hasPermission(player, PlayerStatePermission.IS_VIP)) {
// Server is full and player is NOT VIP; set kick message and proceed with kick
event.setKickMessage(messages.retrieveSingle(MessageKey.KICK_FULL_SERVER));
event.setKickMessage(messages.retrieveSingle(player, MessageKey.KICK_FULL_SERVER));
return true;
}
@@ -135,12 +135,12 @@ public class OnJoinVerifier implements Reloadable {
}
Player nonVipPlayer = generateKickPlayer(onlinePlayers);
if (nonVipPlayer != null) {
nonVipPlayer.kickPlayer(messages.retrieveSingle(MessageKey.KICK_FOR_VIP));
nonVipPlayer.kickPlayer(messages.retrieveSingle(player, MessageKey.KICK_FOR_VIP));
event.allow();
return false;
} else {
ConsoleLogger.info("VIP player " + player.getName() + " tried to join, but the server was full");
event.setKickMessage(messages.retrieveSingle(MessageKey.KICK_FULL_SERVER));
event.setKickMessage(messages.retrieveSingle(player, MessageKey.KICK_FULL_SERVER));
return true;
}
}
@@ -257,7 +257,7 @@ public class PlayerListener implements Listener {
try {
runOnJoinChecks(JoiningPlayer.fromName(name), event.getAddress().getHostAddress());
} catch (FailedVerificationException e) {
event.setKickMessage(m.retrieveSingle(e.getReason(), e.getArgs()));
event.setKickMessage(m.retrieveSingle(name, e.getReason(), e.getArgs()));
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
}
}
@@ -285,7 +285,7 @@ public class PlayerListener implements Listener {
try {
runOnJoinChecks(JoiningPlayer.fromPlayerObject(player), event.getAddress().getHostAddress());
} catch (FailedVerificationException e) {
event.setKickMessage(m.retrieveSingle(e.getReason(), e.getArgs()));
event.setKickMessage(m.retrieveSingle(player, e.getReason(), e.getArgs()));
event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
}
}
@@ -5,6 +5,7 @@ import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.util.expiring.Duration;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.Map;
@@ -18,7 +19,9 @@ public class Messages {
// Custom Authme tag replaced to new line
private static final String NEWLINE_TAG = "%nl%";
private static final String PLAYER_TAG = "%username%";
// Global tag replacements
private static final String USERNAME_TAG = "%username%";
private static final String DISPLAYNAME_TAG = "%displayname%";
/** Contains the keys of the singular messages for time units. */
private static final Map<TimeUnit, MessageKey> TIME_UNIT_SINGULARS = ImmutableMap.<TimeUnit, MessageKey>builder()
@@ -51,9 +54,9 @@ public class Messages {
* @param key The key of the message to send
*/
public void send(CommandSender sender, MessageKey key) {
String[] lines = retrieve(key);
String[] lines = retrieve(key, sender);
for (String line : lines) {
sender.sendMessage(line.replaceAll(PLAYER_TAG, sender.getName()));
sender.sendMessage(line);
}
}
@@ -67,7 +70,7 @@ public class Messages {
* @param replacements The replacements to apply for the tags
*/
public void send(CommandSender sender, MessageKey key, String... replacements) {
String message = retrieveSingle(key, replacements).replaceAll(PLAYER_TAG, sender.getName());
String message = retrieveSingle(sender, key, replacements);
for (String line : message.split("\n")) {
sender.sendMessage(line);
}
@@ -77,10 +80,11 @@ public class Messages {
* Retrieve the message from the text file and return it split by new line as an array.
*
* @param key The message key to retrieve
* @param sender The entity to send the message to
* @return The message split by new lines
*/
public String[] retrieve(MessageKey key) {
String message = retrieveMessage(key);
public String[] retrieve(MessageKey key, CommandSender sender) {
String message = retrieveMessage(key, sender);
if (message.isEmpty()) {
// Return empty array instead of array with 1 empty string as entry
return new String[0];
@@ -101,18 +105,43 @@ public class Messages {
? TIME_UNIT_SINGULARS.get(duration.getTimeUnit())
: TIME_UNIT_PLURALS.get(duration.getTimeUnit());
return value + " " + retrieveMessage(timeUnitKey);
return value + " " + retrieveMessage(timeUnitKey, "");
}
/**
* Retrieve the message from the text file.
*
* @param key The message key to retrieve
* @param sender The entity to send the message to
* @return The message from the file
*/
private String retrieveMessage(MessageKey key) {
return formatMessage(
messagesFileHandler.getMessage(key.getKey()));
private String retrieveMessage(MessageKey key, CommandSender sender) {
String message = messagesFileHandler.getMessage(key.getKey());
String displayName = sender.getName();
if (sender instanceof Player) {
displayName = ((Player) sender).getDisplayName();
}
return ChatColor.translateAlternateColorCodes('&', message)
.replace(NEWLINE_TAG, "\n")
.replace(USERNAME_TAG, sender.getName())
.replace(DISPLAYNAME_TAG, displayName);
}
/**
* Retrieve the message from the text file.
*
* @param key The message key to retrieve
* @param name The name of the entity to send the message to
* @return The message from the file
*/
private String retrieveMessage(MessageKey key, String name) {
String message = messagesFileHandler.getMessage(key.getKey());
return ChatColor.translateAlternateColorCodes('&', message)
.replace(NEWLINE_TAG, "\n")
.replace(USERNAME_TAG, name)
.replace(DISPLAYNAME_TAG, name);
}
/**
@@ -120,12 +149,13 @@ public class Messages {
* logs an error if the number of supplied replacements doesn't correspond to the number of tags
* the message key contains.
*
* @param sender The entity to send the message to
* @param key The key of the message to send
* @param replacements The replacements to apply for the tags
* @return The message from the file with replacements
*/
public String retrieveSingle(MessageKey key, String... replacements) {
String message = retrieveMessage(key);
public String retrieveSingle(CommandSender sender, MessageKey key, String... replacements) {
String message = retrieveMessage(key, sender);
String[] tags = key.getTags();
if (replacements.length == tags.length) {
for (int i = 0; i < tags.length; ++i) {
@@ -137,9 +167,26 @@ public class Messages {
return message;
}
private static String formatMessage(String message) {
return ChatColor.translateAlternateColorCodes('&', message)
.replace(NEWLINE_TAG, "\n");
/**
* Retrieve the given message code with the given tag replacements. Note that this method
* logs an error if the number of supplied replacements doesn't correspond to the number of tags
* the message key contains.
*
* @param name The name of the entity to send the message to
* @param key The key of the message to send
* @param replacements The replacements to apply for the tags
* @return The message from the file with replacements
*/
public String retrieveSingle(String name, MessageKey key, String... replacements) {
String message = retrieveMessage(key, name);
String[] tags = key.getTags();
if (replacements.length == tags.length) {
for (int i = 0; i < tags.length; ++i) {
message = message.replace(tags[i], replacements[i]);
}
} else {
ConsoleLogger.warning("Invalid number of replacements for message key '" + key + "'");
}
return message;
}
}
@@ -138,7 +138,7 @@ public class AsynchronousJoin implements AsynchronousProcess {
private void handlePlayerWithUnmetNameRestriction(Player player, String ip) {
bukkitService.scheduleSyncTaskFromOptionallyAsyncTask(() -> {
player.kickPlayer(service.retrieveSingleMessage(MessageKey.NOT_OWNER_ERROR));
player.kickPlayer(service.retrieveSingleMessage(player, MessageKey.NOT_OWNER_ERROR));
if (service.getProperty(RestrictionSettings.BAN_UNKNOWN_IP)) {
server.banIP(ip);
}
@@ -188,7 +188,7 @@ public class AsynchronousJoin implements AsynchronousProcess {
&& countOnlinePlayersByIp(ip) > service.getProperty(RestrictionSettings.MAX_JOIN_PER_IP)) {
bukkitService.scheduleSyncTaskFromOptionallyAsyncTask(
() -> player.kickPlayer(service.retrieveSingleMessage(MessageKey.SAME_IP_ONLINE)));
() -> player.kickPlayer(service.retrieveSingleMessage(player, MessageKey.SAME_IP_ONLINE)));
return false;
}
return true;
@@ -197,7 +197,7 @@ public class AsynchronousLogin implements AsynchronousProcess {
tempbanManager.tempbanPlayer(player);
} else if (service.getProperty(RestrictionSettings.KICK_ON_WRONG_PASSWORD)) {
bukkitService.scheduleSyncTaskFromOptionallyAsyncTask(
() -> player.kickPlayer(service.retrieveSingleMessage(MessageKey.WRONG_PASSWORD)));
() -> player.kickPlayer(service.retrieveSingleMessage(player, MessageKey.WRONG_PASSWORD)));
} else {
service.send(player, MessageKey.WRONG_PASSWORD);
@@ -58,7 +58,7 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
// Kick Player after Registration is enabled, kick the player
if (service.getProperty(RegistrationSettings.FORCE_KICK_AFTER_REGISTER)) {
player.kickPlayer(service.retrieveSingleMessage(MessageKey.REGISTER_SUCCESS));
player.kickPlayer(service.retrieveSingleMessage(player, MessageKey.REGISTER_SUCCESS));
return;
}
@@ -63,11 +63,12 @@ public class CommonService {
/**
* Retrieves a message in one piece.
*
* @param sender The entity to send the message to
* @param key the key of the message
* @return the message
*/
public String retrieveSingleMessage(MessageKey key) {
return messages.retrieveSingle(key);
public String retrieveSingleMessage(CommandSender sender, MessageKey key) {
return messages.retrieveSingle(sender, key);
}
/**