Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianmakila committed Oct 2, 2024
1 parent b46d0fb commit 65c509b
Show file tree
Hide file tree
Showing 31 changed files with 143 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,64 +35,64 @@ public WebhookLogger(Platform platform) {

new TranslationManager(platform.logger());

this.configManager = new ConfigManager(platform.configPath(), platform.logger());
configManager = new ConfigManager(platform.configPath(), platform.logger());

this.commandManager = platform.commandManager();
commandManager = platform.commandManager();
setupCommandManager();
registerCommands();

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

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

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

return success;
}

public void shutdown() {
this.scheduler.shutdown();
scheduler.shutdown();
}

public MainConfig mainConfig() {
return this.configManager.mainConfig();
return configManager.mainConfig();
}

public EventsConfig eventsConfig() {
return this.configManager.eventsConfig();
return configManager.eventsConfig();
}

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

public ScheduledExecutorService scheduler() {
return this.scheduler;
return scheduler;
}

public DependencyManager dependencyManager() {
return this.dependencyManager;
return dependencyManager;
}

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

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

private void setupCommandManager() {
this.commandManager.registerCommandPreProcessor(new WebhookLoggerCommandPreprocessor(this));
this.commandManager.captionRegistry().registerProvider(TranslatableCaption.translatableCaptionProvider());
commandManager.registerCommandPreProcessor(new WebhookLoggerCommandPreprocessor(this));
commandManager.captionRegistry().registerProvider(TranslatableCaption.translatableCaptionProvider());
MinecraftExceptionHandler.createNative()
.defaultHandlers()
.captionFormatter(new CaptionFormatter())
.registerTo(this.commandManager);
.registerTo(commandManager);
}

private void registerCommands() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public abstract class BaseCommand {

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

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

public abstract void register();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public ReloadCommand(WebhookLogger webhookLogger) {

@Override
public void register() {
this.manager.command(rootBuilder()
manager.command(rootBuilder()
.literal("reload")
.permission("webhooklogger.command.reload")
.handler(this::executeReload)
);
}

private void executeReload(CommandContext<Audience> context) {
if (this.webhookLogger.reload()) {
if (webhookLogger.reload()) {
context.sender().sendMessage(COMPONENT_SUCCESS);
} else {
context.sender().sendMessage(COMPONENT_FAILURE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public WebhookLoggerCommandPreprocessor(WebhookLogger webhookLogger) {
@Override
public void accept(@NonNull CommandPreprocessingContext<Audience> context) {
CommandContext<Audience> commandContext = context.commandContext();
commandContext.store(ContextKeys.WEBHOOK_LOGGER_KEY, this.webhookLogger);
commandContext.store(ContextKeys.WEBHOOK_LOGGER_KEY, webhookLogger);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ public ConfigManager(Path configPath, Logger logger) {
this.logger = logger;

ConfigurationHelperFactory helperFactory = new ConfigurationHelperFactory(configPath);
this.mainConfigHelper = helperFactory.create("config.yml", MainConfig.class);
this.eventsConfigHelper = helperFactory.create("events.yml", EventsConfig.class);
mainConfigHelper = helperFactory.create("config.yml", MainConfig.class);
eventsConfigHelper = helperFactory.create("events.yml", EventsConfig.class);
}

public MainConfig mainConfig() {
MainConfig configData = this.mainConfigData;
MainConfig configData = mainConfigData;
if (configData == null) {
throw new IllegalStateException("Configuration has not been loaded yet");
}
return configData;
}

public EventsConfig eventsConfig() {
EventsConfig configData = this.eventsConfigData;
EventsConfig configData = eventsConfigData;
if (configData == null) {
throw new IllegalStateException("Configuration has not been loaded yet");
}
Expand All @@ -40,18 +40,18 @@ public EventsConfig eventsConfig() {
public boolean reload() {
boolean success = true;
try {
this.mainConfigData = this.mainConfigHelper.reloadConfigData();
mainConfigData = mainConfigHelper.reloadConfigData();
} catch (Exception e) {
this.logger.error("Could not load config.yml, falling back to default configuration", e);
this.mainConfigData = this.mainConfigHelper.getFactory().loadDefaults();
logger.error("Could not load config.yml, falling back to default configuration", e);
mainConfigData = mainConfigHelper.getFactory().loadDefaults();
success = false;
}

try {
this.eventsConfigData = this.eventsConfigHelper.reloadConfigData();
eventsConfigData = eventsConfigHelper.reloadConfigData();
} catch (Exception e) {
this.logger.error("Could not load events.yml, falling back to default configuration", e);
this.eventsConfigData = this.eventsConfigHelper.getFactory().loadDefaults();
logger.error("Could not load events.yml, falling back to default configuration", e);
eventsConfigData = eventsConfigHelper.getFactory().loadDefaults();
success = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public ConfigurationHelperFactory(Path configurationPath) {
public <C> ConfigurationHelper<C> create(String fileName, Class<C> configClass) {
ConfigurationFactory<C> factory = SnakeYamlConfigurationFactory.create(
configClass,
this.options,
this.yamlOptions
options,
yamlOptions
);
return new ConfigurationHelper<>(this.configurationPath, fileName, factory);
return new ConfigurationHelper<>(configurationPath, fileName, factory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ public final class DependencyManager {
private final List<Dependency> presentDependenciesList = new ArrayList<>();

public boolean isPresent(Dependency dependency) {
return this.presentDependenciesList.contains(dependency);
return presentDependenciesList.contains(dependency);
}

public void markAsPresent(Dependency dependency) {
this.presentDependenciesList.add(dependency);
presentDependenciesList.add(dependency);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ChatEventBuilder cancelled(boolean cancelled) {
}

public ChatEventBuilder message(Component message) {
this.resolverBuilder = this.resolverBuilder.resolver(
resolverBuilder = resolverBuilder.resolver(
Placeholder.component("message", message)
);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public CommandEventBuilder command(String command) {
command = "/" + command;
}

this.resolverBuilder = this.resolverBuilder.resolver(
resolverBuilder = resolverBuilder.resolver(
Placeholder.unparsed("command", command)
);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public DeathEventBuilder audience(Audience audience) {

public DeathEventBuilder location(int x, int y, int z) {
String locationString = String.format("x%s, y%s, z%s", x, y, z);
this.resolverBuilder = this.resolverBuilder.resolver(
resolverBuilder = resolverBuilder.resolver(
Placeholder.unparsed("location", locationString)
);

Expand All @@ -37,10 +37,10 @@ public DeathEventBuilder message(@Nullable Component message) {
String messageAsString = "";

if (message != null) {
messageAsString = this.serializer.serialize(message);
messageAsString = serializer.serialize(message);
}

this.resolverBuilder = this.resolverBuilder.resolver(
resolverBuilder = resolverBuilder.resolver(
Placeholder.unparsed("message", messageAsString)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public EventBuilder(WebhookLogger webhookLogger, EventType eventType, String for
this.eventType = eventType;
this.format = format;

this.placeholderConfig = webhookLogger.mainConfig().placeholders();
placeholderConfig = webhookLogger.mainConfig().placeholders();

DateTimeFormatter formatter = DateTimeFormatter
.ofPattern(this.placeholderConfig.timestampFormat())
.withZone(this.placeholderConfig.timestampTimezone());
this.resolverBuilder = TagResolver.builder().resolvers(
.ofPattern(placeholderConfig.timestampFormat())
.withZone(placeholderConfig.timestampTimezone());
resolverBuilder = TagResolver.builder().resolvers(
Placeholder.unparsed("timestamp", formatter.format(Instant.now()))
);
}
Expand All @@ -44,33 +44,33 @@ protected EventBuilder audience(Audience audience) {
UUID uuid = audience.getOrDefault(Identity.UUID, null);
String uuidAsString = uuid == null ? "unknown" : uuid.toString();

this.resolverBuilder = this.resolverBuilder.resolvers(
resolverBuilder = resolverBuilder.resolvers(
Placeholder.unparsed("audience_name", name),
Placeholder.component("audience_display_name", displayName),
Placeholder.unparsed("audience_uuid", uuidAsString)
);

if (this.webhookLogger.dependencyManager().isPresent(Dependency.MINI_PLACEHOLDERS)) {
this.resolverBuilder = this.resolverBuilder.resolver(MiniPlaceholders.getAudienceGlobalPlaceholders(audience));
if (webhookLogger.dependencyManager().isPresent(Dependency.MINI_PLACEHOLDERS)) {
resolverBuilder = resolverBuilder.resolver(MiniPlaceholders.getAudienceGlobalPlaceholders(audience));
}

return this;
}

protected EventBuilder cancelled(boolean cancelled) {
String cancelledString = cancelled ? this.placeholderConfig.cancelled() : "";
this.resolverBuilder = resolverBuilder.resolver(Placeholder.unparsed("cancelled", cancelledString));
String cancelledString = cancelled ? placeholderConfig.cancelled() : "";
resolverBuilder = resolverBuilder.resolver(Placeholder.unparsed("cancelled", cancelledString));
return this;
}

public Component component() {
return MiniMessage.miniMessage().deserialize(
this.format,
this.resolverBuilder.build()
format,
resolverBuilder.build()
);
}

public EventType type() {
return this.eventType;
return eventType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public JoinQuitEventBuilder audience(Audience audience) {

public JoinQuitEventBuilder location(int x, int y, int z) {
String locationString = String.format("x%s, y%s, z%s", x, y, z);
this.resolverBuilder = this.resolverBuilder.resolver(
resolverBuilder = resolverBuilder.resolver(
Placeholder.unparsed("location", locationString)
);

Expand All @@ -39,18 +39,18 @@ public JoinQuitEventBuilder message(@Nullable Component message) {
String messageAsString = "";

if (message != null) {
messageAsString = this.serializer.serialize(message);
messageAsString = serializer.serialize(message);
}

this.resolverBuilder = this.resolverBuilder.resolver(
resolverBuilder = resolverBuilder.resolver(
Placeholder.unparsed("message", messageAsString)
);

return this;
}

public JoinQuitEventBuilder address(InetSocketAddress address) {
this.resolverBuilder = this.resolverBuilder.resolver(
resolverBuilder = resolverBuilder.resolver(
Placeholder.unparsed("address", String.valueOf(address))
);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ public abstract class AbstractListener<B extends EventBuilder> {

public AbstractListener(WebhookLogger webhookLogger) {
this.webhookLogger = webhookLogger;
this.replacements = webhookLogger.mainConfig().textReplacements();
replacements = webhookLogger.mainConfig().textReplacements();
}

public void addWebhook(WebhookClient client) {
this.clients.add(client);
clients.add(client);
}

protected void queue(B builder) {
String discordSerialized = DiscordSerializer.INSTANCE.serialize(builder.component());
for (Map.Entry<Pattern, String> entry : this.replacements.entrySet()) {
for (Map.Entry<Pattern, String> entry : 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));
clients.forEach(client -> client.queue(finalDiscordSerialized));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public void registerWebhookForEvents(WebhookClient client, List<EventType> event
protected abstract ListenerFactory factory();

private void registerWebhook(WebhookClient webhookClient, EventType type) {
this.registry.computeIfAbsent(type, k -> factory().create(type)).addWebhook(webhookClient);
registry.computeIfAbsent(type, k -> factory().create(type)).addWebhook(webhookClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ public final class TranslationManager {
public TranslationManager(Logger logger) {
this.logger = logger;

this.registry = TranslationRegistry.create(Key.key("webhooklogger", "main"));
this.registry.defaultLocale(DEFAULT_LOCALE);
registry = TranslationRegistry.create(Key.key("webhooklogger", "main"));
registry.defaultLocale(DEFAULT_LOCALE);

loadFromResourceBundle();

GlobalTranslator.translator().addSource(this.registry);
GlobalTranslator.translator().addSource(registry);
}

private void loadFromResourceBundle() {
ResourceBundle defaultBundle = ResourceBundle.getBundle("messages", DEFAULT_LOCALE);
try {
this.registry.registerAll(DEFAULT_LOCALE, defaultBundle, false);
registry.registerAll(DEFAULT_LOCALE, defaultBundle, false);
BUNDLED_LOCALES.forEach(locale -> {
ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
this.registry.registerAll(locale, bundle, false);
registry.registerAll(locale, bundle, false);
});
} catch (IllegalArgumentException e) {
this.logger.warn("Error loading default locale file", e);
logger.warn("Error loading default locale file", e);
}
}
}
Loading

0 comments on commit 65c509b

Please sign in to comment.