From 97670c9367a4b3544f4a0b7466e3e2840d8375f9 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Wed, 26 Jan 2022 18:02:53 +0100 Subject: [PATCH] Adds the possibility to use the inverted default colors for per-sign colors Adds code for getting inverted colors if "inverted" is given as the per-sign color Fixes exceptions thrown when the per-sign config value is malformed Changes the default to use inverted colors for crimson, dark_oak, spruce and warped signs Adds methods to get main and highlighting colors from the portal sign drawer in case the given default colors are invalid and fallback colors are necessary instead --- .../stargate/config/ConfigOption.java | 4 +- .../stargate/config/StargateGateConfig.java | 103 +++++++++++++++--- .../stargate/portal/PortalSignDrawer.java | 18 +++ src/main/resources/config.yml | 8 +- 4 files changed, 109 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/knarcraft/stargate/config/ConfigOption.java b/src/main/java/net/knarcraft/stargate/config/ConfigOption.java index 622639c..76d3870 100644 --- a/src/main/java/net/knarcraft/stargate/config/ConfigOption.java +++ b/src/main/java/net/knarcraft/stargate/config/ConfigOption.java @@ -51,8 +51,8 @@ public enum ConfigOption { HIGHLIGHT_SIGN_COLOR("gates.cosmetic.highlightSignColor", "The text color used for highlighting stargate signs", "WHITE"), PER_SIGN_COLORS("gates.cosmetic.perSignColors", "The per-sign color specification", new String[]{ - "'ACACIA:default,default'", "'BIRCH:default,default'", "'CRIMSON:WHITE,BLACK'", "'DARK_OAK:WHITE,BLACK'", - "'JUNGLE:default,default'", "'OAK:default,default'", "'SPRUCE:WHITE,BLACK'", "'WARPED:WHITE,BLACK'"}), + "'ACACIA:default,default'", "'BIRCH:default,default'", "'CRIMSON:inverted,inverted'", "'DARK_OAK:inverted,inverted'", + "'JUNGLE:default,default'", "'OAK:default,default'", "'SPRUCE:inverted,inverted'", "'WARPED:inverted,inverted'"}), /** * Whether to destroy portals when any blocks are broken by explosions diff --git a/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java b/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java index ca26f20..7ac1858 100644 --- a/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java +++ b/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java @@ -2,9 +2,12 @@ package net.knarcraft.stargate.config; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.portal.PortalSignDrawer; +import net.knarcraft.stargate.utility.ColorHelper; import net.md_5.bungee.api.ChatColor; +import org.bukkit.Color; import org.bukkit.Material; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -181,27 +184,91 @@ public final class StargateGateConfig { */ private void loadGateConfig() { //Load the sign colors - loadSignColor((String) configOptions.get(ConfigOption.MAIN_SIGN_COLOR), - (String) configOptions.get(ConfigOption.HIGHLIGHT_SIGN_COLOR)); - List perSignColors = (List) configOptions.get(ConfigOption.PER_SIGN_COLORS); + String mainSignColor = (String) configOptions.get(ConfigOption.MAIN_SIGN_COLOR); + String highlightSignColor = (String) configOptions.get(ConfigOption.HIGHLIGHT_SIGN_COLOR); + loadPerSignColor(mainSignColor, highlightSignColor); + loadPerSignColors(); + } + + /** + * Loads the per-sign colors specified in the config file + */ + private void loadPerSignColors() { + List perSignColors = (List) configOptions.get(ConfigOption.PER_SIGN_COLORS); + ChatColor[] defaultColors = new ChatColor[]{PortalSignDrawer.getMainColor(), PortalSignDrawer.getHighlightColor()}; + List> colorMaps = new ArrayList<>(); + colorMaps.add(new HashMap<>()); + colorMaps.add(new HashMap<>()); - Map signMainColors = new HashMap<>(); - Map signHighlightColors = new HashMap<>(); for (Object signColorSpecification : perSignColors) { - String[] specificationData = String.valueOf(signColorSpecification).split(":"); - String[] colors = specificationData[1].split(","); - if (!colors[0].equalsIgnoreCase("default") && ChatColor.of(colors[0]) != null) { - signMainColors.put(Material.matchMaterial(specificationData[0] + "_SIGN"), ChatColor.of(colors[0])); - signMainColors.put(Material.matchMaterial(specificationData[0] + "_WALL_SIGN"), ChatColor.of(colors[0])); - } - if (!colors[1].equalsIgnoreCase("default") && ChatColor.of(colors[1]) != null) { - signHighlightColors.put(Material.matchMaterial(specificationData[0] + "_SIGN"), ChatColor.of(colors[1])); - signHighlightColors.put(Material.matchMaterial(specificationData[0] + "_WALL_SIGN"), ChatColor.of(colors[1])); - } + parsePerSignColors(signColorSpecification, defaultColors, colorMaps); } - PortalSignDrawer.setPerSignMainColors(signMainColors); - PortalSignDrawer.setPerSignHighlightColors(signHighlightColors); + PortalSignDrawer.setPerSignMainColors(colorMaps.get(0)); + PortalSignDrawer.setPerSignHighlightColors(colorMaps.get(1)); + } + + /** + * Parses a per-sign color specification object and stores the result + * + * @param signColorSpecification

The sign color specification to parse

+ * @param defaultColors

The specified default colors

+ * @param colorMaps

The list of color maps to save the resulting colors to

+ */ + private void parsePerSignColors(Object signColorSpecification, ChatColor[] defaultColors, + List> colorMaps) { + String[] specificationData = String.valueOf(signColorSpecification).split(":"); + Material[] signMaterials = new Material[]{Material.matchMaterial(specificationData[0] + "_SIGN"), + Material.matchMaterial(specificationData[0] + "_WALL_SIGN")}; + + if (specificationData.length != 2) { + Stargate.logWarning("You have an invalid per-sign line in your config.yml file. Please fix it!"); + return; + } + String[] colors = specificationData[1].split(","); + if (colors.length != 2) { + Stargate.logWarning("You have an invalid per-sign line in your config.yml file. Please fix it!"); + return; + } + for (int colorIndex = 0; colorIndex < 2; colorIndex++) { + if (colors[colorIndex].equalsIgnoreCase("default")) { + continue; + } + loadPerSignColor(colors, colorIndex, defaultColors, signMaterials, colorMaps); + } + } + + /** + * Loads a per-sign color + * + * @param colors

The colors specified in the config file

+ * @param colorIndex

The index of the color to load

+ * @param defaultColors

The specified default colors

+ * @param signMaterials

The materials to load this color for

+ * @param colorMaps

The list of color maps to save the resulting color to

+ */ + private void loadPerSignColor(String[] colors, int colorIndex, ChatColor[] defaultColors, Material[] signMaterials, + List> colorMaps) { + ChatColor parsedColor; + if (colors[colorIndex].equalsIgnoreCase("inverted")) { + //Convert from ChatColor to awt.Color to Bukkit.Color then invert and convert to ChatColor + java.awt.Color color = defaultColors[colorIndex].getColor(); + parsedColor = ColorHelper.fromColor(ColorHelper.invert(Color.fromRGB(color.getRed(), + color.getGreen(), color.getBlue()))); + } else { + try { + parsedColor = ChatColor.of(colors[colorIndex]); + } catch (IllegalArgumentException | NullPointerException exception) { + Stargate.logWarning("You have specified an invalid per-sign color in your config.yml. Custom color for \"" + + signMaterials[0] + "\" disabled"); + return; + } + } + if (parsedColor != null) { + for (Material signMaterial : signMaterials) { + colorMaps.get(colorIndex).put(signMaterial, parsedColor); + } + } } /** @@ -209,7 +276,7 @@ public final class StargateGateConfig { * * @param mainSignColor

A string representing the main sign color

*/ - private void loadSignColor(String mainSignColor, String highlightSignColor) { + private void loadPerSignColor(String mainSignColor, String highlightSignColor) { try { PortalSignDrawer.setMainColor(ChatColor.of(mainSignColor.toUpperCase())); PortalSignDrawer.setHighlightColor(ChatColor.of(highlightSignColor.toUpperCase())); diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java b/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java index 299aad7..2bad4c4 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java @@ -85,6 +85,24 @@ public class PortalSignDrawer { PortalSignDrawer.perSignHighlightColors = signHighlightColors; } + /** + * Gets the currently used main sign color + * + * @return

The currently used main sign color

+ */ + public static ChatColor getMainColor() { + return mainColor; + } + + /** + * Gets the currently used highlighting sign color + * + * @return

The currently used highlighting sign color

+ */ + public static ChatColor getHighlightColor() { + return highlightColor; + } + /** * Draws the sign of the portal this sign drawer is responsible for */ diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 769c35a..46d18d2 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -27,12 +27,12 @@ gates: perSignColors: - 'ACACIA:default,default' - 'BIRCH:default,default' - - 'CRIMSON:WHITE,BLACK' - - 'DARK_OAK:WHITE,BLACK' + - 'CRIMSON:inverted,inverted' + - 'DARK_OAK:inverted,inverted' - 'JUNGLE:default,default' - 'OAK:default,default' - - 'SPRUCE:WHITE,BLACK' - - 'WARPED:WHITE,BLACK' + - 'SPRUCE:inverted,inverted' + - 'WARPED:inverted,inverted' integrity: # destroyedByExplosion - Whether to destroy gates with explosions (Creeper, TNT, etc.) destroyedByExplosion: false