2011-06-12 20:33:47 +00:00
|
|
|
package com.earth2me.essentials.signs;
|
|
|
|
|
|
|
|
import com.earth2me.essentials.ChargeException;
|
2011-11-18 17:42:26 +00:00
|
|
|
import com.earth2me.essentials.Trade;
|
2013-05-06 05:40:22 +00:00
|
|
|
import com.earth2me.essentials.Trade.OverflowType;
|
2011-06-12 20:33:47 +00:00
|
|
|
import com.earth2me.essentials.User;
|
2013-10-11 02:44:41 +00:00
|
|
|
import net.ess3.api.IEssentials;
|
2014-02-02 16:07:32 +00:00
|
|
|
import net.ess3.api.MaxMoneyException;
|
2011-06-12 20:33:47 +00:00
|
|
|
|
2016-07-22 22:56:26 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
2011-06-12 20:33:47 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public class SignSell extends EssentialsSign {
|
|
|
|
public SignSell() {
|
|
|
|
super("Sell");
|
|
|
|
}
|
2011-06-26 13:31:13 +00:00
|
|
|
|
2015-04-15 04:06:16 +00: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-26 13:31:13 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
@Override
|
|
|
|
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException, MaxMoneyException {
|
2016-07-22 22:56:26 +00:00
|
|
|
Trade charge = getTrade(sign, 1, 2, player, ess);
|
|
|
|
Trade money = getTrade(sign, 3, ess);
|
|
|
|
|
|
|
|
// Check if the player is trying to sell in bulk.
|
2016-07-26 11:25:20 +00:00
|
|
|
if (ess.getSettings().isAllowBulkBuySell() && player.getBase().isSneaking()) {
|
2016-07-22 22:56:26 +00:00
|
|
|
ItemStack heldItem = player.getItemInHand();
|
|
|
|
if (charge.getItemStack().isSimilar(heldItem)) {
|
|
|
|
int initialItemAmount = charge.getItemStack().getAmount();
|
|
|
|
int newItemAmount = heldItem.getAmount();
|
|
|
|
ItemStack item = charge.getItemStack();
|
|
|
|
item.setAmount(newItemAmount);
|
|
|
|
charge = new Trade(item, ess);
|
|
|
|
|
|
|
|
BigDecimal chargeAmount = money.getMoney();
|
|
|
|
BigDecimal pricePerSingleItem = chargeAmount.divide(new BigDecimal(initialItemAmount));
|
|
|
|
pricePerSingleItem = pricePerSingleItem.multiply(new BigDecimal(newItemAmount));
|
|
|
|
money = new Trade(pricePerSingleItem, ess);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
charge.isAffordableFor(player);
|
|
|
|
money.pay(player, OverflowType.DROP);
|
|
|
|
charge.charge(player);
|
|
|
|
Trade.log("Sign", "Sell", "Interact", username, charge, username, money, sign.getBlock().getLocation(), ess);
|
|
|
|
return true;
|
|
|
|
}
|
2011-06-12 20:33:47 +00:00
|
|
|
}
|