diff --git a/pom.xml b/pom.xml index ed6e297..5e47359 100644 --- a/pom.xml +++ b/pom.xml @@ -83,14 +83,22 @@ net.kyori adventure-platform-bukkit - 4.2.0 + 4.3.0 compile + + + net.kyori + adventure-api + 4.14.0 + provided + + net.kyori adventure-text-serializer-gson - 4.12.0 + 4.13.0 compile @@ -111,7 +119,7 @@ org.bstats bstats-bukkit - 3.0.0 + 3.0.1 compile @@ -212,6 +220,12 @@ worldedit-core 7.0.0-SNAPSHOT provided + + + org.yaml + snakeyaml + + @@ -446,7 +460,7 @@ ${buildNumber} ${buildType} ${project.version} ${buildDescription} - 1.19-R0.1-SNAPSHOT + 1.20.1-R0.1-SNAPSHOT diff --git a/src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java b/src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java index 447bfe0..536407d 100644 --- a/src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java +++ b/src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java @@ -25,7 +25,6 @@ import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.nodes.Tag; import java.util.ArrayList; -import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; @@ -54,6 +53,30 @@ public class MaterialUtil { // 15 dashes fit on one sign line with the default resource pack: public static final int MAXIMUM_SIGN_WIDTH = (short) getMinecraftStringWidth("---------------"); + private static final Map ABBREVIATIONS = StringUtil.map( + "Egg", "Eg", + "Spawn", "Spaw", + "Ender", "End", + "Tropical", "Tropic", + "Terracotta", "Terracot", + "Stained", "Stain", + "Sandstone", "Sandston", + "Sandston", "Sandsto", + "Sandsto", "Sandst", + "Block", "Bloc", + "Brewing", "Brew", + "Dolphin", "Dolph", + "Chicken", "Chick", + "Pottery", "Pot", + "Heartbreak", "Heartbr", + "Sherd", "Sher", + "Template", "Templ" + ); + + private static final Map UNIDIRECTIONAL_ABBREVIATIONS = StringUtil.map( + "Endermite", "Endmite" + ); + private static final SimpleCache MATERIAL_CACHE = new SimpleCache<>(Properties.CACHE_SIZE); private static final Yaml YAML = new Yaml(new YamlBukkitConstructor(), new YamlRepresenter(), new DumperOptions()); @@ -146,6 +169,11 @@ public class MaterialUtil { * @return Material found */ public static Material getMaterial(String name) { + // revert unidirectional abbreviations + for (Map.Entry entry : UNIDIRECTIONAL_ABBREVIATIONS.entrySet()) { + name = name.replaceAll(entry.getValue() + "([A-Z1-9].+)?$", entry.getKey() + "$1"); + } + String formatted = name.replaceAll("(? entry : ABBREVIATIONS.entrySet()) { + itemName = itemName.replaceAll(entry.getKey() + "([A-Z1-9].+)?$", entry.getValue() + "$1"); + width = getMinecraftStringWidth(itemName); + if (width <= maxWidth) { + return itemName; + } + } + + // Apply unidirectional abbreviations if it still doesn't work + for (Map.Entry entry : UNIDIRECTIONAL_ABBREVIATIONS.entrySet()) { + itemName = itemName.replaceAll(entry.getKey() + "([A-Z1-9].+)?$", entry.getValue() + "$1"); + width = getMinecraftStringWidth(itemName); + if (width <= maxWidth) { + return itemName; + } + } + int exceeding = width - maxWidth; int shortestIndex = 0; int longestIndex = 0; @@ -408,6 +455,7 @@ public class MaterialUtil { E currentEnum = null; String[] typeParts = name.replaceAll("(? map(String... values) { + Map map = new LinkedHashMap<>(); + + for (int i = 0; i + 1 < values.length; i+=2) { + map.put(values[i], values[i + 1]); + } + + return map; + } } diff --git a/src/test/java/com/Acrobot/Breeze/Tests/MaterialTest.java b/src/test/java/com/Acrobot/Breeze/Tests/MaterialTest.java index 709319f..2941495 100644 --- a/src/test/java/com/Acrobot/Breeze/Tests/MaterialTest.java +++ b/src/test/java/com/Acrobot/Breeze/Tests/MaterialTest.java @@ -1,6 +1,7 @@ package com.Acrobot.Breeze.Tests; import com.Acrobot.Breeze.Utils.MaterialUtil; +import com.Acrobot.Breeze.Utils.StringUtil; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.junit.Test; @@ -33,7 +34,19 @@ public class MaterialTest { continue; } String shortenedName = MaterialUtil.getShortenedName(material.toString(), MaterialUtil.MAXIMUM_SIGN_WIDTH); - assertSame(material, MaterialUtil.getMaterial(shortenedName)); + assertSame(shortenedName + " did not produce " + material, material, MaterialUtil.getMaterial(shortenedName)); + } + } + + @Test + public void testCodesWithMeta() { + int maxWidth = MaterialUtil.MAXIMUM_SIGN_WIDTH - StringUtil.getMinecraftStringWidth("#AAA"); + for (Material material : Material.values()) { + if (material.isLegacy()) { + continue; + } + String shortenedName = MaterialUtil.getShortenedName(material.toString(), maxWidth); + assertSame(shortenedName + " did not produce " + material, material, MaterialUtil.getMaterial(shortenedName)); } } }