From bfe80be027574eb62ef94f696bdf88251bd94f57 Mon Sep 17 00:00:00 2001 From: Diogo Correia Date: Wed, 26 Jun 2024 01:09:51 +0100 Subject: [PATCH] fix(spigot): conversion of banner to item stack The values of the PatternType enum have changed in recent MC versions. Added tests. --- triton-spigot/build.gradle | 12 ++ .../triton/spigot/banners/BannerBuilder.java | 15 +-- .../triton/spigot/banners/Colors.java | 47 +++++--- .../triton/spigot/banners/Patterns.java | 109 +++++++++--------- .../triton/spigot/banners/ColorsTest.java | 32 +++++ .../triton/spigot/banners/PatternsTest.java | 55 +++++++++ 6 files changed, 190 insertions(+), 80 deletions(-) create mode 100644 triton-spigot/src/test/java/com/rexcantor64/triton/spigot/banners/ColorsTest.java create mode 100644 triton-spigot/src/test/java/com/rexcantor64/triton/spigot/banners/PatternsTest.java diff --git a/triton-spigot/build.gradle b/triton-spigot/build.gradle index d657c875..c822881f 100644 --- a/triton-spigot/build.gradle +++ b/triton-spigot/build.gradle @@ -5,6 +5,10 @@ plugins { group 'com.rexcantor64.triton' +test { + useJUnitPlatform() +} + dependencies { compileOnly project(':core:triton-core-loader') compileOnly project(':api') @@ -27,6 +31,14 @@ dependencies { implementation 'net.byteflux:libby-bukkit:1.3.0' implementation 'org.bstats:bstats-bukkit:3.0.0' + + // Test dependencies + testImplementation project(":api") + + testImplementation 'org.spigotmc:spigot-api:1.21-R0.1-SNAPSHOT' + + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0' } shadowJar { diff --git a/triton-spigot/src/main/java/com/rexcantor64/triton/spigot/banners/BannerBuilder.java b/triton-spigot/src/main/java/com/rexcantor64/triton/spigot/banners/BannerBuilder.java index 9f9ab1a1..06f2297a 100644 --- a/triton-spigot/src/main/java/com/rexcantor64/triton/spigot/banners/BannerBuilder.java +++ b/triton-spigot/src/main/java/com/rexcantor64/triton/spigot/banners/BannerBuilder.java @@ -1,14 +1,11 @@ package com.rexcantor64.triton.spigot.banners; -import com.comphenix.protocol.utility.MinecraftVersion; import com.rexcantor64.triton.Triton; import com.rexcantor64.triton.api.language.Language; import com.rexcantor64.triton.spigot.SpigotTriton; import lombok.val; import net.md_5.bungee.api.ChatColor; -import org.bukkit.DyeColor; import org.bukkit.block.banner.Pattern; -import org.bukkit.block.banner.PatternType; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BannerMeta; @@ -62,8 +59,8 @@ private ItemStack bannerToItemStack(Banner banner, String displayName, boolean a ItemStack itemStack = new ItemStack(SpigotTriton.asSpigot().getWrapperManager().getBannerMaterial()); BannerMeta bannerMeta = Objects.requireNonNull((BannerMeta) itemStack.getItemMeta()); for (Banner.Layer layer : banner.getLayers()) { - val dyeColor = getDyeColor(layer.getColor()); - val patternType = PatternType.valueOf(layer.getPattern().getType()); + val dyeColor = layer.getColor().toDyeColor(); + val patternType = layer.getPattern().toPatternType(); bannerMeta.addPattern(new Pattern(dyeColor, patternType)); } bannerMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', displayName)); @@ -76,12 +73,4 @@ private ItemStack bannerToItemStack(Banner banner, String displayName, boolean a return itemStack; } - private static DyeColor getDyeColor(Colors color) { - if (color == Colors.GRAY && MinecraftVersion.AQUATIC_UPDATE.atOrAbove()) { - // On 1.12 and below, this color is called "SILVER" - return DyeColor.valueOf("SILVER"); - } - return DyeColor.valueOf(color.getColor()); - } - } diff --git a/triton-spigot/src/main/java/com/rexcantor64/triton/spigot/banners/Colors.java b/triton-spigot/src/main/java/com/rexcantor64/triton/spigot/banners/Colors.java index 800a5597..2c2b1ddd 100644 --- a/triton-spigot/src/main/java/com/rexcantor64/triton/spigot/banners/Colors.java +++ b/triton-spigot/src/main/java/com/rexcantor64/triton/spigot/banners/Colors.java @@ -1,18 +1,35 @@ package com.rexcantor64.triton.spigot.banners; +import lombok.Getter; +import org.bukkit.DyeColor; + +import java.util.Arrays; + +@Getter public enum Colors { - BLACK("BLACK", 'a'), RED("RED", 'b'), GREEN("GREEN", 'c'), BROWN("BROWN", 'd'), BLUE( - "BLUE", 'e'), PURPLE("PURPLE", 'f'), CYAN("CYAN", 'g'), GRAY("LIGHT_GRAY", - 'h'), DARK_GRAY("GRAY", 'i'), PINK("PINK", 'j'), LIME("LIME", 'k'), YELLOW( - "YELLOW", 'l'), LIGHT_BLUE("LIGHT_BLUE", 'm'), MAGENTA("MAGENTA", - 'n'), ORANGE("ORANGE", 'o'), WHITE("WHITE", 'p'); + BLACK('a', "BLACK"), + RED('b', "RED"), + GREEN('c', "GREEN"), + BROWN('d', "BROWN"), + BLUE('e', "BLUE"), + PURPLE('f', "PURPLE"), + CYAN('g', "CYAN"), + GRAY('h', "LIGHT_GRAY", "SILVER"), + DARK_GRAY('i', "GRAY"), + PINK('j', "PINK"), + LIME('k', "LIME"), + YELLOW('l', "YELLOW"), + LIGHT_BLUE('m', "LIGHT_BLUE"), + MAGENTA('n', "MAGENTA"), + ORANGE('o', "ORANGE"), + WHITE('p', "WHITE"); - private final String color; private final char code; + private final String[] colorAliases; // some colors have been renamed throughout Spigot versions - Colors(String color, char code) { - this.color = color; + Colors(char code, String... colorAliases) { this.code = code; + this.colorAliases = colorAliases; } public static Colors getByCode(char code) { @@ -22,12 +39,14 @@ public static Colors getByCode(char code) { return null; } - public char getCode() { - return code; - } - - public String getColor() { - return color; + public DyeColor toDyeColor() { + for (String alias : this.colorAliases) { + try { + return DyeColor.valueOf(alias); + } catch (IllegalArgumentException ignore) { + } + } + throw new IllegalArgumentException("Cannot find corresponding dye color to " + Arrays.toString(this.colorAliases)); } } diff --git a/triton-spigot/src/main/java/com/rexcantor64/triton/spigot/banners/Patterns.java b/triton-spigot/src/main/java/com/rexcantor64/triton/spigot/banners/Patterns.java index 89a24d7a..a05db7c8 100644 --- a/triton-spigot/src/main/java/com/rexcantor64/triton/spigot/banners/Patterns.java +++ b/triton-spigot/src/main/java/com/rexcantor64/triton/spigot/banners/Patterns.java @@ -1,57 +1,58 @@ package com.rexcantor64.triton.spigot.banners; +import lombok.Getter; +import org.bukkit.block.banner.PatternType; + +import java.util.Arrays; + +@Getter public enum Patterns { - BASE("BASE", 'a'), BL("SQUARE_BOTTOM_LEFT", 'b'), BO("BORDER", 'c'), BR( - "SQUARE_BOTTOM_RIGHT", - 'd'), BRI("BRICKS", 'e'), BS("STRIPE_BOTTOM", 'f'), BT("TRIANGLE_BOTTOM", - 'g'), BTS("TRIANGLES_BOTTOM", 'h'), CBO("CURLY_BORDER", 'i'), CR( - "CROSS", - 'j'), CRE("CREEPER", 'k'), CS("STRIPE_CENTER", 'l'), DLS( - "STRIPE_DOWNLEFT", - 'm'), DRS("STRIPE_DOWNRIGHT", 'n'), FLO("FLOWER", 'o'), GRA( - "GRADIENT", - 'p'), HH("HALF_HORIZONTAL", 'q'), LD("DIAGONAL_LEFT", - 'r'), LS("STRIPE_LEFT", 's'), MC( - "CIRCLE_MIDDLE", - 't'), MOJ("MOJANG", 'u'), MR( - "RHOMBUS_MIDDLE", - 'v'), MS("STRIPE_MIDDLE", 'w'), RD( - "DIAGONAL_RIGHT", - 'x'), RS("STRIPE_RIGHT", 'y'), SC( - "STRAIGHT_CROSS", - 'z'), SKU("SKULL", - 'A'), SS( - "STRIPE_SMALL", - 'B'), TL( - "SQUARE_TOP_LEFT", - 'C'), TR( - "SQUARE_TOP_RIGHT", - 'D'), TS( - "STRIPE_TOP", - 'E'), TT( - "TRIANGLE_TOP", - 'F'), TTS( - "TRIANGLES_TOP", - 'G'), VH( - "HALF_VERTICAL", - 'H'), LUD( - "DIAGONAL_LEFT_MIRROR", - 'I'), RUD( - "DIAGONAL_RIGHT_MIRROR", - 'J'), GRU( - "GRADIENT_UP", - 'K'), HHB( - "HALF_HORIZONTAL_MIRROR", - 'L'), VHR( - "HALF_VERTICAL_MIRROR", - 'M'); + BASE('a', "BASE"), + BL('b', "SQUARE_BOTTOM_LEFT"), + BO('c', "BORDER"), + BR('d', "SQUARE_BOTTOM_RIGHT"), + BRI('e', "BRICKS"), + BS('f', "STRIPE_BOTTOM"), + BT('g', "TRIANGLE_BOTTOM"), + BTS('h', "TRIANGLES_BOTTOM"), + CBO('i', "CURLY_BORDER"), + CR('j', "CROSS"), + CRE('k', "CREEPER"), + CS('l', "STRIPE_CENTER"), + DLS('m', "STRIPE_DOWNLEFT"), + DRS('n', "STRIPE_DOWNRIGHT"), + FLO('o', "FLOWER"), + GRA('p', "GRADIENT"), + HH('q', "HALF_HORIZONTAL"), + LD('r', "DIAGONAL_LEFT"), + LS('s', "STRIPE_LEFT"), + MC('t', "CIRCLE", "CIRCLE_MIDDLE"), + MOJ('u', "MOJANG"), + MR('v', "RHOMBUS", "RHOMBUS_MIDDLE"), + MS('w', "STRIPE_MIDDLE"), + RD('x', "DIAGONAL_RIGHT"), + RS('y', "STRIPE_RIGHT"), + SC('z', "STRAIGHT_CROSS"), + SKU('A', "SKULL"), + SS('B', "SMALL_STRIPES", "STRIPE_SMALL"), + TL('C', "SQUARE_TOP_LEFT"), + TR('D', "SQUARE_TOP_RIGHT"), + TS('E', "STRIPE_TOP"), + TT('F', "TRIANGLE_TOP"), + TTS('G', "TRIANGLES_TOP"), + VH('H', "HALF_VERTICAL"), + LUD('I', "DIAGONAL_UP_LEFT", "DIAGONAL_LEFT_MIRROR"), + RUD('J', "DIAGONAL_UP_RIGHT", "DIAGONAL_RIGHT_MIRROR"), + GRU('K', "GRADIENT_UP"), + HHB('L', "HALF_HORIZONTAL_BOTTOM", "HALF_HORIZONTAL_MIRROR"), + VHR('M', "HALF_VERTICAL_RIGHT", "HALF_VERTICAL_MIRROR"); - private final String type; private final char code; + private final String[] typeAliases; - Patterns(String type, char code) { - this.type = type; + Patterns(char code, String... typeAliases) { this.code = code; + this.typeAliases = typeAliases; } public static Patterns getByCode(char code) { @@ -61,12 +62,14 @@ public static Patterns getByCode(char code) { return null; } - public char getCode() { - return code; - } - - public String getType() { - return type; + public PatternType toPatternType() { + for (String alias : this.typeAliases) { + try { + return PatternType.valueOf(alias); + } catch (IllegalArgumentException ignore) { + } + } + throw new IllegalArgumentException("Cannot find corresponding pattern type to " + Arrays.toString(this.typeAliases)); } } diff --git a/triton-spigot/src/test/java/com/rexcantor64/triton/spigot/banners/ColorsTest.java b/triton-spigot/src/test/java/com/rexcantor64/triton/spigot/banners/ColorsTest.java new file mode 100644 index 00000000..52e2b984 --- /dev/null +++ b/triton-spigot/src/test/java/com/rexcantor64/triton/spigot/banners/ColorsTest.java @@ -0,0 +1,32 @@ +package com.rexcantor64.triton.spigot.banners; + +import org.bukkit.DyeColor; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ColorsTest { + + @Test + public void testToDyeColor() { + // Test converting all colors to their respective DyeColor + // This helps keep the conversion table up-to-date, since it would fail if the DyeColor enum changes + + assertEquals(Colors.BLACK.toDyeColor(), DyeColor.BLACK); + assertEquals(Colors.RED.toDyeColor(), DyeColor.RED); + assertEquals(Colors.GREEN.toDyeColor(), DyeColor.GREEN); + assertEquals(Colors.BROWN.toDyeColor(), DyeColor.BROWN); + assertEquals(Colors.BLUE.toDyeColor(), DyeColor.BLUE); + assertEquals(Colors.PURPLE.toDyeColor(), DyeColor.PURPLE); + assertEquals(Colors.CYAN.toDyeColor(), DyeColor.CYAN); + assertEquals(Colors.GRAY.toDyeColor(), DyeColor.LIGHT_GRAY); + assertEquals(Colors.DARK_GRAY.toDyeColor(), DyeColor.GRAY); + assertEquals(Colors.PINK.toDyeColor(), DyeColor.PINK); + assertEquals(Colors.LIME.toDyeColor(), DyeColor.LIME); + assertEquals(Colors.YELLOW.toDyeColor(), DyeColor.YELLOW); + assertEquals(Colors.LIGHT_BLUE.toDyeColor(), DyeColor.LIGHT_BLUE); + assertEquals(Colors.MAGENTA.toDyeColor(), DyeColor.MAGENTA); + assertEquals(Colors.ORANGE.toDyeColor(), DyeColor.ORANGE); + assertEquals(Colors.WHITE.toDyeColor(), DyeColor.WHITE); + } +} diff --git a/triton-spigot/src/test/java/com/rexcantor64/triton/spigot/banners/PatternsTest.java b/triton-spigot/src/test/java/com/rexcantor64/triton/spigot/banners/PatternsTest.java new file mode 100644 index 00000000..426df2b2 --- /dev/null +++ b/triton-spigot/src/test/java/com/rexcantor64/triton/spigot/banners/PatternsTest.java @@ -0,0 +1,55 @@ +package com.rexcantor64.triton.spigot.banners; + +import org.bukkit.block.banner.PatternType; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class PatternsTest { + + @Test + public void testToPatternType() { + // Test converting all patterns to their respective PatternType + // This helps keep the conversion table up-to-date, since it would fail if the PatternType enum changes + + assertEquals(Patterns.BASE.toPatternType(), PatternType.BASE); + assertEquals(Patterns.BL.toPatternType(), PatternType.SQUARE_BOTTOM_LEFT); + assertEquals(Patterns.BO.toPatternType(), PatternType.BORDER); + assertEquals(Patterns.BR.toPatternType(), PatternType.SQUARE_BOTTOM_RIGHT); + assertEquals(Patterns.BRI.toPatternType(), PatternType.BRICKS); + assertEquals(Patterns.BS.toPatternType(), PatternType.STRIPE_BOTTOM); + assertEquals(Patterns.BT.toPatternType(), PatternType.TRIANGLE_BOTTOM); + assertEquals(Patterns.BTS.toPatternType(), PatternType.TRIANGLES_BOTTOM); + assertEquals(Patterns.CBO.toPatternType(), PatternType.CURLY_BORDER); + assertEquals(Patterns.CR.toPatternType(), PatternType.CROSS); + assertEquals(Patterns.CRE.toPatternType(), PatternType.CREEPER); + assertEquals(Patterns.CS.toPatternType(), PatternType.STRIPE_CENTER); + assertEquals(Patterns.DLS.toPatternType(), PatternType.STRIPE_DOWNLEFT); + assertEquals(Patterns.DRS.toPatternType(), PatternType.STRIPE_DOWNRIGHT); + assertEquals(Patterns.FLO.toPatternType(), PatternType.FLOWER); + assertEquals(Patterns.GRA.toPatternType(), PatternType.GRADIENT); + assertEquals(Patterns.HH.toPatternType(), PatternType.HALF_HORIZONTAL); + assertEquals(Patterns.LD.toPatternType(), PatternType.DIAGONAL_LEFT); + assertEquals(Patterns.LS.toPatternType(), PatternType.STRIPE_LEFT); + assertEquals(Patterns.MC.toPatternType(), PatternType.CIRCLE); + assertEquals(Patterns.MOJ.toPatternType(), PatternType.MOJANG); + assertEquals(Patterns.MR.toPatternType(), PatternType.RHOMBUS); + assertEquals(Patterns.MS.toPatternType(), PatternType.STRIPE_MIDDLE); + assertEquals(Patterns.RD.toPatternType(), PatternType.DIAGONAL_RIGHT); + assertEquals(Patterns.RS.toPatternType(), PatternType.STRIPE_RIGHT); + assertEquals(Patterns.SC.toPatternType(), PatternType.STRAIGHT_CROSS); + assertEquals(Patterns.SKU.toPatternType(), PatternType.SKULL); + assertEquals(Patterns.SS.toPatternType(), PatternType.SMALL_STRIPES); + assertEquals(Patterns.TL.toPatternType(), PatternType.SQUARE_TOP_LEFT); + assertEquals(Patterns.TR.toPatternType(), PatternType.SQUARE_TOP_RIGHT); + assertEquals(Patterns.TS.toPatternType(), PatternType.STRIPE_TOP); + assertEquals(Patterns.TT.toPatternType(), PatternType.TRIANGLE_TOP); + assertEquals(Patterns.TTS.toPatternType(), PatternType.TRIANGLES_TOP); + assertEquals(Patterns.VH.toPatternType(), PatternType.HALF_VERTICAL); + assertEquals(Patterns.LUD.toPatternType(), PatternType.DIAGONAL_UP_LEFT); + assertEquals(Patterns.RUD.toPatternType(), PatternType.DIAGONAL_UP_RIGHT); + assertEquals(Patterns.GRU.toPatternType(), PatternType.GRADIENT_UP); + assertEquals(Patterns.HHB.toPatternType(), PatternType.HALF_HORIZONTAL_BOTTOM); + assertEquals(Patterns.VHR.toPatternType(), PatternType.HALF_VERTICAL_RIGHT); + } +}