summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/economy/Economy.java
blob: c818dbf75d64a316cda5754e26714916622c7639 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package net.ess3.economy;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import net.ess3.api.*;
import net.ess3.permissions.Permissions;
import net.ess3.utils.Util;


public class Economy implements IEconomy
{
	private final IEssentials ess;
	private final MoneyHolder npcs;

	public Economy(IEssentials ess)
	{
		this.ess = ess;
		this.npcs = new MoneyHolder(ess);
	}

	private double getNPCBalance(String name) throws UserDoesNotExistException
	{
		npcs.acquireReadLock();
		try
		{
			Map<String, Double> balances = npcs.getData().getBalances();
			if (balances == null)
			{
				throw new UserDoesNotExistException(name);
			}
			Double balance = npcs.getData().getBalances().get(name.toLowerCase(Locale.ENGLISH));
			if (balance == null)
			{
				throw new UserDoesNotExistException(name);
			}
			return balance;
		}
		finally
		{
			npcs.unlock();
		}
	}

	private void setNPCBalance(String name, double balance, boolean checkExistance) throws UserDoesNotExistException
	{
		npcs.acquireWriteLock();
		try
		{
			Map<String, Double> balances = npcs.getData().getBalances();
			if (balances == null)
			{
				balances = new HashMap<String, Double>();
				npcs.getData().setBalances(balances);
			}
			if (checkExistance && !balances.containsKey(name.toLowerCase(Locale.ENGLISH)))
			{
				throw new UserDoesNotExistException(name);
			}
			balances.put(name.toLowerCase(Locale.ENGLISH), balance);
		}
		finally
		{
			npcs.unlock();
		}
	}

	private double getStartingBalance()
	{
		double startingBalance = 0;
		ISettings settings = ess.getSettings();
		settings.acquireReadLock();
		try
		{
			startingBalance = settings.getData().getEconomy().getStartingBalance();
		}
		finally
		{
			settings.unlock();
		}
		return startingBalance;
	}

	@Override
	public void onReload()
	{
		this.npcs.onReload(false);
	}

	@Override
	public double getMoney(String name) throws UserDoesNotExistException
	{
		IUser user = ess.getUser(name);
		if (user == null)
		{
			return getNPCBalance(name);
		}
		return user.getMoney();
	}

	@Override
	public void setMoney(String name, double balance) throws NoLoanPermittedException, UserDoesNotExistException
	{
		IUser user = ess.getUser(name);
		if (user == null)
		{
			setNPCBalance(name, balance, true);
			return;
		}
		if (balance < 0.0 && !Permissions.ECO_LOAN.isAuthorized(user))
		{
			throw new NoLoanPermittedException();
		}
		user.setMoney(balance);
	}

	@Override
	public void resetBalance(String name) throws NoLoanPermittedException, UserDoesNotExistException
	{
		setMoney(name, getStartingBalance());
	}

	@Override
	public String format(double amount)
	{
		return Util.displayCurrency(amount, ess);
	}

	@Override
	public boolean playerExists(String name)
	{
		try
		{
			getMoney(name);
			return true;
		}
		catch (UserDoesNotExistException ex)
		{
			return false;
		}
	}

	@Override
	public boolean isNPC(String name) throws UserDoesNotExistException
	{
		boolean result = ess.getUser(name) == null;
		if (result)
		{
			getNPCBalance(name);
		}
		return result;
	}

	@Override
	public boolean createNPC(String name)
	{
		try
		{
			if (isNPC(name))
			{

				setNPCBalance(name, getStartingBalance(), false);
				return true;
			}
		}
		catch (UserDoesNotExistException ex)
		{
			try
			{
				setNPCBalance(name, getStartingBalance(), false);
				return true;
			}
			catch (UserDoesNotExistException ex1)
			{
				//This should never happen!
			}
		}
		return false;
	}

	@Override
	public void removeNPC(String name) throws UserDoesNotExistException
	{
		npcs.acquireWriteLock();
		try
		{
			Map<String, Double> balances = npcs.getData().getBalances();
			if (balances == null)
			{
				balances = new HashMap<String, Double>();
				npcs.getData().setBalances(balances);
			}
			if (balances.containsKey(name.toLowerCase(Locale.ENGLISH)))
			{
				balances.remove(name.toLowerCase(Locale.ENGLISH));
			}
			else
			{
				throw new UserDoesNotExistException(name);
			}
		}
		finally
		{
			npcs.unlock();
		}
	}
}