001/* This file is part of Vault.
002
003    Vault is free software: you can redistribute it and/or modify
004    it under the terms of the GNU Lesser General Public License as published by
005    the Free Software Foundation, either version 3 of the License, or
006    (at your option) any later version.
007
008    Vault is distributed in the hope that it will be useful,
009    but WITHOUT ANY WARRANTY; without even the implied warranty of
010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
011    GNU Lesser General Public License for more details.
012
013    You should have received a copy of the GNU Lesser General Public License
014    along with Vault.  If not, see <http://www.gnu.org/licenses/>.
015*/
016package net.milkbowl.vault;
017
018import net.milkbowl.vault.Vault;
019import net.milkbowl.vault.economy.Economy;
020
021import org.bukkit.plugin.Plugin;
022import org.bukkit.plugin.RegisteredServiceProvider;
023
024import com.nijikokun.register.payment.Method;
025
026public class VaultEco implements Method {
027
028    private Vault vault;
029    private Economy economy;
030
031    public Vault getPlugin() {
032        return this.vault;
033    }
034
035    @Override
036    public boolean createAccount(String name, Double amount) {
037        if(!this.economy.createBank(name, "").transactionSuccess()) {
038            return false;
039        }
040        return this.economy.bankDeposit(name, amount).transactionSuccess();
041    }
042
043    public String getName() {
044        return this.vault.getDescription().getName();
045    }
046
047    public String getVersion() {
048        return this.vault.getDescription().getVersion();
049    }
050
051    public int fractionalDigits() {
052        return this.economy.fractionalDigits();
053    }
054
055    public String format(double amount) {
056        return this.economy.format(amount);
057    }
058
059    public boolean hasBanks() {
060        return this.economy.hasBankSupport();
061    }
062
063    public boolean hasBank(String bank) {
064        return this.economy.getBanks().contains(bank);
065    }
066
067    public boolean hasAccount(String name) {
068        return this.economy.hasAccount(name);
069    }
070
071    public boolean hasBankAccount(String bank, String name) {
072        return this.economy.isBankOwner(bank, name).transactionSuccess() || this.economy.isBankMember(bank, name).transactionSuccess();
073    }
074
075    public boolean createAccount(String name) {
076        return this.economy.createPlayerAccount(name);
077    }
078
079    public MethodAccount getAccount(String name) {
080        if(!hasAccount(name)) {
081            return null;
082        }
083
084        return new VaultAccount(name, this.economy);
085    }
086
087    public MethodBankAccount getBankAccount(String bank, String name) {
088        if(!hasBankAccount(bank, name)) {
089            return null;
090        }
091
092        return new VaultBankAccount(bank, economy);
093    }
094
095    public boolean isCompatible(Plugin plugin) {
096        return plugin instanceof Vault;
097    }
098
099    public void setPlugin(Plugin plugin) {
100        this.vault = (Vault) plugin;
101        RegisteredServiceProvider<Economy> economyProvider = this.vault.getServer().getServicesManager().getRegistration(Economy.class);
102        if (economyProvider != null) {
103            this.economy = economyProvider.getProvider();
104        }
105    }
106
107    public class VaultAccount implements MethodAccount {
108        private final String name;
109        private final Economy economy;
110
111        public VaultAccount(String name, Economy economy) {
112            this.name = name;
113            this.economy = economy;
114        }
115
116        public double balance() {
117            return this.economy.getBalance(this.name);
118        }
119
120        public boolean set(double amount) {
121            if(!this.economy.withdrawPlayer(this.name, this.balance()).transactionSuccess()) {
122                return false;
123            }
124
125            if(amount == 0) {
126                return true;
127            }
128            return this.economy.depositPlayer(this.name, amount).transactionSuccess();
129        }
130
131        public boolean add(double amount) {
132            return this.economy.depositPlayer(this.name, amount).transactionSuccess();
133        }
134
135        public boolean subtract(double amount) {
136            return this.economy.withdrawPlayer(this.name, amount).transactionSuccess();
137        }
138
139        public boolean multiply(double amount) {
140            double balance = this.balance();
141            return this.set(balance * amount);
142        }
143
144        public boolean divide(double amount) {
145            double balance = this.balance();
146            return this.set(balance / amount);
147        }
148
149        public boolean hasEnough(double amount) {
150            return (this.balance() >= amount);
151        }
152
153        public boolean hasOver(double amount) {
154            return (this.balance() > amount);
155        }
156
157        public boolean hasUnder(double amount) {
158            return (this.balance() < amount);
159        }
160
161        public boolean isNegative() {
162            return (this.balance() < 0);
163        }
164
165        public boolean remove() {
166            return this.set(0.0);
167        }
168    }
169
170    public class VaultBankAccount implements MethodBankAccount {
171
172        private final String bank;
173        private final Economy economy;
174
175        public VaultBankAccount(String bank, Economy economy) {
176            this.bank = bank;
177            this.economy = economy;
178        }
179
180        public String getBankName() {
181            return this.bank;
182        }
183
184        public int getBankId() {
185            return -1;
186        }
187
188        public double balance() {
189            return this.economy.bankBalance(this.bank).balance;
190        }
191
192        public boolean set(double amount) {
193            if(!this.economy.bankWithdraw(this.bank, this.balance()).transactionSuccess()) {
194                return false;
195            }
196            if(amount == 0) {
197                return true;
198            }
199            return this.economy.bankDeposit(this.bank, amount).transactionSuccess();
200        }
201
202        public boolean add(double amount) {
203            return this.economy.bankDeposit(this.bank, amount).transactionSuccess();
204        }
205
206        public boolean subtract(double amount) {
207            return this.economy.bankWithdraw(this.bank, amount).transactionSuccess();
208        }
209
210        public boolean multiply(double amount) {
211            double balance = this.balance();
212            return this.set(balance * amount);
213        }
214
215        public boolean divide(double amount) {
216            double balance = this.balance();
217            return this.set(balance / amount);
218        }
219
220        public boolean hasEnough(double amount) {
221            return (this.balance() >= amount);
222        }
223
224        public boolean hasOver(double amount) {
225            return (this.balance() > amount);
226        }
227
228        public boolean hasUnder(double amount) {
229            return (this.balance() < amount);
230        }
231
232        public boolean isNegative() {
233            return (this.balance() < 0);
234        }
235
236        public boolean remove() {
237            return this.set(0.0);
238        }
239
240    }
241}