|
5 | 5 | import net.milkbowl.vault.economy.Economy;
|
6 | 6 | import org.bukkit.Bukkit;
|
7 | 7 | import org.bukkit.OfflinePlayer;
|
| 8 | +import org.bukkit.plugin.RegisteredServiceProvider; |
8 | 9 |
|
9 | 10 | /**
|
10 | 11 | * @author Blank038
|
11 | 12 | */
|
12 | 13 | @SuppressWarnings(value = {"unused"})
|
13 | 14 | public class VaultEconomyImpl extends BaseEconomy {
|
14 |
| - private final Economy economyProvider; |
| 15 | + private Economy economyProvider; |
15 | 16 |
|
16 | 17 | public VaultEconomyImpl() {
|
17 | 18 | super(PayType.VAULT);
|
18 |
| - economyProvider = Bukkit.getServicesManager().getRegistration(Economy.class).getProvider(); |
| 19 | + this.checkEconomy(); |
| 20 | + } |
| 21 | + |
| 22 | + private void checkEconomy() { |
| 23 | + if (this.economyProvider != null) { |
| 24 | + return; |
| 25 | + } |
| 26 | + RegisteredServiceProvider<Economy> service = Bukkit.getServicesManager().getRegistration(Economy.class); |
| 27 | + if (service == null) { |
| 28 | + return; |
| 29 | + } |
| 30 | + this.economyProvider = service.getProvider(); |
19 | 31 | }
|
20 | 32 |
|
21 | 33 | @Override
|
22 | 34 | public double balance(OfflinePlayer player, String key) {
|
| 35 | + this.checkEconomy(); |
| 36 | + if (this.economyProvider == null) { |
| 37 | + return 0.0D; |
| 38 | + } |
23 | 39 | return economyProvider.getBalance(player);
|
24 | 40 | }
|
25 | 41 |
|
26 | 42 | @Override
|
27 | 43 | public void give(OfflinePlayer player, String key, double amount) {
|
| 44 | + this.checkEconomy(); |
| 45 | + if (this.economyProvider == null) { |
| 46 | + return; |
| 47 | + } |
28 | 48 | economyProvider.depositPlayer(player, Math.max(1, amount));
|
29 | 49 | }
|
30 | 50 |
|
31 | 51 | @Override
|
32 | 52 | public boolean take(OfflinePlayer player, String key, double amount) {
|
| 53 | + this.checkEconomy(); |
| 54 | + if (this.economyProvider == null) { |
| 55 | + return false; |
| 56 | + } |
33 | 57 | return economyProvider.withdrawPlayer(player, Math.max(1, amount)).transactionSuccess();
|
34 | 58 | }
|
35 | 59 | }
|
0 commit comments