Unify sign line parsing and strip owner line whitespace

This commit is contained in:
Phoenix616 2022-01-26 20:35:42 +01:00
parent 5ec2ab61e5
commit 99a609b423
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0
24 changed files with 209 additions and 129 deletions

View File

@ -33,7 +33,6 @@ import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState;
import static com.Acrobot.Breeze.Utils.BlockUtil.getAttachedBlock;
import static com.Acrobot.Breeze.Utils.BlockUtil.isSign;
import static com.Acrobot.ChestShop.Permission.OTHER_NAME_DESTROY;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
/**
* @author Acrobot
@ -145,7 +144,7 @@ public class SignBreak implements Listener {
continue;
}
if (Properties.TURN_OFF_SIGN_PROTECTION || canDestroyShop(breaker, sign.getLine(NAME_LINE))) {
if (Properties.TURN_OFF_SIGN_PROTECTION || canDestroyShop(breaker, ChestShopSign.getOwner(sign))) {
brokenBlocks.add(sign);
} else {
canBeBroken = false;

View File

@ -15,7 +15,6 @@ import org.bukkit.event.Listener;
import org.bukkit.event.block.SignChangeEvent;
import static com.Acrobot.ChestShop.Permission.OTHER_NAME_DESTROY;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
/**
* @author Acrobot
@ -32,7 +31,7 @@ public class SignCreate implements Listener {
Sign sign = (Sign) signBlock.getState();
if (ChestShopSign.isValid(sign) && !NameManager.canUseName(event.getPlayer(), OTHER_NAME_DESTROY, StringUtil.stripColourCodes(sign.getLine(NAME_LINE)))) {
if (ChestShopSign.isValid(sign) && !NameManager.canUseName(event.getPlayer(), OTHER_NAME_DESTROY, ChestShopSign.getOwner(sign))) {
event.setCancelled(true);
sign.update();
return;

View File

@ -5,6 +5,7 @@ import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Events.ChestShopReloadEvent;
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
@ -18,7 +19,6 @@ import java.util.HashSet;
import java.util.Set;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.PRICE_LINE;
/**
* @author Acrobot
@ -63,7 +63,7 @@ public class DiscountModule implements Listener {
Player client = event.getClient();
if (!PriceUtil.hasBuyPrice(event.getSign().getLine(PRICE_LINE))) {
if (!PriceUtil.hasBuyPrice(ChestShopSign.getPrice(event.getSign()))) {
return;
}

View File

@ -1,12 +1,12 @@
package com.Acrobot.ChestShop.Listeners.Modules;
import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.Breeze.Utils.QuantityUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Events.ChestShopReloadEvent;
import com.Acrobot.ChestShop.Events.ItemParseEvent;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
@ -25,9 +25,6 @@ import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.BUY_PRICE_BELOW_MIN;
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.SELL_PRICE_ABOVE_MAX;
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.SELL_PRICE_BELOW_MIN;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.PRICE_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.QUANTITY_LINE;
/**
* @author Acrobot
@ -106,7 +103,7 @@ public class PriceRestrictionModule implements Listener {
@EventHandler
public void onPreShopCreation(PreShopCreationEvent event) {
ItemParseEvent parseEvent = new ItemParseEvent(event.getSignLine(ITEM_LINE));
ItemParseEvent parseEvent = new ItemParseEvent(ChestShopSign.getItem(event.getSignLines()));
Bukkit.getPluginManager().callEvent(parseEvent);
ItemStack material = parseEvent.getItem();
@ -117,13 +114,14 @@ public class PriceRestrictionModule implements Listener {
String itemType = material.getType().toString().toLowerCase(Locale.ROOT);
int amount;
try {
amount = QuantityUtil.parseQuantity(event.getSignLine(QUANTITY_LINE));
amount = ChestShopSign.getQuantity(event.getSign());
} catch (IllegalArgumentException e) {
return;
}
if (PriceUtil.hasBuyPrice(event.getSignLine(PRICE_LINE))) {
BigDecimal buyPrice = PriceUtil.getExactBuyPrice(event.getSignLine(PRICE_LINE));
String priceLine = ChestShopSign.getPrice(event.getSignLines());
if (PriceUtil.hasBuyPrice(priceLine)) {
BigDecimal buyPrice = PriceUtil.getExactBuyPrice(priceLine);
BigDecimal minBuyPrice = BigDecimal.valueOf(configuration.getDouble("min.buy_price." + itemType) * amount);
if (isValid("min.buy_price." + itemType) && buyPrice.compareTo(minBuyPrice) < 0) {
@ -138,8 +136,8 @@ public class PriceRestrictionModule implements Listener {
}
}
if (PriceUtil.hasSellPrice(event.getSignLine(PRICE_LINE))) {
BigDecimal sellPrice = PriceUtil.getExactSellPrice(event.getSignLine(PRICE_LINE));
if (PriceUtil.hasSellPrice(priceLine)) {
BigDecimal sellPrice = PriceUtil.getExactSellPrice(priceLine);
BigDecimal minSellPrice = BigDecimal.valueOf(configuration.getDouble("min.sell_price." + itemType) * amount);
if (isValid("min.sell_price." + itemType) && sellPrice.compareTo(minSellPrice) < 0) {

View File

@ -23,8 +23,6 @@ import org.bukkit.inventory.ItemStack;
import java.util.IllegalFormatException;
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getHolder;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.QUANTITY_LINE;
@ -38,17 +36,17 @@ public class StockCounterModule implements Listener {
public static void onPreShopCreation(PreShopCreationEvent event) {
int quantity;
try {
quantity = QuantityUtil.parseQuantity(event.getSignLine(QUANTITY_LINE));
quantity = ChestShopSign.getQuantity(event.getSign());
} catch (IllegalArgumentException invalidQuantity) {
return;
}
if (QuantityUtil.quantityLineContainsCounter(event.getSignLine(QUANTITY_LINE))) {
if (QuantityUtil.quantityLineContainsCounter(ChestShopSign.getQuantityLine(event.getSignLines()))) {
event.setSignLine(QUANTITY_LINE, Integer.toString(quantity));
}
if (!Properties.USE_STOCK_COUNTER
|| (Properties.FORCE_UNLIMITED_ADMIN_SHOP && ChestShopSign.isAdminShop(event.getSignLine(NAME_LINE)))) {
|| (Properties.FORCE_UNLIMITED_ADMIN_SHOP && ChestShopSign.isAdminShop(event.getSignLines()))) {
return;
}
@ -57,7 +55,7 @@ public class StockCounterModule implements Listener {
return;
}
ItemStack itemTradedByShop = determineItemTradedByShop(event.getSignLine(ITEM_LINE));
ItemStack itemTradedByShop = determineItemTradedByShop(ChestShopSign.getItem(event.getSignLines()));
if (itemTradedByShop != null) {
Container container = uBlock.findConnectedContainer(event.getSign());
if (container != null) {
@ -75,7 +73,7 @@ public class StockCounterModule implements Listener {
for (Sign shopSign : uBlock.findConnectedShopSigns(getHolder(event.getInventory(), false))) {
if (!Properties.USE_STOCK_COUNTER
|| (Properties.FORCE_UNLIMITED_ADMIN_SHOP && ChestShopSign.isAdminShop(shopSign))) {
if (QuantityUtil.quantityLineContainsCounter(shopSign.getLine(QUANTITY_LINE))) {
if (QuantityUtil.quantityLineContainsCounter(ChestShopSign.getQuantityLine(shopSign))) {
removeCounterFromQuantityLine(shopSign);
}
continue;
@ -83,7 +81,7 @@ public class StockCounterModule implements Listener {
if (Properties.MAX_SHOP_AMOUNT > 99999) {
ChestShop.getBukkitLogger().warning("Stock counter cannot be used if MAX_SHOP_AMOUNT is over 5 digits");
if (QuantityUtil.quantityLineContainsCounter(shopSign.getLine(QUANTITY_LINE))) {
if (QuantityUtil.quantityLineContainsCounter(ChestShopSign.getQuantityLine(shopSign))) {
removeCounterFromQuantityLine(shopSign);
}
return;
@ -95,8 +93,9 @@ public class StockCounterModule implements Listener {
@EventHandler(priority = EventPriority.HIGH)
public static void onTransaction(final TransactionEvent event) {
String quantityLine = ChestShopSign.getQuantityLine(event.getSign());
if (!Properties.USE_STOCK_COUNTER) {
if (QuantityUtil.quantityLineContainsCounter(event.getSign().getLine(QUANTITY_LINE))) {
if (QuantityUtil.quantityLineContainsCounter(quantityLine)) {
removeCounterFromQuantityLine(event.getSign());
}
return;
@ -104,7 +103,7 @@ public class StockCounterModule implements Listener {
if (Properties.MAX_SHOP_AMOUNT > 99999) {
ChestShop.getBukkitLogger().warning("Stock counter cannot be used if MAX_SHOP_AMOUNT is over 5 digits");
if (QuantityUtil.quantityLineContainsCounter(event.getSign().getLine(QUANTITY_LINE))) {
if (QuantityUtil.quantityLineContainsCounter(quantityLine)) {
removeCounterFromQuantityLine(event.getSign());
}
return;
@ -127,7 +126,7 @@ public class StockCounterModule implements Listener {
int quantity;
try {
quantity = QuantityUtil.parseQuantity(sign.getLine(QUANTITY_LINE));
quantity = ChestShopSign.getQuantity(sign);
} catch (IllegalFormatException invalidQuantity) {
return;
}
@ -141,7 +140,7 @@ public class StockCounterModule implements Listener {
public static void removeCounterFromQuantityLine(Sign sign) {
int quantity;
try {
quantity = QuantityUtil.parseQuantity(sign.getLine(QUANTITY_LINE));
quantity = ChestShopSign.getQuantity(sign);
} catch (IllegalFormatException invalidQuantity) {
return;
}
@ -157,7 +156,7 @@ public class StockCounterModule implements Listener {
}
public static ItemStack determineItemTradedByShop(Sign sign) {
return determineItemTradedByShop(sign.getLine(ITEM_LINE));
return determineItemTradedByShop(ChestShopSign.getItem(sign));
}
public static ItemStack determineItemTradedByShop(String material) {

View File

@ -89,7 +89,7 @@ public class PlayerInteract implements Listener {
return;
}
if (Properties.ALLOW_AUTO_ITEM_FILL && ChatColor.stripColor(sign.getLine(ITEM_LINE)).equals(AUTOFILL_CODE)) {
if (Properties.ALLOW_AUTO_ITEM_FILL && ChatColor.stripColor(ChestShopSign.getItem(sign)).equals(AUTOFILL_CODE)) {
if (ChestShopSign.hasPermission(player, OTHER_NAME_CREATE, sign)) {
ItemStack item = player.getInventory().getItemInMainHand();
if (!MaterialUtil.isEmpty(item)) {
@ -174,10 +174,9 @@ public class PlayerInteract implements Listener {
}
private static PreTransactionEvent preparePreTransactionEvent(Sign sign, Player player, Action action) {
String name = sign.getLine(NAME_LINE);
String quantity = sign.getLine(QUANTITY_LINE);
String prices = sign.getLine(PRICE_LINE);
String material = sign.getLine(ITEM_LINE);
String name = ChestShopSign.getOwner(sign);
String prices = ChestShopSign.getPrice(sign);
String material = ChestShopSign.getItem(sign);
AccountQueryEvent accountQueryEvent = new AccountQueryEvent(name);
Bukkit.getPluginManager().callEvent(accountQueryEvent);
@ -215,7 +214,7 @@ public class PlayerInteract implements Listener {
int amount = -1;
try {
amount = QuantityUtil.parseQuantity(quantity);
amount = ChestShopSign.getQuantity(sign);
} catch (NumberFormatException notANumber) {}
if (amount < 1 || amount > Properties.MAX_SHOP_AMOUNT) {

View File

@ -8,8 +8,6 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.*;
/**
* @author Acrobot
*/
@ -18,25 +16,23 @@ public class ShopCreationLogger implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public static void onShopCreation(final ShopCreatedEvent event) {
ChestShop.getBukkitServer().getScheduler().runTaskAsynchronously(ChestShop.getPlugin(), new Runnable() {
@Override public void run() {
String creator = event.getPlayer().getName();
String shopOwner = event.getSignLine(NAME_LINE);
String typeOfShop = ChestShopSign.isAdminShop(shopOwner) ? "an Admin Shop" : "a shop" + (event.createdByOwner() ? "" : " for " + event.getOwnerAccount().getName());
ChestShop.getBukkitServer().getScheduler().runTaskAsynchronously(ChestShop.getPlugin(), () -> {
String creator = event.getPlayer().getName();
String shopOwner = ChestShopSign.getOwner(event.getSignLines());
String typeOfShop = ChestShopSign.isAdminShop(shopOwner) ? "an Admin Shop" : "a shop" + (event.createdByOwner() ? "" : " for " + event.getOwnerAccount().getName());
String item = event.getSignLine(QUANTITY_LINE) + ' ' + event.getSignLine(ITEM_LINE);
String prices = event.getSignLine(PRICE_LINE);
String location = LocationUtil.locationToString(event.getSign().getLocation());
String item = ChestShopSign.getQuantity(event.getSign()) + ' ' + ChestShopSign.getItem(event.getSign());
String prices = ChestShopSign.getPrice(event.getSign());
String location = LocationUtil.locationToString(event.getSign().getLocation());
String message = String.format(CREATION_MESSAGE,
creator,
typeOfShop,
item,
prices,
location);
String message = String.format(CREATION_MESSAGE,
creator,
typeOfShop,
item,
prices,
location);
ChestShop.getBukkitLogger().info(message);
}
ChestShop.getBukkitLogger().info(message);
});
}
}

View File

@ -13,8 +13,6 @@ import org.bukkit.block.data.type.WallSign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
/**
* @author Acrobot
*/
@ -26,7 +24,7 @@ public class SignSticker implements Listener {
return;
}
if (ChestShopSign.isAdminShop(event.getSignLine(NAME_LINE))) {
if (ChestShopSign.isAdminShop(event.getSign())) {
return;
}

View File

@ -14,7 +14,6 @@ import org.bukkit.event.Listener;
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.NO_CHEST;
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.NO_PERMISSION_FOR_CHEST;
import static com.Acrobot.ChestShop.Permission.ADMIN;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
/**
* @author Acrobot
@ -23,7 +22,7 @@ public class ChestChecker implements Listener {
@EventHandler(priority = EventPriority.LOW)
public static void onPreShopCreation(PreShopCreationEvent event) {
String nameLine = event.getSignLine(NAME_LINE);
String nameLine = ChestShopSign.getOwner(event.getSignLines());
Container connectedContainer = uBlock.findConnectedContainer(event.getSign().getBlock());

View File

@ -18,7 +18,6 @@ import org.bukkit.event.Listener;
import java.math.BigDecimal;
import static com.Acrobot.ChestShop.Permission.NOFEE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
/**
* @author Acrobot
@ -33,7 +32,7 @@ public class CreationFeeGetter implements Listener {
return;
}
if (ChestShopSign.isAdminShop(event.getSignLine(NAME_LINE))) {
if (ChestShopSign.isAdminShop(event.getSign())) {
return;
}

View File

@ -31,7 +31,7 @@ public class ItemChecker implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public static void onPreShopCreation(PreShopCreationEvent event) {
String itemCode = event.getSignLine(ITEM_LINE);
String itemCode = ChestShopSign.getItem(event.getSignLines());
ItemParseEvent parseEvent = new ItemParseEvent(itemCode);
Bukkit.getPluginManager().callEvent(parseEvent);

View File

@ -14,7 +14,6 @@ import java.math.BigDecimal;
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.NOT_ENOUGH_MONEY;
import static com.Acrobot.ChestShop.Permission.NOFEE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
/**
* @author Acrobot
@ -29,7 +28,7 @@ public class MoneyChecker implements Listener {
return;
}
if (ChestShopSign.isAdminShop(event.getSignLine(NAME_LINE))) {
if (ChestShopSign.isAdminShop(event.getSign())) {
return;
}

View File

@ -4,6 +4,7 @@ import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Database.Account;
import com.Acrobot.ChestShop.Events.AccountQueryEvent;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -30,7 +31,7 @@ public class NameChecker implements Listener {
}
private static void handleEvent(PreShopCreationEvent event) {
String name = event.getSignLine(NAME_LINE);
String name = ChestShopSign.getOwner(event.getSignLines());
Player player = event.getPlayer();
Account account = event.getOwnerAccount();

View File

@ -4,6 +4,7 @@ import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.ChestShop.Events.ItemParseEvent;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -15,9 +16,7 @@ import java.util.Locale;
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.NO_PERMISSION;
import static com.Acrobot.ChestShop.Permission.*;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.PRICE_LINE;
import static org.bukkit.event.EventPriority.HIGH;
/**
@ -36,8 +35,8 @@ public class PermissionChecker implements Listener {
return;
}
String priceLine = event.getSignLine(PRICE_LINE);
String itemLine = event.getSignLine(ITEM_LINE);
String priceLine = ChestShopSign.getPrice(event.getSign());
String itemLine = ChestShopSign.getItem(event.getSign());
ItemParseEvent parseEvent = new ItemParseEvent(itemLine);
Bukkit.getPluginManager().callEvent(parseEvent);

View File

@ -3,6 +3,7 @@ package com.Acrobot.ChestShop.Listeners.PreShopCreation;
import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
@ -20,7 +21,7 @@ public class PriceChecker implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public static void onPreShopCreation(PreShopCreationEvent event) {
String line = event.getSignLine(PRICE_LINE).toUpperCase(Locale.ROOT);
String line = ChestShopSign.getPrice(event.getSignLines()).toUpperCase(Locale.ROOT);
if (Properties.PRICE_PRECISION <= 0) {
line = line.replaceAll("\\.\\d*", ""); //remove too many decimal places
} else {

View File

@ -2,6 +2,7 @@ package com.Acrobot.ChestShop.Listeners.PreShopCreation;
import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -10,7 +11,6 @@ import java.math.BigDecimal;
import static com.Acrobot.Breeze.Utils.PriceUtil.hasBuyPrice;
import static com.Acrobot.Breeze.Utils.PriceUtil.hasSellPrice;
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.SELL_PRICE_HIGHER_THAN_BUY_PRICE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.PRICE_LINE;
import static org.bukkit.event.EventPriority.HIGH;
/**
@ -20,7 +20,7 @@ public class PriceRatioChecker implements Listener {
@EventHandler(priority = HIGH)
public static void onPreShopCreation(PreShopCreationEvent event) {
String priceLine = event.getSignLine(PRICE_LINE);
String priceLine = ChestShopSign.getPrice(event.getSignLines());
if (hasBuyPrice(priceLine) && hasSellPrice(priceLine)) {
BigDecimal buyPrice = PriceUtil.getExactBuyPrice(priceLine);

View File

@ -3,12 +3,12 @@ package com.Acrobot.ChestShop.Listeners.PreShopCreation;
import com.Acrobot.Breeze.Utils.QuantityUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.INVALID_QUANTITY;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.QUANTITY_LINE;
/**
* @author Acrobot
@ -19,7 +19,7 @@ public class QuantityChecker implements Listener {
public static void onPreShopCreation(PreShopCreationEvent event) {
int amount = -1;
try {
amount = QuantityUtil.parseQuantity(event.getSignLine(QUANTITY_LINE));
amount = ChestShopSign.getQuantity(event.getSignLines());
} catch (NumberFormatException notANumber) {}
if (amount < 1 || amount > Properties.MAX_SHOP_AMOUNT) {

View File

@ -3,6 +3,7 @@ package com.Acrobot.ChestShop.Listeners.PreTransaction;
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
import com.Acrobot.ChestShop.Events.TransactionEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -12,7 +13,6 @@ import java.util.Locale;
import static com.Acrobot.ChestShop.Events.PreTransactionEvent.TransactionOutcome.CLIENT_DOES_NOT_HAVE_PERMISSION;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE;
/**
* @author Acrobot
@ -27,7 +27,7 @@ public class PermissionChecker implements Listener {
Player client = event.getClient();
TransactionEvent.TransactionType transactionType = event.getTransactionType();
String itemLine = event.getSign().getLine(ITEM_LINE);
String itemLine = ChestShopSign.getItem(event.getSign());
if (itemLine.contains("#") && Permission.hasPermissionSetFalse(client, (transactionType == BUY ? Permission.BUY_ID : Permission.SELL_ID) + itemLine)) {
event.setCancelled(CLIENT_DOES_NOT_HAVE_PERMISSION);
return;

View File

@ -3,11 +3,9 @@ package com.Acrobot.ChestShop.Listeners;
import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.Breeze.Utils.QuantityUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Economy.Economy;
import com.Acrobot.ChestShop.Events.AccountQueryEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencyFormatEvent;
import com.Acrobot.ChestShop.Events.ItemInfoEvent;
@ -23,7 +21,6 @@ import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;
/**
@ -34,15 +31,15 @@ public class ShopInfoListener implements Listener {
@EventHandler(ignoreCancelled = true)
public static void showShopInfo(ShopInfoEvent event) {
if (ChestShopSign.isValid(event.getSign())) {
String nameLine = event.getSign().getLine(ChestShopSign.NAME_LINE);
String nameLine = ChestShopSign.getOwner(event.getSign());
int amount;
try {
amount = QuantityUtil.parseQuantity(event.getSign().getLine(ChestShopSign.QUANTITY_LINE));
amount = ChestShopSign.getQuantity(event.getSign());
} catch (NumberFormatException notANumber) {
Messages.INVALID_SHOP_DETECTED.sendWithPrefix(event.getSender());
return;
}
String pricesLine = event.getSign().getLine(ChestShopSign.PRICE_LINE);
String pricesLine = ChestShopSign.getPrice(event.getSign());
AccountQueryEvent queryEvent = new AccountQueryEvent(nameLine);
ChestShop.callEvent(queryEvent);
@ -54,7 +51,7 @@ public class ShopInfoListener implements Listener {
String ownerName = queryEvent.getAccount().getName();
ownerName = ownerName != null ? ownerName : nameLine;
ItemParseEvent parseEvent = new ItemParseEvent(event.getSign().getLine(ChestShopSign.ITEM_LINE));
ItemParseEvent parseEvent = new ItemParseEvent(ChestShopSign.getItem(event.getSign()));
ItemStack item = ChestShop.callEvent(parseEvent).getItem();
if (item == null || amount < 1) {
Messages.INVALID_SHOP_DETECTED.sendWithPrefix(event.getSender());

View File

@ -10,6 +10,7 @@ import com.Acrobot.ChestShop.Events.Economy.CurrencyAddEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencySubtractEvent;
import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -18,12 +19,9 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import java.math.BigDecimal;
import java.util.UUID;
import static com.Acrobot.ChestShop.Permission.NOFEE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.AUTOFILL_CODE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
/**
* @author Acrobot
@ -37,11 +35,11 @@ public class ShopRefundListener implements Listener {
return;
}
if (ChatColor.stripColor(event.getSign().getLine(ITEM_LINE)).equals(AUTOFILL_CODE)) {
if (ChatColor.stripColor(ChestShopSign.getItem(event.getSign())).equals(AUTOFILL_CODE)) {
return;
}
AccountQueryEvent accountQueryEvent = new AccountQueryEvent(event.getSign().getLine(NAME_LINE));
AccountQueryEvent accountQueryEvent = new AccountQueryEvent(ChestShopSign.getOwner(event.getSign()));
Bukkit.getPluginManager().callEvent(accountQueryEvent);
Account account = accountQueryEvent.getAccount();
if (account == null) {

View File

@ -9,8 +9,6 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.*;
/**
* @author Acrobot
*/
@ -23,24 +21,22 @@ public class ShopRemovalLogger implements Listener {
return;
}
ChestShop.getBukkitServer().getScheduler().runTaskAsynchronously(ChestShop.getPlugin(), new Runnable() {
@Override public void run() {
String shopOwner = event.getSign().getLine(NAME_LINE);
String typeOfShop = ChestShopSign.isAdminShop(shopOwner) ? "An Admin Shop" : "A shop belonging to " + shopOwner;
ChestShop.getBukkitServer().getScheduler().runTaskAsynchronously(ChestShop.getPlugin(), () -> {
String shopOwner = ChestShopSign.getOwner(event.getSign());
String typeOfShop = ChestShopSign.isAdminShop(shopOwner) ? "An Admin Shop" : "A shop belonging to " + shopOwner;
String item = event.getSign().getLine(QUANTITY_LINE) + ' ' + event.getSign().getLine(ITEM_LINE);
String prices = event.getSign().getLine(PRICE_LINE);
String location = LocationUtil.locationToString(event.getSign().getLocation());
String item = ChestShopSign.getQuantity(event.getSign()) + ' ' + ChestShopSign.getItem(event.getSign());
String prices = ChestShopSign.getPrice(event.getSign());
String location = LocationUtil.locationToString(event.getSign().getLocation());
String message = String.format(REMOVAL_MESSAGE,
typeOfShop,
event.getDestroyer() != null ? event.getDestroyer().getName() : "???",
item,
prices,
location);
String message = String.format(REMOVAL_MESSAGE,
typeOfShop,
event.getDestroyer() != null ? event.getDestroyer().getName() : "???",
item,
prices,
location);
ChestShop.getBukkitLogger().info(message);
}
ChestShop.getBukkitLogger().info(message);
});
}
}

View File

@ -98,7 +98,7 @@ public class Security {
continue;
}
AccountQueryEvent accountQueryEvent = new AccountQueryEvent(sign.getLine(ChestShopSign.NAME_LINE));
AccountQueryEvent accountQueryEvent = new AccountQueryEvent(ChestShopSign.getOwner(sign));
Bukkit.getPluginManager().callEvent(accountQueryEvent);
Account account = accountQueryEvent.getAccount();
if (account != null && !account.getUuid().equals(player.getUniqueId())) {

View File

@ -1,6 +1,7 @@
package com.Acrobot.ChestShop.Signs;
import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.Breeze.Utils.QuantityUtil;
import com.Acrobot.Breeze.Utils.StringUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Containers.AdminInventory;
@ -9,6 +10,7 @@ import com.Acrobot.ChestShop.Events.AccountQueryEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
@ -56,16 +58,23 @@ public class ChestShopSign {
}
public static boolean isAdminShop(Sign sign) {
return isAdminShop(sign.getLine(NAME_LINE));
return isAdminShop(sign.getLines());
}
public static boolean isAdminShop(String[] lines) {
return isAdminShop(getOwner(lines));
}
public static boolean isValid(Sign sign) {
return isValid(sign.getLines());
}
public static boolean isValid(String[] line) {
line = StringUtil.stripColourCodes(line);
return isValidPreparedSign(line) && (line[PRICE_LINE].toUpperCase(Locale.ROOT).contains("B") || line[PRICE_LINE].toUpperCase(Locale.ROOT).contains("S")) && !line[NAME_LINE].isEmpty();
public static boolean isValid(String[] lines) {
lines = StringUtil.stripColourCodes(lines);
return isValidPreparedSign(lines)
&& (getPrice(lines).toUpperCase(Locale.ROOT).contains("B")
|| getPrice(lines).toUpperCase(Locale.ROOT).contains("S"))
&& !getOwner(lines).isEmpty();
}
public static boolean isValid(Block sign) {
@ -128,7 +137,7 @@ public class ChestShopSign {
if (player == null) return false;
if (sign == null) return true;
String name = sign.getLine(NAME_LINE);
String name = getOwner(sign);
if (name == null || name.isEmpty()) return true;
return NameManager.canUseName(player, base, name);
@ -137,7 +146,7 @@ public class ChestShopSign {
public static boolean isOwner(Player player, Sign sign) {
if (player == null || sign == null) return false;
String name = sign.getLine(NAME_LINE);
String name = getOwner(sign);
if (name == null || name.isEmpty()) return false;
AccountQueryEvent accountQueryEvent = new AccountQueryEvent(name);
@ -150,8 +159,7 @@ public class ChestShopSign {
}
public static boolean isValidPreparedSign(String[] lines) {
// The first line is the name of the shop owner
String playername = lines[0];
String playername = ChestShopSign.getOwner(lines);
// If the shop owner is not blank (auto-filled) or the admin shop string, we need to validate it
if ((!isAdminShop(playername)) && (playername.length() > 0)) {
@ -171,7 +179,7 @@ public class ChestShopSign {
}
}
// The first line is valid. Now validate the last 3 lines against the predefined regexp patterns.
// The playername on the first line is valid. Now validate the last 3 lines against the predefined regexp patterns.
for (int i = 0; i < 3; i++) {
boolean matches = false;
for (Pattern pattern : SHOP_SIGN_PATTERN[i]) {
@ -186,6 +194,101 @@ public class ChestShopSign {
}
// All lines are looking good. If the price line contains only one ':', then this is a valid prepared sign.
return lines[PRICE_LINE].indexOf(':') == lines[PRICE_LINE].lastIndexOf(':');
String priceLine = getPrice(lines);
return priceLine.indexOf(':') == priceLine.lastIndexOf(':');
}
/**
* Get the owner string of a shop sign
* @param sign The sign
* @return The owner string
*/
public static String getOwner(Sign sign) {
return getOwner(sign.getLines());
}
/**
* Get the owner string of a shop sign
* @param lines The sign lines
* @return The owner string
*/
public static String getOwner(String[] lines) {
return StringUtil.stripColourCodes(StringUtils.strip(lines[NAME_LINE]));
}
/**
* Get the quantity and count line of the shop sign
* @param sign The sign
* @return The quantity line
* @throws IllegalArgumentException Thrown when an invalid quantity is present
*/
public static String getQuantityLine(Sign sign) throws IllegalArgumentException {
return getQuantityLine(sign.getLines());
}
/**
* Get the quantity and count line of sign lines
* @param lines The sign lines
* @return The quantity line
* @throws IllegalArgumentException Thrown when an invalid quantity is present
*/
public static String getQuantityLine(String[] lines) throws IllegalArgumentException {
return lines.length > QUANTITY_LINE ? StringUtils.strip(lines[QUANTITY_LINE]) : "";
}
/**
* Get the quantity of the shop sign
* @param sign The sign
* @return The quantity line
* @throws IllegalArgumentException Thrown when an invalid quantity is present
*/
public static int getQuantity(Sign sign) throws IllegalArgumentException {
return getQuantity(sign.getLines());
}
/**
* Get the quantity of sign lines
* @param lines The sign lines
* @return The quantity
* @throws IllegalArgumentException Thrown when an invalid quantity is present
*/
public static int getQuantity(String[] lines) throws IllegalArgumentException {
return QuantityUtil.parseQuantity(getQuantityLine(lines));
}
/**
* Get the price line of the shop sign
* @param sign The sign
* @return The price line
*/
public static String getPrice(Sign sign) {
return StringUtils.strip(sign.getLine(PRICE_LINE));
}
/**
* Get the price line of sign lines
* @param lines The sign lines
* @return The price line
*/
public static String getPrice(String[] lines) {
return lines.length > PRICE_LINE ? StringUtils.strip(lines[PRICE_LINE]) : "";
}
/**
* Get the item line of the shop sign
* @param sign The sign
* @return The item line
*/
public static String getItem(Sign sign) {
return getItem(sign.getLines());
}
/**
* Get the item line of sign lines
* @param lines The sign lines
* @return The item line
*/
public static String getItem(String[] lines) {
return lines.length > ITEM_LINE ? StringUtils.strip(lines[ITEM_LINE]) : "";
}
}

View File

@ -28,11 +28,11 @@ public class PriceCheckerTest {
public void testLegalBuyPrice() {
PreShopCreationEvent event = new PreShopCreationEvent(null, null, getPriceString("B 1"));
onPreShopCreation(event);
assertEquals(PriceUtil.getExactBuyPrice(event.getSignLine(ChestShopSign.PRICE_LINE)), BigDecimal.valueOf(1));
assertEquals(PriceUtil.getExactBuyPrice(ChestShopSign.getPrice(event.getSignLines())), BigDecimal.valueOf(1));
event = new PreShopCreationEvent(null, null, getPriceString("B FREE"));
onPreShopCreation(event);
assertEquals(PriceUtil.getExactBuyPrice(event.getSignLine(ChestShopSign.PRICE_LINE)), PriceUtil.FREE);
assertEquals(PriceUtil.getExactBuyPrice(ChestShopSign.getPrice(event.getSignLines())), PriceUtil.FREE);
assertFalse(event.isCancelled());
}
@ -41,11 +41,11 @@ public class PriceCheckerTest {
public void testLegalSellPrice() {
PreShopCreationEvent event = new PreShopCreationEvent(null, null, getPriceString("S 1"));
onPreShopCreation(event);
assertEquals(PriceUtil.getExactSellPrice(event.getSignLine(ChestShopSign.PRICE_LINE)), BigDecimal.valueOf(1));
assertEquals(PriceUtil.getExactSellPrice(ChestShopSign.getPrice(event.getSignLines())), BigDecimal.valueOf(1));
event = new PreShopCreationEvent(null, null, getPriceString("S FREE"));
onPreShopCreation(event);
assertEquals(PriceUtil.getExactSellPrice(event.getSignLine(ChestShopSign.PRICE_LINE)), PriceUtil.FREE);
assertEquals(PriceUtil.getExactSellPrice(ChestShopSign.getPrice(event.getSignLines())), PriceUtil.FREE);
assertFalse(event.isCancelled());
}
@ -65,22 +65,22 @@ public class PriceCheckerTest {
public void testLegalBuyAndSellPrices() {
PreShopCreationEvent event = new PreShopCreationEvent(null, null, getPriceString("B 2:S 1"));
onPreShopCreation(event);
assertEquals(PriceUtil.getExactSellPrice(event.getSignLine(ChestShopSign.PRICE_LINE)), BigDecimal.valueOf(1));
assertEquals(PriceUtil.getExactBuyPrice(event.getSignLine(ChestShopSign.PRICE_LINE)), BigDecimal.valueOf(2));
assertEquals(PriceUtil.getExactSellPrice(ChestShopSign.getPrice(event.getSignLines())), BigDecimal.valueOf(1));
assertEquals(PriceUtil.getExactBuyPrice(ChestShopSign.getPrice(event.getSignLines())), BigDecimal.valueOf(2));
assertFalse(event.isCancelled());
event = new PreShopCreationEvent(null, null, getPriceString("2 B:S 1"));
onPreShopCreation(event);
assertEquals(PriceUtil.getExactSellPrice(event.getSignLine(ChestShopSign.PRICE_LINE)), BigDecimal.valueOf(1));
assertEquals(PriceUtil.getExactBuyPrice(event.getSignLine(ChestShopSign.PRICE_LINE)), BigDecimal.valueOf(2));
assertEquals(PriceUtil.getExactSellPrice(ChestShopSign.getPrice(event.getSignLines())), BigDecimal.valueOf(1));
assertEquals(PriceUtil.getExactBuyPrice(ChestShopSign.getPrice(event.getSignLines())), BigDecimal.valueOf(2));
assertFalse(event.isCancelled());
event = new PreShopCreationEvent(null, null, getPriceString("2 B:1 S"));
onPreShopCreation(event);
assertEquals(PriceUtil.getExactSellPrice(event.getSignLine(ChestShopSign.PRICE_LINE)), BigDecimal.valueOf(1));
assertEquals(PriceUtil.getExactBuyPrice(event.getSignLine(ChestShopSign.PRICE_LINE)), BigDecimal.valueOf(2));
assertEquals(PriceUtil.getExactSellPrice(ChestShopSign.getPrice(event.getSignLines())), BigDecimal.valueOf(1));
assertEquals(PriceUtil.getExactBuyPrice(ChestShopSign.getPrice(event.getSignLines())), BigDecimal.valueOf(2));
assertFalse(event.isCancelled());
}
@ -113,18 +113,18 @@ public class PriceCheckerTest {
public void testRemovingTrailingZeroes() {
PreShopCreationEvent event = new PreShopCreationEvent(null, null, getPriceString("S.7500000000"));
onPreShopCreation(event);
assertEquals(event.getSignLine(ChestShopSign.PRICE_LINE), "S.75");
assertEquals(ChestShopSign.getPrice(event.getSignLines()), "S.75");
event = new PreShopCreationEvent(null, null, getPriceString("S7500000000"));
onPreShopCreation(event);
assertEquals(event.getSignLine(ChestShopSign.PRICE_LINE), "S7500000000");
assertEquals(ChestShopSign.getPrice(event.getSignLines()), "S7500000000");
event = new PreShopCreationEvent(null, null, getPriceString("S.75000:B.75000"));
onPreShopCreation(event);
assertEquals(event.getSignLine(ChestShopSign.PRICE_LINE), "S.75:B.75");
assertEquals(ChestShopSign.getPrice(event.getSignLines()), "S.75:B.75");
event = new PreShopCreationEvent(null, null, getPriceString("S75000:B.75000"));
onPreShopCreation(event);
assertEquals(event.getSignLine(ChestShopSign.PRICE_LINE), "S75000:B.75");
assertEquals(ChestShopSign.getPrice(event.getSignLines()), "S75000:B.75");
}
}