|
| 1 | +package org.ve; |
| 2 | + |
| 3 | +import net.milkbowl.vault.economy.Economy; |
| 4 | +import net.milkbowl.vault.economy.EconomyResponse; |
| 5 | +import org.bonge.launch.BongeLaunch; |
| 6 | +import org.bukkit.Bukkit; |
| 7 | +import org.bukkit.OfflinePlayer; |
| 8 | +import org.spongepowered.api.Sponge; |
| 9 | +import org.spongepowered.api.event.cause.Cause; |
| 10 | +import org.spongepowered.api.event.cause.EventContext; |
| 11 | +import org.spongepowered.api.event.cause.EventContextKeys; |
| 12 | +import org.spongepowered.api.service.ProviderRegistration; |
| 13 | +import org.spongepowered.api.service.economy.Currency; |
| 14 | +import org.spongepowered.api.service.economy.EconomyService; |
| 15 | +import org.spongepowered.api.service.economy.account.UniqueAccount; |
| 16 | +import org.spongepowered.api.service.economy.transaction.ResultType; |
| 17 | +import org.spongepowered.api.service.economy.transaction.TransactionResult; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.math.BigDecimal; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Optional; |
| 24 | + |
| 25 | +public class BongeEco implements Economy { |
| 26 | + |
| 27 | + private Currency currency; |
| 28 | + |
| 29 | + public ProviderRegistration<EconomyService> getSpongeEco() throws IOException { |
| 30 | + Optional<ProviderRegistration<EconomyService>> opService = Sponge.getServiceManager().getRegistration(EconomyService.class); |
| 31 | + if(!opService.isPresent()){ |
| 32 | + throw new IOException("No economy service found for Sponge"); |
| 33 | + } |
| 34 | + if(this.currency == null){ |
| 35 | + this.currency = opService.get().getProvider().getDefaultCurrency(); |
| 36 | + } |
| 37 | + return opService.get(); |
| 38 | + } |
| 39 | + |
| 40 | + public EconomyResponse convert(TransactionResult result){ |
| 41 | + EconomyResponse.ResponseType type = EconomyResponse.ResponseType.FAILURE; |
| 42 | + String errorMessage = result.getType().getName(); |
| 43 | + if(result.getResult().equals(ResultType.SUCCESS)){ |
| 44 | + type = EconomyResponse.ResponseType.SUCCESS; |
| 45 | + errorMessage = null; |
| 46 | + } |
| 47 | + return new EconomyResponse(result.getAmount().doubleValue(), result.getAccount().getBalance(result.getCurrency()).doubleValue(), type, errorMessage); |
| 48 | + } |
| 49 | + |
| 50 | + public UniqueAccount getSpongeAccount(OfflinePlayer player) throws IOException{ |
| 51 | + ProviderRegistration<EconomyService> service = this.getSpongeEco(); |
| 52 | + Optional<UniqueAccount> opAccount = service.getProvider().getOrCreateAccount(player.getUniqueId()); |
| 53 | + if(opAccount.isPresent()){ |
| 54 | + return opAccount.get(); |
| 55 | + } |
| 56 | + throw new IOException("Can not create new Sponge account for economy"); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean isEnabled() { |
| 61 | + try { |
| 62 | + getSpongeEco(); |
| 63 | + return true; |
| 64 | + } catch (IOException e) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public String getName() { |
| 71 | + return "BongeEco"; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public boolean hasBankSupport() { |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public int fractionalDigits() { |
| 81 | + return 0; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public String format(double v) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String currencyNamePlural() { |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public String currencyNameSingular() { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public boolean hasAccount(String s) { |
| 101 | + return this.hasAccount(Bukkit.getOfflinePlayer(s)); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public boolean hasAccount(OfflinePlayer offlinePlayer) { |
| 106 | + try { |
| 107 | + return this.getSpongeEco().getProvider().hasAccount(offlinePlayer.getUniqueId()); |
| 108 | + } catch (IOException e) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public boolean hasAccount(String s, String s1) { |
| 116 | + return this.hasAccount(Bukkit.getOfflinePlayer(s), s1); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public boolean hasAccount(OfflinePlayer offlinePlayer, String s) { |
| 121 | + return this.hasAccount(offlinePlayer); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public double getBalance(String s) { |
| 126 | + return this.getBalance(Bukkit.getOfflinePlayer(s)); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public double getBalance(OfflinePlayer offlinePlayer) { |
| 131 | + try { |
| 132 | + return this.getSpongeAccount(offlinePlayer).getBalance(this.currency).doubleValue(); |
| 133 | + } catch (IOException e) { |
| 134 | + return 0.0; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public double getBalance(String s, String s1) { |
| 140 | + return this.getBalance(Bukkit.getOfflinePlayer(s), s1); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public double getBalance(OfflinePlayer offlinePlayer, String s) { |
| 145 | + return this.getBalance(offlinePlayer); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public boolean has(String s, double v) { |
| 150 | + return this.has(Bukkit.getOfflinePlayer(s), v); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public boolean has(OfflinePlayer offlinePlayer, double v) { |
| 155 | + return this.getBalance(offlinePlayer) >= v; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public boolean has(String s, String s1, double v) { |
| 160 | + return this.has(Bukkit.getOfflinePlayer(s), s1, v); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public boolean has(OfflinePlayer offlinePlayer, String s, double v) { |
| 165 | + return this.has(offlinePlayer, v); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public EconomyResponse withdrawPlayer(String s, double v) { |
| 170 | + return this.withdrawPlayer(s, v); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public EconomyResponse withdrawPlayer(OfflinePlayer offlinePlayer, double v) { |
| 175 | + Cause cause = Cause.builder().append(offlinePlayer).build(EventContext.builder().add(EventContextKeys.PLUGIN, BongeLaunch.getContainer()).build()); |
| 176 | + try { |
| 177 | + return this.convert(this.getSpongeAccount(offlinePlayer).withdraw(this.currency, new BigDecimal(v), cause)); |
| 178 | + } catch (IOException e) { |
| 179 | + return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "Unknown Sponge economy service"); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public EconomyResponse withdrawPlayer(String s, String s1, double v) { |
| 185 | + return this.withdrawPlayer(s, s1, v); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public EconomyResponse withdrawPlayer(OfflinePlayer offlinePlayer, String s, double v) { |
| 190 | + return this.withdrawPlayer(offlinePlayer, v); |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public EconomyResponse depositPlayer(String s, double v) { |
| 195 | + return depositPlayer(Bukkit.getOfflinePlayer(s), v); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public EconomyResponse depositPlayer(OfflinePlayer offlinePlayer, double v) { |
| 200 | + Cause cause = Cause.builder().append(offlinePlayer).build(EventContext.builder().add(EventContextKeys.PLUGIN, BongeLaunch.getContainer()).build()); |
| 201 | + try { |
| 202 | + return this.convert(this.getSpongeAccount(offlinePlayer).deposit(this.currency, new BigDecimal(v), cause)); |
| 203 | + } catch (IOException e) { |
| 204 | + return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "Unknown Sponge economy service"); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public EconomyResponse depositPlayer(String s, String s1, double v) { |
| 210 | + return this.depositPlayer(s, v); |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public EconomyResponse depositPlayer(OfflinePlayer offlinePlayer, String s, double v) { |
| 215 | + return this.depositPlayer(offlinePlayer, v); |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public EconomyResponse createBank(String s, String s1) { |
| 220 | + return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "No bank support in Sponge"); |
| 221 | + } |
| 222 | + |
| 223 | + @Override |
| 224 | + public EconomyResponse createBank(String s, OfflinePlayer offlinePlayer) { |
| 225 | + return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "No bank support in Sponge"); |
| 226 | + } |
| 227 | + |
| 228 | + @Override |
| 229 | + public EconomyResponse deleteBank(String s) { |
| 230 | + return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "No bank support in Sponge"); |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + public EconomyResponse bankBalance(String s) { |
| 235 | + return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "No bank support in Sponge"); |
| 236 | + } |
| 237 | + |
| 238 | + @Override |
| 239 | + public EconomyResponse bankHas(String s, double v) { |
| 240 | + return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "No bank support in Sponge"); |
| 241 | + } |
| 242 | + |
| 243 | + @Override |
| 244 | + public EconomyResponse bankWithdraw(String s, double v) { |
| 245 | + return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "No bank support in Sponge"); |
| 246 | + } |
| 247 | + |
| 248 | + @Override |
| 249 | + public EconomyResponse bankDeposit(String s, double v) { |
| 250 | + return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "No bank support in Sponge"); |
| 251 | + } |
| 252 | + |
| 253 | + @Override |
| 254 | + public EconomyResponse isBankOwner(String s, String s1) { |
| 255 | + return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "No bank support in Sponge"); |
| 256 | + } |
| 257 | + |
| 258 | + @Override |
| 259 | + public EconomyResponse isBankOwner(String s, OfflinePlayer offlinePlayer) { |
| 260 | + return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "No bank support in Sponge"); |
| 261 | + } |
| 262 | + |
| 263 | + @Override |
| 264 | + public EconomyResponse isBankMember(String s, String s1) { |
| 265 | + return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "No bank support in Sponge"); |
| 266 | + } |
| 267 | + |
| 268 | + @Override |
| 269 | + public EconomyResponse isBankMember(String s, OfflinePlayer offlinePlayer) { |
| 270 | + return new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "No bank support in Sponge"); |
| 271 | + } |
| 272 | + |
| 273 | + @Override |
| 274 | + public List<String> getBanks() { |
| 275 | + return Collections.EMPTY_LIST; |
| 276 | + } |
| 277 | + |
| 278 | + @Override |
| 279 | + public boolean createPlayerAccount(String s) { |
| 280 | + return this.createPlayerAccount(Bukkit.getOfflinePlayer(s)); |
| 281 | + } |
| 282 | + |
| 283 | + @Override |
| 284 | + public boolean createPlayerAccount(OfflinePlayer offlinePlayer) { |
| 285 | + try { |
| 286 | + this.getSpongeAccount(offlinePlayer); |
| 287 | + return true; |
| 288 | + } catch (IOException e) { |
| 289 | + return false; |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + @Override |
| 294 | + public boolean createPlayerAccount(String s, String s1) { |
| 295 | + return this.createPlayerAccount(Bukkit.getOfflinePlayer(s), s1); |
| 296 | + } |
| 297 | + |
| 298 | + @Override |
| 299 | + public boolean createPlayerAccount(OfflinePlayer offlinePlayer, String s) { |
| 300 | + return this.createPlayerAccount(offlinePlayer); |
| 301 | + } |
| 302 | +} |
0 commit comments