From 040846ef0a33b95cac1298fc3296de499fcdea49 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 4 Jul 2024 02:47:36 +0100 Subject: [PATCH] fix shutdown db saving (fr this time) and fixed embed color parsing --- .../cc/fascinated/bat/BatApplication.java | 10 ++++- .../fascinated/bat/common/HexColorUtils.java | 45 +++++++++++++++++++ .../bat/features/welcomer/WelcomerEmbed.java | 3 +- .../welcomer/command/EmbedSubCommand.java | 11 ++--- 4 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 src/main/java/cc/fascinated/bat/common/HexColorUtils.java diff --git a/src/main/java/cc/fascinated/bat/BatApplication.java b/src/main/java/cc/fascinated/bat/BatApplication.java index fb76d9f..d55bf03 100644 --- a/src/main/java/cc/fascinated/bat/BatApplication.java +++ b/src/main/java/cc/fascinated/bat/BatApplication.java @@ -12,6 +12,7 @@ import lombok.SneakyThrows; import lombok.extern.log4j.Log4j2; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.scheduling.annotation.EnableScheduling; import java.io.File; @@ -39,15 +40,20 @@ public class BatApplication { } log.info("Found configuration at '{}'", config.getAbsolutePath()); // Log the found config + // Start the application + SpringApplication app = new SpringApplication(BatApplication.class); + app.setRegisterShutdownHook(false); // Disable the default shutdown hook + ConfigurableApplicationContext context = app.run(args); + + // Register the shutdown hook Runtime.getRuntime().addShutdownHook(new Thread(() -> { log.info("Shutting down..."); for (EventListener listener : EventService.LISTENERS) { listener.onShutdown(); } + context.close(); })); - // Start the app - SpringApplication.run(BatApplication.class, args); log.info("APP IS RUNNING IN %s MODE!!!!!!!!!".formatted(Config.isProduction() ? "PRODUCTION" : "DEVELOPMENT")); } } \ No newline at end of file diff --git a/src/main/java/cc/fascinated/bat/common/HexColorUtils.java b/src/main/java/cc/fascinated/bat/common/HexColorUtils.java new file mode 100644 index 0000000..bb4bea3 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/common/HexColorUtils.java @@ -0,0 +1,45 @@ +package cc.fascinated.bat.common; + +import java.awt.*; + +/** + * @author Fascinated (fascinated7) + */ +public class HexColorUtils { + /** + * Checks if the given string is a hex color + * + * @param hexColor the hex color to check + * @return if the given string is a hex color + */ + public static boolean isHexColor(String hexColor) { + return hexColor.matches("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"); + } + + /** + * Converts a hex color to a Color object + * + * @param hex the hex color to convert + * @return the Color object + */ + public static Color hexToColor(String hex) { + if (hex == null || (!hex.matches("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"))) { + throw new IllegalArgumentException("Invalid hex color code"); + } + + // Remove the '#' character + hex = hex.substring(1); + + // If the hex code is 3 characters long, expand it to 6 characters + if (hex.length() == 3) { + char r = hex.charAt(0); + char g = hex.charAt(1); + char b = hex.charAt(2); + hex = "" + r + r + g + g + b + b; + } + + // Convert the hex string to an integer and create a Color object + int rgb = Integer.parseInt(hex, 16); + return new Color(rgb); + } +} diff --git a/src/main/java/cc/fascinated/bat/features/welcomer/WelcomerEmbed.java b/src/main/java/cc/fascinated/bat/features/welcomer/WelcomerEmbed.java index c7197f8..01705b1 100644 --- a/src/main/java/cc/fascinated/bat/features/welcomer/WelcomerEmbed.java +++ b/src/main/java/cc/fascinated/bat/features/welcomer/WelcomerEmbed.java @@ -1,5 +1,6 @@ package cc.fascinated.bat.features.welcomer; +import cc.fascinated.bat.common.HexColorUtils; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NonNull; @@ -44,7 +45,7 @@ public class WelcomerEmbed { embedBuilder.setTitle(WelcomerPlaceholders.replaceAllPlaceholders(title, replacements)); } embedBuilder.setDescription(WelcomerPlaceholders.replaceAllPlaceholders(description, replacements)); - embedBuilder.setColor(Integer.parseInt(color, 16)); + embedBuilder.setColor(HexColorUtils.hexToColor(color)); return embedBuilder; } } diff --git a/src/main/java/cc/fascinated/bat/features/welcomer/command/EmbedSubCommand.java b/src/main/java/cc/fascinated/bat/features/welcomer/command/EmbedSubCommand.java index 4c6c283..61eea35 100644 --- a/src/main/java/cc/fascinated/bat/features/welcomer/command/EmbedSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/welcomer/command/EmbedSubCommand.java @@ -5,6 +5,7 @@ import cc.fascinated.bat.command.BatSubCommand; import cc.fascinated.bat.command.CommandInfo; import cc.fascinated.bat.common.EmbedDescriptionBuilder; import cc.fascinated.bat.common.EmbedUtils; +import cc.fascinated.bat.common.HexColorUtils; import cc.fascinated.bat.features.welcomer.WelcomerProfile; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; @@ -46,13 +47,13 @@ public class EmbedSubCommand extends BatSubCommand { String color = colorOption.getAsString(); boolean pingBeforeSend = pingBeforeSendOption.getAsBoolean(); - // Remove # if the user added it - color = color.replace("#", ""); + // Add the # if it's not there + color = !color.startsWith("#") ? "#" + color : color; // Validate the input - if (color.length() != 6 || Color.decode("#" + color).getRGB() == -1){ + if (!HexColorUtils.isHexColor(color)){ event.replyEmbeds(EmbedUtils.errorEmbed() - .setDescription("The color must be a valid hex color code\n" + + .setDescription("The color must be a valid hex color code Example: `#3498DB`\n" + "You can use this website to get a hex color code: https://htmlcolorcodes.com") .build()).queue(); return; @@ -84,7 +85,7 @@ public class EmbedSubCommand extends BatSubCommand { successDescription.appendLine("Title: `%s`".formatted(title), true); } successDescription.appendLine("Description: `%s`".formatted(description), true); - successDescription.appendLine("Color: `#%s`".formatted(color), true); + successDescription.appendLine("Color: `%s`".formatted(color), true); successDescription.appendLine("Ping Before Send: %s".formatted(pingBeforeSend ? Emojis.CHECK_MARK_EMOJI.getFormatted() + " *(Preview won't ping you)*" : Emojis.CROSS_MARK_EMOJI), true); successDescription.emptyLine(); successDescription.appendLine("**Preview Below:**", false);