-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
EconomyScriptContainer.java
331 lines (281 loc) · 12.4 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
package com.denizenscript.denizen.scripts.containers.core;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.BukkitScriptEntryData;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
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.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.util.ArrayList;
import java.util.List;
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 mechanism PlayerTag.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 auto-tag "<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) {
return autoTag(value.replace("<amount", "<element[" + amount + "]"), player);
}
public String autoTag(String value, OfflinePlayer player) {
if (value == null) {
return null;
}
return TagManager.tag(value, new BukkitTagContext(player == null ? null : new PlayerTag(player),
null, false, null, backingScript.shouldDebug(), new ScriptTag(backingScript)));
}
public String runSubScript(String pathName, OfflinePlayer player, double amount) {
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 ArgumentHelper.getIntegerFrom(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) {
return ArgumentHelper.getDoubleFrom(autoTag(backingScript.getString("balance"), player));
}
@Override
public EconomyResponse withdrawPlayer(OfflinePlayer player, double amount) {
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) {
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) {
return ArgumentHelper.getBooleanFrom(autoTagAmount(backingScript.getString("has"), player, amount));
}
@Override
public boolean hasAccount(String playerName) {
return true;
}
@Override
public boolean hasAccount(String playerName, String worldName) {
return true;
}
@Override
public double getBalance(String playerName) {
return getBalance(Bukkit.getPlayerExact(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(Bukkit.getPlayerExact(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(Bukkit.getPlayerExact(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(Bukkit.getPlayerExact(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 = CoreUtilities.toLowerCase(getString("PRIORITY", "normal"));
// Enumeration name casing is weird for ServicePriority.
for (ServicePriority prio : ServicePriority.values()) {
if (CoreUtilities.toLowerCase(prio.name()).equals(prioString)) {
return prio;
}
}
return ServicePriority.Normal;
}
public DenizenEconomyProvider register() {
DenizenEconomyProvider provider = new DenizenEconomyProvider();
provider.backingScript = this;
Bukkit.getServer().getServicesManager().register(Economy.class, provider, DenizenAPI.getCurrentInstance(), 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());
}
}