Use spaces and proper casing for calculating width (Fixes #166)

This also slightly changes which name part gets shortened first. It will first shorten the longest name and then the rest.
This commit is contained in:
Phoenix616 2018-09-09 22:43:49 +01:00
parent 516eefc5b6
commit 173991ab4a

View File

@ -205,21 +205,21 @@ public class MaterialUtil {
durability = ":" + itemStack.getDurability(); durability = ":" + itemStack.getDurability();
} }
} }
data = data != null ? data + "_" : ""; data = data != null ? data + " " : "";
String metaData = ""; String metaData = "";
if (itemStack.hasItemMeta()) { if (itemStack.hasItemMeta()) {
metaData = "#" + Metadata.getItemCode(itemStack); metaData = "#" + Metadata.getItemCode(itemStack);
} }
int codeWidth = getMinecraftStringWidth(data + itemName + durability + metaData); String code = StringUtil.capitalizeFirstLetter(data + itemName, '_');
String code = data + itemName; int codeWidth = getMinecraftStringWidth(code + durability + metaData);
if (maxWidth > 0 && codeWidth > maxWidth) { if (maxWidth > 0 && codeWidth > maxWidth) {
int exceeding = codeWidth - maxWidth; int exceeding = codeWidth - maxWidth;
code = getShortenedName(code, getMinecraftStringWidth(code) - exceeding); code = getShortenedName(code, getMinecraftStringWidth(code) - exceeding);
} }
code = StringUtil.capitalizeFirstLetter(code, '_') + durability + metaData; code += durability + metaData;
ItemStack codeItem = getItem(code); ItemStack codeItem = getItem(code);
if (!equals(itemStack, codeItem)) { if (!equals(itemStack, codeItem)) {
@ -243,7 +243,7 @@ public class MaterialUtil {
return itemName; return itemName;
} }
int exceeding = width - maxWidth; int exceeding = width - maxWidth;
String[] itemParts = itemName.split("_"); String[] itemParts = itemName.split(" ");
int shortestIndex = 0; int shortestIndex = 0;
int longestIndex = 0; int longestIndex = 0;
for (int i = 0; i < itemParts.length; i++) { for (int i = 0; i < itemParts.length; i++) {
@ -254,46 +254,43 @@ public class MaterialUtil {
shortestIndex = i; shortestIndex = i;
} }
} }
String longest = itemParts[longestIndex]; int shortestWidth = getMinecraftStringWidth(itemParts[shortestIndex]);
String shortest = itemParts[shortestIndex]; int longestWidth = getMinecraftStringWidth(itemParts[longestIndex]);
if (getMinecraftStringWidth(longest) - getMinecraftStringWidth(shortest) > exceeding) { int remove = longestWidth - shortestWidth;
int remove = exceeding; while (remove > 0 && exceeding > 0) {
int endWidth = getMinecraftCharWidth(itemParts[longestIndex].charAt(itemParts[longestIndex].length() - 1));
itemParts[longestIndex] = itemParts[longestIndex].substring(0, itemParts[longestIndex].length() - 1);
remove -= endWidth;
exceeding -= endWidth;
}
for (int i = itemParts.length - 1; i >= 0 && exceeding > 0; i--) {
int partWidth = getMinecraftStringWidth(itemParts[i]);
if (partWidth > shortestWidth) {
remove = partWidth - shortestWidth;
}
if (remove > exceeding) {
remove = exceeding;
}
while (remove > 0) { while (remove > 0) {
int endWidth = getMinecraftCharWidth(longest.charAt(longest.length() - 1)); int endWidth = getMinecraftCharWidth(itemParts[i].charAt(itemParts[i].length() - 1));
itemParts[longestIndex] = longest.substring(0, longest.length() - 1); itemParts[i] = itemParts[i].substring(0, itemParts[i].length() - 1);
remove -= endWidth; remove -= endWidth;
} exceeding -= endWidth;
} else {
for (int i = itemParts.length - 1; i >= 0 && exceeding > 0; i--) {
int remove = 0;
int partWidth = getMinecraftStringWidth(itemParts[i]);
int shortestWidth = getMinecraftStringWidth(shortest);
if (partWidth > shortestWidth) {
remove = partWidth - shortestWidth;
}
if (remove > exceeding) {
remove = exceeding;
}
while (remove > 0) {
int endWidth = getMinecraftCharWidth(itemParts[i].charAt(itemParts[i].length() - 1));
itemParts[i] = itemParts[i].substring(0, itemParts[i].length() - 1);
remove -= endWidth;
exceeding -= endWidth;
}
}
while (exceeding > 0) {
for (int i = itemParts.length - 1; i >= 0 && exceeding > 0; i--) {
int endWidth = getMinecraftCharWidth(itemParts[i].charAt(itemParts[i].length() - 1));
itemParts[i] = itemParts[i].substring(0, itemParts[i].length() - 1);
exceeding -= endWidth;
}
} }
} }
return String.join("_", itemParts);
while (exceeding > 0) {
for (int i = itemParts.length - 1; i >= 0 && exceeding > 0; i--) {
int endWidth = getMinecraftCharWidth(itemParts[i].charAt(itemParts[i].length() - 1));
itemParts[i] = itemParts[i].substring(0, itemParts[i].length() - 1);
exceeding -= endWidth;
}
}
return String.join(" ", itemParts);
} }
/** /**