-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathVaultEconomyHandler.java
More file actions
297 lines (241 loc) · 11.1 KB
/
VaultEconomyHandler.java
File metadata and controls
297 lines (241 loc) · 11.1 KB
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
package nl.openminetopia.modules.banking.vault;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import nl.openminetopia.OpenMinetopia;
import nl.openminetopia.modules.banking.BankingModule;
import nl.openminetopia.modules.banking.models.BankAccountModel;
import nl.openminetopia.modules.transactions.TransactionsModule;
import nl.openminetopia.modules.transactions.enums.TransactionType;
import nl.openminetopia.modules.transactions.events.TransactionUpdateEvent;
import nl.openminetopia.utils.events.EventUtils;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import java.util.List;
import java.util.UUID;
public class VaultEconomyHandler implements Economy {
private final BankingModule bankingModule = OpenMinetopia.getModuleManager().get(BankingModule.class);
private final TransactionsModule transactionsModule = OpenMinetopia.getModuleManager().get(TransactionsModule.class);
@Override
public boolean isEnabled() {
return OpenMinetopia.getInstance().isEnabled();
}
@Override
public String getName() {
return "OpenMinetopia";
}
@Override
public boolean hasBankSupport() {
return false;
}
@Override
public int fractionalDigits() {
return 0;
}
@Override
public String format(double amount) {
return bankingModule.format(amount);
}
@Override
public String currencyNamePlural() {
return "euro";
}
@Override
public String currencyNameSingular() {
return "€";
}
@Override
public boolean hasAccount(String playerName) {
Player player = Bukkit.getPlayer(playerName);
if (player == null) return false;
return bankingModule.getAccountById(player.getUniqueId()) != null;
}
@Override
public boolean hasAccount(OfflinePlayer offlinePlayer) {
return bankingModule.getAccountById(offlinePlayer.getUniqueId()) != null;
}
@Override
public boolean hasAccount(String playerName, String worldName) {
return hasAccount(playerName);
}
@Override
public boolean hasAccount(OfflinePlayer offlinePlayer, String worldName) {
return hasAccount(offlinePlayer);
}
@Override
public double getBalance(String playerName) {
Player player = Bukkit.getPlayer(playerName);
if (player == null) return -1;
return bankingModule.getAccountById(player.getUniqueId()).getBalance();
}
@Override
public double getBalance(OfflinePlayer offlinePlayer) {
BankAccountModel accountModel = bankingModule.getAccountById(offlinePlayer.getUniqueId());
if (accountModel == null) return -1;
return accountModel.getBalance();
}
@Override
public double getBalance(String playerName, String worldName) {
return getBalance(playerName);
}
@Override
public double getBalance(OfflinePlayer offlinePlayer, String worldName) {
return getBalance(offlinePlayer);
}
@Override
public boolean has(String playerName, double amount) {
return getBalance(playerName) >= amount;
}
@Override
public boolean has(OfflinePlayer offlinePlayer, double amount) {
return getBalance(offlinePlayer) >= amount;
}
@Override
public boolean has(String playerName, String worldName, double amount) {
return getBalance(playerName) >= amount;
}
@Override
public boolean has(OfflinePlayer offlinePlayer, String worldName, double amount) {
return getBalance(offlinePlayer) >= amount;
}
@Override
public EconomyResponse withdrawPlayer(String playerName, double amount) {
OfflinePlayer player = Bukkit.getOfflinePlayer(playerName);
if (!player.hasPlayedBefore())
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Player is not online or doesn't exist.");
return withdrawPlayer(player, amount);
}
@Override
public EconomyResponse withdrawPlayer(OfflinePlayer offlinePlayer, double amount) {
BankAccountModel accountModel = bankingModule.getAccountById(offlinePlayer.getUniqueId());
if (accountModel != null) {
TransactionUpdateEvent event = new TransactionUpdateEvent(new UUID(0, 0), "Server", TransactionType.WITHDRAW, amount, accountModel, "Vault Interaction", System.currentTimeMillis());
if (EventUtils.callCancellable(event))
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Transaction cancelled by plugin.");
accountModel.setBalance(accountModel.getBalance() - amount);
transactionsModule.createTransactionLog(System.currentTimeMillis(), new UUID(0, 0), "Server", TransactionType.WITHDRAW, amount, accountModel.getUniqueId(), "Vault Interaction");
return new EconomyResponse(amount, accountModel.getBalance(), EconomyResponse.ResponseType.SUCCESS, "");
}
bankingModule.getAccountByIdAsync(offlinePlayer.getUniqueId()).thenAccept(model -> {
if (model != null) {
TransactionUpdateEvent event = new TransactionUpdateEvent(new UUID(0, 0), "Server", TransactionType.WITHDRAW, amount, model, "Vault Interaction", System.currentTimeMillis());
if (EventUtils.callCancellable(event))
return;
model.setBalance(model.getBalance() - amount);
model.save();
transactionsModule.createTransactionLog(System.currentTimeMillis(), new UUID(0, 0), "Server", TransactionType.WITHDRAW, amount, model.getUniqueId(), "Vault Interaction");
}
});
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Account not found.");
}
@Override
public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
return withdrawPlayer(playerName, amount);
}
@Override
public EconomyResponse withdrawPlayer(OfflinePlayer offlinePlayer, String worldName, double amount) {
return withdrawPlayer(offlinePlayer, amount);
}
@Override
public EconomyResponse depositPlayer(String playerName, double amount) {
OfflinePlayer player = Bukkit.getOfflinePlayer(playerName);
if (!player.hasPlayedBefore())
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Player is not online or doesn't exist.");
return depositPlayer(player, amount);
}
@Override
public EconomyResponse depositPlayer(OfflinePlayer offlinePlayer, double amount) {
BankAccountModel accountModel = bankingModule.getAccountById(offlinePlayer.getUniqueId());
if (accountModel != null) {
TransactionUpdateEvent event = new TransactionUpdateEvent(new UUID(0, 0), "Server", TransactionType.DEPOSIT, amount, accountModel, "Vault Interaction", System.currentTimeMillis());
if (EventUtils.callCancellable(event))
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Transaction cancelled by plugin.");
accountModel.setBalance(accountModel.getBalance() + amount);
transactionsModule.createTransactionLog(System.currentTimeMillis(), new UUID(0, 0), "Server", TransactionType.DEPOSIT, amount, accountModel.getUniqueId(), "Vault Interaction");
return new EconomyResponse(amount, accountModel.getBalance(), EconomyResponse.ResponseType.SUCCESS, "");
}
bankingModule.getAccountByIdAsync(offlinePlayer.getUniqueId()).thenAccept(model -> {
if (model != null) {
TransactionUpdateEvent event = new TransactionUpdateEvent(new UUID(0, 0), "Server", TransactionType.DEPOSIT, amount, model, "Vault Interaction", System.currentTimeMillis());
if (EventUtils.callCancellable(event))
return;
model.setBalance(model.getBalance() + amount);
model.save();
transactionsModule.createTransactionLog(System.currentTimeMillis(), new UUID(0, 0), "Server", TransactionType.DEPOSIT, amount, model.getUniqueId(), "Vault Interaction");
}
});
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Account not found");
}
@Override
public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
return depositPlayer(playerName, amount);
}
@Override
public EconomyResponse depositPlayer(OfflinePlayer offlinePlayer, String worldName, double amount) {
return depositPlayer(offlinePlayer, amount);
}
@Override
public EconomyResponse createBank(String s, String s1) {
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "Not implemented");
}
@Override
public EconomyResponse createBank(String s, OfflinePlayer offlinePlayer) {
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "Not implemented");
}
@Override
public EconomyResponse deleteBank(String s) {
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "Not implemented");
}
@Override
public EconomyResponse bankBalance(String s) {
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "Not implemented");
}
@Override
public EconomyResponse bankHas(String s, double v) {
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "Not implemented");
}
@Override
public EconomyResponse bankWithdraw(String s, double v) {
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "Not implemented");
}
@Override
public EconomyResponse bankDeposit(String s, double v) {
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "Not implemented");
}
@Override
public EconomyResponse isBankOwner(String s, String s1) {
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "Not implemented");
}
@Override
public EconomyResponse isBankOwner(String s, OfflinePlayer offlinePlayer) {
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "Not implemented");
}
@Override
public EconomyResponse isBankMember(String s, String s1) {
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "Not implemented");
}
@Override
public EconomyResponse isBankMember(String s, OfflinePlayer offlinePlayer) {
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "Not implemented");
}
@Override
public List<String> getBanks() {
return List.of();
}
@Override
public boolean createPlayerAccount(String s) {
return false;
}
@Override
public boolean createPlayerAccount(OfflinePlayer offlinePlayer) {
return false;
}
@Override
public boolean createPlayerAccount(String s, String s1) {
return false;
}
@Override
public boolean createPlayerAccount(OfflinePlayer offlinePlayer, String s) {
return false;
}
}