Skip to content

Commit

Permalink
Merge pull request #13 from fabianmakila/refactor
Browse files Browse the repository at this point in the history
Refactor bunch of things
  • Loading branch information
fabianmakila authored Sep 26, 2024
2 parents d677651 + 5790a47 commit 4d18928
Show file tree
Hide file tree
Showing 41 changed files with 550 additions and 445 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
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.WebhookLoggerCaptionFormatter;
import fi.fabianadrian.webhooklogger.common.command.WebhookLoggerCommand;
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;
import org.incendo.cloud.minecraft.extras.caption.TranslatableCaption;
Expand All @@ -22,32 +23,33 @@
import java.util.concurrent.ScheduledExecutorService;

public final class WebhookLogger {
private final Logger logger;
private final CommandManager<Commander> commandManager;
private final Platform platform;
private final CommandManager<Audience> commandManager;
private final ConfigManager configManager;
private final ClientManager clientManager;
private final WebhookManager webhookManager;
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
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();
registerCommands();

this.clientManager = new ClientManager(this);

reload();
this.webhookManager = new WebhookManager(this);
}

public boolean reload() {
boolean success = this.configManager.reload();
this.clientManager.reload();
this.platform.listenerManager().unregisterAll();

this.webhookManager.reload();
this.platform.listenerManager().registerAll();

return success;
}
Expand All @@ -65,11 +67,7 @@ public EventsConfig eventsConfig() {
}

public Logger logger() {
return this.logger;
}

public ClientManager clientManager() {
return this.clientManager;
return this.platform.logger();
}

public ScheduledExecutorService scheduler() {
Expand All @@ -80,22 +78,26 @@ public DependencyManager dependencyManager() {
return this.dependencyManager;
}

public CommandManager<Commander> commandManager() {
public CommandManager<Audience> commandManager() {
return commandManager;
}

public ListenerManager listenerManager() {
return this.platform.listenerManager();
}

private void setupCommandManager() {
this.commandManager.registerCommandPreProcessor(new WebhookLoggerCommandPreprocessor(this));
this.commandManager.captionRegistry().registerProvider(TranslatableCaption.translatableCaptionProvider());
MinecraftExceptionHandler.<Commander>createNative()
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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package fi.fabianadrian.webhooklogger.common.command;

import fi.fabianadrian.webhooklogger.common.WebhookLogger;
import org.incendo.cloud.Command;
import net.kyori.adventure.audience.Audience;
import org.incendo.cloud.CommandManager;

public abstract class WebhookLoggerCommand {
public abstract class BaseCommand {
protected final WebhookLogger webhookLogger;
protected final CommandManager<Commander> manager;
protected final CommandManager<Audience> manager;

public WebhookLoggerCommand(WebhookLogger webhookLogger) {
public BaseCommand(WebhookLogger webhookLogger) {
this.webhookLogger = webhookLogger;
this.manager = webhookLogger.commandManager();
}

protected Command.Builder<Commander> rootBuilder() {
protected org.incendo.cloud.Command.Builder<Audience> rootBuilder() {
return this.manager.commandBuilder("webhooklogger");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,9 +14,9 @@
import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.Component.translatable;

public final class WebhookLoggerCaptionFormatter implements ComponentCaptionFormatter<Commander> {
public final class CaptionFormatter implements ComponentCaptionFormatter<Audience> {
@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;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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.WebhookLoggerCommand;
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;

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)
Expand All @@ -32,7 +32,7 @@ public void register() {
);
}

private void executeReload(CommandContext<Commander> context) {
private void executeReload(CommandContext<Audience> context) {
if (this.webhookLogger.reload()) {
context.sender().sendMessage(COMPONENT_SUCCESS);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
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<Commander> {
public final class WebhookLoggerCommandPreprocessor implements CommandPreprocessor<Audience> {
private final WebhookLogger webhookLogger;

public WebhookLoggerCommandPreprocessor(WebhookLogger webhookLogger) {
this.webhookLogger = webhookLogger;
}

@Override
public void accept(@NonNull CommandPreprocessingContext<Commander> context) {
CommandContext<Commander> commandContext = context.commandContext();
public void accept(@NonNull CommandPreprocessingContext<Audience> context) {
CommandContext<Audience> commandContext = context.commandContext();
commandContext.store(ContextKeys.WEBHOOK_LOGGER_KEY, this.webhookLogger);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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.logger.error("Could not load config.yml, falling back to default configuration", 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.logger.error("Could not load events.yml, falling back to default configuration", e);
this.eventsConfigData = this.eventsConfigHelper.getFactory().loadDefaults();
success = false;
}

return success;
}

private <C> C loadConfig(ConfigurationHelper<C> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();

Expand Down
Loading

0 comments on commit 4d18928

Please sign in to comment.