Bukkit Audience

This commit is contained in:
HaHaWTH 2024-07-22 14:29:02 +08:00
parent e02659d850
commit 58f8176dae
5 changed files with 71 additions and 6 deletions

View File

@ -31,6 +31,7 @@ import fr.xephi.authme.listener.ServerListener;
import fr.xephi.authme.mail.EmailService; import fr.xephi.authme.mail.EmailService;
import fr.xephi.authme.output.ConsoleLoggerFactory; import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.security.crypts.Sha256; import fr.xephi.authme.security.crypts.Sha256;
import fr.xephi.authme.service.AdventureService;
import fr.xephi.authme.service.BackupService; import fr.xephi.authme.service.BackupService;
import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.MigrationService; import fr.xephi.authme.service.MigrationService;
@ -81,6 +82,7 @@ public class AuthMe extends JavaPlugin {
private EmailService emailService; private EmailService emailService;
private CommandHandler commandHandler; private CommandHandler commandHandler;
private static TaskScheduler scheduler; private static TaskScheduler scheduler;
private static AdventureService adventureService;
@Inject @Inject
public static Settings settings; public static Settings settings;
private DataSource database; private DataSource database;
@ -139,6 +141,13 @@ public class AuthMe extends JavaPlugin {
return scheduler; return scheduler;
} }
/**
* Get the AdventureService
*/
public static AdventureService getAdventureService() {
return adventureService;
}
/** /**
* The library manager * The library manager
*/ */
@ -153,6 +162,8 @@ public class AuthMe extends JavaPlugin {
loadPluginInfo(getDescription().getVersion()); loadPluginInfo(getDescription().getVersion());
scheduler = UniversalScheduler.getScheduler(this); scheduler = UniversalScheduler.getScheduler(this);
libraryManager = new BukkitLibraryManager(this); libraryManager = new BukkitLibraryManager(this);
adventureService = new AdventureService(this);
adventureService.init();
// Set the Logger instance and log file path // Set the Logger instance and log file path
ConsoleLogger.initialize(getLogger(), new File(getDataFolder(), LOG_FILENAME)); ConsoleLogger.initialize(getLogger(), new File(getDataFolder(), LOG_FILENAME));
@ -403,6 +414,11 @@ public class AuthMe extends JavaPlugin {
// Wait for tasks and close data source // Wait for tasks and close data source
new TaskCloser(database).run(); new TaskCloser(database).run();
// Close AdventureService
if (adventureService != null) {
adventureService.close();
}
// Disabled correctly // Disabled correctly
Consumer<String> infoLogMethod = logger == null ? getLogger()::info : logger::info; Consumer<String> infoLogMethod = logger == null ? getLogger()::info : logger::info;
infoLogMethod.accept("AuthMe " + this.getDescription().getVersion() + " is unloaded successfully!"); infoLogMethod.accept("AuthMe " + this.getDescription().getVersion() + " is unloaded successfully!");

View File

@ -21,7 +21,6 @@ import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.TeleportUtils; import fr.xephi.authme.util.TeleportUtils;
import fr.xephi.authme.util.message.I18NUtils; import fr.xephi.authme.util.message.I18NUtils;
import fr.xephi.authme.util.message.MiniMessageUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
@ -219,7 +218,7 @@ public class PlayerListener implements Listener {
String customJoinMessage = settings.getProperty(RegistrationSettings.CUSTOM_JOIN_MESSAGE); String customJoinMessage = settings.getProperty(RegistrationSettings.CUSTOM_JOIN_MESSAGE);
if (!customJoinMessage.isEmpty()) { if (!customJoinMessage.isEmpty()) {
customJoinMessage = ChatColor.translateAlternateColorCodes('&', MiniMessageUtils.parseMiniMessageToLegacy(customJoinMessage)); customJoinMessage = ChatColor.translateAlternateColorCodes('&', customJoinMessage);
event.setJoinMessage(customJoinMessage event.setJoinMessage(customJoinMessage
.replace("{PLAYERNAME}", player.getName()) .replace("{PLAYERNAME}", player.getName())
.replace("{DISPLAYNAME}", player.getDisplayName()) .replace("{DISPLAYNAME}", player.getDisplayName())

View File

@ -1,9 +1,11 @@
package fr.xephi.authme.message; package fr.xephi.authme.message;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.mail.EmailService; import fr.xephi.authme.mail.EmailService;
import fr.xephi.authme.output.ConsoleLoggerFactory; import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.service.AdventureService;
import fr.xephi.authme.util.expiring.Duration; import fr.xephi.authme.util.expiring.Duration;
import fr.xephi.authme.util.message.I18NUtils; import fr.xephi.authme.util.message.I18NUtils;
import fr.xephi.authme.util.message.MiniMessageUtils; import fr.xephi.authme.util.message.MiniMessageUtils;
@ -44,6 +46,7 @@ public class Messages {
private final ConsoleLogger logger = ConsoleLoggerFactory.get(EmailService.class); private final ConsoleLogger logger = ConsoleLoggerFactory.get(EmailService.class);
private MessagesFileHandler messagesFileHandler; private MessagesFileHandler messagesFileHandler;
private static final AdventureService adventureService = AuthMe.getAdventureService();
/* /*
* Constructor. * Constructor.
@ -62,7 +65,7 @@ public class Messages {
public void send(CommandSender sender, MessageKey key) { public void send(CommandSender sender, MessageKey key) {
String[] lines = retrieve(key, sender); String[] lines = retrieve(key, sender);
for (String line : lines) { for (String line : lines) {
sender.sendMessage(line); adventureService.send(sender, MiniMessageUtils.parseMiniMessage(line));
} }
} }
@ -78,7 +81,7 @@ public class Messages {
public void send(CommandSender sender, MessageKey key, String... replacements) { public void send(CommandSender sender, MessageKey key, String... replacements) {
String message = retrieveSingle(sender, key, replacements); String message = retrieveSingle(sender, key, replacements);
for (String line : message.split("\n")) { for (String line : message.split("\n")) {
sender.sendMessage(line); adventureService.send(sender, MiniMessageUtils.parseMiniMessage(line));
} }
} }
@ -131,7 +134,7 @@ public class Messages {
displayName = ((Player) sender).getDisplayName(); displayName = ((Player) sender).getDisplayName();
} }
return ChatColor.translateAlternateColorCodes('&', MiniMessageUtils.parseMiniMessageToLegacy(message)) return ChatColor.translateAlternateColorCodes('&', message)
.replace(NEWLINE_TAG, "\n") .replace(NEWLINE_TAG, "\n")
.replace(USERNAME_TAG, sender.getName()) .replace(USERNAME_TAG, sender.getName())
.replace(DISPLAYNAME_TAG, displayName); .replace(DISPLAYNAME_TAG, displayName);
@ -147,7 +150,7 @@ public class Messages {
private String retrieveMessage(MessageKey key, String name) { private String retrieveMessage(MessageKey key, String name) {
String message = messagesFileHandler.getMessage(key.getKey()); String message = messagesFileHandler.getMessage(key.getKey());
return ChatColor.translateAlternateColorCodes('&', MiniMessageUtils.parseMiniMessageToLegacy(message)) return ChatColor.translateAlternateColorCodes('&', message)
.replace(NEWLINE_TAG, "\n") .replace(NEWLINE_TAG, "\n")
.replace(USERNAME_TAG, name) .replace(USERNAME_TAG, name)
.replace(DISPLAYNAME_TAG, name); .replace(DISPLAYNAME_TAG, name);

View File

@ -0,0 +1,35 @@
package fr.xephi.authme.service;
import fr.xephi.authme.AuthMe;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class AdventureService {
private final AuthMe plugin;
public BukkitAudiences adventure;
public AdventureService(AuthMe plugin) {
this.plugin = plugin;
}
public void init() {
adventure = BukkitAudiences.create(plugin);
}
public void close() {
if (adventure != null) {
adventure.close();
adventure = null;
}
}
public void send(CommandSender sender, Component component) {
adventure.sender(sender).sendMessage(component);
}
public void send(Player player, Component component) {
adventure.player(player).sendMessage(component);
}
}

View File

@ -1,6 +1,7 @@
package fr.xephi.authme.util.message; package fr.xephi.authme.util.message;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
@ -17,6 +18,17 @@ public class MiniMessageUtils {
Component component = miniMessage.deserialize(message); Component component = miniMessage.deserialize(message);
return LegacyComponentSerializer.legacyAmpersand().serialize(component); return LegacyComponentSerializer.legacyAmpersand().serialize(component);
} }
/**
* Parse a MiniMessage string into a component.
*
* @param message The message to parse.
* @return The parsed message.
*/
public static Component parseMiniMessage(String message) {
TextComponent component = LegacyComponentSerializer.legacySection().deserialize(message);
return miniMessage.deserialize(miniMessage.serialize(component));
}
private MiniMessageUtils() { private MiniMessageUtils() {
} }
} }