From 361724f2be6c8ecbd375246c5657f647685ec9da Mon Sep 17 00:00:00 2001 From: Acrobot Date: Sun, 21 Oct 2012 22:45:30 +0200 Subject: [PATCH] Fixed item and price formatting --- com/Acrobot/Breeze/Utils/MaterialUtil.java | 6 ++-- .../ChestShop/Listeners/Block/SignChange.java | 33 ++++++++++++------- .../PreTransaction/PriceValidator.java | 7 ++-- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/com/Acrobot/Breeze/Utils/MaterialUtil.java b/com/Acrobot/Breeze/Utils/MaterialUtil.java index 6f0a7bc..ba135b5 100644 --- a/com/Acrobot/Breeze/Utils/MaterialUtil.java +++ b/com/Acrobot/Breeze/Utils/MaterialUtil.java @@ -18,8 +18,8 @@ import java.util.regex.Pattern; * @author Acrobot */ public class MaterialUtil { - private static final Pattern DURABILITY = Pattern.compile(":(\\d)*"); - private static final Pattern ENCHANTMENT = Pattern.compile("-([0-9a-zA-Z])*"); + public static final Pattern DURABILITY = Pattern.compile(":(\\d)*"); + public static final Pattern ENCHANTMENT = Pattern.compile("-([0-9a-zA-Z])*"); /** * Checks if the itemStack is empty or null @@ -213,7 +213,7 @@ public class MaterialUtil { data = data.substring(1); - return NumberUtil.isInteger(data) ? Short.valueOf(data) : 0; + return NumberUtil.isShort(data) ? Short.valueOf(data) : 0; } /** diff --git a/com/Acrobot/ChestShop/Listeners/Block/SignChange.java b/com/Acrobot/ChestShop/Listeners/Block/SignChange.java index 1af453c..54b2476 100644 --- a/com/Acrobot/ChestShop/Listeners/Block/SignChange.java +++ b/com/Acrobot/ChestShop/Listeners/Block/SignChange.java @@ -207,28 +207,37 @@ public class SignChange implements Listener { return (line.length() > 15 ? null : line); } - private static String formatItemLine(String line, ItemStack itemStack) { + private static String formatItemLine(String line, ItemStack item) { + String formatted, data = ""; String[] split = line.split(":|-", 2); - StringBuilder formatted = new StringBuilder(15); - String itemName = MaterialUtil.getName(itemStack, false); - short dataLength = (short) (line.length() - split[0].length()); - - if (itemName.length() > (15 - dataLength)) { - itemName = itemName.substring(0, 15 - dataLength); + if (MaterialUtil.ENCHANTMENT.matcher(line).matches()) { + data = '-' + MaterialUtil.ENCHANTMENT.matcher(line).group(); } - if (MaterialUtil.getItem(itemName).getType() != itemStack.getType()) { - itemName = String.valueOf(itemStack.getTypeId()); + String longItemName = MaterialUtil.getName(item, true); + + if (longItemName.length() < (15 - data.length()) && MaterialUtil.equals(MaterialUtil.getItem(longItemName + data), item)) { + return StringUtil.capitalizeFirstLetter(longItemName + data); + } + + formatted = MaterialUtil.getName(item, false); + data = (split.length == 2 ? split[1] : ""); + + if (formatted.length() > (15 - data.length())) { + formatted = formatted.substring(0, (15 - data.length())); + } + + if (MaterialUtil.getItem(formatted).getType() != item.getType()) { + formatted = String.valueOf(item.getTypeId()); } - formatted.append(itemName); if (split.length == 2) { int dataValuePos = line.indexOf(split[1], split[0].length()); - formatted.append(line.charAt(dataValuePos - 1)).append(split[1]); + formatted += line.charAt(dataValuePos - 1) + split[1]; } - return StringUtil.capitalizeFirstLetter(formatted.toString()); + return StringUtil.capitalizeFirstLetter(formatted); } private static boolean playerCanUseName(Player player, String name) { diff --git a/com/Acrobot/ChestShop/Listeners/PreTransaction/PriceValidator.java b/com/Acrobot/ChestShop/Listeners/PreTransaction/PriceValidator.java index 702aa9e..42a7f1b 100644 --- a/com/Acrobot/ChestShop/Listeners/PreTransaction/PriceValidator.java +++ b/com/Acrobot/ChestShop/Listeners/PreTransaction/PriceValidator.java @@ -1,11 +1,12 @@ package com.Acrobot.ChestShop.Listeners.PreTransaction; -import com.Acrobot.Breeze.Utils.PriceUtil; import com.Acrobot.ChestShop.Events.PreTransactionEvent; import com.Acrobot.ChestShop.Events.TransactionEvent; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import static com.Acrobot.Breeze.Utils.PriceUtil.NO_PRICE; import static com.Acrobot.ChestShop.Events.PreTransactionEvent.TransactionOutcome.SHOP_DOES_NOT_BUY_THIS_ITEM; import static com.Acrobot.ChestShop.Events.PreTransactionEvent.TransactionOutcome.SHOP_DOES_NOT_SELL_THIS_ITEM; import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY; @@ -14,7 +15,7 @@ import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY; * @author Acrobot */ public class PriceValidator implements Listener { - @EventHandler + @EventHandler(priority = EventPriority.LOWEST) public static void onPriceCheck(PreTransactionEvent event) { if (event.isCancelled()) { return; @@ -23,7 +24,7 @@ public class PriceValidator implements Listener { TransactionEvent.TransactionType transactionType = event.getTransactionType(); double price = event.getPrice(); - if (price == PriceUtil.NO_PRICE) { + if (price == NO_PRICE) { if (transactionType == BUY) { event.setCancelled(SHOP_DOES_NOT_BUY_THIS_ITEM); } else {