summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/signs/SignBuy.java
blob: f0200cace8a2a5cdc41e12030a59934858a96ce4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.earth2me.essentials.signs;

import com.earth2me.essentials.Charge;
import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.InventoryWorkaround;
import com.earth2me.essentials.User;
import java.util.Map;
import org.bukkit.inventory.ItemStack;


public class SignBuy extends EssentialsSign
{
	public SignBuy()
	{
		super("Buy");
	}

	@Override
	protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
	{
		validateInteger(sign, 1);
		validateItem(sign, 2, true);
		validateCharge(sign, 3);
		return true;
	}

	@Override
	protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
	{
		final ItemStack item = getItemStack(sign.getLine(2));
		final int amount = Math.min(getInteger(sign.getLine(1)), item.getType().getMaxStackSize()*player.getInventory().getSize());
		item.setAmount(amount);
		final Charge charge = getCharge(sign, 3, ess);
		charge.isAffordableFor(player);
		final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItem(player.getInventory(), true, item);
		for (ItemStack itemStack : leftOver.values())
		{
			InventoryWorkaround.dropItem(player.getLocation(), itemStack);
		}
		player.updateInventory();
		charge.charge(player);
		return true;
	}
}