mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
Initial messaging refactor
This commit is contained in:
1 parent
348cba27e7
commit
db2a2325fc
9 files changed
+361
-388
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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,10 +235,6 @@ public class CarbonChatChannel extends ChatChannel {
|
||||
carbonChat.getPluginMessageManager().sendComponent(this, player, component);
|
||||
}
|
||||
|
||||
public void sendMessageToBungee(Player player, String message) {
|
||||
carbonChat.getPluginMessageManager().sendMessage(this, player, message);
|
||||
}
|
||||
|
||||
public Boolean canPlayerSee(ChatUser target, boolean checkSpying) {
|
||||
Player targetPlayer = target.asPlayer();
|
||||
|
||||
|
||||
@@ -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,128 @@
|
||||
package net.draycia.carbon.messaging;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.messaging.impl.BungeeMessageService;
|
||||
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;
|
||||
|
||||
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();
|
||||
|
||||
if (carbonChat.getConfig().getBoolean("redis.enabled")) {
|
||||
carbonChat.getLogger().info("Using Redis for message forwarding!");
|
||||
this.messageService = new RedisMessageService(carbonChat);
|
||||
} else {
|
||||
carbonChat.getLogger().info("Using Bungee Plugin Messaging for message forwarding!");
|
||||
this.messageService = new BungeeMessageService(carbonChat);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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,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()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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).
|
||||
|
||||
Reference in new issue
Block a user