mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 22:16:22 +00:00
Single module setup
This commit is contained in:
1 parent
ad9a794156
commit
66217f6534
70 files changed
+92
-138
No files matched your search
@@ -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.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 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 MessageService {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final BungeeChannelApi api;
|
||||
|
||||
private final Map<String, BiConsumer<ChatUser, ByteArrayDataInput>> userLoadedListeners = new HashMap<>();
|
||||
private final Map<String, BiConsumer<UUID, ByteArrayDataInput>> userNotLoadedListeners = new HashMap<>();
|
||||
|
||||
private final UUID serverUUID = UUID.randomUUID();
|
||||
|
||||
public BungeeMessageService(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
carbonChat.getServer().getMessenger().registerOutgoingPluginChannel(carbonChat, "BungeeCord");
|
||||
|
||||
api = BungeeChannelApi.of(carbonChat);
|
||||
|
||||
api.registerForwardListener((String channel, Player player, byte[] bytes) -> {
|
||||
try {
|
||||
ByteArrayDataInput input = ByteStreams.newDataInput(bytes);
|
||||
|
||||
// Separated out for ease of debugging.
|
||||
long mostServer = input.readLong();
|
||||
long leastServer = input.readLong();
|
||||
|
||||
UUID messageUUID = new UUID(mostServer, leastServer);
|
||||
|
||||
if (messageUUID.equals(serverUUID)) {
|
||||
return;
|
||||
}
|
||||
|
||||
long mostUser = input.readLong();
|
||||
long leastUser = input.readLong();
|
||||
|
||||
UUID userUUID = new UUID(mostUser, leastUser);
|
||||
|
||||
this.receiveMessage(userUUID, channel, input);
|
||||
} catch (IllegalStateException ignored) {}
|
||||
});
|
||||
}
|
||||
|
||||
private void receiveMessage(UUID uuid, String key, ByteArrayDataInput value) {
|
||||
ChatUser user = carbonChat.getUserService().wrapIfLoaded(uuid);
|
||||
|
||||
if (user != null) {
|
||||
for (Map.Entry<String, BiConsumer<ChatUser, ByteArrayDataInput>> listener : userLoadedListeners.entrySet()) {
|
||||
if (key.equals(listener.getKey())) {
|
||||
listener.getValue().accept(user, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, BiConsumer<UUID, ByteArrayDataInput>> listener : userNotLoadedListeners.entrySet()) {
|
||||
if (key.equals(listener.getKey())) {
|
||||
listener.getValue().accept(uuid, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerUserMessageListener(String key, BiConsumer<ChatUser, ByteArrayDataInput> listener) {
|
||||
userLoadedListeners.put(key, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerUUIDMessageListener(String key, BiConsumer<UUID, ByteArrayDataInput> listener) {
|
||||
userNotLoadedListeners.put(key, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterMessageListener(String key) {
|
||||
userLoadedListeners.remove(key);
|
||||
userNotLoadedListeners.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,34 @@
|
||||
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 registerUserMessageListener(String key, BiConsumer<ChatUser, ByteArrayDataInput> listener) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerUUIDMessageListener(String key, BiConsumer<UUID, 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,123 @@
|
||||
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>> userLoadedListeners = new HashMap<>();
|
||||
private Map<String, BiConsumer<UUID, ByteArrayDataInput>> userNotLoadedListeners = new HashMap<>();
|
||||
|
||||
private final RedisPubSubCommands<String, String> subscribeSync;
|
||||
private final RedisPubSubCommands<String, String> publishSync;
|
||||
|
||||
private final UUID serverUUID = UUID.randomUUID();
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
public RedisMessageService(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());
|
||||
}
|
||||
|
||||
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(Base64.getDecoder().decode(message));
|
||||
|
||||
UUID messageUUID = new UUID(input.readLong(), input.readLong());
|
||||
|
||||
if (messageUUID.equals(serverUUID)) {
|
||||
return;
|
||||
}
|
||||
|
||||
UUID uuid = new UUID(input.readLong(), input.readLong());
|
||||
|
||||
this.receiveMessage(uuid, channel, input);
|
||||
});
|
||||
}
|
||||
|
||||
private void receiveMessage(UUID uuid, String key, ByteArrayDataInput value) {
|
||||
ChatUser user = carbonChat.getUserService().wrapIfLoaded(uuid);
|
||||
|
||||
if (user != null) {
|
||||
for (Map.Entry<String, BiConsumer<ChatUser, ByteArrayDataInput>> listener : userLoadedListeners.entrySet()) {
|
||||
if (key.equals(listener.getKey())) {
|
||||
listener.getValue().accept(user, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, BiConsumer<UUID, ByteArrayDataInput>> listener : userNotLoadedListeners.entrySet()) {
|
||||
if (key.equals(listener.getKey())) {
|
||||
listener.getValue().accept(uuid, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerUserMessageListener(String key, BiConsumer<ChatUser, ByteArrayDataInput> listener) {
|
||||
userLoadedListeners.put(key, listener);
|
||||
subscribeSync.subscribe(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerUUIDMessageListener(String key, BiConsumer<UUID, ByteArrayDataInput> listener) {
|
||||
userNotLoadedListeners.put(key, listener);
|
||||
subscribeSync.subscribe(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterMessageListener(String key) {
|
||||
userLoadedListeners.remove(key);
|
||||
userNotLoadedListeners.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()));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user