2013-01-15 21:33:00 +01:00
|
|
|
package com.Acrobot.ChestShop.Listeners.PreShopCreation;
|
|
|
|
|
|
|
|
import com.Acrobot.Breeze.Utils.MaterialUtil;
|
2018-09-09 15:37:09 +02:00
|
|
|
import com.Acrobot.Breeze.Utils.StringUtil;
|
2014-03-12 13:27:30 +01:00
|
|
|
import com.Acrobot.ChestShop.Configuration.Properties;
|
2019-01-17 19:50:47 +01:00
|
|
|
import com.Acrobot.ChestShop.Events.ItemParseEvent;
|
2013-01-15 21:33:00 +01:00
|
|
|
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
|
2018-01-07 02:11:59 +01:00
|
|
|
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
2021-01-03 01:55:01 +01:00
|
|
|
import com.Acrobot.ChestShop.Utils.ItemUtil;
|
2014-03-12 13:27:30 +01:00
|
|
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
2019-01-17 19:50:47 +01:00
|
|
|
import org.bukkit.Bukkit;
|
2018-01-07 02:11:59 +01:00
|
|
|
import org.bukkit.ChatColor;
|
2018-10-23 20:04:18 +02:00
|
|
|
import org.bukkit.block.Container;
|
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;
|
2018-01-07 02:11:59 +01:00
|
|
|
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;
|
2018-01-07 02:11:59 +01:00
|
|
|
import static com.Acrobot.ChestShop.Signs.ChestShopSign.AUTOFILL_CODE;
|
2013-01-15 21:33:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class ItemChecker implements Listener {
|
2018-05-07 16:32:51 +02:00
|
|
|
|
2013-01-15 21:33:00 +01:00
|
|
|
@EventHandler(priority = EventPriority.LOWEST)
|
|
|
|
public static void onPreShopCreation(PreShopCreationEvent event) {
|
|
|
|
String itemCode = event.getSignLine(ITEM_LINE);
|
2019-01-17 19:50:47 +01:00
|
|
|
|
|
|
|
ItemParseEvent parseEvent = new ItemParseEvent(itemCode);
|
|
|
|
Bukkit.getPluginManager().callEvent(parseEvent);
|
|
|
|
ItemStack item = parseEvent.getItem();
|
2013-01-15 21:33:00 +01:00
|
|
|
|
2014-03-12 13:27:30 +01:00
|
|
|
if (item == null) {
|
2018-01-07 02:11:59 +01:00
|
|
|
if (Properties.ALLOW_AUTO_ITEM_FILL && itemCode.equals(AUTOFILL_CODE)) {
|
2018-10-23 20:04:18 +02:00
|
|
|
Container container = uBlock.findConnectedContainer(event.getSign());
|
|
|
|
if (container != null) {
|
|
|
|
for (ItemStack stack : container.getInventory().getContents()) {
|
2018-01-07 02:11:59 +01:00
|
|
|
if (!MaterialUtil.isEmpty(stack)) {
|
2018-09-09 14:26:08 +02:00
|
|
|
item = stack;
|
2018-01-07 02:11:59 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-03-12 13:27:30 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-07 16:32:51 +02:00
|
|
|
|
2018-09-09 14:26:08 +02:00
|
|
|
if (item == null) {
|
2018-01-07 02:11:59 +01:00
|
|
|
event.setSignLine(ITEM_LINE, ChatColor.BOLD + ChestShopSign.AUTOFILL_CODE);
|
|
|
|
event.setOutcome(ITEM_AUTOFILL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2014-03-12 13:27:30 +01:00
|
|
|
event.setOutcome(INVALID_ITEM);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-05-07 16:32:51 +02:00
|
|
|
|
2021-01-03 01:55:01 +01:00
|
|
|
itemCode = ItemUtil.getSignName(item);
|
2018-09-09 14:26:08 +02:00
|
|
|
|
2018-09-09 15:37:09 +02:00
|
|
|
if (StringUtil.getMinecraftStringWidth(itemCode) > MAXIMUM_SIGN_WIDTH) {
|
2018-01-12 17:54:57 +01:00
|
|
|
event.setOutcome(INVALID_ITEM);
|
|
|
|
return;
|
2013-01-15 21:33:00 +01:00
|
|
|
}
|
2018-09-09 14:26:08 +02:00
|
|
|
|
|
|
|
event.setSignLine(ITEM_LINE, itemCode);
|
2013-01-15 21:33:00 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 19:29:31 +01:00
|
|
|
private static boolean isSameItem(String newCode, ItemStack item) {
|
2019-01-17 19:50:47 +01:00
|
|
|
ItemParseEvent parseEvent = new ItemParseEvent(newCode);
|
|
|
|
Bukkit.getPluginManager().callEvent(parseEvent);
|
|
|
|
ItemStack newItem = parseEvent.getItem();
|
2013-01-15 21:33:00 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|