summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/Commandworth.java
blob: 171a974209ace947028e066de37d7daffb526229 (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
package com.earth2me.essentials.commands;

import org.bukkit.Server;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.ItemDb;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import org.bukkit.inventory.ItemStack;


public class Commandworth extends EssentialsCommand
{
	
	public Commandworth()
	{
		super("worth");
	}

	@Override
	public void run(Server server, User user, String commandLabel, String[] args) throws Exception
	{
		ItemStack is = user.getInventory().getItemInHand();
		int amount = is.getAmount();

		if (args.length > 0)
		{
			is = ItemDb.get(args[0]);
		}

		try
		{
			if (args.length > 1)
			{
				amount = Integer.parseInt(args[1]);
			}
		}
		catch (NumberFormatException ex)
		{
			amount = 64;
		}

		is.setAmount(amount);
		double worth = Essentials.getWorth().getPrice(is);
		if (Double.isNaN(worth))
		{
			throw new Exception("That item cannot be sold to the server.");
		}

		user.charge(this);		
		user.sendMessage("§7Stack of " 
				+ is.getType().toString().toLowerCase().replace("_", "") 
				+ " worth §c$" + Util.formatCurrency(worth*amount) + "§7 (" 
				+ amount + " item(s) at $" + Util.formatCurrency(worth) + " each)");
	}
}