mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
Merge pull request #16 from Draycia/refactor/messaging
Abstract away message sending
This commit is contained in:
12 files changed
+481
-433
No files matched your search
@@ -3,6 +3,7 @@ package net.draycia.carbon;
|
||||
import net.draycia.carbon.channels.contexts.impl.DistanceContext;
|
||||
import net.draycia.carbon.listeners.*;
|
||||
import net.draycia.carbon.managers.*;
|
||||
import net.draycia.carbon.messaging.MessageManager;
|
||||
import net.draycia.carbon.storage.UserService;
|
||||
import net.draycia.carbon.storage.impl.JSONUserService;
|
||||
import net.draycia.carbon.storage.impl.MySQLUserService;
|
||||
@@ -16,14 +17,13 @@ public final class CarbonChat extends JavaPlugin {
|
||||
|
||||
private Permission permission;
|
||||
|
||||
private PluginMessageManager pluginMessageManager;
|
||||
private CommandManager commandManager;
|
||||
private ChannelManager channelManager;
|
||||
private RedisManager redisManager = null;
|
||||
private AdventureManager adventureManager;
|
||||
private ContextManager contextManager;
|
||||
|
||||
private UserService userService;
|
||||
private MessageManager messageManager;
|
||||
|
||||
private ItemStackUtils itemStackUtils;
|
||||
|
||||
@@ -48,15 +48,10 @@ public final class CarbonChat extends JavaPlugin {
|
||||
saveDefaultConfig();
|
||||
|
||||
// Initialize managers
|
||||
pluginMessageManager = new PluginMessageManager(this);
|
||||
commandManager = new CommandManager(this);
|
||||
channelManager = new ChannelManager(this);
|
||||
contextManager = new ContextManager();
|
||||
|
||||
if (getConfig().getBoolean("redis.enabled")) {
|
||||
getLogger().info("Enabling Redis support!");
|
||||
redisManager = new RedisManager(this);
|
||||
}
|
||||
messageManager = new MessageManager(this);
|
||||
|
||||
String storageType = getConfig().getString("storage.type");
|
||||
|
||||
@@ -114,12 +109,8 @@ public final class CarbonChat extends JavaPlugin {
|
||||
return channelManager;
|
||||
}
|
||||
|
||||
public PluginMessageManager getPluginMessageManager() {
|
||||
return pluginMessageManager;
|
||||
}
|
||||
|
||||
public RedisManager getRedisManager() {
|
||||
return redisManager;
|
||||
public MessageManager getMessageManager() {
|
||||
return messageManager;
|
||||
}
|
||||
|
||||
public UserService getUserService() {
|
||||
|
||||
@@ -153,11 +153,7 @@ public class CarbonChatChannel extends ChatChannel {
|
||||
// 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).
|
||||
if (user.isOnline() && !fromRemote && shouldBungee()) {
|
||||
if (shouldForwardFormatting()) {
|
||||
sendMessageToBungee(user.asPlayer(), consoleEvent.getComponent());
|
||||
} else {
|
||||
sendMessageToBungee(user.asPlayer(), message);
|
||||
}
|
||||
sendMessageToBungee(user.asPlayer(), consoleEvent.getComponent());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,11 +232,10 @@ public class CarbonChatChannel extends ChatChannel {
|
||||
}
|
||||
|
||||
public void sendMessageToBungee(Player player, Component component) {
|
||||
carbonChat.getPluginMessageManager().sendComponent(this, player, component);
|
||||
}
|
||||
|
||||
public void sendMessageToBungee(Player player, String message) {
|
||||
carbonChat.getPluginMessageManager().sendMessage(this, player, message);
|
||||
carbonChat.getMessageManager().sendMessage("whisper-component", player.getUniqueId(), (byteArray) -> {
|
||||
byteArray.writeUTF(this.getKey());
|
||||
byteArray.writeUTF(carbonChat.getAdventureManager().getAudiences().gsonSerializer().serialize(component));
|
||||
});
|
||||
}
|
||||
|
||||
public Boolean canPlayerSee(ChatUser target, boolean checkSpying) {
|
||||
|
||||
@@ -1,180 +0,0 @@
|
||||
package net.draycia.carbon.managers;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import io.github.leonardosnt.bungeechannelapi.BungeeChannelApi;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class PluginMessageManager implements PluginMessageListener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final BungeeChannelApi api;
|
||||
private final GsonComponentSerializer gsonSerializer;
|
||||
|
||||
public PluginMessageManager(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.gsonSerializer = carbonChat.getAdventureManager().getAudiences().gsonSerializer();
|
||||
|
||||
carbonChat.getServer().getMessenger().registerOutgoingPluginChannel(carbonChat, "BungeeCord");
|
||||
carbonChat.getServer().getMessenger().registerIncomingPluginChannel(carbonChat, "BungeeCord", this);
|
||||
|
||||
api = BungeeChannelApi.of(carbonChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!channel.equals("BungeeCord")) {
|
||||
return;
|
||||
}
|
||||
|
||||
ByteArrayDataInput input = ByteStreams.newDataInput(message);
|
||||
|
||||
String subchannel = input.readUTF();
|
||||
short length = input.readShort();
|
||||
byte[] msgbytes = new byte[length];
|
||||
input.readFully(msgbytes);
|
||||
|
||||
ByteArrayDataInput msg = ByteStreams.newDataInput(msgbytes);
|
||||
|
||||
switch (subchannel) {
|
||||
case "carbonchat:message":
|
||||
onMessageReceived(msg);
|
||||
break;
|
||||
case "carbonchat:component":
|
||||
onComponentReceived(msg);
|
||||
break;
|
||||
case "carbonchat:whisper":
|
||||
onWhisperReceived(msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void onMessageReceived(ByteArrayDataInput in) {
|
||||
String chatChannelName = in.readUTF();
|
||||
|
||||
ChatChannel chatChannel = carbonChat.getChannelManager().getRegistry().get(chatChannelName);
|
||||
|
||||
if (chatChannel == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
UUID playerUUID;
|
||||
|
||||
try {
|
||||
playerUUID = UUID.fromString(in.readUTF());
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrap(playerUUID);
|
||||
|
||||
String chatMessage = in.readUTF();
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
|
||||
chatChannel.sendMessage(user, chatMessage, true);
|
||||
});
|
||||
}
|
||||
|
||||
public void onComponentReceived(ByteArrayDataInput in) {
|
||||
String chatChannelName = in.readUTF();
|
||||
|
||||
ChatChannel chatChannel = carbonChat.getChannelManager().getRegistry().get(chatChannelName);
|
||||
|
||||
if (chatChannel == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
UUID playerUUID;
|
||||
|
||||
try {
|
||||
playerUUID = UUID.fromString(in.readUTF());
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrap(playerUUID);
|
||||
|
||||
String chatMessage = in.readUTF();
|
||||
|
||||
Bukkit.getScheduler().scheduleAsyncDelayedTask(carbonChat, () -> {
|
||||
chatChannel.sendComponent(user, gsonSerializer.deserialize(chatMessage));
|
||||
});
|
||||
}
|
||||
|
||||
private void onWhisperReceived(ByteArrayDataInput in) {
|
||||
UUID targetUUID;
|
||||
UUID senderUUID;
|
||||
|
||||
try {
|
||||
senderUUID = UUID.fromString(in.readUTF());
|
||||
targetUUID = UUID.fromString(in.readUTF());
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
String chatMessage = in.readUTF();
|
||||
|
||||
ChatUser target = carbonChat.getUserService().wrap(targetUUID);
|
||||
|
||||
if (!target.isIgnoringUser(senderUUID)) {
|
||||
target.setReplyTarget(senderUUID);
|
||||
target.sendMessage(gsonSerializer.deserialize(chatMessage));
|
||||
}
|
||||
}
|
||||
|
||||
public void sendMessage(ChatChannel chatChannel, Player player, String message) {
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeUTF(chatChannel.getKey());
|
||||
msg.writeUTF(player.getUniqueId().toString());
|
||||
msg.writeUTF(message);
|
||||
|
||||
api.forward("ALL", "carbonchat:message", msg.toByteArray());
|
||||
}
|
||||
|
||||
public void sendComponent(ChatChannel chatChannel, Player player, Component component) {
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeUTF(chatChannel.getKey());
|
||||
msg.writeUTF(player.getUniqueId().toString());
|
||||
msg.writeUTF(gsonSerializer.serialize(component));
|
||||
|
||||
api.forward("ALL", "carbonchat:component", msg.toByteArray());
|
||||
}
|
||||
|
||||
public void sendComponentToPlayer(ChatUser sender, ChatUser target, Component toComponent, Component fromComponent) {
|
||||
api.getPlayerList("ALL").whenComplete((result, error) -> {
|
||||
if (!result.contains(target.asOfflinePlayer().getName())) {
|
||||
return;
|
||||
}
|
||||
|
||||
sender.sendMessage(toComponent);
|
||||
|
||||
if (sender.isShadowMuted()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeUTF(sender.getUUID().toString());
|
||||
msg.writeUTF(target.getUUID().toString());
|
||||
msg.writeUTF(gsonSerializer.serialize(fromComponent));
|
||||
|
||||
api.forward("ALL", "carbonchat:whisper", msg.toByteArray());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,182 +0,0 @@
|
||||
package net.draycia.carbon.managers;
|
||||
|
||||
import io.lettuce.core.RedisClient;
|
||||
import io.lettuce.core.RedisURI;
|
||||
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
|
||||
import io.lettuce.core.pubsub.api.sync.RedisPubSubCommands;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.util.RedisListener;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class RedisManager {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
private final RedisClient client;
|
||||
|
||||
private final StatefulRedisPubSubConnection<String, String> subscribeConnection;
|
||||
private final RedisPubSubCommands<String, String> subscribeSync;
|
||||
|
||||
private final StatefulRedisPubSubConnection<String, String> publishConnection;
|
||||
private final RedisPubSubCommands<String, String> publishSync;
|
||||
|
||||
private final UUID serverUUID = UUID.randomUUID();
|
||||
|
||||
public RedisManager(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
String host = carbonChat.getConfig().getString("redis.host");
|
||||
String password = carbonChat.getConfig().getString("redis.password");
|
||||
int port = carbonChat.getConfig().getInt("redis.port");
|
||||
int database = carbonChat.getConfig().getInt("redis.database");
|
||||
|
||||
RedisURI.Builder builder = RedisURI.Builder.redis(host, port)
|
||||
.withDatabase(database);
|
||||
|
||||
if (password != null) {
|
||||
builder.withPassword(password.toCharArray());
|
||||
}
|
||||
|
||||
this.client = RedisClient.create(builder.build());
|
||||
|
||||
this.subscribeConnection = client.connectPubSub();
|
||||
this.subscribeSync = subscribeConnection.sync();
|
||||
|
||||
this.publishConnection = client.connectPubSub();
|
||||
this.publishSync = publishConnection.sync();
|
||||
|
||||
subscribeConnection.addListener((RedisListener)(channel, message) -> {
|
||||
String[] messagePieces = message.split(":");
|
||||
|
||||
UUID messageUUID = UUID.fromString(messagePieces[0]);
|
||||
|
||||
if (messageUUID.equals(serverUUID)) {
|
||||
return;
|
||||
}
|
||||
|
||||
UUID uuid = UUID.fromString(messagePieces[1]);
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrapIfLoaded(uuid);
|
||||
|
||||
if (user == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String value = messagePieces[2];
|
||||
|
||||
switch (channel.toLowerCase()) {
|
||||
case "nickname":
|
||||
handleNicknameChange(user, value);
|
||||
break;
|
||||
case "nickname-reset":
|
||||
handleNicknameResetChange(user, value);
|
||||
break;
|
||||
case "selected-channel":
|
||||
handleSelectedChannelChange(user, value);
|
||||
break;
|
||||
case "spying-whispers":
|
||||
handleSpyingWhispersChange(user, value);
|
||||
break;
|
||||
case "muted":
|
||||
handleMutedChange(user, value);
|
||||
break;
|
||||
case "shadow-muted":
|
||||
handleShadowMutedChange(user, value);
|
||||
break;
|
||||
case "reply-target":
|
||||
handleReplyTargetChange(user, value);
|
||||
break;
|
||||
case "ignoring-user":
|
||||
handleIgnoringUserChange(user, value, true);
|
||||
break;
|
||||
case "unignoring-user":
|
||||
handleIgnoringUserChange(user, value, false);
|
||||
break;
|
||||
case "ignoring-channel":
|
||||
handleIgnoringChannelChange(user, value, true);
|
||||
break;
|
||||
case "unignoring-channel":
|
||||
handleIgnoringChannelChange(user, value, false);
|
||||
break;
|
||||
case "spying-channel":
|
||||
handleSpyingChannelChange(user, value, true);
|
||||
break;
|
||||
case "unspying-channel":
|
||||
handleSpyingChannelChange(user, value, false);
|
||||
break;
|
||||
case "channel-color":
|
||||
handleChannelColorChange(user, value);
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
subscribeSync.subscribe("nickname", "selected-channel", "spying-whispers", "muted", "shadow-muted",
|
||||
"reply-target", "ignoring-user", "unignoring-user", "ignoring-channel", "unignoring-channel",
|
||||
"spying-channel", "unspying-channel", "channel-color");
|
||||
}
|
||||
|
||||
private void handleChannelColorChange(ChatUser user, String value) {
|
||||
user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(value)).setColor(TextColor.fromHexString(value), true);
|
||||
}
|
||||
|
||||
private void handleSpyingChannelChange(ChatUser user, String value, boolean b) {
|
||||
user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(value)).setSpying(b, true);
|
||||
}
|
||||
|
||||
private void handleIgnoringChannelChange(ChatUser user, String value, boolean b) {
|
||||
user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(value)).setIgnoring(b, true);
|
||||
}
|
||||
|
||||
private void handleIgnoringUserChange(ChatUser user, String value, boolean b) {
|
||||
user.setIgnoringUser(UUID.fromString(value), b, true);
|
||||
}
|
||||
|
||||
private void handleReplyTargetChange(ChatUser user, String value) {
|
||||
user.setReplyTarget(UUID.fromString(value), true);
|
||||
}
|
||||
|
||||
private void handleShadowMutedChange(ChatUser user, String value) {
|
||||
user.setShadowMuted(value.equalsIgnoreCase("true"), true);
|
||||
}
|
||||
|
||||
private void handleMutedChange(ChatUser user, String value) {
|
||||
user.setMuted(value.equalsIgnoreCase("true"), true);
|
||||
}
|
||||
|
||||
private void handleSpyingWhispersChange(ChatUser user, String value) {
|
||||
user.setSpyingWhispers(value.equalsIgnoreCase("true"), true);
|
||||
}
|
||||
|
||||
private void handleSelectedChannelChange(ChatUser user, String value) {
|
||||
user.setSelectedChannel(carbonChat.getChannelManager().getRegistry().get(value), true);
|
||||
}
|
||||
|
||||
private void handleNicknameResetChange(ChatUser user, String value) {
|
||||
user.setNickname(null, true);
|
||||
|
||||
if (user.isOnline()) {
|
||||
String message = carbonChat.getConfig().getString("language.nickname-reset");
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(user.asPlayer(),
|
||||
message, "nickname", ""));
|
||||
}
|
||||
}
|
||||
|
||||
private void handleNicknameChange(ChatUser user, String value) {
|
||||
user.setNickname(value, true);
|
||||
|
||||
if (user.isOnline()) {
|
||||
String message = carbonChat.getConfig().getString("language.nickname-set");
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(user.asPlayer(),
|
||||
message, "nickname", value));
|
||||
}
|
||||
}
|
||||
|
||||
public void publishChange(UUID uuid, String field, String value) {
|
||||
publishSync.publish(field, serverUUID + ":" + uuid.toString() + ":" + value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
package net.draycia.carbon.messaging;
|
||||
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.messaging.impl.BungeeMessageService;
|
||||
import net.draycia.carbon.messaging.impl.EmptyMessageService;
|
||||
import net.draycia.carbon.messaging.impl.RedisMessageService;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class MessageManager {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final MessageService messageService;
|
||||
|
||||
private final GsonComponentSerializer gsonSerializer;
|
||||
|
||||
public MessageManager(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.gsonSerializer = carbonChat.getAdventureManager().getAudiences().gsonSerializer();
|
||||
|
||||
String messageSystem = carbonChat.getConfig().getString("message-system", "none");
|
||||
|
||||
if (messageSystem == null) {
|
||||
messageSystem = "none";
|
||||
}
|
||||
|
||||
switch(messageSystem.toLowerCase()) {
|
||||
case "bungee":
|
||||
carbonChat.getLogger().info("Using Bungee Plugin Messaging for message forwarding!");
|
||||
messageService = new BungeeMessageService(carbonChat);
|
||||
break;
|
||||
case "redis":
|
||||
carbonChat.getLogger().info("Using Redis for message forwarding!");
|
||||
messageService = new RedisMessageService(carbonChat);
|
||||
break;
|
||||
case "none":
|
||||
messageService = new EmptyMessageService();
|
||||
break;
|
||||
default:
|
||||
carbonChat.getLogger().info("Invalid message service selected! Disabling syncing until next restart!");
|
||||
messageService = new EmptyMessageService();
|
||||
break;
|
||||
}
|
||||
|
||||
this.registerDefaultListeners();
|
||||
}
|
||||
|
||||
private void registerDefaultListeners() {
|
||||
getMessageService().registerMessageListener("nickname", (user, byteArray) -> {
|
||||
String nickname = byteArray.readUTF();
|
||||
|
||||
user.setNickname(nickname, true);
|
||||
|
||||
if (user.isOnline()) {
|
||||
String message = carbonChat.getConfig().getString("language.nickname-set");
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(user.asPlayer(),
|
||||
message, "nickname", nickname));
|
||||
}
|
||||
});
|
||||
|
||||
getMessageService().registerMessageListener("nickname-reset", (user, byteArray) -> {
|
||||
user.setNickname(null, true);
|
||||
|
||||
if (user.isOnline()) {
|
||||
String message = carbonChat.getConfig().getString("language.nickname-reset");
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(user.asPlayer(), message));
|
||||
}
|
||||
});
|
||||
|
||||
getMessageService().registerMessageListener("selected-channel", (user, byteArray) -> {
|
||||
user.setSelectedChannel(carbonChat.getChannelManager().getRegistry().get(byteArray.readUTF()), true);
|
||||
});
|
||||
|
||||
getMessageService().registerMessageListener("spying-whispers", (user, byteArray) -> {
|
||||
user.setSpyingWhispers(byteArray.readBoolean(), true);
|
||||
});
|
||||
|
||||
getMessageService().registerMessageListener("muted", (user, byteArray) -> {
|
||||
user.setMuted(byteArray.readBoolean(), true);
|
||||
});
|
||||
|
||||
getMessageService().registerMessageListener("shadow-muted", (user, byteArray) -> {
|
||||
user.setShadowMuted(byteArray.readBoolean(), true);
|
||||
});
|
||||
|
||||
getMessageService().registerMessageListener("reply-target", (user, byteArray) -> {
|
||||
user.setReplyTarget(new UUID(byteArray.readLong(), byteArray.readLong()), true);
|
||||
});
|
||||
|
||||
getMessageService().registerMessageListener("ignoring-user", (user, byteArray) -> {
|
||||
user.setIgnoringUser(new UUID(byteArray.readLong(), byteArray.readLong()), byteArray.readBoolean(), true);
|
||||
});
|
||||
|
||||
getMessageService().registerMessageListener("ignoring-channel", (user, byteArray) -> {
|
||||
user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(byteArray.readUTF()))
|
||||
.setIgnoring(byteArray.readBoolean(), true);
|
||||
});
|
||||
|
||||
getMessageService().registerMessageListener("spying-channel", (user, byteArray) -> {
|
||||
user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(byteArray.readUTF()))
|
||||
.setSpying(byteArray.readBoolean(), true);
|
||||
});
|
||||
|
||||
getMessageService().registerMessageListener("channel-color", (user, byteArray) -> {
|
||||
user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(byteArray.readUTF()))
|
||||
.setColor(TextColor.fromHexString(byteArray.readUTF()), true);
|
||||
});
|
||||
|
||||
getMessageService().registerMessageListener("channel-color-reset", (user, byteArray) -> {
|
||||
user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(byteArray.readUTF()))
|
||||
.setColor(null, true);
|
||||
});
|
||||
|
||||
getMessageService().registerMessageListener("channel-component", (user, byteArray) -> {
|
||||
ChatChannel channel = carbonChat.getChannelManager().getRegistry().get(byteArray.readUTF());
|
||||
|
||||
if (channel != null) {
|
||||
channel.sendComponent(user, gsonSerializer.deserialize(byteArray.readUTF()));
|
||||
}
|
||||
});
|
||||
|
||||
getMessageService().registerMessageListener("whisper-component", (user, byteArray) -> {
|
||||
UUID recipient = new UUID(byteArray.readLong(), byteArray.readLong());
|
||||
|
||||
ChatUser target = carbonChat.getUserService().wrap(recipient);
|
||||
String message = byteArray.readUTF();
|
||||
|
||||
if (!target.isIgnoringUser(user.getUUID())) {
|
||||
target.setReplyTarget(user.getUUID());
|
||||
target.sendMessage(gsonSerializer.deserialize(message));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public MessageService getMessageService() {
|
||||
return messageService;
|
||||
}
|
||||
|
||||
public void sendMessage(String key, UUID uuid, Consumer<ByteArrayDataOutput> consumer) {
|
||||
getMessageService().sendMessage(key, uuid, consumer);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package net.draycia.carbon.messaging;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public interface MessageService {
|
||||
|
||||
void registerMessageListener(String key, BiConsumer<ChatUser, ByteArrayDataInput> listener);
|
||||
void unregisterMessageListener(String key);
|
||||
|
||||
void sendMessage(String key, UUID uuid, Consumer<ByteArrayDataOutput> consumer);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package net.draycia.carbon.messaging.impl;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import io.github.leonardosnt.bungeechannelapi.BungeeChannelApi;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.messaging.MessageService;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class BungeeMessageService implements PluginMessageListener, MessageService {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final BungeeChannelApi api;
|
||||
|
||||
private Map<String, BiConsumer<ChatUser, ByteArrayDataInput>> listeners = new HashMap<>();
|
||||
|
||||
private final UUID serverUUID = UUID.randomUUID();
|
||||
|
||||
public BungeeMessageService(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
carbonChat.getServer().getMessenger().registerOutgoingPluginChannel(carbonChat, "BungeeCord");
|
||||
carbonChat.getServer().getMessenger().registerIncomingPluginChannel(carbonChat, "BungeeCord", this);
|
||||
|
||||
api = BungeeChannelApi.of(carbonChat);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!channel.equals("BungeeCord")) {
|
||||
return;
|
||||
}
|
||||
|
||||
ByteArrayDataInput input = ByteStreams.newDataInput(message);
|
||||
|
||||
String key = input.readUTF();
|
||||
short length = input.readShort();
|
||||
byte[] msgbytes = new byte[length];
|
||||
input.readFully(msgbytes);
|
||||
|
||||
ByteArrayDataInput msg = ByteStreams.newDataInput(msgbytes);
|
||||
|
||||
UUID messageUUID = new UUID(msg.readLong(), msg.readLong());
|
||||
|
||||
if (messageUUID.equals(serverUUID)) {
|
||||
return;
|
||||
}
|
||||
|
||||
UUID uuid = new UUID(msg.readLong(), msg.readLong());
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrapIfLoaded(uuid);
|
||||
|
||||
if (user == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.receiveMessage(user, key, msg);
|
||||
}
|
||||
|
||||
private void receiveMessage(ChatUser user, String key, ByteArrayDataInput value) {
|
||||
for (Map.Entry<String, BiConsumer<ChatUser, ByteArrayDataInput>> listener : listeners.entrySet()) {
|
||||
if (key.equals(listener.getKey())) {
|
||||
listener.getValue().accept(user, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerMessageListener(String key, BiConsumer<ChatUser, ByteArrayDataInput> listener) {
|
||||
listeners.put(key, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterMessageListener(String key) {
|
||||
listeners.remove(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String key, UUID uuid, Consumer<ByteArrayDataOutput> consumer) {
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeLong(serverUUID.getMostSignificantBits());
|
||||
msg.writeLong(serverUUID.getLeastSignificantBits());
|
||||
msg.writeLong(uuid.getMostSignificantBits());
|
||||
msg.writeLong(uuid.getLeastSignificantBits());
|
||||
|
||||
consumer.accept(msg);
|
||||
|
||||
api.forward("ALL", key, msg.toByteArray());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package net.draycia.carbon.messaging.impl;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import net.draycia.carbon.messaging.MessageService;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class EmptyMessageService implements MessageService {
|
||||
|
||||
@Override
|
||||
public void registerMessageListener(String key, BiConsumer<ChatUser, ByteArrayDataInput> listener) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterMessageListener(String key) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String key, UUID uuid, Consumer<ByteArrayDataOutput> consumer) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package net.draycia.carbon.messaging.impl;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import io.lettuce.core.RedisClient;
|
||||
import io.lettuce.core.RedisURI;
|
||||
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
|
||||
import io.lettuce.core.pubsub.api.sync.RedisPubSubCommands;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.messaging.MessageService;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.util.RedisListener;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class RedisMessageService implements MessageService {
|
||||
|
||||
private Map<String, BiConsumer<ChatUser, ByteArrayDataInput>> listeners = new HashMap<>();
|
||||
|
||||
private final RedisPubSubCommands<String, String> subscribeSync;
|
||||
private final RedisPubSubCommands<String, String> publishSync;
|
||||
|
||||
private final UUID serverUUID = UUID.randomUUID();
|
||||
|
||||
public RedisMessageService(CarbonChat carbonChat) {
|
||||
String host = carbonChat.getConfig().getString("redis.host");
|
||||
String password = carbonChat.getConfig().getString("redis.password");
|
||||
int port = carbonChat.getConfig().getInt("redis.port");
|
||||
int database = carbonChat.getConfig().getInt("redis.database");
|
||||
|
||||
RedisURI.Builder builder = RedisURI.Builder.redis(host, port)
|
||||
.withDatabase(database);
|
||||
|
||||
if (password != null) {
|
||||
builder.withPassword(password.toCharArray());
|
||||
}
|
||||
|
||||
RedisClient client = RedisClient.create(builder.build());
|
||||
|
||||
StatefulRedisPubSubConnection<String, String> subscribeConnection = client.connectPubSub();
|
||||
this.subscribeSync = subscribeConnection.sync();
|
||||
|
||||
StatefulRedisPubSubConnection<String, String> publishConnection = client.connectPubSub();
|
||||
this.publishSync = publishConnection.sync();
|
||||
|
||||
subscribeConnection.addListener((RedisListener)(channel, message) -> {
|
||||
ByteArrayDataInput input = ByteStreams.newDataInput(message.getBytes());
|
||||
|
||||
UUID messageUUID = new UUID(input.readLong(), input.readLong());
|
||||
|
||||
if (messageUUID.equals(serverUUID)) {
|
||||
return;
|
||||
}
|
||||
|
||||
UUID uuid = new UUID(input.readLong(), input.readLong());
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrapIfLoaded(uuid);
|
||||
|
||||
if (user == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.receiveMessage(user, channel, input);
|
||||
});
|
||||
}
|
||||
|
||||
private void receiveMessage(ChatUser user, String key, ByteArrayDataInput value) {
|
||||
for (Map.Entry<String, BiConsumer<ChatUser, ByteArrayDataInput>> listener : listeners.entrySet()) {
|
||||
if (key.equals(listener.getKey())) {
|
||||
listener.getValue().accept(user, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerMessageListener(String key, BiConsumer<ChatUser, ByteArrayDataInput> listener) {
|
||||
listeners.put(key, listener);
|
||||
subscribeSync.subscribe(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterMessageListener(String key) {
|
||||
listeners.remove(key);
|
||||
subscribeSync.unsubscribe(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String key, UUID uuid, Consumer<ByteArrayDataOutput> consumer) {
|
||||
ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||
|
||||
msg.writeLong(serverUUID.getMostSignificantBits());
|
||||
msg.writeLong(serverUUID.getLeastSignificantBits());
|
||||
msg.writeLong(uuid.getMostSignificantBits());
|
||||
msg.writeLong(uuid.getLeastSignificantBits());
|
||||
|
||||
consumer.accept(msg);
|
||||
|
||||
publishSync.publish(key, Base64.getEncoder().encodeToString(msg.toByteArray()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -82,21 +82,24 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
nickname = CarbonChat.LEGACY.serialize(component);
|
||||
}
|
||||
|
||||
this.nickname = nickname;
|
||||
final String newNickname = nickname;
|
||||
this.nickname = newNickname;
|
||||
|
||||
if (isOnline()) {
|
||||
this.asPlayer().setDisplayName(nickname);
|
||||
this.asPlayer().setDisplayName(newNickname);
|
||||
|
||||
if (carbonChat.getConfig().getBoolean("nicknames-set-tab-name")) {
|
||||
this.asPlayer().setPlayerListName(nickname);
|
||||
this.asPlayer().setPlayerListName(newNickname);
|
||||
}
|
||||
}
|
||||
|
||||
if (!fromRemote) {
|
||||
if (nickname == null) {
|
||||
this.publishChangeToRedis("nickname-reset", "true");
|
||||
carbonChat.getMessageManager().sendMessage("nickname-reset", this.getUUID(), (byteArray) -> {});
|
||||
} else {
|
||||
this.publishChangeToRedis("nickname", nickname);
|
||||
carbonChat.getMessageManager().sendMessage("nickname", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeUTF(newNickname);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -124,7 +127,9 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
this.selectedChannel = chatChannel.getKey();
|
||||
|
||||
if (!fromRemote) {
|
||||
this.publishChangeToRedis("selected-channel", chatChannel.getKey());
|
||||
carbonChat.getMessageManager().sendMessage("selected-channel", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeUTF(chatChannel.getKey());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,18 +146,18 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
@Override
|
||||
public void setIgnoringUser(UUID uuid, boolean ignoring, boolean fromRemote) {
|
||||
if (ignoring) {
|
||||
if (!fromRemote) {
|
||||
this.publishChangeToRedis("ignoring-user", uuid.toString());
|
||||
}
|
||||
|
||||
ignoredUsers.add(uuid);
|
||||
} else {
|
||||
if (!fromRemote) {
|
||||
this.publishChangeToRedis("unignoring-user", uuid.toString());
|
||||
}
|
||||
|
||||
ignoredUsers.remove(uuid);
|
||||
}
|
||||
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("ignoring-user", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeLong(uuid.getMostSignificantBits());
|
||||
byteArray.writeLong(uuid.getLeastSignificantBits());
|
||||
byteArray.writeBoolean(ignoring);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public List<UUID> getIgnoredUsers() {
|
||||
@@ -164,7 +169,9 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
this.muted = muted;
|
||||
|
||||
if (!fromRemote) {
|
||||
this.publishChangeToRedis("muted", muted ? "true" : "false");
|
||||
carbonChat.getMessageManager().sendMessage("muted", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeBoolean(muted);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,7 +185,9 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
this.shadowMuted = shadowMuted;
|
||||
|
||||
if (!fromRemote) {
|
||||
this.publishChangeToRedis("shadow-muted", shadowMuted ? "true" : "false");
|
||||
carbonChat.getMessageManager().sendMessage("shadow-muted", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeBoolean(shadowMuted);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,7 +207,10 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
this.replyTarget = target;
|
||||
|
||||
if (!fromRemote) {
|
||||
this.publishChangeToRedis("reply-target", target.toString());
|
||||
carbonChat.getMessageManager().sendMessage("reply-target", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeLong(target.getMostSignificantBits());
|
||||
byteArray.writeLong(target.getLeastSignificantBits());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,7 +230,9 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
this.spyingWhispers = spyingWhispers;
|
||||
|
||||
if (!fromRemote) {
|
||||
this.publishChangeToRedis("spying-whispers", spyingWhispers ? "true" : "false");
|
||||
carbonChat.getMessageManager().sendMessage("spying-whispers", this.getUUID(), (byteArray) -> {
|
||||
byteArray.writeBoolean(spyingWhispers);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -290,7 +304,14 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
}
|
||||
}
|
||||
} else if (sender.isOnline()) {
|
||||
carbonChat.getPluginMessageManager().sendComponentToPlayer(sender, this, toPlayerComponent, fromPlayerComponent);
|
||||
sender.sendMessage(toPlayerComponent);
|
||||
|
||||
// TODO: make sure this works lol
|
||||
carbonChat.getMessageManager().sendMessage("whisper-component", sender.getUUID(), (byteArray) -> {
|
||||
byteArray.writeLong(this.getUUID().getMostSignificantBits());
|
||||
byteArray.writeLong(this.getUUID().getLeastSignificantBits());
|
||||
byteArray.writeUTF(carbonChat.getAdventureManager().getAudiences().gsonSerializer().serialize(fromPlayerComponent));
|
||||
});
|
||||
}
|
||||
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
@@ -310,12 +331,4 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
"target", targetName, "sender", senderName));
|
||||
}
|
||||
}
|
||||
|
||||
/* package */ void publishChangeToRedis(String key, String value) {
|
||||
if (carbonChat.getRedisManager() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
carbonChat.getRedisManager().publishChange(uuid, key, value);
|
||||
}
|
||||
}
|
||||
+18
-16
@@ -17,14 +17,16 @@ public class SimpleUserChannelSettings implements UserChannelSettings {
|
||||
private final UUID uuid;
|
||||
private final String channel;
|
||||
|
||||
private transient CarbonChat carbonChat;
|
||||
|
||||
public SimpleUserChannelSettings(UUID uuid, String channel) {
|
||||
this.uuid = uuid;
|
||||
this.channel = channel;
|
||||
|
||||
carbonChat = (CarbonChat)Bukkit.getPluginManager().getPlugin("CarbonChat");
|
||||
}
|
||||
|
||||
private CarbonChatUser getUser() {
|
||||
CarbonChat carbonChat = (CarbonChat)Bukkit.getPluginManager().getPlugin("CarbonChat");
|
||||
|
||||
return (CarbonChatUser)carbonChat.getUserService().wrap(uuid);
|
||||
}
|
||||
|
||||
@@ -37,12 +39,11 @@ public class SimpleUserChannelSettings implements UserChannelSettings {
|
||||
public void setSpying(boolean spying, boolean fromRemote) {
|
||||
this.spying = spying;
|
||||
|
||||
if (fromRemote) {
|
||||
if (spying) {
|
||||
getUser().publishChangeToRedis("spying-channel", channel);
|
||||
} else {
|
||||
getUser().publishChangeToRedis("unspying-channel", channel);
|
||||
}
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("spying-channel", uuid, (byteArray) -> {
|
||||
byteArray.writeUTF(channel);
|
||||
byteArray.writeBoolean(spying);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,12 +56,11 @@ public class SimpleUserChannelSettings implements UserChannelSettings {
|
||||
public void setIgnoring(boolean ignored, boolean fromRemote) {
|
||||
this.ignored = ignored;
|
||||
|
||||
if (fromRemote) {
|
||||
if (spying) {
|
||||
getUser().publishChangeToRedis("ignoring-channel", channel);
|
||||
} else {
|
||||
getUser().publishChangeToRedis("unignoring-channel", channel);
|
||||
}
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("ignoring-channel", uuid, (byteArray) -> {
|
||||
byteArray.writeUTF(channel);
|
||||
byteArray.writeBoolean(ignored);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,8 +81,10 @@ public class SimpleUserChannelSettings implements UserChannelSettings {
|
||||
this.color = color.asHexString();
|
||||
}
|
||||
|
||||
if (fromRemote) {
|
||||
getUser().publishChangeToRedis("channel-color", this.color);
|
||||
if (!fromRemote) {
|
||||
carbonChat.getMessageManager().sendMessage("channel-color", uuid, (byteArray) -> {
|
||||
byteArray.writeUTF(color.asHexString());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,9 +38,6 @@ default:
|
||||
color: '#FFFFFF'
|
||||
# Can players /toggle this channel and hide its messages?
|
||||
ignorable: true
|
||||
# Setting this to false causes all other servers to re-format the message locally.
|
||||
# Unadvised unless you have notably different formatting on each server.
|
||||
forward-format: true
|
||||
# Enables/disables cross server chat for this channel
|
||||
should-bungee: true
|
||||
# If true, plugin will only use the format corresponding to the user's primary group (vault).
|
||||
@@ -140,8 +137,13 @@ storage:
|
||||
hostname: 'localhost'
|
||||
port: 3306
|
||||
|
||||
# Options:
|
||||
# 'bungee' - Uses bungee plugin messaging, requires BungeeCord or another proxy which supports it (Velocity!)
|
||||
# 'redis' - Uses redis for cross server syncing, does not require a server proxy
|
||||
# 'none' - Do not sync anything cross server, this is the default
|
||||
message-system: 'none'
|
||||
|
||||
redis:
|
||||
enabled: false
|
||||
host: 'localhost'
|
||||
port: 6379
|
||||
#password: 'password'
|
||||
|
||||
Reference in new issue
Block a user