Expose a means to implement the plugin's MessagingService via the API
This commit is contained in:
@@ -62,7 +62,7 @@ import me.lucko.luckperms.common.logging.SenderLogger;
|
||||
import me.lucko.luckperms.common.managers.group.StandardGroupManager;
|
||||
import me.lucko.luckperms.common.managers.track.StandardTrackManager;
|
||||
import me.lucko.luckperms.common.managers.user.StandardUserManager;
|
||||
import me.lucko.luckperms.common.messaging.ExtendedMessagingService;
|
||||
import me.lucko.luckperms.common.messaging.InternalMessagingService;
|
||||
import me.lucko.luckperms.common.model.User;
|
||||
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
||||
import me.lucko.luckperms.common.plugin.SchedulerAdapter;
|
||||
@@ -105,7 +105,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
|
||||
private StandardTrackManager trackManager;
|
||||
private Storage storage;
|
||||
private FileWatcher fileWatcher = null;
|
||||
private ExtendedMessagingService messagingService = null;
|
||||
private InternalMessagingService messagingService = null;
|
||||
private UuidCache uuidCache;
|
||||
private LuckPermsApiProvider apiProvider;
|
||||
private EventFactory eventFactory;
|
||||
@@ -274,10 +274,17 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ExtendedMessagingService> getMessagingService() {
|
||||
public Optional<InternalMessagingService> getMessagingService() {
|
||||
return Optional.ofNullable(this.messagingService);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMessagingService(InternalMessagingService messagingService) {
|
||||
if (this.messagingService == null) {
|
||||
this.messagingService = messagingService;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<FileWatcher> getFileWatcher() {
|
||||
return Optional.ofNullable(this.fileWatcher);
|
||||
|
||||
+52
-8
@@ -25,31 +25,75 @@
|
||||
|
||||
package me.lucko.luckperms.bungee.messaging;
|
||||
|
||||
import me.lucko.luckperms.api.messenger.IncomingMessageConsumer;
|
||||
import me.lucko.luckperms.api.messenger.Messenger;
|
||||
import me.lucko.luckperms.api.messenger.MessengerProvider;
|
||||
import me.lucko.luckperms.bungee.LPBungeePlugin;
|
||||
import me.lucko.luckperms.common.messaging.ExtendedMessagingService;
|
||||
import me.lucko.luckperms.common.messaging.InternalMessagingService;
|
||||
import me.lucko.luckperms.common.messaging.LuckPermsMessagingService;
|
||||
import me.lucko.luckperms.common.messaging.MessagingFactory;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class BungeeMessagingFactory extends MessagingFactory<LPBungeePlugin> {
|
||||
public BungeeMessagingFactory(LPBungeePlugin plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ExtendedMessagingService getServiceFor(String messagingType) {
|
||||
protected InternalMessagingService getServiceFor(String messagingType) {
|
||||
if (messagingType.equals("bungee")) {
|
||||
BungeeMessagingService bungeeMessaging = new BungeeMessagingService(getPlugin());
|
||||
bungeeMessaging.init();
|
||||
return bungeeMessaging;
|
||||
try {
|
||||
return new LuckPermsMessagingService(getPlugin(), new BungeeMessengerProvider());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if (messagingType.equals("redisbungee")) {
|
||||
if (getPlugin().getProxy().getPluginManager().getPlugin("RedisBungee") == null) {
|
||||
getPlugin().getLog().warn("RedisBungee plugin not present.");
|
||||
} else {
|
||||
RedisBungeeMessagingService redisBungeeMessaging = new RedisBungeeMessagingService(getPlugin());
|
||||
redisBungeeMessaging.init();
|
||||
return redisBungeeMessaging;
|
||||
try {
|
||||
return new LuckPermsMessagingService(getPlugin(), new RedisBungeeMessengerProvider());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return super.getServiceFor(messagingType);
|
||||
}
|
||||
|
||||
private class BungeeMessengerProvider implements MessengerProvider {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Bungee";
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Messenger obtain(@Nonnull IncomingMessageConsumer incomingMessageConsumer) {
|
||||
BungeeMessenger bungeeMessaging = new BungeeMessenger(getPlugin(), incomingMessageConsumer);
|
||||
bungeeMessaging.init();
|
||||
return bungeeMessaging;
|
||||
}
|
||||
}
|
||||
|
||||
private class RedisBungeeMessengerProvider implements MessengerProvider {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getName() {
|
||||
return "RedisBungee";
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Messenger obtain(@Nonnull IncomingMessageConsumer incomingMessageConsumer) {
|
||||
RedisBungeeMessenger redisBungeeMessaging = new RedisBungeeMessenger(getPlugin(), incomingMessageConsumer);
|
||||
redisBungeeMessaging.init();
|
||||
return redisBungeeMessaging;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+27
-16
@@ -29,9 +29,10 @@ import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
|
||||
import me.lucko.luckperms.api.messenger.IncomingMessageConsumer;
|
||||
import me.lucko.luckperms.api.messenger.Messenger;
|
||||
import me.lucko.luckperms.api.messenger.message.OutgoingMessage;
|
||||
import me.lucko.luckperms.bungee.LPBungeePlugin;
|
||||
import me.lucko.luckperms.common.messaging.AbstractMessagingService;
|
||||
import me.lucko.luckperms.common.messaging.ExtendedMessagingService;
|
||||
|
||||
import net.md_5.bungee.api.config.ServerInfo;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
@@ -39,15 +40,20 @@ import net.md_5.bungee.api.event.PluginMessageEvent;
|
||||
import net.md_5.bungee.api.plugin.Listener;
|
||||
import net.md_5.bungee.event.EventHandler;
|
||||
|
||||
/**
|
||||
* An implementation of {@link ExtendedMessagingService} using the plugin messaging channels.
|
||||
*/
|
||||
public class BungeeMessagingService extends AbstractMessagingService implements Listener {
|
||||
private final LPBungeePlugin plugin;
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public BungeeMessagingService(LPBungeePlugin plugin) {
|
||||
super(plugin, "Bungee");
|
||||
/**
|
||||
* An implementation of {@link Messenger} using the plugin messaging channels.
|
||||
*/
|
||||
public class BungeeMessenger implements Messenger, Listener {
|
||||
private static final String CHANNEL = "lpuc";
|
||||
|
||||
private final LPBungeePlugin plugin;
|
||||
private final IncomingMessageConsumer consumer;
|
||||
|
||||
public BungeeMessenger(LPBungeePlugin plugin, IncomingMessageConsumer consumer) {
|
||||
this.plugin = plugin;
|
||||
this.consumer = consumer;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
@@ -61,10 +67,9 @@ public class BungeeMessagingService extends AbstractMessagingService implements
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void sendMessage(String message) {
|
||||
|
||||
public void sendOutgoingMessage(@Nonnull OutgoingMessage outgoingMessage) {
|
||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
out.writeUTF(message);
|
||||
out.writeUTF(outgoingMessage.asEncodedString());
|
||||
|
||||
byte[] data = out.toByteArray();
|
||||
|
||||
@@ -85,12 +90,18 @@ public class BungeeMessagingService extends AbstractMessagingService implements
|
||||
return;
|
||||
}
|
||||
|
||||
ByteArrayDataInput in = ByteStreams.newDataInput(e.getData());
|
||||
byte[] data = e.getData();
|
||||
|
||||
ByteArrayDataInput in = ByteStreams.newDataInput(data);
|
||||
String msg = in.readUTF();
|
||||
|
||||
onMessage(msg, u -> {
|
||||
if (this.consumer.consumeIncomingMessageAsString(msg)) {
|
||||
// Forward to other servers
|
||||
this.plugin.getScheduler().doAsync(() -> sendMessage(u));
|
||||
});
|
||||
this.plugin.getScheduler().doAsync(() -> {
|
||||
for (ServerInfo server : this.plugin.getProxy().getServers().values()) {
|
||||
server.sendData(CHANNEL, data, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
-9
@@ -29,23 +29,29 @@ import com.imaginarycode.minecraft.redisbungee.RedisBungee;
|
||||
import com.imaginarycode.minecraft.redisbungee.RedisBungeeAPI;
|
||||
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
|
||||
|
||||
import me.lucko.luckperms.api.messenger.IncomingMessageConsumer;
|
||||
import me.lucko.luckperms.api.messenger.Messenger;
|
||||
import me.lucko.luckperms.api.messenger.message.OutgoingMessage;
|
||||
import me.lucko.luckperms.bungee.LPBungeePlugin;
|
||||
import me.lucko.luckperms.common.messaging.AbstractMessagingService;
|
||||
import me.lucko.luckperms.common.messaging.ExtendedMessagingService;
|
||||
|
||||
import net.md_5.bungee.api.plugin.Listener;
|
||||
import net.md_5.bungee.event.EventHandler;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* An implementation of {@link ExtendedMessagingService} using Redis, via RedisBungee's API.
|
||||
* An implementation of {@link Messenger} using Redis, via RedisBungee's API.
|
||||
*/
|
||||
public class RedisBungeeMessagingService extends AbstractMessagingService implements Listener {
|
||||
public class RedisBungeeMessenger implements Messenger, Listener {
|
||||
private static final String CHANNEL = "lpuc";
|
||||
|
||||
private final LPBungeePlugin plugin;
|
||||
private final IncomingMessageConsumer consumer;
|
||||
private RedisBungeeAPI redisBungee;
|
||||
|
||||
public RedisBungeeMessagingService(LPBungeePlugin plugin) {
|
||||
super(plugin, "RedisBungee");
|
||||
public RedisBungeeMessenger(LPBungeePlugin plugin, IncomingMessageConsumer consumer) {
|
||||
this.plugin = plugin;
|
||||
this.consumer = consumer;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
@@ -64,8 +70,8 @@ public class RedisBungeeMessagingService extends AbstractMessagingService implem
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void sendMessage(String message) {
|
||||
this.redisBungee.sendChannelMessage(CHANNEL, message);
|
||||
public void sendOutgoingMessage(@Nonnull OutgoingMessage outgoingMessage) {
|
||||
this.redisBungee.sendChannelMessage(CHANNEL, outgoingMessage.asEncodedString());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@@ -74,6 +80,6 @@ public class RedisBungeeMessagingService extends AbstractMessagingService implem
|
||||
return;
|
||||
}
|
||||
|
||||
onMessage(e.getMessage(), null);
|
||||
this.consumer.consumeIncomingMessageAsString(e.getMessage());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user