ChestShop-3/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java

84 lines
2.6 KiB
Java
Raw Normal View History

2013-01-15 21:33:00 +01:00
package com.Acrobot.ChestShop.Listeners.PreShopCreation;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.Breeze.Utils.StringUtil;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
2013-01-24 22:35:28 +01:00
import java.util.regex.Matcher;
2013-01-15 21:33:00 +01:00
import static com.Acrobot.Breeze.Utils.MaterialUtil.*;
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.INVALID_ITEM;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE;
/**
* @author Acrobot
*/
public class ItemChecker implements Listener {
private static final short MAXIMUM_SIGN_LETTERS = 15;
@EventHandler(priority = EventPriority.LOWEST)
public static void onPreShopCreation(PreShopCreationEvent event) {
String itemCode = event.getSignLine(ITEM_LINE);
ItemStack item = MaterialUtil.getItem(itemCode);
if (item == null) {
event.setOutcome(INVALID_ITEM);
return;
}
if (Odd.getFromString(itemCode) != null) {
return; // The OddItem name is OK
}
2013-01-24 22:35:28 +01:00
String metadata = getMetadata(itemCode);
2013-02-08 19:29:31 +01:00
String longName = MaterialUtil.getName(item);
2013-01-15 21:33:00 +01:00
2013-01-24 22:35:28 +01:00
if (longName.length() <= (MAXIMUM_SIGN_LETTERS - metadata.length())) {
2013-02-08 19:29:31 +01:00
if (isSameItem(longName + metadata, item)) {
2013-01-24 22:35:28 +01:00
String itemName = StringUtil.capitalizeFirstLetter(longName + metadata);
2013-01-15 21:33:00 +01:00
event.setSignLine(ITEM_LINE, itemName);
return;
}
}
String code = MaterialUtil.getName(item, SHORT_NAME);
2013-01-24 22:35:28 +01:00
String[] parts = itemCode.split("(?=:|-|#)", 2);
2013-01-15 21:33:00 +01:00
String data = (parts.length > 1 ? parts[1] : "");
2013-02-08 19:29:31 +01:00
if (!data.isEmpty() && code.length() > (MAXIMUM_SIGN_LETTERS - data.length())) {
code = code.substring(0, MAXIMUM_SIGN_LETTERS - data.length());
2013-01-15 21:33:00 +01:00
}
2013-02-15 21:06:27 +01:00
if (!isSameItem(code + data, item)) {
code = String.valueOf(item.getTypeId());
2013-01-15 21:33:00 +01:00
}
code = StringUtil.capitalizeFirstLetter(code);
2013-02-15 21:06:27 +01:00
code += data;
2013-01-15 21:33:00 +01:00
event.setSignLine(ITEM_LINE, code);
}
2013-02-08 19:29:31 +01:00
private static boolean isSameItem(String newCode, ItemStack item) {
2013-01-15 21:33:00 +01:00
ItemStack newItem = MaterialUtil.getItem(newCode);
return newItem != null && MaterialUtil.equals(newItem, item);
}
2013-01-24 22:35:28 +01:00
private static String getMetadata(String itemCode) {
Matcher m = METADATA.matcher(itemCode);
if (!m.find()) {
2013-01-15 21:33:00 +01:00
return "";
}
2013-01-24 22:35:28 +01:00
return m.group();
2013-01-15 21:33:00 +01:00
}
}