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


import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
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 = ess.getItemDb().get(args[0]);
		}

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

		is.setAmount(amount);
		double worth = ess.getWorth().getPrice(is);
		if (Double.isNaN(worth))
		{
			throw new Exception(Util.i18n("itemCannotBeSold"));
		}

		user.sendMessage(is.getDurability() != 0
						 ? Util.format("worthMeta",
									   is.getType().toString().toLowerCase().replace("_", ""),
									   is.getDurability(),
									   Util.formatCurrency(worth * amount, ess),
									   amount,
									   Util.formatCurrency(worth, ess))
						 : Util.format("worth",
									   is.getType().toString().toLowerCase().replace("_", ""),
									   Util.formatCurrency(worth * amount, ess),
									   amount,
									   Util.formatCurrency(worth, ess)));
	}

	@Override
	protected void run(Server server, CommandSender sender, String commandLabel, String[] args) throws Exception
	{
		if (args.length < 1)
		{
			throw new NotEnoughArgumentsException();
		}

		ItemStack is = ess.getItemDb().get(args[0]);
		int amount = is.getAmount();

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

		is.setAmount(amount);
		double worth = ess.getWorth().getPrice(is);
		if (Double.isNaN(worth))
		{
			throw new Exception(Util.i18n("itemCannotBeSold"));
		}

		sender.sendMessage(is.getDurability() != 0
						   ? Util.format("worthMeta",
										 is.getType().toString().toLowerCase().replace("_", ""),
										 is.getDurability(),
										 Util.formatCurrency(worth * amount, ess),
										 amount,
										 Util.formatCurrency(worth, ess))
						   : Util.format("worth",
										 is.getType().toString().toLowerCase().replace("_", ""),
										 Util.formatCurrency(worth * amount, ess),
										 amount,
										 Util.formatCurrency(worth, ess)));

	}
}