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

114 lines
4.0 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.Configuration.Messages;
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;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
2014-03-12 13:27:30 +01:00
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.ChatColor;
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.Events.PreShopCreationEvent.CreationOutcome.ITEM_AUTOFILL;
2013-01-15 21:33:00 +01:00
import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.AUTOFILL_CODE;
2013-01-15 21:33:00 +01:00
/**
* @author Acrobot
*/
public class ItemChecker implements Listener {
private static final short MAXIMUM_SIGN_LETTERS = 15;
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) {
if (Properties.ALLOW_AUTO_ITEM_FILL && itemCode.equals(AUTOFILL_CODE)) {
boolean foundItem = false;
if (uBlock.findConnectedChest(event.getSign()) != null) {
for (ItemStack stack : uBlock.findConnectedChest(event.getSign()).getInventory().getContents()) {
if (!MaterialUtil.isEmpty(stack)) {
item = stack;
itemCode = MaterialUtil.getSignName(stack);
event.setSignLine(ITEM_LINE, itemCode);
foundItem = true;
break;
}
2014-03-12 13:27:30 +01:00
}
}
if (!foundItem) {
event.setSignLine(ITEM_LINE, ChatColor.BOLD + ChestShopSign.AUTOFILL_CODE);
event.setOutcome(ITEM_AUTOFILL);
event.getPlayer().sendMessage(Messages.prefix(Messages.CLICK_TO_AUTOFILL_ITEM));
return;
}
} else {
2014-03-12 13:27:30 +01:00
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] : "");
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
}
}