Skip to content

Commit

Permalink
more progress on refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianmakila committed Sep 26, 2024
1 parent 6a2e267 commit b139622
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,7 +26,7 @@ public final class WebhookLogger {
private final Platform platform;
private final CommandManager<Audience> 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();

Expand All @@ -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;
}
Expand All @@ -73,10 +70,6 @@ public Logger logger() {
return this.platform.logger();
}

public WebhookManager clientManager() {
return this.clientManager;
}

public ScheduledExecutorService scheduler() {
return this.scheduler;
}
Expand All @@ -89,14 +82,14 @@ public CommandManager<Audience> 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.<Audience>createNative()
MinecraftExceptionHandler.createNative()
.defaultHandlers()
.captionFormatter(new CaptionFormatter())
.registerTo(this.commandManager);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -10,5 +11,5 @@ public ListenerFactory(WebhookLogger webhookLogger) {
this.webhookLogger = webhookLogger;
}

protected abstract AbstractListener<?> create(EventType eventType);
protected abstract AbstractListener<? extends EventBuilder> create(EventType eventType);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
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;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class ListenerRegistry {
public abstract class ListenerManager {
protected final WebhookLogger webhookLogger;
protected Map<EventType, AbstractListener<?>> registry = new HashMap<>();
protected Map<EventType, AbstractListener<? extends EventBuilder>> registry = new HashMap<>();

public ListenerRegistry(WebhookLogger webhookLogger) {
public ListenerManager(WebhookLogger webhookLogger) {
this.webhookLogger = webhookLogger;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,5 +11,5 @@ public interface Platform {
Logger logger();
Path configPath();
CommandManager<Audience> commandManager();
ListenerRegistry listenerRegistry();
ListenerManager listenerManager();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void queue(String message) {
this.messageQueue.add(message);
}

public void sendAll() throws RuntimeException {
public void sendAll() {
// Copy messageBuffer
List<String> messages = List.copyOf(this.messageQueue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,13 +21,14 @@
public final class WebhookLoggerPaper extends JavaPlugin implements Platform {
private WebhookLogger webhookLogger;
private LegacyPaperCommandManager<Audience> 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")) {
Expand Down Expand Up @@ -63,7 +64,7 @@ public CommandManager<Audience> commandManager() {
}

@Override
public ListenerRegistry listenerRegistry() {
public ListenerManager listenerManager() {
return this.listenerRegistry;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,7 +16,7 @@ public PaperListenerFactory(WebhookLogger webhookLogger) {
}

@Override
public AbstractListener<?> create(EventType type) {
public AbstractListener<? extends EventBuilder> create(EventType type) {
switch (type) {
case CHAT -> {
return new ChatListener(this.webhookLogger);
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,7 +73,7 @@ public CommandManager<Audience> commandManager() {
}

@Override
public ListenerRegistry listenerRegistry() {
public ListenerManager listenerManager() {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b139622

Please sign in to comment.