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;
|
2014-03-12 13:27:30 +01:00
|
|
|
import com.Acrobot.ChestShop.Configuration.Properties;
|
2013-01-15 21:33:00 +01:00
|
|
|
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
|
2014-03-12 13:27:30 +01:00
|
|
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
2013-01-15 21:33:00 +01:00
|
|
|
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;
|
2014-03-12 13:27:30 +01:00
|
|
|
private static final String AUTOFILL_CODE = "?";
|
2013-01-15 21:33:00 +01:00
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.LOWEST)
|
|
|
|
public static void onPreShopCreation(PreShopCreationEvent event) {
|
|
|
|
String itemCode = event.getSignLine(ITEM_LINE);
|
|
|
|
ItemStack item = MaterialUtil.getItem(itemCode);
|
|
|
|
|
|
|
|
if (Odd.getFromString(itemCode) != null) {
|
|
|
|
return; // The OddItem name is OK
|
|
|
|
}
|
|
|
|
|
2014-03-12 13:27:30 +01:00
|
|
|
if (item == null) {
|
|
|
|
boolean foundItem = false;
|
|
|
|
|
|
|
|
if (Properties.ALLOW_AUTO_ITEM_FILL && itemCode.equals(AUTOFILL_CODE) && uBlock.findConnectedChest(event.getSign()) != null) {
|
2014-04-21 17:08:31 +02:00
|
|
|
for (ItemStack stack : uBlock.findConnectedChest(event.getSign()).getInventory().getContents()) {
|
2014-03-12 13:27:30 +01:00
|
|
|
if (!MaterialUtil.isEmpty(stack)) {
|
|
|
|
item = stack;
|
|
|
|
itemCode = MaterialUtil.getSignName(stack);
|
|
|
|
|
|
|
|
event.setSignLine(ITEM_LINE, itemCode);
|
|
|
|
foundItem = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundItem) {
|
|
|
|
event.setOutcome(INVALID_ITEM);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-24 00:53:33 +01:00
|
|
|
String itemName = StringUtil.capitalizeFirstLetter(longName);
|
2013-01-15 21:33:00 +01:00
|
|
|
|
2013-03-24 00:53:33 +01:00
|
|
|
event.setSignLine(ITEM_LINE, itemName + metadata);
|
2013-01-15 21:33:00 +01:00
|
|
|
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] : "");
|
|
|
|
|
2014-02-10 12:03:13 +01:00
|
|
|
if (code.length() > (MAXIMUM_SIGN_LETTERS - data.length())) {
|
2013-02-08 19:29:31 +01:00
|
|
|
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-03-24 00:53:33 +01:00
|
|
|
event.setSignLine(ITEM_LINE, code + data);
|
2013-01-15 21:33:00 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|