Allow bulk buy/sell when sneaking. Resolves #65

This commit is contained in:
Ali Moghnieh 2016-07-22 23:56:26 +01:00
parent 3245ce10ac
commit 87adbb477d
No known key found for this signature in database
GPG Key ID: F09D3A1BAF2E6D70
3 changed files with 62 additions and 4 deletions

View File

@ -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();
}
}
}

View File

@ -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

View File

@ -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);