Add ability to set max amount and improve error messages

This commit is contained in:
Phoenix616 2020-03-24 00:31:08 +01:00
parent 059abc569c
commit bb17cc35a0
6 changed files with 23 additions and 10 deletions

View File

@ -48,6 +48,8 @@ public class Messages {
public static String YOU_CANNOT_CREATE_SHOP = "You can't create this type of shop!";
public static String NO_CHEST_DETECTED = "Couldn't find a chest!";
public static String INVALID_SHOP_DETECTED = "The shop cannot be used!";
public static String INVALID_SHOP_PRICE = "The shop has an invalid price!";
public static String INVALID_SHOP_QUANTITY = "The shop has an invalid quantity!";
public static String CANNOT_ACCESS_THE_CHEST = "You don't have permissions to access this chest!";
@PrecededBySpace

View File

@ -154,6 +154,10 @@ public class Properties {
@ConfigurationComment("Should we block shops that sell things for more than they buy? (This prevents newbies from creating shops that would be exploited)")
public static boolean BLOCK_SHOPS_WITH_SELL_PRICE_HIGHER_THAN_BUY_PRICE = true;
@PrecededBySpace
@ConfigurationComment("Maximum amount of items that can be bought/sold at a shop. Default 3456 is a double chest of 64 stacks.")
public static int MAX_SHOP_AMOUNT = 3456;
@PrecededBySpace
@ConfigurationComment("Do you want to allow other players to build a shop on a block where there's one already?")
public static boolean ALLOW_MULTIPLE_SHOPS_AT_ONE_BLOCK = false;

View File

@ -182,15 +182,19 @@ public class PlayerInteract implements Listener {
ItemParseEvent parseEvent = new ItemParseEvent(material);
Bukkit.getPluginManager().callEvent(parseEvent);
ItemStack item = parseEvent.getItem();
if (item == null || !NumberUtil.isInteger(quantity)) {
if (item == null) {
player.sendMessage(Messages.prefix(Messages.INVALID_SHOP_DETECTED));
return null;
}
int amount = Integer.parseInt(quantity);
int amount = -1;
try {
amount = Integer.parseInt(quantity);
} catch (NumberFormatException notANumber) {}
if (amount < 1) {
amount = 1;
if (amount < 1 || amount > Properties.MAX_SHOP_AMOUNT) {
player.sendMessage(Messages.prefix(Messages.INVALID_SHOP_PRICE));
return null;
}
if (Properties.SHIFT_SELLS_IN_STACKS && player.isSneaking() && !price.equals(PriceUtil.NO_PRICE) && isAllowedForShift(action == buy)) {

View File

@ -27,10 +27,10 @@ public class ErrorMessageSender implements Listener {
message = Messages.INCORRECT_ITEM_ID;
break;
case INVALID_PRICE:
message = Messages.INVALID_SHOP_DETECTED;
message = Messages.INVALID_SHOP_PRICE;
break;
case INVALID_QUANTITY:
message = Messages.INVALID_SHOP_DETECTED;
message = Messages.INVALID_SHOP_QUANTITY;
break;
case SELL_PRICE_HIGHER_THAN_BUY_PRICE:
message = Messages.YOU_CANNOT_CREATE_SHOP;

View File

@ -1,6 +1,6 @@
package com.Acrobot.ChestShop.Listeners.PreShopCreation;
import com.Acrobot.Breeze.Utils.NumberUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -16,9 +16,12 @@ public class QuantityChecker implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public static void onPreShopCreation(PreShopCreationEvent event) {
String quantity = event.getSignLine(QUANTITY_LINE);
int amount = -1;
try {
amount = Integer.parseInt(event.getSignLine(QUANTITY_LINE));
} catch (NumberFormatException notANumber) {}
if (!NumberUtil.isInteger(quantity)) {
if (amount < 1 || amount > Properties.MAX_SHOP_AMOUNT) {
event.setOutcome(INVALID_QUANTITY);
}
}

View File

@ -33,7 +33,7 @@ public class ChestShopSign {
public static final Pattern[] SHOP_SIGN_PATTERN = {
Pattern.compile("^?[\\w -.:]*$"),
Pattern.compile("^[1-9][0-9]*$"),
Pattern.compile("^[1-9][0-9]{0,6}$"),
Pattern.compile("(?i)^[\\d.bs(free) :]+$"),
Pattern.compile("^[\\w? #:-]+$")
};