Fix shortened item aliases not working (Fixes #425)

This commit is contained in:
Phoenix616 2021-03-26 15:55:29 +01:00
parent a3c5ec618a
commit b93361cd07
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0
1 changed files with 26 additions and 0 deletions

View File

@ -15,6 +15,7 @@ import org.bukkit.event.Listener;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import java.util.Map;
/**
* @author Acrobot
@ -69,6 +70,31 @@ public class ItemAliasModule implements Listener {
@EventHandler(priority = EventPriority.LOW)
public void onItemParse(ItemParseEvent event) {
String code = aliases.inverse().get(event.getItemString());
if (code == null) {
String[] typeParts = event.getItemString().replaceAll("(?<!^)([A-Z1-9])", "_$1").toUpperCase(Locale.ROOT).split("[ _]");
int length = Short.MAX_VALUE;
for (Map.Entry<String, String> entry : aliases.entrySet()) {
if (entry.getValue().length() < length && entry.getValue().startsWith(event.getItemString())) {
length = (short) entry.getValue().length();
code = entry.getKey();
} else if (typeParts.length > 1) {
String[] nameParts = entry.getValue().split("[ _]");
if (typeParts.length == nameParts.length) {
boolean matched = true;
for (int i = 0; i < nameParts.length; i++) {
if (!nameParts[i].startsWith(typeParts[i])) {
matched = false;
break;
}
}
if (matched) {
code = entry.getKey();
break;
}
}
}
}
}
if (code != null) {
event.setItem(MaterialUtil.getItem(code));
}