From 20deb65a57bde949726a8839994fed2a8f1d2546 Mon Sep 17 00:00:00 2001 From: Andrzej Pomirski Date: Wed, 12 Mar 2014 13:27:30 +0100 Subject: [PATCH] Add auto-item-fill --- .../ChestShop/Configuration/Properties.java | 3 ++ .../PreShopCreation/ItemChecker.java | 31 ++++++++++++++++--- .../ChestShop/Signs/ChestShopSign.java | 2 +- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java b/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java index 9bd0971..bb909d6 100644 --- a/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java +++ b/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java @@ -75,6 +75,9 @@ public class Properties { @ConfigurationComment("Can shops be used even when the seller doesn't have enough items? (The price will be scaled adequatly to the item amount)") public static boolean ALLOW_PARTIAL_TRANSACTIONS = true; + @ConfigurationComment("Can '?' be put in place of item name in order for the sign to be auto-filled?") + public static boolean ALLOW_AUTO_ITEM_FILL = true; + @PrecededBySpace @ConfigurationComment("Do you want to show \"Out of stock\" messages?") public static boolean SHOW_MESSAGE_OUT_OF_STOCK = true; diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java index 0afaf1f..ab3da0e 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java @@ -2,7 +2,9 @@ package com.Acrobot.ChestShop.Listeners.PreShopCreation; import com.Acrobot.Breeze.Utils.MaterialUtil; import com.Acrobot.Breeze.Utils.StringUtil; +import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Events.PreShopCreationEvent; +import com.Acrobot.ChestShop.Utils.uBlock; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -19,21 +21,40 @@ import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE; */ public class ItemChecker implements Listener { private static final short MAXIMUM_SIGN_LETTERS = 15; + private static final String AUTOFILL_CODE = "?"; @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 } + if (item == null) { + boolean foundItem = false; + + if (Properties.ALLOW_AUTO_ITEM_FILL && itemCode.equals(AUTOFILL_CODE) && uBlock.findConnectedChest(event.getSign()) != null) { + for (ItemStack stack : uBlock.findConnectedChest(event.getSign()).getBlockInventory().getContents()) { + 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; + } + } + String metadata = getMetadata(itemCode); String longName = MaterialUtil.getName(item); diff --git a/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java b/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java index a9f01ee..c44be3e 100644 --- a/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java +++ b/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java @@ -28,7 +28,7 @@ public class ChestShopSign { Pattern.compile("^?[\\w -.]*$"), Pattern.compile("^[1-9][0-9]*$"), Pattern.compile("(?i)^[\\d.bs(free) :]+$"), - Pattern.compile("^[\\w #:-]+$") + Pattern.compile("^[\\w? #:-]+$") }; public static boolean isAdminShop(Inventory ownerInventory) {