Add auto-item-fill

This commit is contained in:
Andrzej Pomirski 2014-03-12 13:27:30 +01:00
parent 467cd34316
commit 20deb65a57
3 changed files with 30 additions and 6 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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) {