2011-06-13 15:05:31 +02:00
|
|
|
package com.earth2me.essentials.signs;
|
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
import com.earth2me.essentials.ChargeException;
|
|
|
|
import com.earth2me.essentials.Trade;
|
2013-05-06 07:40:22 +02:00
|
|
|
import com.earth2me.essentials.Trade.OverflowType;
|
2013-10-11 04:44:41 +02:00
|
|
|
import com.earth2me.essentials.Trade.TradeType;
|
2015-04-15 06:06:16 +02:00
|
|
|
import com.earth2me.essentials.User;
|
2022-12-24 18:58:51 +01:00
|
|
|
import com.earth2me.essentials.craftbukkit.Inventories;
|
2022-02-13 21:33:51 +01:00
|
|
|
import com.earth2me.essentials.utils.MaterialUtil;
|
2013-06-08 23:31:19 +02:00
|
|
|
import com.earth2me.essentials.utils.NumberUtil;
|
2013-10-11 04:44:41 +02:00
|
|
|
import net.ess3.api.IEssentials;
|
2014-02-02 17:07:32 +01:00
|
|
|
import net.ess3.api.MaxMoneyException;
|
2013-09-17 13:29:03 +02:00
|
|
|
import org.bukkit.Material;
|
2011-06-13 15:15:19 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2011-06-13 15:05:31 +02:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2013-03-07 03:42:19 +01:00
|
|
|
//TODO: TL exceptions
|
2015-04-15 06:06:16 +02:00
|
|
|
public class SignTrade extends EssentialsSign {
|
2022-02-13 21:54:19 +01:00
|
|
|
private static final int MAX_STOCK_LINE_LENGTH = 15;
|
2021-04-14 18:24:24 +02:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
public SignTrade() {
|
|
|
|
super("Trade");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException {
|
|
|
|
validateTrade(sign, 1, false, ess);
|
|
|
|
validateTrade(sign, 2, true, ess);
|
2019-01-07 23:38:13 +01:00
|
|
|
final Trade trade = getTrade(sign, 2, AmountType.ROUNDED, true, true, ess);
|
|
|
|
final Trade charge = getTrade(sign, 1, AmountType.ROUNDED, false, true, ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
if (trade.getType() == charge.getType() && (trade.getType() != TradeType.ITEM || trade.getItemStack().isSimilar(charge.getItemStack()))) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("tradeSignSameType");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
trade.isAffordableFor(player);
|
2022-02-13 21:54:19 +01:00
|
|
|
setOwner(ess, player, sign, 3, "§8");
|
2015-04-15 06:06:16 +02:00
|
|
|
trade.charge(player);
|
2021-01-02 06:12:53 +01:00
|
|
|
Trade.log("Sign", "Trade", "Create", username, trade, username, null, sign.getBlock().getLocation(), player.getMoney(), ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException, MaxMoneyException {
|
2022-02-13 21:54:19 +01:00
|
|
|
if (isOwner(ess, player, sign, 3, "§8")) {
|
2015-04-15 06:06:16 +02:00
|
|
|
final Trade store = rechargeSign(sign, ess, player);
|
2020-10-03 19:46:05 +02:00
|
|
|
final Trade stored;
|
2015-04-15 06:06:16 +02:00
|
|
|
try {
|
2019-01-07 23:38:13 +01:00
|
|
|
stored = getTrade(sign, 1, AmountType.TOTAL, true, true, ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
subtractAmount(sign, 1, stored, ess);
|
|
|
|
|
2020-10-03 19:46:05 +02:00
|
|
|
final Map<Integer, ItemStack> withdraw = stored.pay(player, OverflowType.RETURN);
|
2015-04-15 06:06:16 +02:00
|
|
|
|
|
|
|
if (withdraw == null) {
|
2021-01-02 06:12:53 +01:00
|
|
|
Trade.log("Sign", "Trade", "Withdraw", username, store, username, null, sign.getBlock().getLocation(), player.getMoney(), ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
} else {
|
|
|
|
setAmount(sign, 1, BigDecimal.valueOf(withdraw.get(0).getAmount()), ess);
|
2021-01-02 06:12:53 +01:00
|
|
|
Trade.log("Sign", "Trade", "Withdraw", username, stored, username, new Trade(withdraw.get(0), ess), sign.getBlock().getLocation(), player.getMoney(), ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2020-10-03 19:46:05 +02:00
|
|
|
} catch (final SignException e) {
|
2015-04-15 06:06:16 +02:00
|
|
|
if (store == null) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException(e, "tradeSignEmptyOwner");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-02 06:12:53 +01:00
|
|
|
Trade.log("Sign", "Trade", "Deposit", username, store, username, null, sign.getBlock().getLocation(), player.getMoney(), ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
} else {
|
2019-01-07 23:38:13 +01:00
|
|
|
final Trade charge = getTrade(sign, 1, AmountType.COST, false, true, ess);
|
|
|
|
final Trade trade = getTrade(sign, 2, AmountType.COST, true, true, ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
charge.isAffordableFor(player);
|
|
|
|
addAmount(sign, 1, charge, ess);
|
|
|
|
subtractAmount(sign, 2, trade, ess);
|
|
|
|
if (!trade.pay(player)) {
|
|
|
|
subtractAmount(sign, 1, charge, ess);
|
|
|
|
addAmount(sign, 2, trade, ess);
|
2024-04-13 23:50:45 +02:00
|
|
|
throw new ChargeException("inventoryFull");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
charge.charge(player);
|
2021-01-02 06:12:53 +01:00
|
|
|
Trade.log("Sign", "Trade", "Interact", sign.getLine(3).substring(2), charge, username, trade, sign.getBlock().getLocation(), player.getMoney(), ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
sign.updateSign();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Trade rechargeSign(final ISign sign, final IEssentials ess, final User player) throws SignException, ChargeException {
|
2019-01-07 23:38:13 +01:00
|
|
|
final Trade trade = getTrade(sign, 2, AmountType.COST, false, true, ess);
|
2022-12-24 18:58:51 +01:00
|
|
|
ItemStack stack = Inventories.getItemInHand(player.getBase());
|
|
|
|
if (trade.getItemStack() != null && stack != null && !MaterialUtil.isAir(stack.getType()) && trade.getItemStack().getType() == stack.getType() && MaterialUtil.getDamage(trade.getItemStack()) == MaterialUtil.getDamage(stack) && trade.getItemStack().getEnchantments().equals(stack.getEnchantments())) {
|
2023-04-02 23:45:46 +02:00
|
|
|
if (MaterialUtil.isPotion(trade.getItemStack().getType()) && !trade.getItemStack().isSimilar(stack)) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-04-14 18:24:24 +02:00
|
|
|
final int amount = trade.getItemStack().getAmount();
|
2022-12-24 18:58:51 +01:00
|
|
|
if (Inventories.containsAtLeast(player.getBase(), trade.getItemStack(), amount)) {
|
|
|
|
stack = stack.clone();
|
2015-04-15 06:06:16 +02:00
|
|
|
stack.setAmount(amount);
|
|
|
|
final Trade store = new Trade(stack, ess);
|
|
|
|
addAmount(sign, 2, store, ess);
|
|
|
|
store.charge(player);
|
|
|
|
return store;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean onSignBreak(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, MaxMoneyException {
|
|
|
|
final String signOwner = sign.getLine(3);
|
|
|
|
|
2022-02-13 21:54:19 +01:00
|
|
|
final boolean isOwner = isOwner(ess, player, sign, 3, "§8");
|
2020-04-25 14:08:57 +02:00
|
|
|
final boolean canBreak = isOwner || player.isAuthorized("essentials.signs.trade.override");
|
|
|
|
final boolean canCollect = isOwner || player.isAuthorized("essentials.signs.trade.override.collect");
|
2015-04-15 06:06:16 +02:00
|
|
|
|
|
|
|
if (canBreak) {
|
|
|
|
try {
|
2019-01-07 23:38:13 +01:00
|
|
|
final Trade stored1 = getTrade(sign, 1, AmountType.TOTAL, false, true, ess);
|
|
|
|
final Trade stored2 = getTrade(sign, 2, AmountType.TOTAL, false, true, ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
|
|
|
|
if (!canCollect) {
|
2021-01-02 06:12:53 +01:00
|
|
|
Trade.log("Sign", "Trade", "Destroy", signOwner.substring(2), stored2, username, stored1, sign.getBlock().getLocation(), player.getMoney(), ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:50:43 +01:00
|
|
|
final Map<Integer, ItemStack> withdraw1 = stored1.pay(player, OverflowType.DROP);
|
|
|
|
final Map<Integer, ItemStack> withdraw2 = stored2.pay(player, OverflowType.DROP);
|
2015-04-15 06:06:16 +02:00
|
|
|
|
|
|
|
if (withdraw1 == null && withdraw2 == null) {
|
2021-01-02 06:12:53 +01:00
|
|
|
Trade.log("Sign", "Trade", "Break", signOwner.substring(2), stored2, username, stored1, sign.getBlock().getLocation(), player.getMoney(), ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
setAmount(sign, 1, BigDecimal.valueOf(withdraw1 == null ? 0L : withdraw1.get(0).getAmount()), ess);
|
2021-01-02 06:12:53 +01:00
|
|
|
Trade.log("Sign", "Trade", "Withdraw", signOwner.substring(2), stored1, username, withdraw1 == null ? null : new Trade(withdraw1.get(0), ess), sign.getBlock().getLocation(), player.getMoney(), ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
|
|
|
|
setAmount(sign, 2, BigDecimal.valueOf(withdraw2 == null ? 0L : withdraw2.get(0).getAmount()), ess);
|
2021-01-02 06:12:53 +01:00
|
|
|
Trade.log("Sign", "Trade", "Withdraw", signOwner.substring(2), stored2, username, withdraw2 == null ? null : new Trade(withdraw2.get(0), ess), sign.getBlock().getLocation(), player.getMoney(), ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
|
|
|
|
sign.updateSign();
|
2020-10-03 19:46:05 +02:00
|
|
|
} catch (final SignException e) {
|
2015-04-15 06:06:16 +02:00
|
|
|
if (player.isAuthorized("essentials.signs.trade.override")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:24:24 +02:00
|
|
|
private void validateSignLength(final String newLine) throws SignException {
|
|
|
|
if (newLine.length() > MAX_STOCK_LINE_LENGTH) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("tradeSignFull");
|
2021-04-14 18:24:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
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();
|
|
|
|
if (line.isEmpty()) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("emptySignLine", index + 1);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
final String[] split = line.split("[ :]+");
|
|
|
|
|
|
|
|
if (split.length == 1 && !amountNeeded) {
|
2020-05-11 16:20:00 +02:00
|
|
|
final BigDecimal money = getMoney(split[0], ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
if (money != null) {
|
2021-04-14 18:24:24 +02:00
|
|
|
final String newLine = NumberUtil.shortCurrency(money, ess) + ":0";
|
|
|
|
validateSignLength(newLine);
|
|
|
|
sign.setLine(index, newLine);
|
2015-04-15 06:06:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (split.length == 2 && amountNeeded) {
|
2020-05-11 16:20:00 +02:00
|
|
|
final BigDecimal money = getMoney(split[0], ess);
|
2021-03-13 17:36:18 +01:00
|
|
|
BigDecimal amount = getBigDecimalPositive(split[1], ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
if (money != null && amount != null) {
|
|
|
|
amount = amount.subtract(amount.remainder(money));
|
|
|
|
if (amount.compareTo(MINTRANSACTION) < 0 || money.compareTo(MINTRANSACTION) < 0) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("moreThanZero");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2023-07-25 23:16:50 +02:00
|
|
|
final String newLine = NumberUtil.shortCurrency(money, ess) + ":" + NumberUtil.formatAsCurrency(amount);
|
2021-04-14 18:24:24 +02:00
|
|
|
validateSignLength(newLine);
|
|
|
|
sign.setLine(index, newLine);
|
2015-04-15 06:06:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (split.length == 2 && !amountNeeded) {
|
|
|
|
final int amount = getIntegerPositive(split[0]);
|
|
|
|
|
|
|
|
if (amount < 1) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("moreThanZero");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
if (!(split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp")) && getItemStack(split[1], amount, ess).getType() == Material.AIR) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("moreThanZero");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2020-10-03 19:46:05 +02:00
|
|
|
final String newline = amount + " " + split[1] + ":0";
|
2021-04-14 18:24:24 +02:00
|
|
|
validateSignLength(newline);
|
2015-04-15 06:06:16 +02:00
|
|
|
sign.setLine(index, newline);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (split.length == 3 && amountNeeded) {
|
|
|
|
final int stackamount = getIntegerPositive(split[0]);
|
|
|
|
int amount = getIntegerPositive(split[2]);
|
|
|
|
amount -= amount % stackamount;
|
|
|
|
if (amount < 1 || stackamount < 1) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("moreThanZero");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
if (!(split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp")) && getItemStack(split[1], stackamount, ess).getType() == Material.AIR) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("moreThanZero");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2022-02-13 21:50:43 +01:00
|
|
|
final String newline = stackamount + " " + split[1] + ":" + amount;
|
|
|
|
validateSignLength(newline);
|
|
|
|
sign.setLine(index, newline);
|
2015-04-15 06:06:16 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("invalidSignLine", index + 1);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected final Trade getTrade(final ISign sign, final int index, final AmountType amountType, final boolean notEmpty, final IEssentials ess) throws SignException {
|
2019-01-07 23:38:13 +01:00
|
|
|
return getTrade(sign, index, amountType, notEmpty, false, ess);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected final Trade getTrade(final ISign sign, final int index, final AmountType amountType, final boolean notEmpty, final boolean allowId, final IEssentials ess) throws SignException {
|
2015-04-15 06:06:16 +02:00
|
|
|
final String line = sign.getLine(index).trim();
|
|
|
|
if (line.isEmpty()) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("emptySignLine", index + 1);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
final String[] split = line.split("[ :]+");
|
|
|
|
|
|
|
|
if (split.length == 2) {
|
|
|
|
try {
|
2020-05-11 16:20:00 +02:00
|
|
|
final BigDecimal money = getMoney(split[0], ess);
|
2021-03-13 17:36:18 +01:00
|
|
|
final BigDecimal amount = notEmpty ? getBigDecimalPositive(split[1], ess) : getBigDecimal(split[1], ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
if (money != null && amount != null) {
|
|
|
|
return new Trade(amountType == AmountType.COST ? money : amount, ess);
|
|
|
|
}
|
2020-10-03 19:46:05 +02:00
|
|
|
} catch (final SignException e) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException(e, "tradeSignEmpty");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (split.length == 3) {
|
2022-02-13 21:54:19 +01:00
|
|
|
final int stackAmount = getIntegerPositive(split[0]);
|
2015-04-15 06:06:16 +02:00
|
|
|
if (split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp")) {
|
|
|
|
int amount = getInteger(split[2]);
|
|
|
|
if (amountType == AmountType.ROUNDED) {
|
2022-02-13 21:54:19 +01:00
|
|
|
amount -= amount % stackAmount;
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2022-02-13 21:54:19 +01:00
|
|
|
if (notEmpty && (amount < 1 || stackAmount < 1)) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("tradeSignEmpty");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2022-02-13 21:54:19 +01:00
|
|
|
return new Trade(amountType == AmountType.COST ? stackAmount : amount, ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
} else {
|
2022-02-13 21:54:19 +01:00
|
|
|
final ItemStack item = getItemStack(split[1], stackAmount, allowId, ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
int amount = getInteger(split[2]);
|
|
|
|
if (amountType == AmountType.ROUNDED) {
|
2022-02-13 21:54:19 +01:00
|
|
|
amount -= amount % stackAmount;
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2022-02-13 21:54:19 +01:00
|
|
|
if (notEmpty && (amount < 1 || stackAmount < 1 || item.getType() == Material.AIR || amount < stackAmount)) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("tradeSignEmpty");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2022-02-13 21:54:19 +01:00
|
|
|
item.setAmount(amountType == AmountType.COST ? stackAmount : amount);
|
2015-04-15 06:06:16 +02:00
|
|
|
return new Trade(item, ess);
|
|
|
|
}
|
|
|
|
}
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("invalidSignLine", index + 1);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected final void subtractAmount(final ISign sign, final int index, final Trade trade, final IEssentials ess) throws SignException {
|
|
|
|
final BigDecimal money = trade.getMoney();
|
|
|
|
if (money != null) {
|
|
|
|
changeAmount(sign, index, money.negate(), ess);
|
|
|
|
}
|
|
|
|
final ItemStack item = trade.getItemStack();
|
|
|
|
if (item != null) {
|
|
|
|
changeAmount(sign, index, BigDecimal.valueOf(-item.getAmount()), ess);
|
|
|
|
}
|
|
|
|
final Integer exp = trade.getExperience();
|
|
|
|
if (exp != null) {
|
2020-04-25 14:08:57 +02:00
|
|
|
changeAmount(sign, index, BigDecimal.valueOf(-exp), ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected final void addAmount(final ISign sign, final int index, final Trade trade, final IEssentials ess) throws SignException {
|
|
|
|
final BigDecimal money = trade.getMoney();
|
|
|
|
if (money != null) {
|
|
|
|
changeAmount(sign, index, money, ess);
|
|
|
|
}
|
|
|
|
final ItemStack item = trade.getItemStack();
|
|
|
|
if (item != null) {
|
|
|
|
changeAmount(sign, index, BigDecimal.valueOf(item.getAmount()), ess);
|
|
|
|
}
|
|
|
|
final Integer exp = trade.getExperience();
|
|
|
|
if (exp != null) {
|
2020-04-25 14:08:57 +02:00
|
|
|
changeAmount(sign, index, BigDecimal.valueOf(exp), ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: Translate these exceptions.
|
|
|
|
private void changeAmount(final ISign sign, final int index, final BigDecimal value, final IEssentials ess) throws SignException {
|
|
|
|
final String line = sign.getLine(index).trim();
|
|
|
|
if (line.isEmpty()) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("emptySignLine", index + 1);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
final String[] split = line.split("[ :]+");
|
|
|
|
|
|
|
|
if (split.length == 2) {
|
2021-03-13 17:36:18 +01:00
|
|
|
final BigDecimal amount = getBigDecimal(split[1], ess).add(value);
|
2015-04-15 06:06:16 +02:00
|
|
|
setAmount(sign, index, amount, ess);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (split.length == 3) {
|
2021-03-13 17:36:18 +01:00
|
|
|
final BigDecimal amount = getBigDecimal(split[2], ess).add(value);
|
2015-04-15 06:06:16 +02:00
|
|
|
setAmount(sign, index, amount, ess);
|
|
|
|
return;
|
|
|
|
}
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("invalidSignLine", index + 1);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: Translate these exceptions.
|
|
|
|
private void setAmount(final ISign sign, final int index, final BigDecimal value, final IEssentials ess) throws SignException {
|
|
|
|
|
|
|
|
final String line = sign.getLine(index).trim();
|
|
|
|
if (line.isEmpty()) {
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("emptySignLine", index + 1);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
final String[] split = line.split("[ :]+");
|
|
|
|
|
|
|
|
if (split.length == 2) {
|
2020-05-11 16:20:00 +02:00
|
|
|
final BigDecimal money = getMoney(split[0], ess);
|
2021-03-13 17:36:18 +01:00
|
|
|
final BigDecimal amount = getBigDecimal(split[1], ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
if (money != null && amount != null) {
|
2023-07-25 23:16:50 +02:00
|
|
|
final String newline = NumberUtil.shortCurrency(money, ess) + ":" + NumberUtil.formatAsCurrency(value);
|
2021-04-14 18:24:24 +02:00
|
|
|
validateSignLength(newline);
|
2015-04-15 06:06:16 +02:00
|
|
|
sign.setLine(index, newline);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (split.length == 3) {
|
2022-02-13 21:54:19 +01:00
|
|
|
final int stackAmount = getIntegerPositive(split[0]);
|
2015-04-15 06:06:16 +02:00
|
|
|
if (split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp")) {
|
2022-02-13 21:54:19 +01:00
|
|
|
final String newline = stackAmount + " " + split[1] + ":" + value.intValueExact();
|
2021-04-14 18:24:24 +02:00
|
|
|
validateSignLength(newline);
|
2015-04-15 06:06:16 +02:00
|
|
|
sign.setLine(index, newline);
|
|
|
|
} else {
|
2022-02-13 21:54:19 +01:00
|
|
|
getItemStack(split[1], stackAmount, ess);
|
|
|
|
final String newline = stackAmount + " " + split[1] + ":" + value.intValueExact();
|
2021-04-14 18:24:24 +02:00
|
|
|
validateSignLength(newline);
|
2015-04-15 06:06:16 +02:00
|
|
|
sign.setLine(index, newline);
|
|
|
|
}
|
2022-02-13 21:54:19 +01:00
|
|
|
return;
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2024-02-03 21:38:14 +01:00
|
|
|
throw new SignException("invalidSignLine", index + 1);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public enum AmountType {
|
|
|
|
TOTAL,
|
|
|
|
ROUNDED,
|
|
|
|
COST
|
|
|
|
}
|
2011-06-13 15:05:31 +02:00
|
|
|
}
|