summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/commands/Commandsell.java
blob: 36e1893d1b2e10a061ac95a204fdf7e536c16a57 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package net.ess3.commands;

import java.util.Locale;
import java.util.logging.Level;
import static net.ess3.I18n._;
import net.ess3.api.IUser;
import net.ess3.economy.Trade;
import net.ess3.utils.FormatUtil;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;


public class Commandsell extends EssentialsCommand
{
	@Override
	public void run(final IUser user, final String commandLabel, final String[] args) throws Exception
	{
		if (args.length < 1)
		{
			throw new NotEnoughArgumentsException();
		}
		ItemStack is = null;
		if (args[0].equalsIgnoreCase("hand"))
		{
			is = user.getPlayer().getItemInHand();
		}
		else if (args[0].equalsIgnoreCase("inventory") || args[0].equalsIgnoreCase("invent") || args[0].equalsIgnoreCase("all"))
		{
			for (ItemStack stack : user.getPlayer().getInventory().getContents())
			{
				if (stack == null || stack.getType() == Material.AIR)
				{
					continue;
				}
				try
				{
					sellItem(user, stack, args, true);
				}
				catch (Exception e)
				{
				}
			}
			return;
		}
		else if (args[0].equalsIgnoreCase("blocks"))
		{
			for (ItemStack stack : user.getPlayer().getInventory().getContents())
			{
				if (stack == null || stack.getTypeId() > 255 || stack.getType() == Material.AIR)
				{
					continue;
				}
				try
				{
					sellItem(user, stack, args, true);
				}
				catch (Exception e)
				{
				}
			}
			return;
		}
		if (is == null)
		{
			is = ess.getItemDb().get(args[0]);
		}
		sellItem(user, is, args, false);
	}

	private void sellItem(IUser user, ItemStack is, String[] args, boolean isBulkSell) throws Exception
	{
		if (is == null || is.getType() == Material.AIR)
		{
			throw new Exception(_("You really tried to sell Air? Put an item in your hand."));
		}
		int amount = 0;
		if (args.length > 1)
		{
			amount = Integer.parseInt(args[1].replaceAll("[^0-9]", ""));
			if (args[1].startsWith("-"))
			{
				amount = -amount;
			}
		}
		final double worth = ess.getWorth().getPrice(is);
		final boolean stack = args.length > 1 && args[1].endsWith("s");

		if (Double.isNaN(worth))
		{
			throw new Exception(_("§4That item cannot be sold to the server."));
		}


		int max = 0;
		for (ItemStack s : user.getPlayer().getInventory().getContents())
		{
			if (s == null)
			{
				continue;
			}
			if (s.getTypeId() != is.getTypeId())
			{
				continue;
			}
			if (s.getDurability() != is.getDurability())
			{
				continue;
			}
			if (!s.getEnchantments().equals(is.getEnchantments()))
			{
				continue;
			}
			max += s.getAmount();
		}

		if (stack)
		{
			amount *= is.getType().getMaxStackSize();
		}
		if (amount < 1)
		{
			amount += max;
		}

		if (amount > max || amount < 1)
		{
			if (!isBulkSell)
			{
				user.sendMessage(_("§4You do not have enough of that item to sell."));
				user.sendMessage(_("§6If you meant to sell all of your items of that type, use /sell itemname."));
				throw new Exception(_("§6/sell itemname -1 will sell all but one item, etc."));
			}
			else
			{
				return;
			}
		}

		final Player player = user.getPlayer();

		//TODO: Prices for Enchantments
		final ItemStack ris = is.clone();
		ris.setAmount(amount);
		player.getInventory().removeItem(ris);
		player.updateInventory();
		Trade.log("Command", "Sell", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(worth * amount, ess), player.getLocation(), ess);
		user.giveMoney(worth * amount);
		user.sendMessage(
				_(
						"itemSold", FormatUtil.displayCurrency(worth * amount, ess), amount, is.getType().toString().toLowerCase(Locale.ENGLISH),
						FormatUtil.displayCurrency(worth, ess)));
		logger.log(
				Level.INFO, _(
				"itemSoldConsole", player.getDisplayName(), is.getType().toString().toLowerCase(Locale.ENGLISH),
				FormatUtil.displayCurrency(worth * amount, ess), amount, FormatUtil.displayCurrency(worth, ess)));

	}
}