Compile against 1.20.1 and fix some short name issues

Partially helps with #516, #521, #549
This commit is contained in:
Phoenix616 2023-06-15 00:01:45 +01:00
parent 999f596125
commit ed642ccf3c
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0
4 changed files with 99 additions and 7 deletions

22
pom.xml
View File

@ -83,14 +83,22 @@
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-platform-bukkit</artifactId>
<version>4.2.0</version>
<version>4.3.0</version>
<scope>compile</scope>
</dependency>
<!-- Should be kept in sync with Paper -->
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-api</artifactId>
<version>4.14.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-gson</artifactId>
<version>4.12.0</version>
<version>4.13.0</version>
<scope>compile</scope>
<exclusions>
<exclusion>
@ -111,7 +119,7 @@
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>3.0.0</version>
<version>3.0.1</version>
<scope>compile</scope>
</dependency>
@ -212,6 +220,12 @@
<artifactId>worldedit-core</artifactId>
<version>7.0.0-SNAPSHOT</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
@ -446,7 +460,7 @@
<build.number>${buildNumber}</build.number>
<user.name>${buildType}</user.name>
<bukkit.plugin.version>${project.version} ${buildDescription}</bukkit.plugin.version>
<bukkit.dependency.version>1.19-R0.1-SNAPSHOT</bukkit.dependency.version>
<bukkit.dependency.version>1.20.1-R0.1-SNAPSHOT</bukkit.dependency.version>
</properties>
<profiles>

View File

@ -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<String, String> 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<String, String> UNIDIRECTIONAL_ABBREVIATIONS = StringUtil.map(
"Endermite", "Endmite"
);
private static final SimpleCache<String, Material> 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<String, String> entry : UNIDIRECTIONAL_ABBREVIATIONS.entrySet()) {
name = name.replaceAll(entry.getValue() + "([A-Z1-9].+)?$", entry.getKey() + "$1");
}
String formatted = name.replaceAll("(?<!^)([A-Z1-9])", "_$1").replace(' ', '_').toUpperCase(Locale.ROOT);
Material material = MATERIAL_CACHE.get(formatted);
@ -264,7 +292,7 @@ public class MaterialUtil {
* @return The name shortened to the max length
*/
public static String getShortenedName(String itemName, int maxWidth) {
itemName = StringUtil.capitalizeFirstLetter(itemName.replace('_', ' '), ' ');
itemName = StringUtil.capitalizeFirstLetter(itemName, '_');
int width = getMinecraftStringWidth(itemName);
if (width <= maxWidth) {
return itemName;
@ -275,6 +303,25 @@ public class MaterialUtil {
if (width <= maxWidth) {
return itemName;
}
// Abbreviate some terms manually
for (Map.Entry<String, String> 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<String, String> 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("(?<!^)([A-Z1-9])", "_$1").toUpperCase(Locale.ROOT).split("[ _]");
int length = Short.MAX_VALUE;
name = name.toUpperCase(Locale.ROOT);
for (E e : values) {
String enumName = e.name();
if (enumName.length() < length && enumName.startsWith(name)) {

View File

@ -4,7 +4,9 @@ import org.bukkit.ChatColor;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
/**
@ -154,4 +156,19 @@ public class StringUtil {
// Return the stripped string, without any whitespace from the end left
return stripped.toString();
}
/**
* Create a map from strings
* @param values The values to add
* @return The map
*/
public static Map<String, String> map(String... values) {
Map<String, String> map = new LinkedHashMap<>();
for (int i = 0; i + 1 < values.length; i+=2) {
map.put(values[i], values[i + 1]);
}
return map;
}
}

View File

@ -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));
}
}
}