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

import com.earth2me.essentials.commands.EssentialsCommand;
import org.bukkit.inventory.ItemStack;


public class Charge
{
	private String command = null;
	private Double costs = null;
	private ItemStack items = null;
	private Essentials ess = Essentials.getStatic();

	public Charge(String command)
	{
		this.command = command;
	}

	public Charge(double money)
	{
		this.costs = money;
	}

	public Charge(ItemStack items)
	{
		this.items = items;
	}

	public Charge(EssentialsCommand command)
	{
		this.command = command.getName();
	}

	public void isAffordableFor(User user) throws Exception
	{
		double mon = user.getMoney();
		if (costs != null)
		{
			if (mon < costs && !user.isAuthorized("essentials.eco.loan"))
			{
				throw new Exception(Util.i18n("notEnoughMoney"));
			}
		}
		if (items != null)
		{
			if (!InventoryWorkaround.containsItem(user.getInventory(), true, items))
			{
				throw new Exception(Util.format("missingItems", items.getAmount(), items.getType().toString().toLowerCase().replace("_", " ")));
			}
		}
		if (command != null && !command.isEmpty())
		{
			if (user.isAuthorized("essentials.nocommandcost.all")
				|| user.isAuthorized("essentials.nocommandcost." + command))
			{
				return;
			}
			double cost = ess.getSettings().getCommandCost(command.startsWith("/") ? command.substring(1) : command);
			if (mon < cost && !user.isAuthorized("essentials.eco.loan"))
			{
				throw new Exception(Util.i18n("notEnoughMoney"));
			}
		}
	}

	public void charge(User user) throws Exception
	{
		double mon = user.getMoney();
		if (costs != null)
		{
			if (mon < costs && !user.isAuthorized("essentials.eco.loan"))
			{
				throw new Exception(Util.i18n("notEnoughMoney"));
			}
			user.takeMoney(costs);
		}
		if (items != null)
		{
			if (!InventoryWorkaround.containsItem(user.getInventory(), true, items))
			{
				throw new Exception(Util.format("missingItems", items.getAmount(), items.getType().toString().toLowerCase().replace("_", " ")));
			}
			InventoryWorkaround.removeItem(user.getInventory(), true, items);
			user.updateInventory();
		}
		if (command != null && !command.isEmpty())
		{
			if (user.isAuthorized("essentials.nocommandcost.all")
				|| user.isAuthorized("essentials.nocommandcost." + command))
			{
				return;
			}

			double cost = ess.getSettings().getCommandCost(command.startsWith("/") ? command.substring(1) : command);
			if (mon < cost && !user.isAuthorized("essentials.eco.loan"))
			{
				throw new Exception(Util.i18n("notEnoughMoney"));
			}
			user.takeMoney(cost);
		}
	}
}