-
Notifications
You must be signed in to change notification settings - Fork 0
/
VaultEconomy.java
415 lines (347 loc) · 11.7 KB
/
VaultEconomy.java
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package com.github.jikoo.planarwrappers.service;
import java.util.Collection;
import java.util.Collections;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;
/** A bridge for Vault-supporting economy plugins. */
public class VaultEconomy extends ManagerProvidedService<Economy> {
public VaultEconomy(@NotNull Plugin plugin) {
super(plugin);
}
@Override
protected boolean isUsable(@NotNull Economy provider) {
return provider.isEnabled();
}
@Override
protected @Nullable Supplier<@NotNull String> logServiceClassNotLoaded() {
return () -> "[VaultEconomyProvider] Vault is not loaded, cannot use economy integration.";
}
@Override
protected @Nullable Supplier<@NotNull String> logNoProviderRegistered(
@NotNull Class<Economy> clazz) {
return () -> "[VaultEconomyProvider] No economy providers are registered.";
}
@Override
protected @Nullable Supplier<String> logServiceProviderChange(
@NotNull Class<Economy> clazz,
@NotNull Economy instance) {
return () -> "[VaultEconomyProvider] Hooked into economy provider " + instance.getName();
}
/**
* Get the name of the active {@link Economy} implementation. If no implementation is present,
* returns {@code "null"}.
*
* @return the name
*/
public @NotNull String getName() {
Wrapper<Economy> service = getService();
if (service == null) {
return "null";
}
return service.unwrap().getName();
}
/**
* Get the number of decimal places kept by the {@link Economy} implementation. Some plugins only
* support certain levels of precision. If no rounding is done or no implementation is present,
* returns {@code -1}.
*
* @return the number of decimal places kept
*/
public int getFractionalDigits() {
Wrapper<Economy> service = getService();
if (service == null) {
return -1;
}
return service.unwrap().fractionalDigits();
}
/**
* Format an amount into a human-readable String. This allows economies to handle formatting and
* currency symbol placement.
*
* @param amount to format
* @return a readable description of the amount
*/
public @NotNull String format(double amount) {
Wrapper<Economy> service = getService();
if (service == null) {
return String.valueOf(amount);
}
return service.unwrap().format(amount);
}
/**
* Get the name of the currency of the {@link Economy} implementation in plural form. If the
* implementation does not support currency names or no implementation is present, the return
* value is empty.
*
* @return the plural name of the currency
*/
public @NotNull String getCurrencyNamePlural() {
Wrapper<Economy> service = getService();
if (service == null) {
return "";
}
return service.unwrap().currencyNamePlural();
}
/**
* Get the name of the currency of the {@link Economy} implementation in singular form. If the
* implementation does not support currency names or no implementation is present, the return
* value is empty.
*
* @return the singular name of the currency
*/
public @NotNull String getCurrencyNameSingular() {
Wrapper<Economy> service = getService();
if (service == null) {
return "";
}
return service.unwrap().currencyNameSingular();
}
/**
* Check if the {@link Economy} implementation has a user account for an {@link OfflinePlayer} in
* the given world. If no implementation is present, returns {@code false}.
*
* @param player the user
* @param world the world name
* @return true if the user has an account
*/
public boolean hasPlayerAccount(@NotNull OfflinePlayer player, @Nullable String world) {
Wrapper<Economy> service = getService();
if (service == null) {
return false;
}
if (world == null) {
return service.unwrap().hasAccount(player);
}
return service.unwrap().hasAccount(player, world);
}
/**
* Create a user account for an {@link OfflinePlayer} in the specified world. If the
* implementation does not support account creation or no implementation is present, returns
* {@code false}.
*
* @param player the user
* @param world the world name
* @return true if the account was created
*/
public boolean createPlayerAccount(@NotNull OfflinePlayer player, @Nullable String world) {
Wrapper<Economy> service = getService();
if (service == null) {
return false;
}
if (world == null) {
return service.unwrap().createPlayerAccount(player);
}
return service.unwrap().createPlayerAccount(player, world);
}
/**
* Get the balance of a user in a world. Balance may be global and not per-world. If no
* implementation is present, returns {@code 0}.
*
* @param player the user
* @param world the world name
* @return the amount in the user's account
*/
public double getPlayerBalance(@NotNull OfflinePlayer player, @Nullable String world) {
Wrapper<Economy> service = getService();
if (service == null) {
return 0;
}
if (world == null) {
return service.unwrap().getBalance(player);
}
return service.unwrap().getBalance(player, world);
}
/**
* Check if a user's account for the world contains an amount. Balance may be global and not
* per-world. If no implementation is present, returns {@code 0}.
*
* @param player the user
* @param world the world name
* @param amount the amount of currency required
* @return true if the user's account contains the amount specified
*/
public boolean hasPlayerBalance(
@NotNull OfflinePlayer player,
@Nullable String world,
double amount) {
Wrapper<Economy> service = getService();
if (service == null) {
return false;
}
if (amount <= 0) {
return true;
}
if (world == null) {
return service.unwrap().has(player, amount);
}
return service.unwrap().has(player, world, amount);
}
/**
* Withdraw or deposit an amount to a user's account. Balance may be global and not per-world.
* If the transaction fails or no implementation is present, returns {@code false}.
*
* @param player the user
* @param world the world name
* @param amount the amount to modify the account by
* @return true if the transaction succeeded
*/
public boolean modifyPlayerBalance(
@NotNull OfflinePlayer player,
@Nullable String world,
double amount) {
Wrapper<Economy> service = getService();
if (service == null) {
return false;
}
if (amount == 0) {
return true;
}
Economy economy = service.unwrap();
Supplier<EconomyResponse> supplier;
if (amount < 0) {
// Negative amount is a withdrawal of the inverse.
if (world == null) {
supplier = () -> economy.withdrawPlayer(player, -amount);
} else {
supplier = () -> economy.withdrawPlayer(player, world, -amount);
}
} else {
if (world == null) {
supplier = () -> economy.depositPlayer(player, amount);
} else {
supplier = () -> economy.depositPlayer(player, world, amount);
}
}
return supplier.get().transactionSuccess();
}
/**
* Get whether the active {@link Economy} implementation supports banks. If no implementation is
* present, returns {@code false}.
*
* @return true if banks are supported
*/
public boolean hasBankSupport() {
Wrapper<Economy> service = getService();
return service != null && service.unwrap().hasBankSupport();
}
/**
* Create a bank account with the specified name and the user as the owner. If no implementation
* is present, returns {@code false}.
*
* @param name the name of the account
* @param player the account owner
* @return true if the bank account was created
*/
public boolean createBank(String name, OfflinePlayer player) {
Wrapper<Economy> service = getService();
return service != null && service.unwrap().createBank(name, player).transactionSuccess();
}
/**
* Delete a bank account with the specified name. If no implementation is present, returns
* {@code false}.
*
* @param name the name of the account
* @return true if the bank account was deleted
*/
public boolean deleteBank(String name) {
Wrapper<Economy> service = getService();
return service != null && service.unwrap().deleteBank(name).transactionSuccess();
}
/**
* Get the balance of a bank account. If no implementation is present, returns {@code 0}.
*
* @param name the name of the account
* @return the amount in the bank account
*/
public double getBankBalance(String name) {
Wrapper<Economy> service = getService();
if (service == null) {
return 0;
}
EconomyResponse response = service.unwrap().bankBalance(name);
return response.transactionSuccess() ? response.balance : 0;
}
/**
* Check if a bank account contains an amount. If no implementation is present, returns
* {@code false}.
*
* @param name the name of the account
* @param amount the amount of currency required
* @return true if the bank account contains the amount specified
*/
public boolean hasBankBalance(String name, double amount) {
Wrapper<Economy> service = getService();
return service != null
&& (amount <= 0 || service.unwrap().bankHas(name, amount).transactionSuccess());
}
/**
* Withdraw or deposit an amount to a bank account. If the transaction fails or no implementation
* is present, returns {@code false}.
*
* @param name the name of the account
* @param amount the amount to modify the account by
* @return true if the transaction succeeded
*/
public boolean modifyBankBalance(String name, double amount) {
Wrapper<Economy> service = getService();
if (service == null) {
return false;
}
if (amount == 0) {
return true;
}
Economy economy = service.unwrap();
BiFunction<String, Double, EconomyResponse> function;
if (amount < 0) {
function = economy::bankWithdraw;
amount = -amount;
} else {
function = economy::bankDeposit;
}
EconomyResponse response = function.apply(name, amount);
return response.transactionSuccess();
}
/**
* Check if a user is the owner of a bank account. If no implementation is present, returns
* {@code false}.
*
* @param name the name of the account
* @param player the potential owner
* @return true if the user is the owner
*/
public boolean isBankOwner(String name, OfflinePlayer player) {
Wrapper<Economy> service = getService();
return service != null && service.unwrap().isBankOwner(name, player).transactionSuccess();
}
/**
* Check if a user is a member of a bank account. If no implementation is present, returns
* {@code false}.
*
* @param name the name of the account
* @param player the potential bank member
* @return true if the user is a bank member
*/
public boolean isBankMember(String name, OfflinePlayer player) {
Wrapper<Economy> service = getService();
return service != null && service.unwrap().isBankMember(name, player).transactionSuccess();
}
/**
* Get all bank names. If no implementation is present, returns an empty collection.
*
* @return a collection of all bank names.
*/
public @NotNull @UnmodifiableView Collection<String> getBanks() {
Wrapper<Economy> service = getService();
if (service == null) {
return Collections.emptyList();
}
return service.unwrap().getBanks();
}
}