Skip to content

Commit

Permalink
Rather send message after load.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wertik committed Feb 15, 2021
1 parent 951afdb commit 833a922
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -41,26 +42,22 @@ 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);
}
});

if (messageManager.isEnabled(MessageType.LEAVE))
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("");
}
});

Expand All @@ -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
Expand Down Expand Up @@ -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);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String> getFormattedMessage(OfflinePlayer player, MessageType type) {
return getFormattedMessage(player, type, new Object[0]);
}

public CompletableFuture<String> 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<String> getMessages(MessageType type) {
MessageStorage storage = this.loadedMessages.get(type);
return storage == null ? new ArrayList<>() : new ArrayList<>(storage.getMessages().keySet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

public enum MessagePosition {

TEXT,
TEXT(text -> {
Bukkit.getOnlinePlayers().forEach(p -> p.sendMessage(text));
}),

TITLE(text -> {
String[] arr = text.split("\n");
Expand All @@ -28,7 +30,6 @@ public enum MessagePosition {
title,
subtitle);
}
return null;
}),

ACTION_BAR(text -> {
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
}
}

0 comments on commit 833a922

Please sign in to comment.