2011-06-12 22:33:47 +02:00
|
|
|
package com.earth2me.essentials.signs;
|
|
|
|
|
|
|
|
import com.earth2me.essentials.ChargeException;
|
2011-11-18 18:42:26 +01:00
|
|
|
import com.earth2me.essentials.Trade;
|
2011-06-12 22:33:47 +02:00
|
|
|
import com.earth2me.essentials.User;
|
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;
|
2016-07-23 00:56:26 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
public class SignBuy extends EssentialsSign {
|
|
|
|
public SignBuy() {
|
|
|
|
super("Buy");
|
|
|
|
}
|
2011-06-12 22:33:47 +02:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
@Override
|
|
|
|
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException {
|
|
|
|
validateTrade(sign, 1, 2, player, ess);
|
|
|
|
validateTrade(sign, 3, ess);
|
|
|
|
return true;
|
|
|
|
}
|
2011-06-12 22:33:47 +02:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
@Override
|
|
|
|
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException, MaxMoneyException {
|
2016-07-23 00:56:26 +02:00
|
|
|
Trade items = getTrade(sign, 1, 2, player, ess);
|
|
|
|
Trade charge = getTrade(sign, 3, ess);
|
|
|
|
|
|
|
|
// Check if the player is trying to buy in bulk.
|
2016-07-26 13:25:20 +02:00
|
|
|
if (ess.getSettings().isAllowBulkBuySell() && player.getBase().isSneaking()) {
|
2020-10-03 19:46:05 +02:00
|
|
|
final ItemStack heldItem = player.getItemInHand();
|
2016-07-23 00:56:26 +02:00
|
|
|
if (items.getItemStack().isSimilar(heldItem)) {
|
2020-10-03 19:46:05 +02:00
|
|
|
final int initialItemAmount = items.getItemStack().getAmount();
|
|
|
|
final int newItemAmount = heldItem.getAmount();
|
|
|
|
final ItemStack item = items.getItemStack();
|
2016-07-23 00:56:26 +02:00
|
|
|
item.setAmount(newItemAmount);
|
|
|
|
items = new Trade(item, ess);
|
|
|
|
|
2020-10-03 19:46:05 +02:00
|
|
|
final BigDecimal chargeAmount = charge.getMoney();
|
2020-04-25 14:08:57 +02:00
|
|
|
//noinspection BigDecimalMethodWithoutRoundingCalled
|
2016-07-23 00:56:26 +02:00
|
|
|
BigDecimal pricePerSingleItem = chargeAmount.divide(new BigDecimal(initialItemAmount));
|
|
|
|
pricePerSingleItem = pricePerSingleItem.multiply(new BigDecimal(newItemAmount));
|
|
|
|
charge = new Trade(pricePerSingleItem, ess);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
charge.isAffordableFor(player);
|
|
|
|
if (!items.pay(player)) {
|
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", "Buy", "Interact", username, charge, username, items, sign.getBlock().getLocation(), player.getMoney(), ess);
|
2015-04-15 06:06:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
2011-06-12 22:33:47 +02:00
|
|
|
}
|