mirror of
https://github.com/EssentialsX/Essentials.git
synced 2024-11-17 16:15:21 +01:00
Allow bulk buy/sell when sneaking. Resolves #65
This commit is contained in:
parent
3245ce10ac
commit
87adbb477d
@ -19,6 +19,8 @@ import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
|
||||
@ -787,4 +789,16 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
|
||||
this.afkMessage = message;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link ItemStack} in the main hand or off-hand. If the main hand is empty then the offhand item is returned - also nullable.
|
||||
*/
|
||||
public ItemStack getItemInHand() {
|
||||
if (ReflUtil.getNmsVersionObject().isLowerThan(ReflUtil.V1_9_R1)) {
|
||||
return getBase().getInventory().getItemInHand();
|
||||
} else {
|
||||
PlayerInventory inventory = getBase().getInventory();
|
||||
return inventory.getItemInMainHand() != null ? inventory.getItemInMainHand() : inventory.getItemInOffHand();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,10 @@ import com.earth2me.essentials.User;
|
||||
import net.ess3.api.IEssentials;
|
||||
import net.ess3.api.MaxMoneyException;
|
||||
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
|
||||
public class SignBuy extends EssentialsSign {
|
||||
public SignBuy() {
|
||||
@ -21,8 +25,26 @@ public class SignBuy extends EssentialsSign {
|
||||
|
||||
@Override
|
||||
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException, MaxMoneyException {
|
||||
final Trade items = getTrade(sign, 1, 2, player, ess);
|
||||
final Trade charge = getTrade(sign, 3, ess);
|
||||
Trade items = getTrade(sign, 1, 2, player, ess);
|
||||
Trade charge = getTrade(sign, 3, ess);
|
||||
|
||||
// Check if the player is trying to buy in bulk.
|
||||
if (player.getBase().isSneaking()) {
|
||||
ItemStack heldItem = player.getItemInHand();
|
||||
if (items.getItemStack().isSimilar(heldItem)) {
|
||||
int initialItemAmount = items.getItemStack().getAmount();
|
||||
int newItemAmount = heldItem.getAmount();
|
||||
ItemStack item = items.getItemStack();
|
||||
item.setAmount(newItemAmount);
|
||||
items = new Trade(item, ess);
|
||||
|
||||
BigDecimal chargeAmount = charge.getMoney();
|
||||
BigDecimal pricePerSingleItem = chargeAmount.divide(new BigDecimal(initialItemAmount));
|
||||
pricePerSingleItem = pricePerSingleItem.multiply(new BigDecimal(newItemAmount));
|
||||
charge = new Trade(pricePerSingleItem, ess);
|
||||
}
|
||||
}
|
||||
|
||||
charge.isAffordableFor(player);
|
||||
if (!items.pay(player)) {
|
||||
throw new ChargeException("Inventory full"); //TODO: TL
|
||||
|
@ -7,6 +7,10 @@ import com.earth2me.essentials.User;
|
||||
import net.ess3.api.IEssentials;
|
||||
import net.ess3.api.MaxMoneyException;
|
||||
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
|
||||
public class SignSell extends EssentialsSign {
|
||||
public SignSell() {
|
||||
@ -22,8 +26,26 @@ public class SignSell extends EssentialsSign {
|
||||
|
||||
@Override
|
||||
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException, MaxMoneyException {
|
||||
final Trade charge = getTrade(sign, 1, 2, player, ess);
|
||||
final Trade money = getTrade(sign, 3, ess);
|
||||
Trade charge = getTrade(sign, 1, 2, player, ess);
|
||||
Trade money = getTrade(sign, 3, ess);
|
||||
|
||||
// Check if the player is trying to sell in bulk.
|
||||
if (player.getBase().isSneaking()) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
charge.isAffordableFor(player);
|
||||
money.pay(player, OverflowType.DROP);
|
||||
charge.charge(player);
|
||||
|
Loading…
Reference in New Issue
Block a user