Fix various trade sign issues (#4056)

Fixes various bugs with line length validation and fixes issues with overfilled stacks.
This commit is contained in:
Josh Roy 2021-04-14 12:24:24 -04:00 committed by GitHub
parent 195148a2a2
commit 5171215817
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 20 deletions

View File

@ -18,6 +18,8 @@ import static com.earth2me.essentials.I18n.tl;
//TODO: TL exceptions //TODO: TL exceptions
public class SignTrade extends EssentialsSign { public class SignTrade extends EssentialsSign {
private static int MAX_STOCK_LINE_LENGTH = 15;
public SignTrade() { public SignTrade() {
super("Trade"); super("Trade");
} }
@ -82,9 +84,8 @@ public class SignTrade extends EssentialsSign {
private Trade rechargeSign(final ISign sign, final IEssentials ess, final User player) throws SignException, ChargeException { private Trade rechargeSign(final ISign sign, final IEssentials ess, final User player) throws SignException, ChargeException {
final Trade trade = getTrade(sign, 2, AmountType.COST, false, true, ess); final Trade trade = getTrade(sign, 2, AmountType.COST, false, true, ess);
if (trade.getItemStack() != null && player.getBase().getItemInHand() != null && trade.getItemStack().getType() == player.getBase().getItemInHand().getType() && trade.getItemStack().getDurability() == player.getBase().getItemInHand().getDurability() && trade.getItemStack().getEnchantments().equals(player.getBase().getItemInHand().getEnchantments())) { if (trade.getItemStack() != null && player.getBase().getItemInHand() != null && trade.getItemStack().getType() == player.getBase().getItemInHand().getType() && trade.getItemStack().getDurability() == player.getBase().getItemInHand().getDurability() && trade.getItemStack().getEnchantments().equals(player.getBase().getItemInHand().getEnchantments())) {
int amount = player.getBase().getItemInHand().getAmount(); final int amount = trade.getItemStack().getAmount();
amount -= amount % trade.getItemStack().getAmount(); if (player.getBase().getInventory().containsAtLeast(trade.getItemStack(), amount)) {
if (amount > 0) {
final ItemStack stack = player.getBase().getItemInHand().clone(); final ItemStack stack = player.getBase().getItemInHand().clone();
stack.setAmount(amount); stack.setAmount(amount);
final Trade store = new Trade(stack, ess); final Trade store = new Trade(stack, ess);
@ -141,6 +142,12 @@ public class SignTrade extends EssentialsSign {
} }
} }
private void validateSignLength(final String newLine) throws SignException {
if (newLine.length() > MAX_STOCK_LINE_LENGTH) {
throw new SignException("This sign is full!");
}
}
protected final void validateTrade(final ISign sign, final int index, final boolean amountNeeded, final IEssentials ess) throws SignException { protected final void validateTrade(final ISign sign, final int index, final boolean amountNeeded, final IEssentials ess) throws SignException {
final String line = sign.getLine(index).trim(); final String line = sign.getLine(index).trim();
if (line.isEmpty()) { if (line.isEmpty()) {
@ -151,10 +158,9 @@ public class SignTrade extends EssentialsSign {
if (split.length == 1 && !amountNeeded) { if (split.length == 1 && !amountNeeded) {
final BigDecimal money = getMoney(split[0], ess); final BigDecimal money = getMoney(split[0], ess);
if (money != null) { if (money != null) {
if (NumberUtil.shortCurrency(money, ess).length() * 2 > 15) { final String newLine = NumberUtil.shortCurrency(money, ess) + ":0";
throw new SignException("Line can be too long!"); validateSignLength(newLine);
} sign.setLine(index, newLine);
sign.setLine(index, NumberUtil.shortCurrency(money, ess) + ":0");
return; return;
} }
} }
@ -167,7 +173,9 @@ public class SignTrade extends EssentialsSign {
if (amount.compareTo(MINTRANSACTION) < 0 || money.compareTo(MINTRANSACTION) < 0) { if (amount.compareTo(MINTRANSACTION) < 0 || money.compareTo(MINTRANSACTION) < 0) {
throw new SignException(tl("moreThanZero")); throw new SignException(tl("moreThanZero"));
} }
sign.setLine(index, NumberUtil.shortCurrency(money, ess) + ":" + NumberUtil.shortCurrency(amount, ess).substring(1)); final String newLine = NumberUtil.shortCurrency(money, ess) + ":" + NumberUtil.shortCurrency(amount, ess).substring(1);
validateSignLength(newLine);
sign.setLine(index, newLine);
return; return;
} }
} }
@ -182,9 +190,7 @@ public class SignTrade extends EssentialsSign {
throw new SignException(tl("moreThanZero")); throw new SignException(tl("moreThanZero"));
} }
final String newline = amount + " " + split[1] + ":0"; final String newline = amount + " " + split[1] + ":0";
if ((newline + amount).length() > 15) { validateSignLength(newline);
throw new SignException("Line can be too long!");
}
sign.setLine(index, newline); sign.setLine(index, newline);
return; return;
} }
@ -321,9 +327,7 @@ public class SignTrade extends EssentialsSign {
final BigDecimal amount = getBigDecimal(split[1], ess); final BigDecimal amount = getBigDecimal(split[1], ess);
if (money != null && amount != null) { if (money != null && amount != null) {
final String newline = NumberUtil.shortCurrency(money, ess) + ":" + NumberUtil.shortCurrency(value, ess).substring(1); final String newline = NumberUtil.shortCurrency(money, ess) + ":" + NumberUtil.shortCurrency(value, ess).substring(1);
if (newline.length() > 15) { validateSignLength(newline);
throw new SignException("This sign is full: Line too long!");
}
sign.setLine(index, newline); sign.setLine(index, newline);
return; return;
} }
@ -333,18 +337,14 @@ public class SignTrade extends EssentialsSign {
if (split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp")) { if (split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp")) {
final int stackamount = getIntegerPositive(split[0]); final int stackamount = getIntegerPositive(split[0]);
final String newline = stackamount + " " + split[1] + ":" + value.intValueExact(); final String newline = stackamount + " " + split[1] + ":" + value.intValueExact();
if (newline.length() > 15) { validateSignLength(newline);
throw new SignException("This sign is full: Line too long!");
}
sign.setLine(index, newline); sign.setLine(index, newline);
return; return;
} else { } else {
final int stackamount = getIntegerPositive(split[0]); final int stackamount = getIntegerPositive(split[0]);
getItemStack(split[1], stackamount, ess); getItemStack(split[1], stackamount, ess);
final String newline = stackamount + " " + split[1] + ":" + value.intValueExact(); final String newline = stackamount + " " + split[1] + ":" + value.intValueExact();
if (newline.length() > 15) { validateSignLength(newline);
throw new SignException("This sign is full: Line too long!");
}
sign.setLine(index, newline); sign.setLine(index, newline);
return; return;
} }