mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 14:38:51 +00:00
Format event message
This commit is contained in:
1 parent
610f75d2cc
commit
9abdc0df4c
6 files changed
+46
-27
No files matched your search
@@ -110,9 +110,9 @@ public abstract class ChatChannel implements ForwardingAudience {
|
|||||||
* @param user The player who is saying the message.
|
* @param user The player who is saying the message.
|
||||||
* @param message The message to be sent.
|
* @param message The message to be sent.
|
||||||
*/
|
*/
|
||||||
public abstract void sendMessage(ChatUser user, String message, boolean fromBungee);
|
public abstract Component sendMessage(ChatUser user, String message, boolean fromBungee);
|
||||||
|
|
||||||
public abstract void sendMessage(ChatUser user, Collection<ChatUser> recipients, String message, boolean fromBungee);
|
public abstract Component sendMessage(ChatUser user, Collection<ChatUser> recipients, String message, boolean fromBungee);
|
||||||
|
|
||||||
public abstract void sendComponent(ChatUser user, Component component);
|
public abstract void sendComponent(ChatUser user, Component component);
|
||||||
|
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ public class CarbonChatChannel extends ChatChannel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendMessage(ChatUser user, Collection<ChatUser> recipients, String message, boolean fromRemote) {
|
public Component sendMessage(ChatUser user, Collection<ChatUser> recipients, String message, boolean fromRemote) {
|
||||||
updateUserNickname(user);
|
updateUserNickname(user);
|
||||||
|
|
||||||
// Get player's formatting
|
// Get player's formatting
|
||||||
@@ -99,12 +99,8 @@ public class CarbonChatChannel extends ChatChannel {
|
|||||||
|
|
||||||
// Return if cancelled or message is emptied
|
// Return if cancelled or message is emptied
|
||||||
|
|
||||||
if (preFormatEvent.isCancelled()) {
|
if (preFormatEvent.isCancelled() || preFormatEvent.getMessage().trim().isEmpty()) {
|
||||||
return;
|
return TextComponent.empty();
|
||||||
}
|
|
||||||
|
|
||||||
if (preFormatEvent.getMessage().trim().isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String displayName;
|
String displayName;
|
||||||
@@ -122,14 +118,10 @@ public class CarbonChatChannel extends ChatChannel {
|
|||||||
Bukkit.getPluginManager().callEvent(formatEvent);
|
Bukkit.getPluginManager().callEvent(formatEvent);
|
||||||
|
|
||||||
// Again, return if cancelled or message is emptied
|
// Again, return if cancelled or message is emptied
|
||||||
if (formatEvent.isCancelled()) {
|
if (formatEvent.isCancelled() || formatEvent.getMessage().trim().isEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (formatEvent.getMessage().trim().isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
TextComponent formattedMessage = (TextComponent) carbonChat.getAdventureManager().processMessage(formatEvent.getFormat(),
|
TextComponent formattedMessage = (TextComponent) carbonChat.getAdventureManager().processMessage(formatEvent.getFormat(),
|
||||||
"br", "\n",
|
"br", "\n",
|
||||||
"displayname", displayName,
|
"displayname", displayName,
|
||||||
@@ -171,20 +163,19 @@ public class CarbonChatChannel extends ChatChannel {
|
|||||||
|
|
||||||
Bukkit.getPluginManager().callEvent(consoleEvent);
|
Bukkit.getPluginManager().callEvent(consoleEvent);
|
||||||
|
|
||||||
// Log message to console
|
|
||||||
System.out.println(CarbonChat.LEGACY.serialize(consoleEvent.getComponent()));
|
|
||||||
|
|
||||||
// Route message to bungee / discord (if message originates from this server)
|
// Route message to bungee / discord (if message originates from this server)
|
||||||
// Use instanceof and not isOnline, if this message originates from another then the instanceof will
|
// Use instanceof and not isOnline, if this message originates from another then the instanceof will
|
||||||
// fail, but isOnline may succeed if the player is online on both servers (somehow).
|
// fail, but isOnline may succeed if the player is online on both servers (somehow).
|
||||||
if (user.isOnline() && !fromRemote && shouldBungee()) {
|
if (user.isOnline() && !fromRemote && shouldBungee()) {
|
||||||
sendMessageToBungee(user.asPlayer(), consoleEvent.getComponent());
|
sendMessageToBungee(user.asPlayer(), consoleEvent.getComponent());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return consoleEvent.getComponent();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMessage(ChatUser user, String message, boolean fromRemote) {
|
public Component sendMessage(ChatUser user, String message, boolean fromRemote) {
|
||||||
this.sendMessage(user, this.audiences(), message, fromRemote);
|
return this.sendMessage(user, this.audiences(), message, fromRemote);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFormat(ChatUser user) {
|
public String getFormat(ChatUser user) {
|
||||||
@@ -285,8 +276,6 @@ public class CarbonChatChannel extends ChatChannel {
|
|||||||
user.sendMessage(component);
|
user.sendMessage(component);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println(CarbonChat.LEGACY.serialize(component));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendMessageToBungee(Player player, Component component) {
|
public void sendMessageToBungee(Player player, Component component) {
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import co.aikar.commands.annotation.*;
|
|||||||
import net.draycia.carbon.CarbonChat;
|
import net.draycia.carbon.CarbonChat;
|
||||||
import net.draycia.carbon.channels.ChatChannel;
|
import net.draycia.carbon.channels.ChatChannel;
|
||||||
import net.draycia.carbon.storage.ChatUser;
|
import net.draycia.carbon.storage.ChatUser;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
@@ -39,7 +41,10 @@ public class AliasedChannelCommand extends BaseCommand {
|
|||||||
"channel", getChatChannel().getName()));
|
"channel", getChatChannel().getName()));
|
||||||
} else {
|
} else {
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
||||||
getChatChannel().sendMessage(user, String.join(" ", args), false);
|
Component component = getChatChannel().sendMessage(user, String.join(" ", args), false);
|
||||||
|
|
||||||
|
carbonChat.getLogger().info(LegacyComponentSerializer.legacySection().serialize(component)
|
||||||
|
.replaceAll("(?:[^%]|\\A)%(?:[^%]|\\z)", "%%"));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import co.aikar.commands.annotation.*;
|
|||||||
import net.draycia.carbon.CarbonChat;
|
import net.draycia.carbon.CarbonChat;
|
||||||
import net.draycia.carbon.channels.ChatChannel;
|
import net.draycia.carbon.channels.ChatChannel;
|
||||||
import net.draycia.carbon.storage.ChatUser;
|
import net.draycia.carbon.storage.ChatUser;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@@ -31,7 +33,10 @@ public class ChannelCommand extends BaseCommand {
|
|||||||
"channel", channel.getName()));
|
"channel", channel.getName()));
|
||||||
} else {
|
} else {
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
||||||
channel.sendMessage(user, String.join(" ", args), false);
|
Component component = channel.sendMessage(user, String.join(" ", args), false);
|
||||||
|
|
||||||
|
carbonChat.getLogger().info(LegacyComponentSerializer.legacySection().serialize(component)
|
||||||
|
.replaceAll("(?:[^%]|\\A)%(?:[^%]|\\z)", "%%"));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package net.draycia.carbon.listeners;
|
|||||||
import net.draycia.carbon.CarbonChat;
|
import net.draycia.carbon.CarbonChat;
|
||||||
import net.draycia.carbon.channels.ChatChannel;
|
import net.draycia.carbon.channels.ChatChannel;
|
||||||
import net.draycia.carbon.storage.ChatUser;
|
import net.draycia.carbon.storage.ChatUser;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
@@ -13,7 +15,6 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class BukkitChatListener implements Listener {
|
public class BukkitChatListener implements Listener {
|
||||||
|
|
||||||
@@ -68,10 +69,22 @@ public class BukkitChatListener implements Listener {
|
|||||||
event.getRecipients().clear();
|
event.getRecipients().clear();
|
||||||
|
|
||||||
if (event.isAsynchronous()) {
|
if (event.isAsynchronous()) {
|
||||||
selectedChannel.sendMessage(user, recipients, event.getMessage(), false);
|
Component component = selectedChannel.sendMessage(user, recipients, event.getMessage(), false);
|
||||||
|
|
||||||
|
event.setFormat(LegacyComponentSerializer.legacySection().serialize(component)
|
||||||
|
.replaceAll("(?:[^%]|\\A)%(?:[^%]|\\z)", "%%"));
|
||||||
} else {
|
} else {
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
||||||
selectedChannel.sendMessage(user, recipients, event.getMessage(), false);
|
Component component = selectedChannel.sendMessage(user, recipients, event.getMessage(), false);
|
||||||
|
|
||||||
|
carbonChat.getLogger().info(LegacyComponentSerializer.legacySection().serialize(component)
|
||||||
|
.replaceAll("(?:[^%]|\\A)%(?:[^%]|\\z)", "%%"));
|
||||||
|
|
||||||
|
if (carbonChat.getConfig().getBoolean("show-tips")) {
|
||||||
|
carbonChat.getLogger().info("Tip: Sync chat event! I cannot set the message format due to this. :(");
|
||||||
|
carbonChat.getLogger().info("Tip: To 'solve' this, do a binary search and see which plugin is triggering");
|
||||||
|
carbonChat.getLogger().info("Tip: sync chat events and causing this, and let that plugin author know.");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,10 @@ import net.draycia.carbon.messaging.impl.BungeeMessageService;
|
|||||||
import net.draycia.carbon.messaging.impl.EmptyMessageService;
|
import net.draycia.carbon.messaging.impl.EmptyMessageService;
|
||||||
import net.draycia.carbon.messaging.impl.RedisMessageService;
|
import net.draycia.carbon.messaging.impl.RedisMessageService;
|
||||||
import net.draycia.carbon.storage.ChatUser;
|
import net.draycia.carbon.storage.ChatUser;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.format.TextColor;
|
import net.kyori.adventure.text.format.TextColor;
|
||||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||||
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
@@ -124,7 +126,12 @@ public class MessageManager {
|
|||||||
ChatUser user = carbonChat.getUserService().wrap(uuid);
|
ChatUser user = carbonChat.getUserService().wrap(uuid);
|
||||||
|
|
||||||
if (channel != null) {
|
if (channel != null) {
|
||||||
channel.sendComponent(user, gsonSerializer.deserialize(byteArray.readUTF()));
|
Component component = gsonSerializer.deserialize(byteArray.readUTF());
|
||||||
|
|
||||||
|
channel.sendComponent(user, component);
|
||||||
|
|
||||||
|
carbonChat.getLogger().info(LegacyComponentSerializer.legacySection().serialize(component)
|
||||||
|
.replaceAll("(?:[^%]|\\A)%(?:[^%]|\\z)", "%%"));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user