Skip to content

Commit

Permalink
feat: add debug option for plugin messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
Silthus committed Feb 17, 2022
1 parent 58bf9b4 commit e0cb422
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,4 @@ public interface MessengerGatewayProvider {
*/
@NonNull MessengerGateway obtain(@NonNull IncomingMessageConsumer incomingMessageConsumer);

interface Registry {
MessengerGatewayProvider get(String name);

void register(String name, MessengerGatewayProvider provider);
}
}
2 changes: 2 additions & 0 deletions core/src/main/java/net/silthus/schat/PluginMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import java.util.UUID;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import lombok.experimental.Accessors;

@Getter
@Accessors(fluent = true)
@ToString
@EqualsAndHashCode(of = "id")
public abstract class PluginMessage {
private final UUID id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import lombok.experimental.Accessors;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.silthus.schat.Messenger;
import net.silthus.schat.MessengerGatewayProvider;
import net.silthus.schat.bukkit.adapter.BukkitChatterFactory;
import net.silthus.schat.bukkit.adapter.BukkitConnectionListener;
import net.silthus.schat.bukkit.adapter.BukkitEventBus;
Expand All @@ -46,13 +45,15 @@
import net.silthus.schat.platform.chatter.AbstractChatterFactory;
import net.silthus.schat.platform.chatter.ConnectionListener;
import net.silthus.schat.platform.config.adapter.ConfigurationAdapter;
import net.silthus.schat.platform.messaging.GatewayProviderRegistry;
import net.silthus.schat.platform.plugin.AbstractSChatServerPlugin;
import net.silthus.schat.platform.sender.Sender;
import net.silthus.schat.ui.view.ViewProvider;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;

import static cloud.commandframework.execution.CommandExecutionCoordinator.simpleCoordinator;
import static net.silthus.schat.bukkit.adapter.BukkitMessengerGateway.createBukkitMessengerGateway;
import static net.silthus.schat.platform.config.adapter.ConfigurationAdapters.YAML;

@Getter
Expand Down Expand Up @@ -89,13 +90,13 @@ protected void setupSenderFactory() {
}

@Override
protected void registerMessengerGateway(MessengerGatewayProvider.Registry registry) {
registry.register(BukkitMessengerGateway.GATEWAY_TYPE, consumer -> new BukkitMessengerGateway(
protected void registerMessengerGateway(GatewayProviderRegistry registry) {
registry.register(BukkitMessengerGateway.GATEWAY_TYPE, consumer -> createBukkitMessengerGateway(
bootstrap().loader(),
Bukkit.getServer(),
bootstrap().scheduler(),
consumer
));
consumer,
config()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
package net.silthus.schat.bukkit.adapter;

import java.nio.charset.StandardCharsets;
import lombok.extern.java.Log;
import net.silthus.schat.IncomingMessageConsumer;
import net.silthus.schat.MessengerGateway;
import net.silthus.schat.platform.config.ConfigKeys;
import net.silthus.schat.platform.config.SChatConfig;
import net.silthus.schat.platform.plugin.scheduler.SchedulerAdapter;
import org.bukkit.Server;
import org.bukkit.entity.Player;
Expand All @@ -38,12 +41,23 @@ public class BukkitMessengerGateway implements MessengerGateway, PluginMessageLi

public static final String GATEWAY_TYPE = "pluginmessage";

public static BukkitMessengerGateway createBukkitMessengerGateway(Plugin plugin,
Server server,
SchedulerAdapter scheduler,
IncomingMessageConsumer consumer,
SChatConfig config) {
if (config.get(ConfigKeys.DEBUG))
return new Logging(plugin, server, scheduler, consumer);
else
return new BukkitMessengerGateway(plugin, server, scheduler, consumer);
}

private final Plugin plugin;
private final Server server;
private final SchedulerAdapter scheduler;
private final IncomingMessageConsumer consumer;

public BukkitMessengerGateway(Plugin plugin, Server server, SchedulerAdapter scheduler, IncomingMessageConsumer consumer) {
private BukkitMessengerGateway(Plugin plugin, Server server, SchedulerAdapter scheduler, IncomingMessageConsumer consumer) {
this.plugin = plugin;
this.server = server;
this.scheduler = scheduler;
Expand All @@ -57,7 +71,7 @@ public void sendOutgoingMessage(String encodedMessage) {
scheduler.async().execute(() -> sendPluginMessage(encodedMessage));
}

private void sendPluginMessage(String encodedMessage) {
protected void sendPluginMessage(String encodedMessage) {
server.sendPluginMessage(plugin, CHANNEL, encodedMessage.getBytes(StandardCharsets.UTF_8));
}

Expand All @@ -72,4 +86,18 @@ public void close() {
server.getMessenger().unregisterOutgoingPluginChannel(plugin, CHANNEL);
server.getMessenger().unregisterIncomingPluginChannel(plugin, CHANNEL);
}

@Log
private static final class Logging extends BukkitMessengerGateway {

private Logging(Plugin plugin, Server server, SchedulerAdapter scheduler, IncomingMessageConsumer consumer) {
super(plugin, server, scheduler, consumer);
}

@Override
protected void sendPluginMessage(String encodedMessage) {
log.info("Sending Outgoing Message over " + CHANNEL + ": " + encodedMessage);
super.sendPluginMessage(encodedMessage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.nio.charset.StandardCharsets;
import net.silthus.schat.bukkit.BukkitTests;
import net.silthus.schat.platform.SchedulerMock;
import net.silthus.schat.platform.config.ConfigKeys;
import net.silthus.schat.platform.config.SChatConfig;
import net.silthus.schat.platform.messaging.MessagingServiceMock;
import net.silthus.schat.platform.messaging.MockPluginMessage;
import org.bukkit.Server;
Expand Down Expand Up @@ -54,7 +56,9 @@ void setUp() {
messenger = mock(Messenger.class);
when(server.getMessenger()).thenReturn(messenger);
consumer = new MessagingServiceMock();
gateway = new BukkitMessengerGateway(mockPlugin, server, scheduler, consumer);
final SChatConfig config = mock(SChatConfig.class);
when(config.get(ConfigKeys.DEBUG)).thenReturn(false);
gateway = BukkitMessengerGateway.createBukkitMessengerGateway(mockPlugin, server, scheduler, consumer, config);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@

import lombok.Getter;
import lombok.experimental.Accessors;
import net.silthus.schat.MessengerGatewayProvider;
import net.silthus.schat.bungeecord.adapter.BungeecordEventBus;
import net.silthus.schat.bungeecord.adapter.BungeecordMessengerGateway;
import net.silthus.schat.bungeecord.adapter.BungeecordSenderFactory;
import net.silthus.schat.eventbus.EventBus;
import net.silthus.schat.platform.config.adapter.ConfigurationAdapter;
import net.silthus.schat.platform.messaging.GatewayProviderRegistry;
import net.silthus.schat.platform.plugin.AbstractSChatProxyPlugin;
import net.silthus.schat.platform.sender.Sender;

Expand Down Expand Up @@ -70,8 +70,8 @@ protected ConfigurationAdapter createConfigurationAdapter() {
}

@Override
protected void registerMessengerGateway(MessengerGatewayProvider.Registry registry) {
registry.register(GATEWAY_TYPE, in -> new BungeecordMessengerGateway(bootstrap()));
protected void registerMessengerGateway(GatewayProviderRegistry registry) {
registry.register(GATEWAY_TYPE, in -> BungeecordMessengerGateway.createBungeecordMessengerGateway(bootstrap()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,33 @@
package net.silthus.schat.bungeecord.adapter;

import java.nio.charset.StandardCharsets;
import lombok.extern.java.Log;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
import net.silthus.schat.MessengerGateway;
import net.silthus.schat.bungeecord.BungeecordBootstrap;
import net.silthus.schat.platform.config.ConfigKeys;
import net.silthus.schat.platform.plugin.scheduler.SchedulerAdapter;

public final class BungeecordMessengerGateway implements MessengerGateway, Listener {
public class BungeecordMessengerGateway implements MessengerGateway, Listener {

public static final String GATEWAY_TYPE = "pluginmessage";
public static final String CHANNEL = "schat:update";

public static BungeecordMessengerGateway createBungeecordMessengerGateway(BungeecordBootstrap bootstrap) {
if (bootstrap.plugin().config().get(ConfigKeys.DEBUG))
return new Logging(bootstrap);
else
return new BungeecordMessengerGateway(bootstrap);
}

private final ProxyServer proxy;
private final SchedulerAdapter scheduler;

public BungeecordMessengerGateway(BungeecordBootstrap bootstrap) {
private BungeecordMessengerGateway(BungeecordBootstrap bootstrap) {
this.proxy = bootstrap.proxy();
this.scheduler = bootstrap.scheduler();
this.proxy.getPluginManager().registerListener(bootstrap.loader(), this);
Expand All @@ -55,10 +64,14 @@ public void sendOutgoingMessage(String encodedMessage) {

private void sendToAllServers(byte[] bytes) {
for (ServerInfo server : proxy.getServers().values()) {
scheduler.async().execute(() -> server.sendData(CHANNEL, bytes));
scheduler.async().execute(() -> sendToServer(bytes, server));
}
}

protected void sendToServer(byte[] bytes, ServerInfo server) {
server.sendData(CHANNEL, bytes);
}

@EventHandler
public void onIncomingMessage(PluginMessageEvent event) {
if (!event.getTag().equals(CHANNEL))
Expand All @@ -70,4 +83,18 @@ public void onIncomingMessage(PluginMessageEvent event) {
public void close() {
this.proxy.getPluginManager().unregisterListener(this);
}

@Log
private static final class Logging extends BungeecordMessengerGateway {

private Logging(BungeecordBootstrap bootstrap) {
super(bootstrap);
}

@Override
protected void sendToServer(byte[] bytes, ServerInfo server) {
log.info("Forwarding Message to: " + server.getName());
super.sendToServer(bytes, server);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.silthus.schat.platform.config.key.KeyedConfiguration;
import net.silthus.schat.pointer.Settings;

import static net.silthus.schat.platform.config.key.ConfigKeyFactory.booleanKey;
import static net.silthus.schat.platform.config.key.ConfigKeyFactory.key;
import static net.silthus.schat.platform.config.key.ConfigKeyFactory.lowercaseStringKey;
import static net.silthus.schat.platform.config.key.ConfigKeyFactory.modifiable;
Expand All @@ -45,6 +46,8 @@ private ConfigKeys() {

public static final ConfigKey<String> MESSENGER = notReloadable(lowercaseStringKey("messenger", "pluginmessage"));

public static final ConfigKey<Boolean> DEBUG = notReloadable(booleanKey("debug", false));

public static final ConfigKey<List<Channel>> CHANNELS = modifiable(key(config -> {
final ArrayList<Channel> channels = new ArrayList<>();
for (final String key : config.keys("channels", new ArrayList<>())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import net.silthus.schat.platform.config.adapter.ConfigurationAdapter;
import net.silthus.schat.platform.config.key.KeyedConfiguration;

public final class SChatConfig extends KeyedConfiguration {
public class SChatConfig extends KeyedConfiguration {

private final EventBus eventBus;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
import java.util.Map;
import net.silthus.schat.MessengerGatewayProvider;

public final class GatewayProviderRegistry implements MessengerGatewayProvider.Registry {
public final class GatewayProviderRegistry {

private final Map<String, MessengerGatewayProvider> providerMap = new HashMap<>();

@Override
public MessengerGatewayProvider get(String name) {
final String key = name.toLowerCase();
if (!providerMap.containsKey(key))
Expand All @@ -41,7 +40,6 @@ public MessengerGatewayProvider get(String name) {
return providerMap.get(key);
}

@Override
public void register(String name, MessengerGatewayProvider provider) {
final String key = name.toLowerCase();
if (providerMap.containsKey(key))
Expand Down
Loading

0 comments on commit e0cb422

Please sign in to comment.