/
EconomyScriptContainer.java
393 lines (341 loc) · 15.1 KB
/
EconomyScriptContainer.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
package com.denizenscript.denizen.scripts.containers.core;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ScriptTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.scripts.queues.core.InstantQueue;
import com.denizenscript.denizencore.tags.TagManager;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.DefinitionProvider;
import com.denizenscript.denizencore.utilities.SimpleDefinitionProvider;
import com.denizenscript.denizencore.utilities.YamlConfiguration;
import net.milkbowl.vault.economy.AbstractEconomy;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.ServicePriority;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class EconomyScriptContainer extends ScriptContainer {
// <--[language]
// @name Economy Script Containers
// @group Script Container System
// @plugin Vault
// @description
// Economy script containers
//
// Economy script containers provide a Vault economy, which can be used in scripts by
// <@link tag PlayerTag.money> and <@link command money>
// and as well by any other plugin that relies on economy functionality (such as shop plugins).
//
// Note that vault economy bank systems are not currently supported.
// Per-world economies are also not currently supported.
//
// Note that in most cases, you do not want to have multiple economy providers, as only one will actually be in use.
//
// ALL SCRIPT KEYS ARE REQUIRED.
//
// <code>
// # The script name will be shown to the economy provider as the name of the economy system.
// Economy_Script_Name:
//
// type: economy
//
// # The Bukkit service priority. Priorities are Lowest, Low, Normal, High, Highest.
// priority: normal
// # The name of the currency in the singular (such as "dollar" or "euro").
// name single: scripto
// # The name of the currency in the plural (such as "dollars" or "euros").
// name plural: scriptos
// # How many digits after the decimal to include. For example, '2' means '1.05' is a valid amount, but '1.005' will be rounded.
// digits: 2
// # Format the standard output for the money in human-readable format. Use "<[amount]>" for the actual amount to display.
// # Fully supports tags.
// format: $<[amount]>
// # A tag that returns the balance of a linked player. Use a 'proc[]' tag if you need more complex logic.
// # Must return a decimal number.
// balance: <player.flag[money]>
// # A tag that returns a boolean indicating whether the linked player has the amount specified by def "<[amount]>".
// # Use a 'proc[]' tag if you need more complex logic.
// # Must return 'true' or 'false'.
// has: <player.flag[money].is[or_more].than[<[amount]>]>
// # A script that removes the amount of money needed from a player.
// # Note that it's generally up to the systems calling this script to verify that the amount can be safely withdrawn, not this script itself.
// # However you may wish to verify that the player has the amount required within this script.
// # The script may determine a failure message if the withdraw was refused. Determine nothing for no error.
// # Use def 'amount' for the amount to withdraw.
// withdraw:
// - flag <player> money:-:<[amount]>
// # A script that adds the amount of money needed to a player.
// # The script may determine a failure message if the deposit was refused. Determine nothing for no error.
// # Use def 'amount' for the amount to deposit.
// deposit:
// - flag <player> money:+:<[amount]>
//
// </code>
//
// -->
public static class DenizenEconomyProvider extends AbstractEconomy {
public EconomyScriptContainer backingScript;
public String autoTagAmount(String value, OfflinePlayer player, double amount) {
int digits = fractionalDigits();
String amountText;
if (digits <= 0) {
amountText = String.valueOf((long) amount);
}
else {
DecimalFormat d = new DecimalFormat("0." + new String(new char[digits]).replace('\0', '0'), CoreUtilities.decimalFormatSymbols);
amountText = d.format(amount);
}
DefinitionProvider defProvider = new SimpleDefinitionProvider();
defProvider.addDefinition("amount", new ElementTag(amountText));
value = value.replace("<amount", "<element[" + amountText + "]"); // TODO: Deprecated amount hack-tag
return autoTag(value, player, defProvider);
}
public void validateThread() {
if (!Bukkit.isPrimaryThread()) {
try {
throw new RuntimeException("Stack reference");
}
catch (RuntimeException ex) {
Debug.echoError("Warning: economy access from wrong thread, errors will result");
Debug.echoError(ex);
}
}
}
public String autoTag(String value, OfflinePlayer player, DefinitionProvider defProvider) {
if (value == null) {
return null;
}
validateThread();
BukkitTagContext context = new BukkitTagContext(player == null ? null : new PlayerTag(player), null, new ScriptTag(backingScript));
context.definitionProvider = defProvider;
return TagManager.tag(value, context);
}
public String runSubScript(String pathName, OfflinePlayer player, double amount) {
validateThread();
List<ScriptEntry> entries = backingScript.getEntries(new BukkitScriptEntryData(new PlayerTag(player), null), pathName);
InstantQueue queue = new InstantQueue(backingScript.getName());
queue.addEntries(entries);
queue.addDefinition("amount", new ElementTag(amount));
queue.start();
if (queue.determinations != null && queue.determinations.size() > 0) {
return queue.determinations.get(0);
}
return null;
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public String getName() {
return backingScript.getName();
}
@Override
public boolean hasBankSupport() {
return false;
}
@Override
public int fractionalDigits() {
return Integer.parseInt(backingScript.getString("digits", "2"));
}
@Override
public String format(double amount) {
return autoTagAmount(backingScript.getString("format"), null, amount);
}
@Override
public String currencyNamePlural() {
return backingScript.getString("name plural", "moneys");
}
@Override
public String currencyNameSingular() {
return backingScript.getString("name single", "money");
}
@Override
public double getBalance(OfflinePlayer player) {
if (player == null) {
Debug.echoError("Economy attempted BALANCE-CHECK to NULL player.");
return 0;
}
try {
return Double.parseDouble(autoTag(backingScript.getString("balance"), player, null));
}
catch (NumberFormatException ex) {
Debug.echoError("Economy script '" + getName() + "' returned invalid balance for player '" + new PlayerTag(player).debuggable() + "': " + ex.getMessage());
return 0;
}
}
@Override
public EconomyResponse withdrawPlayer(OfflinePlayer player, double amount) {
if (player == null) {
Debug.echoError("Economy attempted WITHDRAW to NULL player for " + amount);
return null;
}
String determination = runSubScript("withdraw", player, amount);
return new EconomyResponse(amount, getBalance(player), determination == null ?
EconomyResponse.ResponseType.SUCCESS : EconomyResponse.ResponseType.FAILURE, determination);
}
@Override
public EconomyResponse depositPlayer(OfflinePlayer player, double amount) {
if (player == null) {
Debug.echoError("Economy attempted DEPOSIT to NULL player for " + amount);
return null;
}
String determination = runSubScript("deposit", player, amount);
return new EconomyResponse(amount, getBalance(player), determination == null ?
EconomyResponse.ResponseType.SUCCESS : EconomyResponse.ResponseType.FAILURE, determination);
}
@Override
public boolean has(OfflinePlayer player, double amount) {
if (player == null) {
Debug.echoError("Economy attempted HAS-CHECK to NULL player for " + amount);
return false;
}
return autoTagAmount(backingScript.getString("has"), player, amount).equalsIgnoreCase("true");
}
@Override
public boolean hasAccount(String playerName) {
return true;
}
@Override
public boolean hasAccount(String playerName, String worldName) {
return true;
}
public OfflinePlayer playerForName(String name) {
UUID id = PlayerTag.getAllPlayers().get(CoreUtilities.toLowerCase(name));
if (id == null) {
Debug.echoError("Economy attempted access to unknown player '" + name + "'");
return null;
}
return Bukkit.getOfflinePlayer(id);
}
@Override
public double getBalance(String playerName) {
return getBalance(playerForName(playerName));
}
@Override
public double getBalance(String playerName, String worldName) {
return getBalance(playerName);
}
@Override
public double getBalance(OfflinePlayer player, String worldName) {
return getBalance(player);
}
@Override
public boolean has(String playerName, double amount) {
return has(playerForName(playerName), amount);
}
@Override
public boolean has(String playerName, String worldName, double amount) {
return has(playerName, amount);
}
@Override
public boolean has(OfflinePlayer player, String worldName, double amount) {
return has(player, amount);
}
@Override
public EconomyResponse withdrawPlayer(String playerName, double amount) {
return withdrawPlayer(playerForName(playerName), amount);
}
@Override
public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
return withdrawPlayer(playerName, amount);
}
@Override
public EconomyResponse withdrawPlayer(OfflinePlayer player, String worldName, double amount) {
return withdrawPlayer(player, amount);
}
@Override
public EconomyResponse depositPlayer(String playerName, double amount) {
return depositPlayer(playerForName(playerName), amount);
}
@Override
public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
return depositPlayer(playerName, amount);
}
@Override
public EconomyResponse depositPlayer(OfflinePlayer player, String worldName, double amount) {
return depositPlayer(player, amount);
}
@Override
public EconomyResponse createBank(String s, String s1) {
return null;
}
@Override
public EconomyResponse deleteBank(String s) {
return null;
}
@Override
public EconomyResponse bankBalance(String s) {
return null;
}
@Override
public EconomyResponse bankHas(String s, double v) {
return null;
}
@Override
public EconomyResponse bankWithdraw(String s, double v) {
return null;
}
@Override
public EconomyResponse bankDeposit(String s, double v) {
return null;
}
@Override
public EconomyResponse isBankOwner(String s, String s1) {
return null;
}
@Override
public EconomyResponse isBankMember(String s, String s1) {
return null;
}
@Override
public List<String> getBanks() {
return null;
}
@Override
public boolean createPlayerAccount(String s) {
return false;
}
@Override
public boolean createPlayerAccount(String s, String s1) {
return false;
}
}
public ServicePriority getPriority() {
String prioString = getString("priority", "normal");
// Enumeration name casing is weird for ServicePriority.
for (ServicePriority prio : ServicePriority.values()) {
if (CoreUtilities.equalsIgnoreCase(prio.name(), prioString)) {
return prio;
}
}
return ServicePriority.Normal;
}
public DenizenEconomyProvider register() {
DenizenEconomyProvider provider = new DenizenEconomyProvider();
provider.backingScript = this;
Bukkit.getServer().getServicesManager().register(Economy.class, provider, Denizen.getInstance(), getPriority());
return provider;
}
public static void cleanup() {
for (DenizenEconomyProvider provider : providersRegistered) {
Bukkit.getServer().getServicesManager().unregister(provider);
}
providersRegistered.clear();
}
public static List<DenizenEconomyProvider> providersRegistered = new ArrayList<>();
public EconomyScriptContainer(YamlConfiguration configurationSection, String scriptContainerName) {
super(configurationSection, scriptContainerName);
providersRegistered.add(register());
}
}