From 56844bea69cfe484b76658984b1098676148d208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 03:23:48 +0300 Subject: [PATCH 01/18] refactor bunch of things --- .../webhooklogger/common/WebhookLogger.java | 12 ++-- .../common/client/ClientManager.java | 69 ++++++++----------- .../common/client/DiscordClient.java | 8 +-- .../common/client/WebhookRegistry.java | 36 ++++++++++ .../common/config/MainConfig.java | 18 ++++- .../webhooklogger/common/config/Webhook.java | 16 +++++ .../config/event/CancellableEventConfig.java | 12 ++++ .../common/config/event/ChatEventConfig.java | 12 +--- .../config/event/CommandEventConfig.java | 14 +--- .../common/config/event/DeathEventConfig.java | 12 +--- .../config/event/JoinQuitEventConfig.java | 5 -- .../common/platform/Platform.java | 1 + .../paper/WebhookLoggerPaper.java | 23 +++---- .../paper/listener/ChatListener.java | 2 +- .../paper/listener/CommandListener.java | 6 +- .../paper/listener/DeathListener.java | 2 +- .../paper/listener/JoinQuitListener.java | 11 --- .../paper/listener/ListenerFactory.java | 36 ++++++++++ .../sponge/WebhookLoggerSponge.java | 17 ++--- .../sponge/listener/ChatListener.java | 7 +- .../sponge/listener/CommandListener.java | 7 +- .../sponge/listener/DeathListener.java | 2 +- .../sponge/listener/JoinQuitListener.java | 11 --- .../sponge/listener/ListenerFactory.java | 39 +++++++++++ 24 files changed, 222 insertions(+), 156 deletions(-) create mode 100644 common/src/main/java/fi/fabianadrian/webhooklogger/common/client/WebhookRegistry.java create mode 100644 common/src/main/java/fi/fabianadrian/webhooklogger/common/config/Webhook.java create mode 100644 common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/CancellableEventConfig.java create mode 100644 paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/ListenerFactory.java create mode 100644 sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/ListenerFactory.java diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java index 569b031..2e33933 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java @@ -22,7 +22,7 @@ import java.util.concurrent.ScheduledExecutorService; public final class WebhookLogger { - private final Logger logger; + private final Platform platform; private final CommandManager commandManager; private final ConfigManager configManager; private final ClientManager clientManager; @@ -30,11 +30,11 @@ public final class WebhookLogger { private final DependencyManager dependencyManager = new DependencyManager(); public WebhookLogger(Platform platform) { - this.logger = platform.logger(); + this.platform = platform; - new TranslationManager(this.logger); + new TranslationManager(platform.logger()); - this.configManager = new ConfigManager(platform.configPath(), this.logger); + this.configManager = new ConfigManager(platform.configPath(), platform.logger()); this.commandManager = platform.commandManager(); setupCommandManager(); @@ -49,6 +49,8 @@ public boolean reload() { boolean success = this.configManager.reload(); this.clientManager.reload(); + this.platform.registerListeners(); + return success; } @@ -65,7 +67,7 @@ public EventsConfig eventsConfig() { } public Logger logger() { - return this.logger; + return this.platform.logger(); } public ClientManager clientManager() { diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/ClientManager.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/ClientManager.java index 962f991..8a5b66f 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/ClientManager.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/ClientManager.java @@ -5,16 +5,18 @@ import fi.fabianadrian.webhooklogger.common.config.MainConfig; import fi.fabianadrian.webhooklogger.common.event.EventBuilder; import fi.fabianadrian.webhooklogger.common.event.EventType; +import org.slf4j.Logger; -import java.util.HashMap; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; public final class ClientManager { private final WebhookLogger webhookLogger; - private final Map clients = new HashMap<>(); - private DiscordClient defaultClient; + private final WebhookRegistry registry = new WebhookRegistry(); private ScheduledFuture scheduledSendMessageTask; private MainConfig config; @@ -23,10 +25,8 @@ public ClientManager(WebhookLogger webhookLogger) { } public void send(EventBuilder eventBuilder) { - DiscordClient client = this.clients.getOrDefault(eventBuilder.type(), this.defaultClient); - - if (client == null) { - this.webhookLogger.logger().warn("No webhook found for {} and default webhook is not configured! Consider turning off logging for this specific event or configure a default webhook", eventBuilder.type()); + List clients = this.registry.forEventType(eventBuilder.type()); + if (clients.isEmpty()) { return; } @@ -35,12 +35,15 @@ public void send(EventBuilder eventBuilder) { discordSerialized = discordSerialized.replaceAll(entry.getKey(), entry.getValue()); } - client.add(discordSerialized); + //TODO Fix ugly + String finalDiscordSerialized = discordSerialized; + clients.forEach(client -> client.queue(finalDiscordSerialized)); } public void reload() { this.config = this.webhookLogger.mainConfig(); + this.registry.clear(); parseWebhooks(); if (this.scheduledSendMessageTask != null) { @@ -48,53 +51,35 @@ public void reload() { } this.scheduledSendMessageTask = this.webhookLogger.scheduler().scheduleAtFixedRate( - () -> { - this.defaultClient.send(); - this.clients.values().forEach(DiscordClient::send); - }, + () -> this.registry.webhooks().forEach(DiscordClient::sendAll), 0, this.config.sendRate(), TimeUnit.SECONDS ); } - private void parseWebhooks() { - this.defaultClient = null; - this.clients.clear(); + public WebhookRegistry registry() { + return this.registry; + } - this.config.webhooks().forEach((key, url) -> { - if (url.isBlank()) { - this.webhookLogger.logger().warn("{} webhook has empty URL! Please check the configuration", key); + private void parseWebhooks() { + Logger logger = this.webhookLogger.logger(); + this.config.webhooks().forEach(webhook -> { + if (webhook.url().isBlank()) { + logger.warn("webhook url blank"); + // TODO Log message return; } - if (key.equalsIgnoreCase("default")) { - if (this.defaultClient != null) { - this.webhookLogger.logger().warn("Duplicate default logger found! Please check the webhook configuration"); + List events = new ArrayList<>(); + for (EventType event : EventType.values()) { + Matcher matcher = webhook.regex().matcher(event.name()); + if (matcher.find()) { + events.add(event); } - - this.defaultClient = new DiscordClient(this.webhookLogger.logger(), url); - return; } - EventType type; - try { - type = EventType.valueOf(key.toUpperCase()); - } catch (IllegalStateException e) { - this.webhookLogger.logger().warn("Unknown event {}! Please check the webhook configuration", key); - return; - } - - this.clients.put(type, createClient(url)); + this.registry.register(new DiscordClient(logger, webhook.url()), events); }); } - - private DiscordClient createClient(String url) { - for (DiscordClient client : this.clients.values()) { - if (url.equals(client.url())) { - return client; - } - } - return new DiscordClient(this.webhookLogger.logger(), url); - } } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/DiscordClient.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/DiscordClient.java index a890262..853bf06 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/DiscordClient.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/DiscordClient.java @@ -21,15 +21,11 @@ public DiscordClient(Logger logger, String url) { this.client = WebHookClient.fromURL(url); } - public String url() { - return this.url; - } - - public void add(String message) { + public void queue(String message) { this.messageQueue.add(message); } - public void send() throws RuntimeException { + public void sendAll() throws RuntimeException { // Copy messageBuffer List messages = List.copyOf(this.messageQueue); diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/WebhookRegistry.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/WebhookRegistry.java new file mode 100644 index 0000000..39ba3c8 --- /dev/null +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/WebhookRegistry.java @@ -0,0 +1,36 @@ +package fi.fabianadrian.webhooklogger.common.client; + +import fi.fabianadrian.webhooklogger.common.event.EventType; + +import java.util.*; + +public final class WebhookRegistry { + private final Map> eventClientMap = new EnumMap<>(EventType.class); + private final List clients = new ArrayList<>(); + + public void register(DiscordClient client, List events) { + this.clients.add(client); + events.forEach(event -> { + List clients = eventClientMap.getOrDefault(event, new ArrayList<>()); + clients.add(client); + this.eventClientMap.put(event, clients); + }); + } + + public void clear() { + this.clients.clear(); + this.eventClientMap.clear(); + } + + public List forEventType(EventType type) { + return this.eventClientMap.get(type); + } + + public List webhooks() { + return List.copyOf(this.clients); + } + + public Set registeredEventTypes() { + return Set.copyOf(this.eventClientMap.keySet()); + } +} diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java index 95a9427..1936900 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java @@ -7,11 +7,23 @@ import space.arim.dazzleconf.sorter.AnnotationBasedSorter; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.regex.Pattern; public interface MainConfig { - static Map defaultWebhooks() { - return Map.of("default", ""); + static List defaultWebhooks() { + return List.of(new Webhook() { + @Override + public String url() { + return ""; + } + + @Override + public Pattern regex() { + return Pattern.compile("chat"); + } + }); } static Map defaultTextReplacements() { @@ -28,7 +40,7 @@ static Map defaultTextReplacements() { "If no webhook is defined for a specific event the \"default\" webhook will be used instead.", "Available events can be found in the events.yml file." }) - Map webhooks(); + List webhooks(); @AnnotationBasedSorter.Order(1) @ConfComments({ diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/Webhook.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/Webhook.java new file mode 100644 index 0000000..d2fe69c --- /dev/null +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/Webhook.java @@ -0,0 +1,16 @@ +package fi.fabianadrian.webhooklogger.common.config; + +import space.arim.dazzleconf.annote.ConfDefault; +import space.arim.dazzleconf.sorter.AnnotationBasedSorter; + +import java.util.regex.Pattern; + +public interface Webhook { + @AnnotationBasedSorter.Order(0) + @ConfDefault.DefaultString("") + String url(); + + @AnnotationBasedSorter.Order(1) + @ConfDefault.DefaultStrings({}) + Pattern regex(); +} diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/CancellableEventConfig.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/CancellableEventConfig.java new file mode 100644 index 0000000..4ea1a3f --- /dev/null +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/CancellableEventConfig.java @@ -0,0 +1,12 @@ +package fi.fabianadrian.webhooklogger.common.config.event; + +import space.arim.dazzleconf.annote.ConfComments; +import space.arim.dazzleconf.annote.ConfDefault; +import space.arim.dazzleconf.sorter.AnnotationBasedSorter; + +public interface CancellableEventConfig { + @AnnotationBasedSorter.Order(1) + @ConfDefault.DefaultBoolean(true) + @ConfComments("Whether this event should be logged even when cancelled.") + boolean logCancelled(); +} diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/ChatEventConfig.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/ChatEventConfig.java index cd203b3..7dc3b93 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/ChatEventConfig.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/ChatEventConfig.java @@ -4,22 +4,12 @@ import space.arim.dazzleconf.annote.ConfDefault; import space.arim.dazzleconf.sorter.AnnotationBasedSorter; -public interface ChatEventConfig { +public interface ChatEventConfig extends CancellableEventConfig { @AnnotationBasedSorter.Order(0) - @ConfDefault.DefaultBoolean(true) - @ConfComments("Whether chat messages will be logged.") - boolean enabled(); - - @AnnotationBasedSorter.Order(1) @ConfDefault.DefaultString("[] : ") @ConfComments({ "The webhook format for chat messages. Available placeholders:", ", , , , " }) String format(); - - @AnnotationBasedSorter.Order(2) - @ConfDefault.DefaultBoolean(true) - @ConfComments("Whether cancelled chat messages will be logged.") - boolean logCancelled(); } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/CommandEventConfig.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/CommandEventConfig.java index 036effd..f1cb947 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/CommandEventConfig.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/CommandEventConfig.java @@ -4,13 +4,8 @@ import space.arim.dazzleconf.annote.ConfDefault; import space.arim.dazzleconf.sorter.AnnotationBasedSorter; -public interface CommandEventConfig { +public interface CommandEventConfig extends CancellableEventConfig { @AnnotationBasedSorter.Order(0) - @ConfDefault.DefaultBoolean(false) - @ConfComments("Whether commands will be logged.") - boolean enabled(); - - @AnnotationBasedSorter.Order(1) @ConfDefault.DefaultString("[] : ") @ConfComments({ "The webhook format for executed commands. Available placeholders:", @@ -19,16 +14,11 @@ public interface CommandEventConfig { String format(); @AnnotationBasedSorter.Order(2) - @ConfDefault.DefaultBoolean(true) - @ConfComments("Whether cancelled commands will be logged.") - boolean logCancelled(); - - @AnnotationBasedSorter.Order(3) @ConfDefault.DefaultBoolean(false) @ConfComments("Whether console commands will be logged.") boolean logConsole(); - @AnnotationBasedSorter.Order(4) + @AnnotationBasedSorter.Order(3) @ConfDefault.DefaultBoolean(false) @ConfComments("Whether other entities commands will be logged.") boolean logOther(); diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/DeathEventConfig.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/DeathEventConfig.java index ab91a02..58943c3 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/DeathEventConfig.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/DeathEventConfig.java @@ -4,22 +4,12 @@ import space.arim.dazzleconf.annote.ConfDefault; import space.arim.dazzleconf.sorter.AnnotationBasedSorter; -public interface DeathEventConfig { +public interface DeathEventConfig extends CancellableEventConfig { @AnnotationBasedSorter.Order(0) - @ConfDefault.DefaultBoolean(false) - @ConfComments("Whether player deaths will be logged.") - boolean enabled(); - - @AnnotationBasedSorter.Order(1) @ConfDefault.DefaultString("[] ()") @ConfComments({ "The webhook format for when a player dies. Available placeholders:", ", , , , , " }) String format(); - - @AnnotationBasedSorter.Order(2) - @ConfDefault.DefaultBoolean(false) - @ConfComments("Whether cancelled death events will be logged.") - boolean logCancelled(); } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/JoinQuitEventConfig.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/JoinQuitEventConfig.java index adeb76a..b2a5dde 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/JoinQuitEventConfig.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/event/JoinQuitEventConfig.java @@ -6,11 +6,6 @@ public interface JoinQuitEventConfig { @AnnotationBasedSorter.Order(0) - @ConfDefault.DefaultBoolean(false) - @ConfComments("Whether player joins and quits will be logged.") - boolean enabled(); - - @AnnotationBasedSorter.Order(1) @ConfDefault.DefaultString("[] ") @ConfComments({ "The webhook format for when a player joins or quits. Available placeholders:", diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Platform.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Platform.java index c9258f9..9abfefe 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Platform.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Platform.java @@ -10,4 +10,5 @@ public interface Platform { Logger logger(); Path configPath(); CommandManager commandManager(); + void registerListeners(); } diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java index 725cd76..be5bcd6 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java @@ -5,12 +5,10 @@ import fi.fabianadrian.webhooklogger.common.dependency.Dependency; import fi.fabianadrian.webhooklogger.common.platform.Platform; import fi.fabianadrian.webhooklogger.paper.command.PaperCommander; -import fi.fabianadrian.webhooklogger.paper.listener.ChatListener; -import fi.fabianadrian.webhooklogger.paper.listener.CommandListener; -import fi.fabianadrian.webhooklogger.paper.listener.DeathListener; -import fi.fabianadrian.webhooklogger.paper.listener.JoinQuitListener; +import fi.fabianadrian.webhooklogger.paper.listener.*; import io.papermc.paper.command.brigadier.CommandSourceStack; import org.bstats.bukkit.Metrics; +import org.bukkit.event.HandlerList; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.incendo.cloud.CommandManager; @@ -20,7 +18,6 @@ import org.slf4j.Logger; import java.nio.file.Path; -import java.util.stream.Stream; public final class WebhookLoggerPaper extends JavaPlugin implements Platform { private WebhookLogger webhookLogger; @@ -36,7 +33,6 @@ public void onEnable() { this.webhookLogger.dependencyManager().markAsPresent(Dependency.MINI_PLACEHOLDERS); } - registerListeners(); // bStats @@ -52,14 +48,13 @@ public WebhookLogger webhookLogger() { return this.webhookLogger; } - private void registerListeners() { - PluginManager manager = getServer().getPluginManager(); - Stream.of( - new ChatListener(this), - new CommandListener(this), - new DeathListener(this), - new JoinQuitListener(this) - ).forEach(listener -> manager.registerEvents(listener, this)); + @Override + public void registerListeners() { + // Unregister any existing listeners + HandlerList.unregisterAll(this); + + ListenerFactory factory = new ListenerFactory(this); + this.webhookLogger.clientManager().registry().registeredEventTypes().forEach(factory::register); } @Override diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/ChatListener.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/ChatListener.java index 685704e..ede7f71 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/ChatListener.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/ChatListener.java @@ -20,7 +20,7 @@ public ChatListener(WebhookLoggerPaper plugin) { public void onChat(AsyncChatEvent event) { ChatEventConfig config = this.webhookLogger.eventsConfig().chat(); - if (!config.enabled() || !config.logCancelled() && event.isCancelled()) { + if (!config.logCancelled() && event.isCancelled()) { return; } diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/CommandListener.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/CommandListener.java index 0625ca1..d33a40a 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/CommandListener.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/CommandListener.java @@ -21,8 +21,7 @@ public CommandListener(WebhookLoggerPaper plugin) { @EventHandler public void onServerCommand(ServerCommandEvent event) { CommandEventConfig config = this.webhookLogger.eventsConfig().command(); - - if (!config.enabled() || !config.logCancelled() && event.isCancelled()) { + if (!config.logCancelled() && event.isCancelled()) { return; } @@ -44,8 +43,7 @@ public void onServerCommand(ServerCommandEvent event) { @EventHandler public void onPlayerCommand(PlayerCommandPreprocessEvent event) { CommandEventConfig config = this.webhookLogger.eventsConfig().command(); - - if (!config.enabled() || !config.logCancelled() && event.isCancelled()) { + if (!config.logCancelled() && event.isCancelled()) { return; } diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/DeathListener.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/DeathListener.java index b002b9b..c75a870 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/DeathListener.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/DeathListener.java @@ -21,7 +21,7 @@ public DeathListener(WebhookLoggerPaper plugin) { public void onDeath(PlayerDeathEvent event) { DeathEventConfig config = this.webhookLogger.eventsConfig().death(); - if (!config.enabled() || !config.logCancelled() && event.isCancelled()) { + if (!config.logCancelled() && event.isCancelled()) { return; } diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/JoinQuitListener.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/JoinQuitListener.java index 2c1e5eb..7ae96fd 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/JoinQuitListener.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/JoinQuitListener.java @@ -1,7 +1,6 @@ package fi.fabianadrian.webhooklogger.paper.listener; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.config.event.JoinQuitEventConfig; import fi.fabianadrian.webhooklogger.common.event.EventBuilder; import fi.fabianadrian.webhooklogger.common.event.JoinQuitEventBuilder; import fi.fabianadrian.webhooklogger.paper.WebhookLoggerPaper; @@ -20,11 +19,6 @@ public JoinQuitListener(WebhookLoggerPaper plugin) { @EventHandler public void onJoin(PlayerJoinEvent event) { - JoinQuitEventConfig config = this.webhookLogger.eventsConfig().joinQuit(); - if (!config.enabled()) { - return; - } - Location loc = event.getPlayer().getLocation(); EventBuilder builder = new JoinQuitEventBuilder(this.webhookLogger) .audience(event.getPlayer()) @@ -36,11 +30,6 @@ public void onJoin(PlayerJoinEvent event) { @EventHandler public void onQuit(PlayerQuitEvent event) { - JoinQuitEventConfig config = this.webhookLogger.eventsConfig().joinQuit(); - if (!config.enabled()) { - return; - } - Location loc = event.getPlayer().getLocation(); EventBuilder builder = new JoinQuitEventBuilder(this.webhookLogger) diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/ListenerFactory.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/ListenerFactory.java new file mode 100644 index 0000000..0e61301 --- /dev/null +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/ListenerFactory.java @@ -0,0 +1,36 @@ +package fi.fabianadrian.webhooklogger.paper.listener; + +import fi.fabianadrian.webhooklogger.common.event.EventType; +import fi.fabianadrian.webhooklogger.paper.WebhookLoggerPaper; +import org.bukkit.event.Listener; + +public final class ListenerFactory { + private final WebhookLoggerPaper plugin; + + public ListenerFactory(WebhookLoggerPaper plugin) { + this.plugin = plugin; + } + + public void register(EventType type) { + Listener listener = create(type); + this.plugin.getServer().getPluginManager().registerEvents(listener, this.plugin); + } + + private Listener create(EventType type) { + switch (type) { + case CHAT -> { + return new ChatListener(this.plugin); + } + case COMMAND -> { + return new CommandListener(this.plugin); + } + case DEATH -> { + return new DeathListener(this.plugin); + } + case JOINQUIT -> { + return new JoinQuitListener(this.plugin); + } + } + throw new IllegalStateException("Unknown EventType"); + } +} diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/WebhookLoggerSponge.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/WebhookLoggerSponge.java index 2a39835..a028bb4 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/WebhookLoggerSponge.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/WebhookLoggerSponge.java @@ -5,8 +5,7 @@ import fi.fabianadrian.webhooklogger.common.command.Commander; import fi.fabianadrian.webhooklogger.common.dependency.Dependency; import fi.fabianadrian.webhooklogger.common.platform.Platform; -import fi.fabianadrian.webhooklogger.sponge.listener.ChatListener; -import fi.fabianadrian.webhooklogger.sponge.listener.CommandListener; +import fi.fabianadrian.webhooklogger.sponge.listener.ListenerFactory; import org.bstats.sponge.Metrics; import org.incendo.cloud.CommandManager; import org.slf4j.Logger; @@ -14,7 +13,6 @@ import org.spongepowered.api.Server; import org.spongepowered.api.Sponge; import org.spongepowered.api.config.ConfigDir; -import org.spongepowered.api.event.EventManager; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.lifecycle.StartedEngineEvent; import org.spongepowered.api.event.lifecycle.StoppingEngineEvent; @@ -22,7 +20,6 @@ import org.spongepowered.plugin.builtin.jvm.Plugin; import java.nio.file.Path; -import java.util.List; @Plugin("webhooklogger") public final class WebhookLoggerSponge implements Platform { @@ -62,12 +59,12 @@ public WebhookLogger webhookLogger() { return this.webhookLogger; } - private void registerListeners() { - EventManager manager = Sponge.eventManager(); - List.of( - new ChatListener(this), - new CommandListener(this) - ).forEach(listener -> manager.registerListeners(this.container, listener)); + @Override + public void registerListeners() { + //TODO Unregister listeners here + + ListenerFactory factory = new ListenerFactory(this.container, this.webhookLogger); + this.webhookLogger.clientManager().registry().registeredEventTypes().forEach(factory::register); } @Override diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/ChatListener.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/ChatListener.java index 44d54b9..45f1b8b 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/ChatListener.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/ChatListener.java @@ -4,7 +4,6 @@ import fi.fabianadrian.webhooklogger.common.config.event.ChatEventConfig; import fi.fabianadrian.webhooklogger.common.event.ChatEventBuilder; import fi.fabianadrian.webhooklogger.common.event.EventBuilder; -import fi.fabianadrian.webhooklogger.sponge.WebhookLoggerSponge; import org.spongepowered.api.entity.living.player.server.ServerPlayer; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.filter.IsCancelled; @@ -16,8 +15,8 @@ public final class ChatListener { private final WebhookLogger webhookLogger; - public ChatListener(WebhookLoggerSponge plugin) { - this.webhookLogger = plugin.webhookLogger(); + public ChatListener(WebhookLogger webhookLogger) { + this.webhookLogger = webhookLogger; } @Listener @@ -25,7 +24,7 @@ public ChatListener(WebhookLoggerSponge plugin) { public void onChat(PlayerChatEvent.Submit event) { ChatEventConfig config = this.webhookLogger.eventsConfig().chat(); - if (!config.enabled() || !config.logCancelled() && event.isCancelled()) { + if (!config.logCancelled() && event.isCancelled()) { return; } diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/CommandListener.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/CommandListener.java index 09e0153..3686918 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/CommandListener.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/CommandListener.java @@ -4,7 +4,6 @@ import fi.fabianadrian.webhooklogger.common.config.event.CommandEventConfig; import fi.fabianadrian.webhooklogger.common.event.CommandEventBuilder; import fi.fabianadrian.webhooklogger.common.event.EventBuilder; -import fi.fabianadrian.webhooklogger.sponge.WebhookLoggerSponge; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.command.ExecuteCommandEvent; @@ -16,8 +15,8 @@ public final class CommandListener { private final WebhookLogger webhookLogger; - public CommandListener(WebhookLoggerSponge plugin) { - this.webhookLogger = plugin.webhookLogger(); + public CommandListener(WebhookLogger webhookLogger) { + this.webhookLogger = webhookLogger; } @Listener @@ -25,7 +24,7 @@ public CommandListener(WebhookLoggerSponge plugin) { public void onCommand(ExecuteCommandEvent.Pre event) { CommandEventConfig config = this.webhookLogger.eventsConfig().command(); - if (!config.enabled() || !config.logCancelled() && event.isCancelled()) { + if (!config.logCancelled() && event.isCancelled()) { return; } diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/DeathListener.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/DeathListener.java index 20746f4..f5cd619 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/DeathListener.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/DeathListener.java @@ -26,7 +26,7 @@ public void onDeath(DestructEntityEvent.Death event) { } DeathEventConfig config = this.webhookLogger.eventsConfig().death(); - if (!config.enabled() || !config.logCancelled() && event.isCancelled()) { + if (!config.logCancelled() && event.isCancelled()) { return; } diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/JoinQuitListener.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/JoinQuitListener.java index e5b35f1..86e40c5 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/JoinQuitListener.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/JoinQuitListener.java @@ -1,7 +1,6 @@ package fi.fabianadrian.webhooklogger.sponge.listener; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.config.event.JoinQuitEventConfig; import fi.fabianadrian.webhooklogger.common.event.EventBuilder; import fi.fabianadrian.webhooklogger.common.event.JoinQuitEventBuilder; import org.spongepowered.api.event.Listener; @@ -17,11 +16,6 @@ public JoinQuitListener(WebhookLogger webhookLogger) { @Listener public void onJoin(ServerSideConnectionEvent.Join event) { - JoinQuitEventConfig config = this.webhookLogger.eventsConfig().joinQuit(); - if (!config.enabled()) { - return; - } - Location loc = event.player().location(); EventBuilder builder = new JoinQuitEventBuilder(this.webhookLogger) @@ -34,11 +28,6 @@ public void onJoin(ServerSideConnectionEvent.Join event) { @Listener public void onQuit(ServerSideConnectionEvent.Disconnect event) { - JoinQuitEventConfig config = this.webhookLogger.eventsConfig().joinQuit(); - if (!config.enabled()) { - return; - } - Location loc = event.player().location(); EventBuilder builder = new JoinQuitEventBuilder(this.webhookLogger) diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/ListenerFactory.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/ListenerFactory.java new file mode 100644 index 0000000..2c44783 --- /dev/null +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/ListenerFactory.java @@ -0,0 +1,39 @@ +package fi.fabianadrian.webhooklogger.sponge.listener; + +import fi.fabianadrian.webhooklogger.common.WebhookLogger; +import fi.fabianadrian.webhooklogger.common.event.EventType; +import org.spongepowered.api.Sponge; +import org.spongepowered.plugin.PluginContainer; + +public final class ListenerFactory { + private final PluginContainer container; + private final WebhookLogger webhookLogger; + + public ListenerFactory(PluginContainer container, WebhookLogger webhookLogger) { + this.container = container; + this.webhookLogger = webhookLogger; + } + + public void register(EventType type) { + Object listener = create(type); + Sponge.eventManager().registerListeners(this.container, listener); + } + + private Object create(EventType type) { + switch (type) { + case CHAT -> { + return new ChatListener(this.webhookLogger); + } + case COMMAND -> { + return new CommandListener(this.webhookLogger); + } + case DEATH -> { + return new DeathListener(this.webhookLogger); + } + case JOINQUIT -> { + return new JoinQuitListener(this.webhookLogger); + } + } + throw new IllegalStateException("Unknown EventType"); + } +} From a0a0003adb47bb3b1b882e7313a5fdf8e2d7f1ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 05:33:54 +0300 Subject: [PATCH 02/18] actually no need for regex --- .../webhooklogger/common/client/ClientManager.java | 13 +------------ .../webhooklogger/common/config/MainConfig.java | 6 +++--- .../webhooklogger/common/config/Webhook.java | 5 +++-- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/ClientManager.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/ClientManager.java index 8a5b66f..2aacb16 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/ClientManager.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/ClientManager.java @@ -4,15 +4,12 @@ import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.config.MainConfig; import fi.fabianadrian.webhooklogger.common.event.EventBuilder; -import fi.fabianadrian.webhooklogger.common.event.EventType; import org.slf4j.Logger; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; -import java.util.regex.Matcher; public final class ClientManager { private final WebhookLogger webhookLogger; @@ -71,15 +68,7 @@ private void parseWebhooks() { return; } - List events = new ArrayList<>(); - for (EventType event : EventType.values()) { - Matcher matcher = webhook.regex().matcher(event.name()); - if (matcher.find()) { - events.add(event); - } - } - - this.registry.register(new DiscordClient(logger, webhook.url()), events); + this.registry.register(new DiscordClient(logger, webhook.url()), webhook.events()); }); } } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java index 1936900..22950ef 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java @@ -1,6 +1,7 @@ package fi.fabianadrian.webhooklogger.common.config; import fi.fabianadrian.webhooklogger.common.config.section.PlaceholderConfigSection; +import fi.fabianadrian.webhooklogger.common.event.EventType; import space.arim.dazzleconf.annote.ConfComments; import space.arim.dazzleconf.annote.ConfDefault; import space.arim.dazzleconf.annote.SubSection; @@ -9,7 +10,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.regex.Pattern; public interface MainConfig { static List defaultWebhooks() { @@ -20,8 +20,8 @@ public String url() { } @Override - public Pattern regex() { - return Pattern.compile("chat"); + public List events() { + return List.of(EventType.CHAT); } }); } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/Webhook.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/Webhook.java index d2fe69c..ddbcd15 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/Webhook.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/Webhook.java @@ -1,9 +1,10 @@ package fi.fabianadrian.webhooklogger.common.config; +import fi.fabianadrian.webhooklogger.common.event.EventType; import space.arim.dazzleconf.annote.ConfDefault; import space.arim.dazzleconf.sorter.AnnotationBasedSorter; -import java.util.regex.Pattern; +import java.util.List; public interface Webhook { @AnnotationBasedSorter.Order(0) @@ -12,5 +13,5 @@ public interface Webhook { @AnnotationBasedSorter.Order(1) @ConfDefault.DefaultStrings({}) - Pattern regex(); + List events(); } From 36e29e0ea838134b49587c6b7a2a1a66c08f12ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 05:46:18 +0300 Subject: [PATCH 03/18] fixed plugin startup --- .../webhooklogger/common/WebhookLogger.java | 4 ++-- .../webhooklogger/common/config/MainConfig.java | 12 +++++++++++- .../webhooklogger/common/config/Webhook.java | 17 ----------------- 3 files changed, 13 insertions(+), 20 deletions(-) delete mode 100644 common/src/main/java/fi/fabianadrian/webhooklogger/common/config/Webhook.java diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java index 2e33933..76aa723 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java @@ -35,14 +35,14 @@ public WebhookLogger(Platform platform) { new TranslationManager(platform.logger()); this.configManager = new ConfigManager(platform.configPath(), platform.logger()); + this.configManager.reload(); this.commandManager = platform.commandManager(); setupCommandManager(); registerCommands(); this.clientManager = new ClientManager(this); - - reload(); + this.clientManager.reload(); } public boolean reload() { diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java index 22950ef..b148f75 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java @@ -40,7 +40,7 @@ static Map defaultTextReplacements() { "If no webhook is defined for a specific event the \"default\" webhook will be used instead.", "Available events can be found in the events.yml file." }) - List webhooks(); + List<@SubSection Webhook> webhooks(); @AnnotationBasedSorter.Order(1) @ConfComments({ @@ -63,4 +63,14 @@ static Map defaultTextReplacements() { @ConfComments("Configuration options for various placeholders.") @SubSection PlaceholderConfigSection placeholders(); + + interface Webhook { + @AnnotationBasedSorter.Order(0) + @ConfDefault.DefaultString("") + String url(); + + @AnnotationBasedSorter.Order(1) + @ConfDefault.DefaultStrings({}) + List events(); + } } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/Webhook.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/Webhook.java deleted file mode 100644 index ddbcd15..0000000 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/Webhook.java +++ /dev/null @@ -1,17 +0,0 @@ -package fi.fabianadrian.webhooklogger.common.config; - -import fi.fabianadrian.webhooklogger.common.event.EventType; -import space.arim.dazzleconf.annote.ConfDefault; -import space.arim.dazzleconf.sorter.AnnotationBasedSorter; - -import java.util.List; - -public interface Webhook { - @AnnotationBasedSorter.Order(0) - @ConfDefault.DefaultString("") - String url(); - - @AnnotationBasedSorter.Order(1) - @ConfDefault.DefaultStrings({}) - List events(); -} From a21b5f4f9b103cd04856e695b76c56826619ef38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 06:13:32 +0300 Subject: [PATCH 04/18] clean up config reloading --- .../common/config/ConfigLoadException.java | 11 ---------- .../common/config/ConfigManager.java | 21 ++++--------------- 2 files changed, 4 insertions(+), 28 deletions(-) delete mode 100644 common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigLoadException.java diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigLoadException.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigLoadException.java deleted file mode 100644 index cec64f4..0000000 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigLoadException.java +++ /dev/null @@ -1,11 +0,0 @@ -package fi.fabianadrian.webhooklogger.common.config; - -public class ConfigLoadException extends Exception { - public ConfigLoadException(String message, Throwable cause) { - super(message, cause); - } - - public ConfigLoadException(Throwable cause) { - super(cause); - } -} diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigManager.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigManager.java index 7392167..713e984 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigManager.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigManager.java @@ -1,11 +1,8 @@ package fi.fabianadrian.webhooklogger.common.config; import org.slf4j.Logger; -import space.arim.dazzleconf.error.ConfigFormatSyntaxException; -import space.arim.dazzleconf.error.InvalidConfigException; import space.arim.dazzleconf.helper.ConfigurationHelper; -import java.io.IOException; import java.nio.file.Path; public final class ConfigManager { @@ -43,31 +40,21 @@ public EventsConfig eventsConfig() { public boolean reload() { boolean success = true; try { - this.mainConfigData = loadConfig(this.mainConfigHelper); + this.mainConfigData = this.mainConfigHelper.reloadConfigData(); } catch (Exception e) { this.logger.error("Could not load config.yml", e); + this.mainConfigData = this.mainConfigHelper.getFactory().loadDefaults(); success = false; } try { - this.eventsConfigData = loadConfig(this.eventsConfigHelper); + this.eventsConfigData = this.eventsConfigHelper.reloadConfigData(); } catch (Exception e) { this.logger.error("Could not load events.yml", e); + this.eventsConfigData = this.eventsConfigHelper.getFactory().loadDefaults(); success = false; } return success; } - - private C loadConfig(ConfigurationHelper configHelper) throws ConfigLoadException { - try { - return configHelper.reloadConfigData(); - } catch (IOException e) { - throw new ConfigLoadException(e); - } catch (ConfigFormatSyntaxException e) { - throw new ConfigLoadException("The YAML syntax in your configuration is invalid. Check your YAML syntax with a tool such as https://yaml-online-parser.appspot.com/", e); - } catch (InvalidConfigException e) { - throw new ConfigLoadException("One of the values in your configuration is not valid. Check to make sure you have specified the right data types.", e); - } - } } From b28a20f7e95b511d499672caa231534cd5963954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 06:16:48 +0300 Subject: [PATCH 05/18] improve log messages --- .../webhooklogger/common/config/ConfigManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigManager.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigManager.java index 713e984..218fe47 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigManager.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigManager.java @@ -42,7 +42,7 @@ public boolean reload() { try { this.mainConfigData = this.mainConfigHelper.reloadConfigData(); } catch (Exception e) { - this.logger.error("Could not load config.yml", e); + this.logger.error("Could not load config.yml, falling back to default configuration", e); this.mainConfigData = this.mainConfigHelper.getFactory().loadDefaults(); success = false; } @@ -50,7 +50,7 @@ public boolean reload() { try { this.eventsConfigData = this.eventsConfigHelper.reloadConfigData(); } catch (Exception e) { - this.logger.error("Could not load events.yml", e); + this.logger.error("Could not load events.yml, falling back to default configuration", e); this.eventsConfigData = this.eventsConfigHelper.getFactory().loadDefaults(); success = false; } From 579c733d004c054cc5442d1596bc1c22f15ebbbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 06:24:57 +0300 Subject: [PATCH 06/18] cleanup command class names --- .../fabianadrian/webhooklogger/common/WebhookLogger.java | 8 ++++---- .../{WebhookLoggerCommand.java => BaseCommand.java} | 7 +++---- ...kLoggerCaptionFormatter.java => CaptionFormatter.java} | 2 +- .../common/command/commands/ReloadCommand.java | 4 ++-- 4 files changed, 10 insertions(+), 11 deletions(-) rename common/src/main/java/fi/fabianadrian/webhooklogger/common/command/{WebhookLoggerCommand.java => BaseCommand.java} (69%) rename common/src/main/java/fi/fabianadrian/webhooklogger/common/command/{WebhookLoggerCaptionFormatter.java => CaptionFormatter.java} (91%) diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java index 76aa723..a2a9fe2 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java @@ -2,8 +2,8 @@ import fi.fabianadrian.webhooklogger.common.client.ClientManager; import fi.fabianadrian.webhooklogger.common.command.Commander; -import fi.fabianadrian.webhooklogger.common.command.WebhookLoggerCaptionFormatter; -import fi.fabianadrian.webhooklogger.common.command.WebhookLoggerCommand; +import fi.fabianadrian.webhooklogger.common.command.CaptionFormatter; +import fi.fabianadrian.webhooklogger.common.command.BaseCommand; import fi.fabianadrian.webhooklogger.common.command.commands.ReloadCommand; import fi.fabianadrian.webhooklogger.common.command.processor.WebhookLoggerCommandPreprocessor; import fi.fabianadrian.webhooklogger.common.config.ConfigManager; @@ -91,13 +91,13 @@ private void setupCommandManager() { this.commandManager.captionRegistry().registerProvider(TranslatableCaption.translatableCaptionProvider()); MinecraftExceptionHandler.createNative() .defaultHandlers() - .captionFormatter(new WebhookLoggerCaptionFormatter()) + .captionFormatter(new CaptionFormatter()) .registerTo(this.commandManager); } private void registerCommands() { List.of( new ReloadCommand(this) - ).forEach(WebhookLoggerCommand::register); + ).forEach(BaseCommand::register); } } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/WebhookLoggerCommand.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/BaseCommand.java similarity index 69% rename from common/src/main/java/fi/fabianadrian/webhooklogger/common/command/WebhookLoggerCommand.java rename to common/src/main/java/fi/fabianadrian/webhooklogger/common/command/BaseCommand.java index 7073b0a..57ef834 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/WebhookLoggerCommand.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/BaseCommand.java @@ -1,19 +1,18 @@ package fi.fabianadrian.webhooklogger.common.command; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import org.incendo.cloud.Command; import org.incendo.cloud.CommandManager; -public abstract class WebhookLoggerCommand { +public abstract class BaseCommand { protected final WebhookLogger webhookLogger; protected final CommandManager manager; - public WebhookLoggerCommand(WebhookLogger webhookLogger) { + public BaseCommand(WebhookLogger webhookLogger) { this.webhookLogger = webhookLogger; this.manager = webhookLogger.commandManager(); } - protected Command.Builder rootBuilder() { + protected org.incendo.cloud.Command.Builder rootBuilder() { return this.manager.commandBuilder("webhooklogger"); } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/WebhookLoggerCaptionFormatter.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/CaptionFormatter.java similarity index 91% rename from common/src/main/java/fi/fabianadrian/webhooklogger/common/command/WebhookLoggerCaptionFormatter.java rename to common/src/main/java/fi/fabianadrian/webhooklogger/common/command/CaptionFormatter.java index 4a0c0c7..e77225e 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/WebhookLoggerCaptionFormatter.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/CaptionFormatter.java @@ -13,7 +13,7 @@ import static net.kyori.adventure.text.Component.text; import static net.kyori.adventure.text.Component.translatable; -public final class WebhookLoggerCaptionFormatter implements ComponentCaptionFormatter { +public final class CaptionFormatter implements ComponentCaptionFormatter { @Override public @NonNull Component formatCaption(@NonNull Caption captionKey, @NonNull Commander recipient, @NonNull String caption, @NonNull List<@NonNull CaptionVariable> variables) { return translatable("cloud." + captionKey.key(), variables.stream().map(variable -> { diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/commands/ReloadCommand.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/commands/ReloadCommand.java index 68ed352..8ca1b17 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/commands/ReloadCommand.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/commands/ReloadCommand.java @@ -2,14 +2,14 @@ import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.command.Commander; -import fi.fabianadrian.webhooklogger.common.command.WebhookLoggerCommand; +import fi.fabianadrian.webhooklogger.common.command.BaseCommand; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.incendo.cloud.context.CommandContext; import static net.kyori.adventure.text.Component.translatable; -public final class ReloadCommand extends WebhookLoggerCommand { +public final class ReloadCommand extends BaseCommand { private static final Component COMPONENT_SUCCESS = translatable() .key("webhooklogger.command.reload.success") .color(NamedTextColor.GREEN) From 3f62d46224f75d2c4194a84893d0d3bf87028dd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 06:35:23 +0300 Subject: [PATCH 07/18] Compile replacement pattern when serializing the config --- .../common/client/ClientManager.java | 7 +++-- .../config/ConfigurationHelperFactory.java | 2 ++ .../common/config/MainConfig.java | 3 ++- .../config/serializer/PatternSerializer.java | 26 +++++++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 common/src/main/java/fi/fabianadrian/webhooklogger/common/config/serializer/PatternSerializer.java diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/ClientManager.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/ClientManager.java index 2aacb16..8fa0d89 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/ClientManager.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/ClientManager.java @@ -10,6 +10,8 @@ import java.util.Map; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public final class ClientManager { private final WebhookLogger webhookLogger; @@ -28,8 +30,9 @@ public void send(EventBuilder eventBuilder) { } String discordSerialized = DiscordSerializer.INSTANCE.serialize(eventBuilder.component()); - for (Map.Entry entry : this.config.textReplacements().entrySet()) { - discordSerialized = discordSerialized.replaceAll(entry.getKey(), entry.getValue()); + for (Map.Entry entry : this.config.textReplacements().entrySet()) { + Matcher matcher = entry.getKey().matcher(discordSerialized); + discordSerialized = matcher.replaceAll(entry.getValue()); } //TODO Fix ugly diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigurationHelperFactory.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigurationHelperFactory.java index 9f07a02..d85824a 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigurationHelperFactory.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/ConfigurationHelperFactory.java @@ -1,5 +1,6 @@ package fi.fabianadrian.webhooklogger.common.config; +import fi.fabianadrian.webhooklogger.common.config.serializer.PatternSerializer; import fi.fabianadrian.webhooklogger.common.config.serializer.ZoneIdSerializer; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @@ -27,6 +28,7 @@ public final class ConfigurationHelperFactory { .build(); private final ConfigurationOptions options = new ConfigurationOptions.Builder() .addSerialiser(new ZoneIdSerializer()) + .addSerialiser(new PatternSerializer()) .sorter(new AnnotationBasedSorter()) .build(); diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java index b148f75..5a7c236 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java @@ -10,6 +10,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; public interface MainConfig { static List defaultWebhooks() { @@ -57,7 +58,7 @@ static Map defaultTextReplacements() { "You can use this to filter or replace text.", "Supports regex." }) - Map textReplacements(); + Map textReplacements(); @AnnotationBasedSorter.Order(3) @ConfComments("Configuration options for various placeholders.") diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/serializer/PatternSerializer.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/serializer/PatternSerializer.java new file mode 100644 index 0000000..e8810d2 --- /dev/null +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/serializer/PatternSerializer.java @@ -0,0 +1,26 @@ +package fi.fabianadrian.webhooklogger.common.config.serializer; + +import space.arim.dazzleconf.error.BadValueException; +import space.arim.dazzleconf.serialiser.Decomposer; +import space.arim.dazzleconf.serialiser.FlexibleType; +import space.arim.dazzleconf.serialiser.ValueSerialiser; + +import java.util.regex.Pattern; + +public class PatternSerializer implements ValueSerialiser { + @Override + public Class getTargetClass() { + return Pattern.class; + } + + @Override + public Pattern deserialise(FlexibleType flexibleType) throws BadValueException { + String string = flexibleType.getString(); + return Pattern.compile(string); + } + + @Override + public Object serialise(Pattern value, Decomposer decomposer) { + return value.pattern(); + } +} From 137342ed353a187120104fedc7cf4c87177901bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 06:35:43 +0300 Subject: [PATCH 08/18] final --- .../common/config/serializer/PatternSerializer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/serializer/PatternSerializer.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/serializer/PatternSerializer.java index e8810d2..a193d3d 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/serializer/PatternSerializer.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/serializer/PatternSerializer.java @@ -7,7 +7,7 @@ import java.util.regex.Pattern; -public class PatternSerializer implements ValueSerialiser { +public final class PatternSerializer implements ValueSerialiser { @Override public Class getTargetClass() { return Pattern.class; From bc3c742996e14f36ccd309aff075aedc371c4157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 06:55:35 +0300 Subject: [PATCH 09/18] simplify command manager --- .../webhooklogger/common/WebhookLogger.java | 8 +++---- .../common/command/BaseCommand.java | 5 ++-- .../common/command/CaptionFormatter.java | 5 ++-- .../common/command/Commander.java | 6 ----- .../command/commands/ReloadCommand.java | 4 ++-- .../WebhookLoggerCommandPreprocessor.java | 8 +++---- .../common/platform/Platform.java | 4 ++-- .../paper/WebhookLoggerPaper.java | 24 ++++++++----------- .../paper/command/PaperCommander.java | 16 ------------- .../sponge/WebhookLoggerSponge.java | 6 ++--- 10 files changed, 31 insertions(+), 55 deletions(-) delete mode 100644 common/src/main/java/fi/fabianadrian/webhooklogger/common/command/Commander.java delete mode 100644 paper/src/main/java/fi/fabianadrian/webhooklogger/paper/command/PaperCommander.java diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java index a2a9fe2..c1cf1b1 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java @@ -1,7 +1,6 @@ package fi.fabianadrian.webhooklogger.common; import fi.fabianadrian.webhooklogger.common.client.ClientManager; -import fi.fabianadrian.webhooklogger.common.command.Commander; import fi.fabianadrian.webhooklogger.common.command.CaptionFormatter; import fi.fabianadrian.webhooklogger.common.command.BaseCommand; import fi.fabianadrian.webhooklogger.common.command.commands.ReloadCommand; @@ -12,6 +11,7 @@ import fi.fabianadrian.webhooklogger.common.dependency.DependencyManager; import fi.fabianadrian.webhooklogger.common.locale.TranslationManager; import fi.fabianadrian.webhooklogger.common.platform.Platform; +import net.kyori.adventure.audience.Audience; import org.incendo.cloud.CommandManager; import org.incendo.cloud.minecraft.extras.MinecraftExceptionHandler; import org.incendo.cloud.minecraft.extras.caption.TranslatableCaption; @@ -23,7 +23,7 @@ public final class WebhookLogger { private final Platform platform; - private final CommandManager commandManager; + private final CommandManager commandManager; private final ConfigManager configManager; private final ClientManager clientManager; private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); @@ -82,14 +82,14 @@ public DependencyManager dependencyManager() { return this.dependencyManager; } - public CommandManager commandManager() { + public CommandManager commandManager() { return commandManager; } private void setupCommandManager() { this.commandManager.registerCommandPreProcessor(new WebhookLoggerCommandPreprocessor(this)); this.commandManager.captionRegistry().registerProvider(TranslatableCaption.translatableCaptionProvider()); - MinecraftExceptionHandler.createNative() + MinecraftExceptionHandler.createNative() .defaultHandlers() .captionFormatter(new CaptionFormatter()) .registerTo(this.commandManager); diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/BaseCommand.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/BaseCommand.java index 57ef834..828ad44 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/BaseCommand.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/BaseCommand.java @@ -1,18 +1,19 @@ package fi.fabianadrian.webhooklogger.common.command; import fi.fabianadrian.webhooklogger.common.WebhookLogger; +import net.kyori.adventure.audience.Audience; import org.incendo.cloud.CommandManager; public abstract class BaseCommand { protected final WebhookLogger webhookLogger; - protected final CommandManager manager; + protected final CommandManager manager; public BaseCommand(WebhookLogger webhookLogger) { this.webhookLogger = webhookLogger; this.manager = webhookLogger.commandManager(); } - protected org.incendo.cloud.Command.Builder rootBuilder() { + protected org.incendo.cloud.Command.Builder rootBuilder() { return this.manager.commandBuilder("webhooklogger"); } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/CaptionFormatter.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/CaptionFormatter.java index e77225e..e72877f 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/CaptionFormatter.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/CaptionFormatter.java @@ -1,5 +1,6 @@ package fi.fabianadrian.webhooklogger.common.command; +import net.kyori.adventure.audience.Audience; import net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.NonNull; import org.incendo.cloud.caption.Caption; @@ -13,9 +14,9 @@ import static net.kyori.adventure.text.Component.text; import static net.kyori.adventure.text.Component.translatable; -public final class CaptionFormatter implements ComponentCaptionFormatter { +public final class CaptionFormatter implements ComponentCaptionFormatter { @Override - public @NonNull Component formatCaption(@NonNull Caption captionKey, @NonNull Commander recipient, @NonNull String caption, @NonNull List<@NonNull CaptionVariable> variables) { + public @NonNull Component formatCaption(@NonNull Caption captionKey, @NonNull Audience recipient, @NonNull String caption, @NonNull List<@NonNull CaptionVariable> variables) { return translatable("cloud." + captionKey.key(), variables.stream().map(variable -> { if (variable instanceof RichVariable) { return (RichVariable) variable; diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/Commander.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/Commander.java deleted file mode 100644 index da52c67..0000000 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/Commander.java +++ /dev/null @@ -1,6 +0,0 @@ -package fi.fabianadrian.webhooklogger.common.command; - -import net.kyori.adventure.audience.ForwardingAudience; - -public interface Commander extends ForwardingAudience.Single { -} diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/commands/ReloadCommand.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/commands/ReloadCommand.java index 8ca1b17..ec61621 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/commands/ReloadCommand.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/commands/ReloadCommand.java @@ -1,8 +1,8 @@ package fi.fabianadrian.webhooklogger.common.command.commands; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.command.Commander; import fi.fabianadrian.webhooklogger.common.command.BaseCommand; +import net.kyori.adventure.audience.Audience; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.incendo.cloud.context.CommandContext; @@ -32,7 +32,7 @@ public void register() { ); } - private void executeReload(CommandContext context) { + private void executeReload(CommandContext context) { if (this.webhookLogger.reload()) { context.sender().sendMessage(COMPONENT_SUCCESS); } else { diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/processor/WebhookLoggerCommandPreprocessor.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/processor/WebhookLoggerCommandPreprocessor.java index 8620aa1..8a75dbf 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/processor/WebhookLoggerCommandPreprocessor.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/command/processor/WebhookLoggerCommandPreprocessor.java @@ -1,14 +1,14 @@ package fi.fabianadrian.webhooklogger.common.command.processor; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.command.Commander; import fi.fabianadrian.webhooklogger.common.command.ContextKeys; +import net.kyori.adventure.audience.Audience; import org.checkerframework.checker.nullness.qual.NonNull; import org.incendo.cloud.context.CommandContext; import org.incendo.cloud.execution.preprocessor.CommandPreprocessingContext; import org.incendo.cloud.execution.preprocessor.CommandPreprocessor; -public final class WebhookLoggerCommandPreprocessor implements CommandPreprocessor { +public final class WebhookLoggerCommandPreprocessor implements CommandPreprocessor { private final WebhookLogger webhookLogger; public WebhookLoggerCommandPreprocessor(WebhookLogger webhookLogger) { @@ -16,8 +16,8 @@ public WebhookLoggerCommandPreprocessor(WebhookLogger webhookLogger) { } @Override - public void accept(@NonNull CommandPreprocessingContext context) { - CommandContext commandContext = context.commandContext(); + public void accept(@NonNull CommandPreprocessingContext context) { + CommandContext commandContext = context.commandContext(); commandContext.store(ContextKeys.WEBHOOK_LOGGER_KEY, this.webhookLogger); } } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Platform.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Platform.java index 9abfefe..ff12f24 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Platform.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Platform.java @@ -1,6 +1,6 @@ package fi.fabianadrian.webhooklogger.common.platform; -import fi.fabianadrian.webhooklogger.common.command.Commander; +import net.kyori.adventure.audience.Audience; import org.incendo.cloud.CommandManager; import org.slf4j.Logger; @@ -9,6 +9,6 @@ public interface Platform { Logger logger(); Path configPath(); - CommandManager commandManager(); + CommandManager commandManager(); void registerListeners(); } diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java index be5bcd6..03a4fa1 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java @@ -1,27 +1,26 @@ package fi.fabianadrian.webhooklogger.paper; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.command.Commander; import fi.fabianadrian.webhooklogger.common.dependency.Dependency; import fi.fabianadrian.webhooklogger.common.platform.Platform; -import fi.fabianadrian.webhooklogger.paper.command.PaperCommander; -import fi.fabianadrian.webhooklogger.paper.listener.*; -import io.papermc.paper.command.brigadier.CommandSourceStack; +import fi.fabianadrian.webhooklogger.paper.listener.ListenerFactory; +import net.kyori.adventure.audience.Audience; import org.bstats.bukkit.Metrics; +import org.bukkit.command.CommandSender; import org.bukkit.event.HandlerList; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.incendo.cloud.CommandManager; import org.incendo.cloud.SenderMapper; import org.incendo.cloud.execution.ExecutionCoordinator; -import org.incendo.cloud.paper.PaperCommandManager; +import org.incendo.cloud.paper.LegacyPaperCommandManager; import org.slf4j.Logger; import java.nio.file.Path; public final class WebhookLoggerPaper extends JavaPlugin implements Platform { private WebhookLogger webhookLogger; - private CommandManager commandManager; + private LegacyPaperCommandManager commandManager; @Override public void onEnable() { @@ -68,18 +67,15 @@ public Path configPath() { } @Override - public CommandManager commandManager() { + public CommandManager commandManager() { return this.commandManager; } private void createCommandManager() { - SenderMapper senderMapper = SenderMapper.create( - PaperCommander::new, - commander -> ((PaperCommander) commander).stack() + SenderMapper mapper = SenderMapper.create( + commandSender -> commandSender, + audience -> (CommandSender) audience ); - - this.commandManager = PaperCommandManager.builder(senderMapper) - .executionCoordinator(ExecutionCoordinator.simpleCoordinator()) - .buildOnEnable(this); + this.commandManager = new LegacyPaperCommandManager<>(this, ExecutionCoordinator.simpleCoordinator(), mapper); } } diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/command/PaperCommander.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/command/PaperCommander.java deleted file mode 100644 index dfb6d0d..0000000 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/command/PaperCommander.java +++ /dev/null @@ -1,16 +0,0 @@ -package fi.fabianadrian.webhooklogger.paper.command; - -import fi.fabianadrian.webhooklogger.common.command.Commander; -import io.papermc.paper.command.brigadier.CommandSourceStack; -import net.kyori.adventure.audience.Audience; -import org.jetbrains.annotations.NotNull; - -public record PaperCommander(CommandSourceStack stack) implements Commander { - - @Override - public @NotNull Audience audience() { - return this.stack.getSender(); - } - - -} diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/WebhookLoggerSponge.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/WebhookLoggerSponge.java index a028bb4..9ba8bcc 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/WebhookLoggerSponge.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/WebhookLoggerSponge.java @@ -2,10 +2,10 @@ import com.google.inject.Inject; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.command.Commander; import fi.fabianadrian.webhooklogger.common.dependency.Dependency; import fi.fabianadrian.webhooklogger.common.platform.Platform; import fi.fabianadrian.webhooklogger.sponge.listener.ListenerFactory; +import net.kyori.adventure.audience.Audience; import org.bstats.sponge.Metrics; import org.incendo.cloud.CommandManager; import org.slf4j.Logger; @@ -27,7 +27,7 @@ public final class WebhookLoggerSponge implements Platform { private final PluginContainer container; private final Path configDir; private final Logger logger; - private CommandManager commandManager; + private CommandManager commandManager; @Inject public WebhookLoggerSponge(PluginContainer container, @ConfigDir(sharedRoot = false) Path configDir, Metrics.Factory metricsFactory) { @@ -78,7 +78,7 @@ public Path configPath() { } @Override - public CommandManager commandManager() { + public CommandManager commandManager() { return this.commandManager; } From 827a31a0fb7749291cea8b42ef037d1189c182b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 19:41:39 +0300 Subject: [PATCH 10/18] cleanup webhook registry --- .../webhooklogger/common/client/WebhookRegistry.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/WebhookRegistry.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/WebhookRegistry.java index 39ba3c8..8b8c7c1 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/WebhookRegistry.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/WebhookRegistry.java @@ -8,13 +8,9 @@ public final class WebhookRegistry { private final Map> eventClientMap = new EnumMap<>(EventType.class); private final List clients = new ArrayList<>(); - public void register(DiscordClient client, List events) { + public void register(final DiscordClient client, final List events) { this.clients.add(client); - events.forEach(event -> { - List clients = eventClientMap.getOrDefault(event, new ArrayList<>()); - clients.add(client); - this.eventClientMap.put(event, clients); - }); + events.forEach(event -> this.eventClientMap.computeIfAbsent(event, key -> new ArrayList<>()).add(client)); } public void clear() { From b879029b6e449bd4f721233d3b1cdfb8dfe3c48e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 19:44:08 +0300 Subject: [PATCH 11/18] use set for webhook clients --- .../webhooklogger/common/client/WebhookRegistry.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/WebhookRegistry.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/WebhookRegistry.java index 8b8c7c1..313be0a 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/WebhookRegistry.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/WebhookRegistry.java @@ -6,7 +6,7 @@ public final class WebhookRegistry { private final Map> eventClientMap = new EnumMap<>(EventType.class); - private final List clients = new ArrayList<>(); + private final Set clients = new HashSet<>(); public void register(final DiscordClient client, final List events) { this.clients.add(client); @@ -22,8 +22,8 @@ public List forEventType(EventType type) { return this.eventClientMap.get(type); } - public List webhooks() { - return List.copyOf(this.clients); + public Set webhooks() { + return Set.copyOf(this.clients); } public Set registeredEventTypes() { From e776215b5c918d66ef8db14fe3ae112b854cb6c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 19:56:02 +0300 Subject: [PATCH 12/18] rename webhook package and classes --- .../webhooklogger/common/WebhookLogger.java | 8 ++++---- .../WebhookClient.java} | 6 +++--- .../WebhookManager.java} | 12 ++++++------ .../common/{client => webhook}/WebhookRegistry.java | 12 ++++++------ 4 files changed, 19 insertions(+), 19 deletions(-) rename common/src/main/java/fi/fabianadrian/webhooklogger/common/{client/DiscordClient.java => webhook/WebhookClient.java} (90%) rename common/src/main/java/fi/fabianadrian/webhooklogger/common/{client/ClientManager.java => webhook/WebhookManager.java} (85%) rename common/src/main/java/fi/fabianadrian/webhooklogger/common/{client => webhook}/WebhookRegistry.java (63%) diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java index c1cf1b1..e752297 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java @@ -1,6 +1,6 @@ package fi.fabianadrian.webhooklogger.common; -import fi.fabianadrian.webhooklogger.common.client.ClientManager; +import fi.fabianadrian.webhooklogger.common.webhook.WebhookManager; import fi.fabianadrian.webhooklogger.common.command.CaptionFormatter; import fi.fabianadrian.webhooklogger.common.command.BaseCommand; import fi.fabianadrian.webhooklogger.common.command.commands.ReloadCommand; @@ -25,7 +25,7 @@ public final class WebhookLogger { private final Platform platform; private final CommandManager commandManager; private final ConfigManager configManager; - private final ClientManager clientManager; + private final WebhookManager clientManager; private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); private final DependencyManager dependencyManager = new DependencyManager(); @@ -41,7 +41,7 @@ public WebhookLogger(Platform platform) { setupCommandManager(); registerCommands(); - this.clientManager = new ClientManager(this); + this.clientManager = new WebhookManager(this); this.clientManager.reload(); } @@ -70,7 +70,7 @@ public Logger logger() { return this.platform.logger(); } - public ClientManager clientManager() { + public WebhookManager clientManager() { return this.clientManager; } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/DiscordClient.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookClient.java similarity index 90% rename from common/src/main/java/fi/fabianadrian/webhooklogger/common/client/DiscordClient.java rename to common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookClient.java index 853bf06..c13df54 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/DiscordClient.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookClient.java @@ -1,4 +1,4 @@ -package fi.fabianadrian.webhooklogger.common.client; +package fi.fabianadrian.webhooklogger.common.webhook; import io.github._4drian3d.jdwebhooks.WebHook; import io.github._4drian3d.jdwebhooks.WebHookClient; @@ -8,13 +8,13 @@ import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; -public final class DiscordClient { +public final class WebhookClient { private final Queue messageQueue = new ConcurrentLinkedQueue<>(); private final WebHookClient client; private final Logger logger; private final String url; - public DiscordClient(Logger logger, String url) { + public WebhookClient(Logger logger, String url) { this.logger = logger; this.url = url; diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/ClientManager.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookManager.java similarity index 85% rename from common/src/main/java/fi/fabianadrian/webhooklogger/common/client/ClientManager.java rename to common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookManager.java index 8fa0d89..12cd59f 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/ClientManager.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookManager.java @@ -1,4 +1,4 @@ -package fi.fabianadrian.webhooklogger.common.client; +package fi.fabianadrian.webhooklogger.common.webhook; import dev.vankka.mcdiscordreserializer.discord.DiscordSerializer; import fi.fabianadrian.webhooklogger.common.WebhookLogger; @@ -13,18 +13,18 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -public final class ClientManager { +public final class WebhookManager { private final WebhookLogger webhookLogger; private final WebhookRegistry registry = new WebhookRegistry(); private ScheduledFuture scheduledSendMessageTask; private MainConfig config; - public ClientManager(WebhookLogger webhookLogger) { + public WebhookManager(WebhookLogger webhookLogger) { this.webhookLogger = webhookLogger; } public void send(EventBuilder eventBuilder) { - List clients = this.registry.forEventType(eventBuilder.type()); + List clients = this.registry.forEventType(eventBuilder.type()); if (clients.isEmpty()) { return; } @@ -51,7 +51,7 @@ public void reload() { } this.scheduledSendMessageTask = this.webhookLogger.scheduler().scheduleAtFixedRate( - () -> this.registry.webhooks().forEach(DiscordClient::sendAll), + () -> this.registry.webhooks().forEach(WebhookClient::sendAll), 0, this.config.sendRate(), TimeUnit.SECONDS @@ -71,7 +71,7 @@ private void parseWebhooks() { return; } - this.registry.register(new DiscordClient(logger, webhook.url()), webhook.events()); + this.registry.register(new WebhookClient(logger, webhook.url()), webhook.events()); }); } } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/WebhookRegistry.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookRegistry.java similarity index 63% rename from common/src/main/java/fi/fabianadrian/webhooklogger/common/client/WebhookRegistry.java rename to common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookRegistry.java index 313be0a..3f64458 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/client/WebhookRegistry.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookRegistry.java @@ -1,14 +1,14 @@ -package fi.fabianadrian.webhooklogger.common.client; +package fi.fabianadrian.webhooklogger.common.webhook; import fi.fabianadrian.webhooklogger.common.event.EventType; import java.util.*; public final class WebhookRegistry { - private final Map> eventClientMap = new EnumMap<>(EventType.class); - private final Set clients = new HashSet<>(); + private final Map> eventClientMap = new EnumMap<>(EventType.class); + private final Set clients = new HashSet<>(); - public void register(final DiscordClient client, final List events) { + public void register(final WebhookClient client, final List events) { this.clients.add(client); events.forEach(event -> this.eventClientMap.computeIfAbsent(event, key -> new ArrayList<>()).add(client)); } @@ -18,11 +18,11 @@ public void clear() { this.eventClientMap.clear(); } - public List forEventType(EventType type) { + public List forEventType(EventType type) { return this.eventClientMap.get(type); } - public Set webhooks() { + public Set webhooks() { return Set.copyOf(this.clients); } From 6a2e267a5a29a842179979591f396916541ac9d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 20:58:12 +0300 Subject: [PATCH 13/18] break many things --- .../webhooklogger/common/WebhookLogger.java | 9 ++++- .../common/listener/AbstractListener.java | 39 +++++++++++++++++++ .../common/listener/ListenerFactory.java | 14 +++++++ .../common/listener/ListenerRegistry.java | 32 +++++++++++++++ .../common/platform/Platform.java | 3 +- .../common/webhook/WebhookManager.java | 39 ++++--------------- .../common/webhook/WebhookRegistry.java | 32 --------------- .../paper/WebhookLoggerPaper.java | 22 +++++------ .../paper/listener/ListenerFactory.java | 36 ----------------- .../paper/listener/PaperListenerFactory.java | 35 +++++++++++++++++ .../paper/listener/PaperListenerRegistry.java | 36 +++++++++++++++++ .../{ => listeners}/ChatListener.java | 16 ++++---- .../{ => listeners}/CommandListener.java | 21 +++++----- .../{ => listeners}/DeathListener.java | 17 ++++---- .../{ => listeners}/JoinQuitListener.java | 21 +++++----- .../sponge/WebhookLoggerSponge.java | 17 +++----- .../sponge/listener/ListenerFactory.java | 39 ------------------- .../listener/SpongeListenerFactory.java | 34 ++++++++++++++++ .../listener/SpongeListenerRegistry.java | 35 +++++++++++++++++ .../{ => listeners}/ChatListener.java | 13 +++---- .../{ => listeners}/CommandListener.java | 14 +++---- .../{ => listeners}/DeathListener.java | 14 +++---- .../{ => listeners}/JoinQuitListener.java | 18 ++++----- 23 files changed, 316 insertions(+), 240 deletions(-) create mode 100644 common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/AbstractListener.java create mode 100644 common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerFactory.java create mode 100644 common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerRegistry.java delete mode 100644 common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookRegistry.java delete mode 100644 paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/ListenerFactory.java create mode 100644 paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerFactory.java create mode 100644 paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerRegistry.java rename paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/{ => listeners}/ChatListener.java (56%) rename paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/{ => listeners}/CommandListener.java (66%) rename paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/{ => listeners}/DeathListener.java (61%) rename paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/{ => listeners}/JoinQuitListener.java (59%) delete mode 100644 sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/ListenerFactory.java create mode 100644 sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerFactory.java create mode 100644 sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerRegistry.java rename sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/{ => listeners}/ChatListener.java (74%) rename sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/{ => listeners}/CommandListener.java (74%) rename sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/{ => listeners}/DeathListener.java (75%) rename sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/{ => listeners}/JoinQuitListener.java (65%) diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java index e752297..1903fee 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java @@ -1,5 +1,6 @@ package fi.fabianadrian.webhooklogger.common; +import fi.fabianadrian.webhooklogger.common.listener.ListenerRegistry; import fi.fabianadrian.webhooklogger.common.webhook.WebhookManager; import fi.fabianadrian.webhooklogger.common.command.CaptionFormatter; import fi.fabianadrian.webhooklogger.common.command.BaseCommand; @@ -47,9 +48,11 @@ public WebhookLogger(Platform platform) { public boolean reload() { boolean success = this.configManager.reload(); + + this.platform.listenerRegistry().unregisterAll(); this.clientManager.reload(); + this.platform.listenerRegistry().registerAll(); - this.platform.registerListeners(); return success; } @@ -86,6 +89,10 @@ public CommandManager commandManager() { return commandManager; } + public ListenerRegistry listenerRegistry() { + return this.platform.listenerRegistry(); + } + private void setupCommandManager() { this.commandManager.registerCommandPreProcessor(new WebhookLoggerCommandPreprocessor(this)); this.commandManager.captionRegistry().registerProvider(TranslatableCaption.translatableCaptionProvider()); diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/AbstractListener.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/AbstractListener.java new file mode 100644 index 0000000..83dc643 --- /dev/null +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/AbstractListener.java @@ -0,0 +1,39 @@ +package fi.fabianadrian.webhooklogger.common.listener; + +import dev.vankka.mcdiscordreserializer.discord.DiscordSerializer; +import fi.fabianadrian.webhooklogger.common.WebhookLogger; +import fi.fabianadrian.webhooklogger.common.event.EventBuilder; +import fi.fabianadrian.webhooklogger.common.webhook.WebhookClient; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public abstract class AbstractListener { + protected final WebhookLogger webhookLogger; + private final Map replacements; + List clients = new ArrayList<>(); + + public AbstractListener(WebhookLogger webhookLogger) { + this.webhookLogger = webhookLogger; + this.replacements = webhookLogger.mainConfig().textReplacements(); + } + + public void addWebhook(WebhookClient client) { + this.clients.add(client); + } + + protected void queue(B builder) { + String discordSerialized = DiscordSerializer.INSTANCE.serialize(builder.component()); + for (Map.Entry entry : this.replacements.entrySet()) { + Matcher matcher = entry.getKey().matcher(discordSerialized); + discordSerialized = matcher.replaceAll(entry.getValue()); + } + + //TODO Fix ugly + String finalDiscordSerialized = discordSerialized; + this.clients.forEach(client -> client.queue(finalDiscordSerialized)); + } +} diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerFactory.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerFactory.java new file mode 100644 index 0000000..93518b2 --- /dev/null +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerFactory.java @@ -0,0 +1,14 @@ +package fi.fabianadrian.webhooklogger.common.listener; + +import fi.fabianadrian.webhooklogger.common.WebhookLogger; +import fi.fabianadrian.webhooklogger.common.event.EventType; + +public abstract class ListenerFactory { + protected final WebhookLogger webhookLogger; + + public ListenerFactory(WebhookLogger webhookLogger) { + this.webhookLogger = webhookLogger; + } + + protected abstract AbstractListener create(EventType eventType); +} diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerRegistry.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerRegistry.java new file mode 100644 index 0000000..46e9952 --- /dev/null +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerRegistry.java @@ -0,0 +1,32 @@ +package fi.fabianadrian.webhooklogger.common.listener; + +import fi.fabianadrian.webhooklogger.common.WebhookLogger; +import fi.fabianadrian.webhooklogger.common.event.EventType; +import fi.fabianadrian.webhooklogger.common.webhook.WebhookClient; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public abstract class ListenerRegistry { + protected final WebhookLogger webhookLogger; + protected Map> registry = new HashMap<>(); + + public ListenerRegistry(WebhookLogger webhookLogger) { + this.webhookLogger = webhookLogger; + } + + public void registerWebhookForEvents(WebhookClient client, List events) { + events.forEach(event -> registerWebhook(client, event)); + } + + public abstract void registerAll(); + + public abstract void unregisterAll(); + + protected abstract ListenerFactory factory(); + + private void registerWebhook(WebhookClient webhookClient, EventType type) { + this.registry.computeIfAbsent(type, k -> factory().create(type)).addWebhook(webhookClient); + } +} diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Platform.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Platform.java index ff12f24..78d6e2e 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Platform.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Platform.java @@ -1,5 +1,6 @@ package fi.fabianadrian.webhooklogger.common.platform; +import fi.fabianadrian.webhooklogger.common.listener.ListenerRegistry; import net.kyori.adventure.audience.Audience; import org.incendo.cloud.CommandManager; import org.slf4j.Logger; @@ -10,5 +11,5 @@ public interface Platform { Logger logger(); Path configPath(); CommandManager commandManager(); - void registerListeners(); + ListenerRegistry listenerRegistry(); } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookManager.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookManager.java index 12cd59f..458cb3c 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookManager.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookManager.java @@ -1,21 +1,17 @@ package fi.fabianadrian.webhooklogger.common.webhook; -import dev.vankka.mcdiscordreserializer.discord.DiscordSerializer; import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.config.MainConfig; -import fi.fabianadrian.webhooklogger.common.event.EventBuilder; import org.slf4j.Logger; +import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; -import java.util.regex.Matcher; -import java.util.regex.Pattern; public final class WebhookManager { private final WebhookLogger webhookLogger; - private final WebhookRegistry registry = new WebhookRegistry(); + private final List clients = new ArrayList<>(); private ScheduledFuture scheduledSendMessageTask; private MainConfig config; @@ -23,27 +19,8 @@ public WebhookManager(WebhookLogger webhookLogger) { this.webhookLogger = webhookLogger; } - public void send(EventBuilder eventBuilder) { - List clients = this.registry.forEventType(eventBuilder.type()); - if (clients.isEmpty()) { - return; - } - - String discordSerialized = DiscordSerializer.INSTANCE.serialize(eventBuilder.component()); - for (Map.Entry entry : this.config.textReplacements().entrySet()) { - Matcher matcher = entry.getKey().matcher(discordSerialized); - discordSerialized = matcher.replaceAll(entry.getValue()); - } - - //TODO Fix ugly - String finalDiscordSerialized = discordSerialized; - clients.forEach(client -> client.queue(finalDiscordSerialized)); - } - public void reload() { this.config = this.webhookLogger.mainConfig(); - - this.registry.clear(); parseWebhooks(); if (this.scheduledSendMessageTask != null) { @@ -51,18 +28,16 @@ public void reload() { } this.scheduledSendMessageTask = this.webhookLogger.scheduler().scheduleAtFixedRate( - () -> this.registry.webhooks().forEach(WebhookClient::sendAll), + () -> this.clients.forEach(WebhookClient::sendAll), 0, this.config.sendRate(), TimeUnit.SECONDS ); } - public WebhookRegistry registry() { - return this.registry; - } - private void parseWebhooks() { + this.clients.clear(); + Logger logger = this.webhookLogger.logger(); this.config.webhooks().forEach(webhook -> { if (webhook.url().isBlank()) { @@ -71,7 +46,9 @@ private void parseWebhooks() { return; } - this.registry.register(new WebhookClient(logger, webhook.url()), webhook.events()); + WebhookClient client = new WebhookClient(logger, webhook.url()); + this.webhookLogger.listenerRegistry().registerWebhookForEvents(client, webhook.events()); + this.clients.add(client); }); } } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookRegistry.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookRegistry.java deleted file mode 100644 index 3f64458..0000000 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookRegistry.java +++ /dev/null @@ -1,32 +0,0 @@ -package fi.fabianadrian.webhooklogger.common.webhook; - -import fi.fabianadrian.webhooklogger.common.event.EventType; - -import java.util.*; - -public final class WebhookRegistry { - private final Map> eventClientMap = new EnumMap<>(EventType.class); - private final Set clients = new HashSet<>(); - - public void register(final WebhookClient client, final List events) { - this.clients.add(client); - events.forEach(event -> this.eventClientMap.computeIfAbsent(event, key -> new ArrayList<>()).add(client)); - } - - public void clear() { - this.clients.clear(); - this.eventClientMap.clear(); - } - - public List forEventType(EventType type) { - return this.eventClientMap.get(type); - } - - public Set webhooks() { - return Set.copyOf(this.clients); - } - - public Set registeredEventTypes() { - return Set.copyOf(this.eventClientMap.keySet()); - } -} diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java index 03a4fa1..f6424fe 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java @@ -2,12 +2,12 @@ import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.dependency.Dependency; +import fi.fabianadrian.webhooklogger.common.listener.ListenerRegistry; import fi.fabianadrian.webhooklogger.common.platform.Platform; -import fi.fabianadrian.webhooklogger.paper.listener.ListenerFactory; +import fi.fabianadrian.webhooklogger.paper.listener.PaperListenerRegistry; import net.kyori.adventure.audience.Audience; import org.bstats.bukkit.Metrics; import org.bukkit.command.CommandSender; -import org.bukkit.event.HandlerList; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.incendo.cloud.CommandManager; @@ -21,9 +21,11 @@ public final class WebhookLoggerPaper extends JavaPlugin implements Platform { private WebhookLogger webhookLogger; private LegacyPaperCommandManager commandManager; + private PaperListenerRegistry listenerRegistry; @Override public void onEnable() { + this.listenerRegistry = new PaperListenerRegistry(this); createCommandManager(); this.webhookLogger = new WebhookLogger(this); @@ -32,8 +34,6 @@ public void onEnable() { this.webhookLogger.dependencyManager().markAsPresent(Dependency.MINI_PLACEHOLDERS); } - registerListeners(); - // bStats new Metrics(this, 18436); } @@ -47,15 +47,6 @@ public WebhookLogger webhookLogger() { return this.webhookLogger; } - @Override - public void registerListeners() { - // Unregister any existing listeners - HandlerList.unregisterAll(this); - - ListenerFactory factory = new ListenerFactory(this); - this.webhookLogger.clientManager().registry().registeredEventTypes().forEach(factory::register); - } - @Override public Logger logger() { return getSLF4JLogger(); @@ -71,6 +62,11 @@ public CommandManager commandManager() { return this.commandManager; } + @Override + public ListenerRegistry listenerRegistry() { + return this.listenerRegistry; + } + private void createCommandManager() { SenderMapper mapper = SenderMapper.create( commandSender -> commandSender, diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/ListenerFactory.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/ListenerFactory.java deleted file mode 100644 index 0e61301..0000000 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/ListenerFactory.java +++ /dev/null @@ -1,36 +0,0 @@ -package fi.fabianadrian.webhooklogger.paper.listener; - -import fi.fabianadrian.webhooklogger.common.event.EventType; -import fi.fabianadrian.webhooklogger.paper.WebhookLoggerPaper; -import org.bukkit.event.Listener; - -public final class ListenerFactory { - private final WebhookLoggerPaper plugin; - - public ListenerFactory(WebhookLoggerPaper plugin) { - this.plugin = plugin; - } - - public void register(EventType type) { - Listener listener = create(type); - this.plugin.getServer().getPluginManager().registerEvents(listener, this.plugin); - } - - private Listener create(EventType type) { - switch (type) { - case CHAT -> { - return new ChatListener(this.plugin); - } - case COMMAND -> { - return new CommandListener(this.plugin); - } - case DEATH -> { - return new DeathListener(this.plugin); - } - case JOINQUIT -> { - return new JoinQuitListener(this.plugin); - } - } - throw new IllegalStateException("Unknown EventType"); - } -} diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerFactory.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerFactory.java new file mode 100644 index 0000000..013bb1c --- /dev/null +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerFactory.java @@ -0,0 +1,35 @@ +package fi.fabianadrian.webhooklogger.paper.listener; + +import fi.fabianadrian.webhooklogger.common.WebhookLogger; +import fi.fabianadrian.webhooklogger.common.event.EventType; +import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; +import fi.fabianadrian.webhooklogger.common.listener.ListenerFactory; +import fi.fabianadrian.webhooklogger.paper.listener.listeners.ChatListener; +import fi.fabianadrian.webhooklogger.paper.listener.listeners.CommandListener; +import fi.fabianadrian.webhooklogger.paper.listener.listeners.DeathListener; +import fi.fabianadrian.webhooklogger.paper.listener.listeners.JoinQuitListener; + +public final class PaperListenerFactory extends ListenerFactory { + public PaperListenerFactory(WebhookLogger webhookLogger) { + super(webhookLogger); + } + + @Override + public AbstractListener create(EventType type) { + switch (type) { + case CHAT -> { + return new ChatListener(this.webhookLogger); + } + case COMMAND -> { + return new CommandListener(this.webhookLogger); + } + case DEATH -> { + return new DeathListener(this.webhookLogger); + } + case JOINQUIT -> { + return new JoinQuitListener(this.webhookLogger); + } + } + throw new IllegalStateException("Unknown EventType"); + } +} diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerRegistry.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerRegistry.java new file mode 100644 index 0000000..1097654 --- /dev/null +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerRegistry.java @@ -0,0 +1,36 @@ +package fi.fabianadrian.webhooklogger.paper.listener; + +import fi.fabianadrian.webhooklogger.common.listener.ListenerFactory; +import fi.fabianadrian.webhooklogger.common.listener.ListenerRegistry; +import fi.fabianadrian.webhooklogger.paper.WebhookLoggerPaper; +import org.bukkit.event.HandlerList; +import org.bukkit.event.Listener; +import org.bukkit.plugin.PluginManager; + +public class PaperListenerRegistry extends ListenerRegistry { + private final WebhookLoggerPaper plugin; + private final ListenerFactory factory; + + public PaperListenerRegistry(WebhookLoggerPaper plugin) { + super(plugin.webhookLogger()); + this.plugin = plugin; + this.factory = new PaperListenerFactory(plugin.webhookLogger()); + } + + @Override + public void registerAll() { + PluginManager manager = this.plugin.getServer().getPluginManager(); + this.registry.values().forEach(listener -> manager.registerEvents((Listener) listener, this.plugin)); + } + + @Override + public void unregisterAll() { + HandlerList.unregisterAll(this.plugin); + this.registry.clear(); + } + + @Override + protected ListenerFactory factory() { + return this.factory; + } +} diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/ChatListener.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/ChatListener.java similarity index 56% rename from paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/ChatListener.java rename to paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/ChatListener.java index ede7f71..1840d12 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/ChatListener.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/ChatListener.java @@ -1,19 +1,17 @@ -package fi.fabianadrian.webhooklogger.paper.listener; +package fi.fabianadrian.webhooklogger.paper.listener.listeners; import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.config.event.ChatEventConfig; import fi.fabianadrian.webhooklogger.common.event.ChatEventBuilder; -import fi.fabianadrian.webhooklogger.common.event.EventBuilder; -import fi.fabianadrian.webhooklogger.paper.WebhookLoggerPaper; +import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; import io.papermc.paper.event.player.AsyncChatEvent; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -public final class ChatListener implements Listener { - private final WebhookLogger webhookLogger; +public final class ChatListener extends AbstractListener implements Listener { - public ChatListener(WebhookLoggerPaper plugin) { - this.webhookLogger = plugin.webhookLogger(); + public ChatListener(WebhookLogger webhookLogger) { + super(webhookLogger); } @EventHandler @@ -24,10 +22,10 @@ public void onChat(AsyncChatEvent event) { return; } - EventBuilder builder = new ChatEventBuilder(this.webhookLogger) + ChatEventBuilder builder = new ChatEventBuilder(this.webhookLogger) .audience(event.getPlayer()) .cancelled(event.isCancelled()) .message(event.message()); - this.webhookLogger.clientManager().send(builder); + queue(builder); } } diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/CommandListener.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/CommandListener.java similarity index 66% rename from paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/CommandListener.java rename to paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/CommandListener.java index d33a40a..084bbbe 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/CommandListener.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/CommandListener.java @@ -1,21 +1,18 @@ -package fi.fabianadrian.webhooklogger.paper.listener; +package fi.fabianadrian.webhooklogger.paper.listener.listeners; import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.config.event.CommandEventConfig; import fi.fabianadrian.webhooklogger.common.event.CommandEventBuilder; -import fi.fabianadrian.webhooklogger.common.event.EventBuilder; -import fi.fabianadrian.webhooklogger.paper.WebhookLoggerPaper; +import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.server.ServerCommandEvent; -public final class CommandListener implements Listener { - private final WebhookLogger webhookLogger; - - public CommandListener(WebhookLoggerPaper plugin) { - this.webhookLogger = plugin.webhookLogger(); +public final class CommandListener extends AbstractListener implements Listener { + public CommandListener(WebhookLogger webhookLogger) { + super(webhookLogger); } @EventHandler @@ -33,11 +30,11 @@ public void onServerCommand(ServerCommandEvent event) { return; } - EventBuilder builder = new CommandEventBuilder(this.webhookLogger) + CommandEventBuilder builder = new CommandEventBuilder(this.webhookLogger) .cancelled(event.isCancelled()) .audience(event.getSender()) .command(event.getCommand()); - this.webhookLogger.clientManager().send(builder); + queue(builder); } @EventHandler @@ -47,10 +44,10 @@ public void onPlayerCommand(PlayerCommandPreprocessEvent event) { return; } - EventBuilder builder = new CommandEventBuilder(this.webhookLogger) + CommandEventBuilder builder = new CommandEventBuilder(this.webhookLogger) .cancelled(event.isCancelled()) .audience(event.getPlayer()) .command(event.getMessage()); - this.webhookLogger.clientManager().send(builder); + queue(builder); } } diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/DeathListener.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/DeathListener.java similarity index 61% rename from paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/DeathListener.java rename to paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/DeathListener.java index c75a870..69d6f3d 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/DeathListener.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/DeathListener.java @@ -1,20 +1,17 @@ -package fi.fabianadrian.webhooklogger.paper.listener; +package fi.fabianadrian.webhooklogger.paper.listener.listeners; import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.config.event.DeathEventConfig; import fi.fabianadrian.webhooklogger.common.event.DeathEventBuilder; -import fi.fabianadrian.webhooklogger.common.event.EventBuilder; -import fi.fabianadrian.webhooklogger.paper.WebhookLoggerPaper; +import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; import org.bukkit.Location; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.PlayerDeathEvent; -public final class DeathListener implements Listener { - private final WebhookLogger webhookLogger; - - public DeathListener(WebhookLoggerPaper plugin) { - this.webhookLogger = plugin.webhookLogger(); +public final class DeathListener extends AbstractListener implements Listener { + public DeathListener(WebhookLogger webhookLogger) { + super(webhookLogger); } @EventHandler @@ -27,11 +24,11 @@ public void onDeath(PlayerDeathEvent event) { Location loc = event.getEntity().getLocation(); - EventBuilder builder = new DeathEventBuilder(this.webhookLogger) + DeathEventBuilder builder = new DeathEventBuilder(this.webhookLogger) .audience(event.getEntity()) .cancelled(event.isCancelled()) .message(event.deathMessage()) .location(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); - this.webhookLogger.clientManager().send(builder); + queue(builder); } } diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/JoinQuitListener.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/JoinQuitListener.java similarity index 59% rename from paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/JoinQuitListener.java rename to paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/JoinQuitListener.java index 7ae96fd..23f3f31 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/JoinQuitListener.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/JoinQuitListener.java @@ -1,42 +1,39 @@ -package fi.fabianadrian.webhooklogger.paper.listener; +package fi.fabianadrian.webhooklogger.paper.listener.listeners; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.event.EventBuilder; import fi.fabianadrian.webhooklogger.common.event.JoinQuitEventBuilder; -import fi.fabianadrian.webhooklogger.paper.WebhookLoggerPaper; +import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; import org.bukkit.Location; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; -public final class JoinQuitListener implements Listener { - private final WebhookLogger webhookLogger; - - public JoinQuitListener(WebhookLoggerPaper plugin) { - this.webhookLogger = plugin.webhookLogger(); +public final class JoinQuitListener extends AbstractListener implements Listener { + public JoinQuitListener(WebhookLogger webhookLogger) { + super(webhookLogger); } @EventHandler public void onJoin(PlayerJoinEvent event) { Location loc = event.getPlayer().getLocation(); - EventBuilder builder = new JoinQuitEventBuilder(this.webhookLogger) + JoinQuitEventBuilder builder = new JoinQuitEventBuilder(this.webhookLogger) .audience(event.getPlayer()) .message(event.joinMessage()) .location(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()) .address(event.getPlayer().getAddress()); - this.webhookLogger.clientManager().send(builder); + queue(builder); } @EventHandler public void onQuit(PlayerQuitEvent event) { Location loc = event.getPlayer().getLocation(); - EventBuilder builder = new JoinQuitEventBuilder(this.webhookLogger) + JoinQuitEventBuilder builder = new JoinQuitEventBuilder(this.webhookLogger) .audience(event.getPlayer()) .message(event.quitMessage()) .location(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()) .address(event.getPlayer().getAddress()); - this.webhookLogger.clientManager().send(builder); + queue(builder); } } diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/WebhookLoggerSponge.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/WebhookLoggerSponge.java index 9ba8bcc..67429d8 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/WebhookLoggerSponge.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/WebhookLoggerSponge.java @@ -3,8 +3,8 @@ import com.google.inject.Inject; import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.dependency.Dependency; +import fi.fabianadrian.webhooklogger.common.listener.ListenerRegistry; import fi.fabianadrian.webhooklogger.common.platform.Platform; -import fi.fabianadrian.webhooklogger.sponge.listener.ListenerFactory; import net.kyori.adventure.audience.Audience; import org.bstats.sponge.Metrics; import org.incendo.cloud.CommandManager; @@ -46,8 +46,6 @@ public void onServerStart(final StartedEngineEvent event) { if (Sponge.pluginManager().plugin("miniplaceholders").isPresent()) { this.webhookLogger.dependencyManager().markAsPresent(Dependency.MINI_PLACEHOLDERS); } - - registerListeners(); } @Listener @@ -59,14 +57,6 @@ public WebhookLogger webhookLogger() { return this.webhookLogger; } - @Override - public void registerListeners() { - //TODO Unregister listeners here - - ListenerFactory factory = new ListenerFactory(this.container, this.webhookLogger); - this.webhookLogger.clientManager().registry().registeredEventTypes().forEach(factory::register); - } - @Override public Logger logger() { return this.logger; @@ -82,6 +72,11 @@ public CommandManager commandManager() { return this.commandManager; } + @Override + public ListenerRegistry listenerRegistry() { + return null; + } + private void createCommandManager() { } diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/ListenerFactory.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/ListenerFactory.java deleted file mode 100644 index 2c44783..0000000 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/ListenerFactory.java +++ /dev/null @@ -1,39 +0,0 @@ -package fi.fabianadrian.webhooklogger.sponge.listener; - -import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.event.EventType; -import org.spongepowered.api.Sponge; -import org.spongepowered.plugin.PluginContainer; - -public final class ListenerFactory { - private final PluginContainer container; - private final WebhookLogger webhookLogger; - - public ListenerFactory(PluginContainer container, WebhookLogger webhookLogger) { - this.container = container; - this.webhookLogger = webhookLogger; - } - - public void register(EventType type) { - Object listener = create(type); - Sponge.eventManager().registerListeners(this.container, listener); - } - - private Object create(EventType type) { - switch (type) { - case CHAT -> { - return new ChatListener(this.webhookLogger); - } - case COMMAND -> { - return new CommandListener(this.webhookLogger); - } - case DEATH -> { - return new DeathListener(this.webhookLogger); - } - case JOINQUIT -> { - return new JoinQuitListener(this.webhookLogger); - } - } - throw new IllegalStateException("Unknown EventType"); - } -} diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerFactory.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerFactory.java new file mode 100644 index 0000000..0d378df --- /dev/null +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerFactory.java @@ -0,0 +1,34 @@ +package fi.fabianadrian.webhooklogger.sponge.listener; + +import fi.fabianadrian.webhooklogger.common.WebhookLogger; +import fi.fabianadrian.webhooklogger.common.event.EventType; +import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; +import fi.fabianadrian.webhooklogger.common.listener.ListenerFactory; +import fi.fabianadrian.webhooklogger.sponge.listener.listeners.ChatListener; +import fi.fabianadrian.webhooklogger.sponge.listener.listeners.CommandListener; +import fi.fabianadrian.webhooklogger.sponge.listener.listeners.DeathListener; +import fi.fabianadrian.webhooklogger.sponge.listener.listeners.JoinQuitListener; + +public final class SpongeListenerFactory extends ListenerFactory { + public SpongeListenerFactory(WebhookLogger webhookLogger) { + super(webhookLogger); + } + + protected AbstractListener create(EventType type) { + switch (type) { + case CHAT -> { + return new ChatListener(this.webhookLogger); + } + case COMMAND -> { + return new CommandListener(this.webhookLogger); + } + case DEATH -> { + return new DeathListener(this.webhookLogger); + } + case JOINQUIT -> { + return new JoinQuitListener(this.webhookLogger); + } + } + throw new IllegalStateException("Unknown EventType"); + } +} diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerRegistry.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerRegistry.java new file mode 100644 index 0000000..6a5f75e --- /dev/null +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerRegistry.java @@ -0,0 +1,35 @@ +package fi.fabianadrian.webhooklogger.sponge.listener; + +import fi.fabianadrian.webhooklogger.common.WebhookLogger; +import fi.fabianadrian.webhooklogger.common.listener.ListenerFactory; +import fi.fabianadrian.webhooklogger.common.listener.ListenerRegistry; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.event.EventManager; +import org.spongepowered.plugin.PluginContainer; + +public class SpongeListenerRegistry extends ListenerRegistry { + private final EventManager manager = Sponge.eventManager(); + private final PluginContainer container; + private final SpongeListenerFactory factory; + + public SpongeListenerRegistry(WebhookLogger webhookLogger, PluginContainer container) { + super(webhookLogger); + this.container = container; + this.factory = new SpongeListenerFactory(webhookLogger); + } + + @Override + public void registerAll() { + this.registry.values().forEach(listener -> this.manager.registerListeners(this.container, listener)); + } + + @Override + public void unregisterAll() { + this.registry.values().forEach(this.manager::unregisterListeners); + } + + @Override + protected ListenerFactory factory() { + return this.factory; + } +} diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/ChatListener.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/ChatListener.java similarity index 74% rename from sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/ChatListener.java rename to sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/ChatListener.java index 45f1b8b..a846277 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/ChatListener.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/ChatListener.java @@ -1,9 +1,9 @@ -package fi.fabianadrian.webhooklogger.sponge.listener; +package fi.fabianadrian.webhooklogger.sponge.listener.listeners; import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.config.event.ChatEventConfig; import fi.fabianadrian.webhooklogger.common.event.ChatEventBuilder; -import fi.fabianadrian.webhooklogger.common.event.EventBuilder; +import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; import org.spongepowered.api.entity.living.player.server.ServerPlayer; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.filter.IsCancelled; @@ -12,11 +12,10 @@ import java.util.Optional; -public final class ChatListener { - private final WebhookLogger webhookLogger; +public final class ChatListener extends AbstractListener { public ChatListener(WebhookLogger webhookLogger) { - this.webhookLogger = webhookLogger; + super(webhookLogger); } @Listener @@ -33,10 +32,10 @@ public void onChat(PlayerChatEvent.Submit event) { return; } - EventBuilder builder = new ChatEventBuilder(this.webhookLogger) + ChatEventBuilder builder = new ChatEventBuilder(this.webhookLogger) .audience(playerOptional.get()) .cancelled(event.isCancelled()) .message(event.message()); - this.webhookLogger.clientManager().send(builder); + queue(builder); } } diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/CommandListener.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/CommandListener.java similarity index 74% rename from sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/CommandListener.java rename to sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/CommandListener.java index 3686918..5eb8619 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/CommandListener.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/CommandListener.java @@ -1,9 +1,9 @@ -package fi.fabianadrian.webhooklogger.sponge.listener; +package fi.fabianadrian.webhooklogger.sponge.listener.listeners; import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.config.event.CommandEventConfig; import fi.fabianadrian.webhooklogger.common.event.CommandEventBuilder; -import fi.fabianadrian.webhooklogger.common.event.EventBuilder; +import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.command.ExecuteCommandEvent; @@ -12,11 +12,9 @@ import java.util.Optional; -public final class CommandListener { - private final WebhookLogger webhookLogger; - +public final class CommandListener extends AbstractListener { public CommandListener(WebhookLogger webhookLogger) { - this.webhookLogger = webhookLogger; + super(webhookLogger); } @Listener @@ -33,10 +31,10 @@ public void onCommand(ExecuteCommandEvent.Pre event) { return; } - EventBuilder builder = new CommandEventBuilder(this.webhookLogger) + CommandEventBuilder builder = new CommandEventBuilder(this.webhookLogger) .audience(playerOptional.get()) .cancelled(event.isCancelled()) .command(event.command()); - this.webhookLogger.clientManager().send(builder); + queue(builder); } } diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/DeathListener.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/DeathListener.java similarity index 75% rename from sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/DeathListener.java rename to sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/DeathListener.java index f5cd619..4bafa60 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/DeathListener.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/DeathListener.java @@ -1,9 +1,9 @@ -package fi.fabianadrian.webhooklogger.sponge.listener; +package fi.fabianadrian.webhooklogger.sponge.listener.listeners; import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.config.event.DeathEventConfig; import fi.fabianadrian.webhooklogger.common.event.DeathEventBuilder; -import fi.fabianadrian.webhooklogger.common.event.EventBuilder; +import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.entity.DestructEntityEvent; @@ -11,11 +11,9 @@ import org.spongepowered.api.util.Tristate; import org.spongepowered.api.world.Location; -public final class DeathListener { - private final WebhookLogger webhookLogger; - +public final class DeathListener extends AbstractListener { public DeathListener(WebhookLogger webhookLogger) { - this.webhookLogger = webhookLogger; + super(webhookLogger); } @Listener @@ -31,11 +29,11 @@ public void onDeath(DestructEntityEvent.Death event) { } Location loc = player.location(); - EventBuilder builder = new DeathEventBuilder(this.webhookLogger) + DeathEventBuilder builder = new DeathEventBuilder(this.webhookLogger) .audience(player) .cancelled(event.isCancelled()) .message(event.message()) .location(loc.blockX(), loc.blockY(), loc.blockZ()); - this.webhookLogger.clientManager().send(builder); + queue(builder); } } diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/JoinQuitListener.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/JoinQuitListener.java similarity index 65% rename from sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/JoinQuitListener.java rename to sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/JoinQuitListener.java index 86e40c5..7c161b4 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/JoinQuitListener.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/listeners/JoinQuitListener.java @@ -1,40 +1,38 @@ -package fi.fabianadrian.webhooklogger.sponge.listener; +package fi.fabianadrian.webhooklogger.sponge.listener.listeners; import fi.fabianadrian.webhooklogger.common.WebhookLogger; -import fi.fabianadrian.webhooklogger.common.event.EventBuilder; import fi.fabianadrian.webhooklogger.common.event.JoinQuitEventBuilder; +import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.network.ServerSideConnectionEvent; import org.spongepowered.api.world.Location; -public final class JoinQuitListener { - private final WebhookLogger webhookLogger; - +public final class JoinQuitListener extends AbstractListener { public JoinQuitListener(WebhookLogger webhookLogger) { - this.webhookLogger = webhookLogger; + super(webhookLogger); } @Listener public void onJoin(ServerSideConnectionEvent.Join event) { Location loc = event.player().location(); - EventBuilder builder = new JoinQuitEventBuilder(this.webhookLogger) + JoinQuitEventBuilder builder = new JoinQuitEventBuilder(this.webhookLogger) .audience(event.player()) .message(event.message()) .location(loc.blockX(), loc.blockY(), loc.blockZ()) .address(event.connection().address()); - this.webhookLogger.clientManager().send(builder); + queue(builder); } @Listener public void onQuit(ServerSideConnectionEvent.Disconnect event) { Location loc = event.player().location(); - EventBuilder builder = new JoinQuitEventBuilder(this.webhookLogger) + JoinQuitEventBuilder builder = new JoinQuitEventBuilder(this.webhookLogger) .audience(event.player()) .message(event.message()) .location(loc.blockX(), loc.blockY(), loc.blockZ()) .address(event.connection().address()); - this.webhookLogger.clientManager().send(builder); + queue(builder); } } From b13962257111fe12e4c4f1076218b1471c9fdf71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 21:54:01 +0300 Subject: [PATCH 14/18] more progress on refactor --- .../webhooklogger/common/WebhookLogger.java | 29 +++++++------------ .../common/listener/ListenerFactory.java | 3 +- ...enerRegistry.java => ListenerManager.java} | 7 +++-- .../common/platform/Platform.java | 4 +-- .../common/webhook/WebhookClient.java | 2 +- .../common/webhook/WebhookManager.java | 22 ++++++++++---- .../paper/WebhookLoggerPaper.java | 11 +++---- .../paper/listener/PaperListenerFactory.java | 3 +- ...egistry.java => PaperListenerManager.java} | 6 ++-- .../sponge/WebhookLoggerSponge.java | 4 +-- ...gistry.java => SpongeListenerManager.java} | 6 ++-- 11 files changed, 52 insertions(+), 45 deletions(-) rename common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/{ListenerRegistry.java => ListenerManager.java} (77%) rename paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/{PaperListenerRegistry.java => PaperListenerManager.java} (82%) rename sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/{SpongeListenerRegistry.java => SpongeListenerManager.java} (80%) diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java index 1903fee..a9ad0c2 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/WebhookLogger.java @@ -1,17 +1,17 @@ package fi.fabianadrian.webhooklogger.common; -import fi.fabianadrian.webhooklogger.common.listener.ListenerRegistry; -import fi.fabianadrian.webhooklogger.common.webhook.WebhookManager; -import fi.fabianadrian.webhooklogger.common.command.CaptionFormatter; import fi.fabianadrian.webhooklogger.common.command.BaseCommand; +import fi.fabianadrian.webhooklogger.common.command.CaptionFormatter; import fi.fabianadrian.webhooklogger.common.command.commands.ReloadCommand; import fi.fabianadrian.webhooklogger.common.command.processor.WebhookLoggerCommandPreprocessor; import fi.fabianadrian.webhooklogger.common.config.ConfigManager; import fi.fabianadrian.webhooklogger.common.config.EventsConfig; import fi.fabianadrian.webhooklogger.common.config.MainConfig; import fi.fabianadrian.webhooklogger.common.dependency.DependencyManager; +import fi.fabianadrian.webhooklogger.common.listener.ListenerManager; import fi.fabianadrian.webhooklogger.common.locale.TranslationManager; import fi.fabianadrian.webhooklogger.common.platform.Platform; +import fi.fabianadrian.webhooklogger.common.webhook.WebhookManager; import net.kyori.adventure.audience.Audience; import org.incendo.cloud.CommandManager; import org.incendo.cloud.minecraft.extras.MinecraftExceptionHandler; @@ -26,7 +26,7 @@ public final class WebhookLogger { private final Platform platform; private final CommandManager commandManager; private final ConfigManager configManager; - private final WebhookManager clientManager; + private final WebhookManager webhookManager; private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); private final DependencyManager dependencyManager = new DependencyManager(); @@ -36,23 +36,20 @@ public WebhookLogger(Platform platform) { new TranslationManager(platform.logger()); this.configManager = new ConfigManager(platform.configPath(), platform.logger()); - this.configManager.reload(); this.commandManager = platform.commandManager(); setupCommandManager(); registerCommands(); - this.clientManager = new WebhookManager(this); - this.clientManager.reload(); + this.webhookManager = new WebhookManager(this); } public boolean reload() { boolean success = this.configManager.reload(); + this.platform.listenerManager().unregisterAll(); - this.platform.listenerRegistry().unregisterAll(); - this.clientManager.reload(); - this.platform.listenerRegistry().registerAll(); - + this.webhookManager.reload(); + this.platform.listenerManager().registerAll(); return success; } @@ -73,10 +70,6 @@ public Logger logger() { return this.platform.logger(); } - public WebhookManager clientManager() { - return this.clientManager; - } - public ScheduledExecutorService scheduler() { return this.scheduler; } @@ -89,14 +82,14 @@ public CommandManager commandManager() { return commandManager; } - public ListenerRegistry listenerRegistry() { - return this.platform.listenerRegistry(); + public ListenerManager listenerManager() { + return this.platform.listenerManager(); } private void setupCommandManager() { this.commandManager.registerCommandPreProcessor(new WebhookLoggerCommandPreprocessor(this)); this.commandManager.captionRegistry().registerProvider(TranslatableCaption.translatableCaptionProvider()); - MinecraftExceptionHandler.createNative() + MinecraftExceptionHandler.createNative() .defaultHandlers() .captionFormatter(new CaptionFormatter()) .registerTo(this.commandManager); diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerFactory.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerFactory.java index 93518b2..4c12aa1 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerFactory.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerFactory.java @@ -1,6 +1,7 @@ package fi.fabianadrian.webhooklogger.common.listener; import fi.fabianadrian.webhooklogger.common.WebhookLogger; +import fi.fabianadrian.webhooklogger.common.event.EventBuilder; import fi.fabianadrian.webhooklogger.common.event.EventType; public abstract class ListenerFactory { @@ -10,5 +11,5 @@ public ListenerFactory(WebhookLogger webhookLogger) { this.webhookLogger = webhookLogger; } - protected abstract AbstractListener create(EventType eventType); + protected abstract AbstractListener create(EventType eventType); } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerRegistry.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerManager.java similarity index 77% rename from common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerRegistry.java rename to common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerManager.java index 46e9952..e1f8c91 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerRegistry.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/listener/ListenerManager.java @@ -1,6 +1,7 @@ package fi.fabianadrian.webhooklogger.common.listener; import fi.fabianadrian.webhooklogger.common.WebhookLogger; +import fi.fabianadrian.webhooklogger.common.event.EventBuilder; import fi.fabianadrian.webhooklogger.common.event.EventType; import fi.fabianadrian.webhooklogger.common.webhook.WebhookClient; @@ -8,11 +9,11 @@ import java.util.List; import java.util.Map; -public abstract class ListenerRegistry { +public abstract class ListenerManager { protected final WebhookLogger webhookLogger; - protected Map> registry = new HashMap<>(); + protected Map> registry = new HashMap<>(); - public ListenerRegistry(WebhookLogger webhookLogger) { + public ListenerManager(WebhookLogger webhookLogger) { this.webhookLogger = webhookLogger; } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Platform.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Platform.java index 78d6e2e..ef5809b 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Platform.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/platform/Platform.java @@ -1,6 +1,6 @@ package fi.fabianadrian.webhooklogger.common.platform; -import fi.fabianadrian.webhooklogger.common.listener.ListenerRegistry; +import fi.fabianadrian.webhooklogger.common.listener.ListenerManager; import net.kyori.adventure.audience.Audience; import org.incendo.cloud.CommandManager; import org.slf4j.Logger; @@ -11,5 +11,5 @@ public interface Platform { Logger logger(); Path configPath(); CommandManager commandManager(); - ListenerRegistry listenerRegistry(); + ListenerManager listenerManager(); } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookClient.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookClient.java index c13df54..0c751ea 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookClient.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookClient.java @@ -25,7 +25,7 @@ public void queue(String message) { this.messageQueue.add(message); } - public void sendAll() throws RuntimeException { + public void sendAll() { // Copy messageBuffer List messages = List.copyOf(this.messageQueue); diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookManager.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookManager.java index 458cb3c..9ef2f84 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookManager.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookManager.java @@ -20,11 +20,16 @@ public WebhookManager(WebhookLogger webhookLogger) { } public void reload() { + if (this.scheduledSendMessageTask != null) { + this.scheduledSendMessageTask.cancel(false); + } + this.config = this.webhookLogger.mainConfig(); + parseWebhooks(); - if (this.scheduledSendMessageTask != null) { - this.scheduledSendMessageTask.cancel(false); + if (this.clients.isEmpty()) { + return; } this.scheduledSendMessageTask = this.webhookLogger.scheduler().scheduleAtFixedRate( @@ -36,19 +41,24 @@ public void reload() { } private void parseWebhooks() { - this.clients.clear(); - Logger logger = this.webhookLogger.logger(); + this.config.webhooks().forEach(webhook -> { if (webhook.url().isBlank()) { logger.warn("webhook url blank"); - // TODO Log message + // TODO Better log message + return; + } + + if (webhook.events().isEmpty()) { + logger.warn("webhook events blank"); + // TODO Better log message return; } WebhookClient client = new WebhookClient(logger, webhook.url()); - this.webhookLogger.listenerRegistry().registerWebhookForEvents(client, webhook.events()); this.clients.add(client); + this.webhookLogger.listenerManager().registerWebhookForEvents(client, webhook.events()); }); } } diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java index f6424fe..e76935a 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java @@ -2,9 +2,9 @@ import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.dependency.Dependency; -import fi.fabianadrian.webhooklogger.common.listener.ListenerRegistry; +import fi.fabianadrian.webhooklogger.common.listener.ListenerManager; import fi.fabianadrian.webhooklogger.common.platform.Platform; -import fi.fabianadrian.webhooklogger.paper.listener.PaperListenerRegistry; +import fi.fabianadrian.webhooklogger.paper.listener.PaperListenerManager; import net.kyori.adventure.audience.Audience; import org.bstats.bukkit.Metrics; import org.bukkit.command.CommandSender; @@ -21,13 +21,14 @@ public final class WebhookLoggerPaper extends JavaPlugin implements Platform { private WebhookLogger webhookLogger; private LegacyPaperCommandManager commandManager; - private PaperListenerRegistry listenerRegistry; + private PaperListenerManager listenerRegistry; @Override public void onEnable() { - this.listenerRegistry = new PaperListenerRegistry(this); createCommandManager(); this.webhookLogger = new WebhookLogger(this); + this.listenerRegistry = new PaperListenerManager(this); + this.webhookLogger.reload(); PluginManager manager = getServer().getPluginManager(); if (manager.isPluginEnabled("MiniPlaceholders")) { @@ -63,7 +64,7 @@ public CommandManager commandManager() { } @Override - public ListenerRegistry listenerRegistry() { + public ListenerManager listenerManager() { return this.listenerRegistry; } diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerFactory.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerFactory.java index 013bb1c..fa37a07 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerFactory.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerFactory.java @@ -1,6 +1,7 @@ package fi.fabianadrian.webhooklogger.paper.listener; import fi.fabianadrian.webhooklogger.common.WebhookLogger; +import fi.fabianadrian.webhooklogger.common.event.EventBuilder; import fi.fabianadrian.webhooklogger.common.event.EventType; import fi.fabianadrian.webhooklogger.common.listener.AbstractListener; import fi.fabianadrian.webhooklogger.common.listener.ListenerFactory; @@ -15,7 +16,7 @@ public PaperListenerFactory(WebhookLogger webhookLogger) { } @Override - public AbstractListener create(EventType type) { + public AbstractListener create(EventType type) { switch (type) { case CHAT -> { return new ChatListener(this.webhookLogger); diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerRegistry.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerManager.java similarity index 82% rename from paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerRegistry.java rename to paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerManager.java index 1097654..168bd9c 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerRegistry.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerManager.java @@ -1,17 +1,17 @@ package fi.fabianadrian.webhooklogger.paper.listener; import fi.fabianadrian.webhooklogger.common.listener.ListenerFactory; -import fi.fabianadrian.webhooklogger.common.listener.ListenerRegistry; +import fi.fabianadrian.webhooklogger.common.listener.ListenerManager; import fi.fabianadrian.webhooklogger.paper.WebhookLoggerPaper; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.plugin.PluginManager; -public class PaperListenerRegistry extends ListenerRegistry { +public class PaperListenerManager extends ListenerManager { private final WebhookLoggerPaper plugin; private final ListenerFactory factory; - public PaperListenerRegistry(WebhookLoggerPaper plugin) { + public PaperListenerManager(WebhookLoggerPaper plugin) { super(plugin.webhookLogger()); this.plugin = plugin; this.factory = new PaperListenerFactory(plugin.webhookLogger()); diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/WebhookLoggerSponge.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/WebhookLoggerSponge.java index 67429d8..3dec623 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/WebhookLoggerSponge.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/WebhookLoggerSponge.java @@ -3,7 +3,7 @@ import com.google.inject.Inject; import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.dependency.Dependency; -import fi.fabianadrian.webhooklogger.common.listener.ListenerRegistry; +import fi.fabianadrian.webhooklogger.common.listener.ListenerManager; import fi.fabianadrian.webhooklogger.common.platform.Platform; import net.kyori.adventure.audience.Audience; import org.bstats.sponge.Metrics; @@ -73,7 +73,7 @@ public CommandManager commandManager() { } @Override - public ListenerRegistry listenerRegistry() { + public ListenerManager listenerManager() { return null; } diff --git a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerRegistry.java b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerManager.java similarity index 80% rename from sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerRegistry.java rename to sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerManager.java index 6a5f75e..c016458 100644 --- a/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerRegistry.java +++ b/sponge/src/main/java/fi/fabianadrian/webhooklogger/sponge/listener/SpongeListenerManager.java @@ -2,17 +2,17 @@ import fi.fabianadrian.webhooklogger.common.WebhookLogger; import fi.fabianadrian.webhooklogger.common.listener.ListenerFactory; -import fi.fabianadrian.webhooklogger.common.listener.ListenerRegistry; +import fi.fabianadrian.webhooklogger.common.listener.ListenerManager; import org.spongepowered.api.Sponge; import org.spongepowered.api.event.EventManager; import org.spongepowered.plugin.PluginContainer; -public class SpongeListenerRegistry extends ListenerRegistry { +public class SpongeListenerManager extends ListenerManager { private final EventManager manager = Sponge.eventManager(); private final PluginContainer container; private final SpongeListenerFactory factory; - public SpongeListenerRegistry(WebhookLogger webhookLogger, PluginContainer container) { + public SpongeListenerManager(WebhookLogger webhookLogger, PluginContainer container) { super(webhookLogger); this.container = container; this.factory = new SpongeListenerFactory(webhookLogger); From 9b0b6066d80087471bb194aef015f81902358d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 22:06:22 +0300 Subject: [PATCH 15/18] fix permission --- paper/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paper/build.gradle.kts b/paper/build.gradle.kts index dc868f9..0fc1f55 100644 --- a/paper/build.gradle.kts +++ b/paper/build.gradle.kts @@ -20,7 +20,7 @@ paper { website = "https://github.com/fabianmakila/WebhookLogger" permissions { - register("webhooklogger.reload") { + register("webhooklogger.command.reload") { description = "Allows you to run the reload command" } } From 50ba01eae37f40ee44cd3b4b83b5f8f600cf4c8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 22:12:05 +0300 Subject: [PATCH 16/18] fix bugs --- .../webhooklogger/common/event/DeathEventBuilder.java | 2 +- .../webhooklogger/common/webhook/WebhookClient.java | 2 +- .../webhooklogger/paper/listener/PaperListenerManager.java | 2 +- .../webhooklogger/paper/listener/listeners/ChatListener.java | 1 - 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/DeathEventBuilder.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/DeathEventBuilder.java index 420bcde..ab03b5c 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/DeathEventBuilder.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/event/DeathEventBuilder.java @@ -37,7 +37,7 @@ public DeathEventBuilder message(@Nullable Component message) { String messageAsString = ""; if (message != null) { - this.serializer.serialize(message); + messageAsString = this.serializer.serialize(message); } this.resolverBuilder = this.resolverBuilder.resolver( diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookClient.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookClient.java index 0c751ea..cbb3537 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookClient.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookClient.java @@ -35,7 +35,7 @@ public void sendAll() { } // Construct webhook - WebHook webHook = WebHook.builder().content(String.join(", ", messages)).build(); + WebHook webHook = WebHook.builder().content(String.join("\n", messages)).build(); this.client.sendWebHook(webHook).thenAccept(response -> { switch (response.statusCode()) { case 204 -> this.messageQueue.removeAll(messages); diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerManager.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerManager.java index 168bd9c..d358705 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerManager.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/PaperListenerManager.java @@ -7,7 +7,7 @@ import org.bukkit.event.Listener; import org.bukkit.plugin.PluginManager; -public class PaperListenerManager extends ListenerManager { +public final class PaperListenerManager extends ListenerManager { private final WebhookLoggerPaper plugin; private final ListenerFactory factory; diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/ChatListener.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/ChatListener.java index 1840d12..772d04c 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/ChatListener.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/listener/listeners/ChatListener.java @@ -9,7 +9,6 @@ import org.bukkit.event.Listener; public final class ChatListener extends AbstractListener implements Listener { - public ChatListener(WebhookLogger webhookLogger) { super(webhookLogger); } From 476916c767a3c300430789be4caa553f4ef2861b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 22:27:33 +0300 Subject: [PATCH 17/18] add backwards support for 1.19.4 --- paper/build.gradle.kts | 4 ++-- .../fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/paper/build.gradle.kts b/paper/build.gradle.kts index 0fc1f55..797e5b4 100644 --- a/paper/build.gradle.kts +++ b/paper/build.gradle.kts @@ -15,7 +15,7 @@ dependencies { paper { main = "fi.fabianadrian.webhooklogger.paper.WebhookLoggerPaper" name = rootProject.name - apiVersion = "1.20.6" + apiVersion = "1.19" authors = listOf("FabianAdrian") website = "https://github.com/fabianmakila/WebhookLogger" @@ -31,6 +31,6 @@ tasks { downloadPlugins { modrinth("miniplaceholders", "J2guR3GH") } - minecraftVersion("1.20.6") + minecraftVersion("1.19.4") } } \ No newline at end of file diff --git a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java index e76935a..3181518 100644 --- a/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java +++ b/paper/src/main/java/fi/fabianadrian/webhooklogger/paper/WebhookLoggerPaper.java @@ -55,7 +55,7 @@ public Logger logger() { @Override public Path configPath() { - return getDataPath(); + return getDataFolder().toPath(); } @Override From 5790a47f2f26d9b665fea390885128e3fedac940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=A4kil=C3=A4?= Date: Thu, 26 Sep 2024 22:39:15 +0300 Subject: [PATCH 18/18] clean up messages --- .../webhooklogger/common/config/MainConfig.java | 7 ++----- .../webhooklogger/common/webhook/WebhookManager.java | 6 ++---- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java index 5a7c236..af56adf 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/config/MainConfig.java @@ -36,11 +36,7 @@ static Map defaultTextReplacements() { @AnnotationBasedSorter.Order(0) @ConfDefault.DefaultObject("defaultWebhooks") - @ConfComments({ - "Here you can configure your webhook URL's.", - "If no webhook is defined for a specific event the \"default\" webhook will be used instead.", - "Available events can be found in the events.yml file." - }) + @ConfComments("You should only configure 1 webhook per Discord channel to avoid rate limits.") List<@SubSection Webhook> webhooks(); @AnnotationBasedSorter.Order(1) @@ -72,6 +68,7 @@ interface Webhook { @AnnotationBasedSorter.Order(1) @ConfDefault.DefaultStrings({}) + @ConfComments("Available events: CHAT, COMMAND, DEATH and JOINQUIT") List events(); } } diff --git a/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookManager.java b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookManager.java index 9ef2f84..8f46c24 100644 --- a/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookManager.java +++ b/common/src/main/java/fi/fabianadrian/webhooklogger/common/webhook/WebhookManager.java @@ -45,14 +45,12 @@ private void parseWebhooks() { this.config.webhooks().forEach(webhook -> { if (webhook.url().isBlank()) { - logger.warn("webhook url blank"); - // TODO Better log message + logger.warn("You have a webhook with empty URL."); return; } if (webhook.events().isEmpty()) { - logger.warn("webhook events blank"); - // TODO Better log message + logger.warn("You have a webhook with empty events."); return; }