Use full item name instead of shortened for aliases

This fixes an issue where aliases would not show up in transaction
 messages when the aliases item's string was shortened.

Also remove an unnecessary string length calculation from the
 MaterialUtil getName method if there was no max width set.
This commit is contained in:
Phoenix616 2021-01-03 20:47:07 +01:00
parent 819f028774
commit f12e6ed3b6
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0
5 changed files with 45 additions and 34 deletions

View File

@ -228,10 +228,12 @@ public class MaterialUtil {
}
String code = StringUtil.capitalizeFirstLetter(itemName, '_');
int codeWidth = getMinecraftStringWidth(code + durability + metaData);
if (maxWidth > 0 && codeWidth > maxWidth) {
int exceeding = codeWidth - maxWidth;
code = getShortenedName(code, getMinecraftStringWidth(code) - exceeding);
if (maxWidth > 0) {
int codeWidth = getMinecraftStringWidth(code + durability + metaData);
if (codeWidth > maxWidth) {
int exceeding = codeWidth - maxWidth;
code = getShortenedName(code, getMinecraftStringWidth(code) - exceeding);
}
}
return code + durability + metaData;

View File

@ -9,16 +9,13 @@ public class ItemStringQueryEvent extends Event {
private String itemString = null;
private final ItemStack item;
private final int maxLength;
/**
* Query the item string representation of a certain item with a certain length
* @param item The item to query the string for
* @param maxLength The maximum length the string should have. <= 0 means unlimited
*/
public ItemStringQueryEvent(ItemStack item, int maxLength) {
public ItemStringQueryEvent(ItemStack item) {
this.item = item;
this.maxLength = maxLength;
}
@Override
@ -38,14 +35,6 @@ public class ItemStringQueryEvent extends Event {
return item;
}
/**
* Get the maximum length that the item string should have. <= 0 means unlimited
* @return The maximum string length
*/
public int getMaxLength() {
return maxLength;
}
/**
* Get the item string that represents the item
* @return The item string that represents the item

View File

@ -1,32 +1,17 @@
package com.Acrobot.ChestShop.Listeners.Item;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.ChestShop.Events.ItemParseEvent;
import com.Acrobot.ChestShop.Events.ItemStringQueryEvent;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
public class ItemStringListener implements Listener {
@EventHandler(priority = EventPriority.NORMAL)
public static void calculateItemString(ItemStringQueryEvent event) {
if (event.getItemString() == null) {
event.setItemString(MaterialUtil.getName(event.getItem(), event.getMaxLength()));
}
}
@EventHandler(priority = EventPriority.MONITOR)
public static void checkValidity(ItemStringQueryEvent event) {
ItemParseEvent parseEvent = new ItemParseEvent(event.getItemString());
Bukkit.getPluginManager().callEvent(parseEvent);
ItemStack codeItem = parseEvent.getItem();
if (!MaterialUtil.equals(event.getItem(), codeItem)) {
throw new IllegalArgumentException("Cannot generate code for item " + event.getItem()
+ " with maximum length of " + event.getMaxLength()
+ " (code " + event.getItemString() + " results in item " + codeItem + ")");
event.setItemString(MaterialUtil.getName(event.getItem(), 0));
}
}

View File

@ -36,7 +36,7 @@ public class ItemAliasModule implements Listener {
configuration = YamlConfiguration.loadConfiguration(file);
configuration.options().header(
"This file specified optional aliases for certain item codes. (Use the sign name from /iteminfo)"
"This file specified optional aliases for certain item codes. (Use the full name from /iteminfo)"
+ "\nPlease note that these aliases should fit on a sign for it to work properly!"
);

View File

@ -1,14 +1,18 @@
package com.Acrobot.ChestShop.Utils;
import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Events.ItemParseEvent;
import com.Acrobot.ChestShop.Events.ItemStringQueryEvent;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
import static com.Acrobot.Breeze.Utils.MaterialUtil.MAXIMUM_SIGN_WIDTH;
import static com.Acrobot.Breeze.Utils.StringUtil.getMinecraftStringWidth;
public class ItemUtil {
@ -49,7 +53,38 @@ public class ItemUtil {
* @return ItemStack's name
*/
public static String getName(ItemStack itemStack, int maxWidth) {
return ChestShop.callEvent(new ItemStringQueryEvent(itemStack, maxWidth)).getItemString();
String code = ChestShop.callEvent(new ItemStringQueryEvent(itemStack)).getItemString();
if (code != null) {
if (maxWidth > 0) {
int codeWidth = getMinecraftStringWidth(code);
if (codeWidth > maxWidth) {
int exceeding = codeWidth - maxWidth;
int poundIndex = code.indexOf('#');
int colonIndex = code.indexOf(':');
String material = code;
String rest = "";
if (poundIndex > 0 && poundIndex < colonIndex) {
material = code.substring(0, poundIndex);
rest = code.substring(poundIndex);
} else if (colonIndex > 0 && colonIndex < poundIndex) {
material = code.substring(0, colonIndex);
rest = code.substring(colonIndex);
}
code = MaterialUtil.getShortenedName(material, getMinecraftStringWidth(material) - exceeding) + rest;
}
}
ItemParseEvent parseEvent = new ItemParseEvent(code);
Bukkit.getPluginManager().callEvent(parseEvent);
ItemStack codeItem = parseEvent.getItem();
if (!MaterialUtil.equals(itemStack, codeItem)) {
throw new IllegalArgumentException("Cannot generate code for item " + itemStack
+ " with maximum length of " + maxWidth
+ " (code " + code + " results in item " + codeItem + ")");
}
}
return code;
}
/**