diff --git a/src/main/java/space/devport/wertik/custommessages/MessageExpansion.java b/src/main/java/space/devport/wertik/custommessages/MessageExpansion.java index a7fd72c..e1b41f2 100644 --- a/src/main/java/space/devport/wertik/custommessages/MessageExpansion.java +++ b/src/main/java/space/devport/wertik/custommessages/MessageExpansion.java @@ -44,7 +44,7 @@ public String onPlaceholderRequest(Player player, @NotNull String params) { return user.getMessage(type); if (args[1].equalsIgnoreCase("formatted")) { - String msg = plugin.getMessageManager().getFormattedMessage(player, type); + String msg = plugin.getMessageManager().getLoadedFormattedMessage(player, type); return msg != null ? msg : "none"; } diff --git a/src/main/java/space/devport/wertik/custommessages/commands/MessageCommand.java b/src/main/java/space/devport/wertik/custommessages/commands/MessageCommand.java index 397ea66..85969e7 100644 --- a/src/main/java/space/devport/wertik/custommessages/commands/MessageCommand.java +++ b/src/main/java/space/devport/wertik/custommessages/commands/MessageCommand.java @@ -51,7 +51,7 @@ public MessageCommand(MessagePlugin plugin) { header.append(lineFormat.clone() .replace("%type%", type.toString().toLowerCase()) .replace("%message%", user.getMessage(type)) - .replace("%preview%", type.parseDefaults(plugin.getMessageManager().getFormattedMessage(target, type), + .replace("%preview%", type.parseDefaults(plugin.getMessageManager().getLoadedFormattedMessage(target, type), plugin.getCommandParser().obtainDefaults(type)))); } diff --git a/src/main/java/space/devport/wertik/custommessages/commands/subcommands/PreviewSubCommand.java b/src/main/java/space/devport/wertik/custommessages/commands/subcommands/PreviewSubCommand.java index 6f3dcdd..9055175 100644 --- a/src/main/java/space/devport/wertik/custommessages/commands/subcommands/PreviewSubCommand.java +++ b/src/main/java/space/devport/wertik/custommessages/commands/subcommands/PreviewSubCommand.java @@ -41,12 +41,13 @@ public PreviewSubCommand(MessagePlugin plugin) { target = (Player) sender; } - String message = plugin.getMessageManager().getFormattedMessage(target, type); - language.getPrefixed("Commands.Preview.Done") - .replace("%player%", target.getName()) - .replace("%type%", type.toString().toLowerCase()) - .replace("%message%", message == null ? "&cNone" : type.parseDefaults(message, plugin.getCommandParser().obtainDefaults(type))) - .send(sender); + plugin.getMessageManager().getFormattedMessage(target, type).thenAcceptAsync(message -> { + language.getPrefixed("Commands.Preview.Done") + .replace("%player%", target.getName()) + .replace("%type%", type.toString().toLowerCase()) + .replace("%message%", message == null ? "&cNone" : type.parseDefaults(message, plugin.getCommandParser().obtainDefaults(type))) + .send(sender); + }); return CommandResult.SUCCESS; } diff --git a/src/main/java/space/devport/wertik/custommessages/listeners/ListenerRegistry.java b/src/main/java/space/devport/wertik/custommessages/listeners/ListenerRegistry.java index f58189f..32d1343 100644 --- a/src/main/java/space/devport/wertik/custommessages/listeners/ListenerRegistry.java +++ b/src/main/java/space/devport/wertik/custommessages/listeners/ListenerRegistry.java @@ -1,6 +1,7 @@ package space.devport.wertik.custommessages.listeners; import lombok.extern.java.Log; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -41,8 +42,8 @@ public void registerListeners() { registerListener(MessageType.JOIN, new Listener() { @EventHandler public void onJoin(PlayerJoinEvent event) { - event.setJoinMessage(handle(event.getPlayer(), MessageType.JOIN)); - plugin.getSoundRegistry().play(event.getPlayer(), SoundType.MESSAGE_JOIN); + event.setJoinMessage(null); + handle(event.getPlayer(), MessageType.JOIN, SoundType.MESSAGE_JOIN); } }); @@ -50,17 +51,13 @@ public void onJoin(PlayerJoinEvent event) { registerListener(MessageType.LEAVE, new Listener() { @EventHandler public void onLeave(PlayerQuitEvent event) { - event.setQuitMessage(handle(event.getPlayer(), MessageType.LEAVE, event.getPlayer().getWorld())); - plugin.getSoundRegistry().play(event.getPlayer(), SoundType.MESSAGE_LEAVE); + event.setQuitMessage(null); + handle(event.getPlayer(), MessageType.LEAVE, SoundType.MESSAGE_LEAVE, event.getPlayer().getWorld()); } @EventHandler public void onKick(PlayerKickEvent event) { - String msg = handle(event.getPlayer(), MessageType.LEAVE); - // Both kick and leave message are sent when a player is kicked. - // When we are sending a leave message, avoid the kick one. - if (msg != null) - event.setLeaveMessage(""); + event.setLeaveMessage(""); } }); @@ -74,7 +71,8 @@ public void onKill(PlayerDeathEvent event) { if (killer == null) return; - event.setDeathMessage(handle(killer, MessageType.KILL, player)); + event.setDeathMessage(null); + handle(killer, MessageType.KILL, SoundType.MESSAGE_KILL, player); plugin.getSoundRegistry().get(SoundType.MESSAGE_KILL).ifPresent(s -> { // Play the sound for both @@ -106,9 +104,10 @@ public void unregisterAll() { log.info(String.format("Unregistered %d listener(s)...", count)); } - @Nullable - private String handle(@NotNull Player player, @NotNull MessageType type, Object... extra) { - String message = messageManager.getFormattedMessage(player, type, extra); - return messageManager.getPosition().display(message); + private void handle(@NotNull Player player, @NotNull MessageType type, SoundType soundType, Object... extra) { + messageManager.getFormattedMessage(player, type, extra).thenAcceptAsync(message -> { + messageManager.getPosition().display(message); + plugin.getSoundRegistry().play(player, soundType); + }); } } \ No newline at end of file diff --git a/src/main/java/space/devport/wertik/custommessages/system/message/MessageManager.java b/src/main/java/space/devport/wertik/custommessages/system/message/MessageManager.java index 8765af2..541eabe 100644 --- a/src/main/java/space/devport/wertik/custommessages/system/message/MessageManager.java +++ b/src/main/java/space/devport/wertik/custommessages/system/message/MessageManager.java @@ -14,6 +14,7 @@ import space.devport.wertik.custommessages.system.user.User; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; @Log @@ -97,19 +98,26 @@ public String getFormattedMessage(@Nullable OfflinePlayer player, @NotNull Messa return MessageUtil.formatMessage(format, player); } - @Nullable - public String getFormattedMessage(OfflinePlayer player, MessageType type) { - return getFormattedMessage(player, type, new Object[0]); - } - - @Nullable - public String getFormattedMessage(OfflinePlayer player, MessageType type, Object... extra) { + public String getLoadedFormattedMessage(OfflinePlayer player, MessageType type, Object... extra) { User user = plugin.getUserManager().getUser(player); if (user == null) plugin.getUserManager().loadUser(player.getUniqueId()); return getFormattedMessage(player, type, user == null ? "default" : user.getMessage(type), extra); } + public String getLoadedFormattedMessage(OfflinePlayer player, MessageType type) { + return getLoadedFormattedMessage(player, type, new Object[0]); + } + + public CompletableFuture getFormattedMessage(OfflinePlayer player, MessageType type) { + return getFormattedMessage(player, type, new Object[0]); + } + + public CompletableFuture getFormattedMessage(OfflinePlayer player, MessageType type, Object... extra) { + return plugin.getUserManager().getOrLoadUser(player.getUniqueId()) + .thenApplyAsync(user -> getFormattedMessage(player, type, user == null ? "default" : user.getMessage(type), extra)); + } + public List getMessages(MessageType type) { MessageStorage storage = this.loadedMessages.get(type); return storage == null ? new ArrayList<>() : new ArrayList<>(storage.getMessages().keySet()); diff --git a/src/main/java/space/devport/wertik/custommessages/system/message/MessagePosition.java b/src/main/java/space/devport/wertik/custommessages/system/message/MessagePosition.java index dc0a8f8..de0658a 100644 --- a/src/main/java/space/devport/wertik/custommessages/system/message/MessagePosition.java +++ b/src/main/java/space/devport/wertik/custommessages/system/message/MessagePosition.java @@ -11,7 +11,9 @@ public enum MessagePosition { - TEXT, + TEXT(text -> { + Bukkit.getOnlinePlayers().forEach(p -> p.sendMessage(text)); + }), TITLE(text -> { String[] arr = text.split("\n"); @@ -28,7 +30,6 @@ public enum MessagePosition { title, subtitle); } - return null; }), ACTION_BAR(text -> { @@ -37,7 +38,6 @@ public enum MessagePosition { for (Player player : Bukkit.getOnlinePlayers()) { ActionBar.sendActionBar(plugin, player, colored, plugin.getConfig().getInt("action-bar.duration", 20)); } - return null; }); private final MessageDisplay handler; @@ -47,11 +47,12 @@ public enum MessagePosition { } MessagePosition() { - this.handler = text -> text; + this.handler = text -> { + }; } private interface MessageDisplay { - String display(String text); + void display(String text); } public static MessagePosition fromString(@Nullable String str, MessagePosition def) { @@ -65,8 +66,7 @@ public static MessagePosition fromString(@Nullable String str, MessagePosition d } } - @Nullable - public String display(String text) { - return handler.display(text); + public void display(String text) { + handler.display(text); } } \ No newline at end of file