diff --git a/src/main/java/com/plotsquared/sponge/util/SpongeEconHandler.java b/src/main/java/com/plotsquared/sponge/util/SpongeEconHandler.java index 51fa1a85c..7807cf644 100644 --- a/src/main/java/com/plotsquared/sponge/util/SpongeEconHandler.java +++ b/src/main/java/com/plotsquared/sponge/util/SpongeEconHandler.java @@ -1,51 +1,55 @@ package com.plotsquared.sponge.util; -import java.util.UUID; - -import org.apache.commons.lang.NotImplementedException; - import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.object.OfflinePlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.EconHandler; import com.intellectualcrafters.plot.util.UUIDHandler; -import com.plotsquared.sponge.SpongeMain; import com.plotsquared.sponge.object.SpongePlayer; +import org.apache.commons.lang.NotImplementedException; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.event.cause.Cause; +import org.spongepowered.api.service.economy.EconomyService; +import org.spongepowered.api.service.economy.account.UniqueAccount; + +import java.math.BigDecimal; +import java.util.Optional; public class SpongeEconHandler extends EconHandler { - - private Object TE_SERVICE; - private Object EL_SERVICE; - + private EconomyService economyService; public SpongeEconHandler() { - try { - Class clazz = Class.forName("com.erigitic.service.TEService"); - this.TE_SERVICE = SpongeMain.THIS.getGame().getServiceManager().provide(clazz).get(); - - } catch (Exception e) { - try { - Class clazz = Class.forName("me.Flibio.EconomyLite.API.EconomyLiteAPI"); - this.EL_SERVICE = SpongeMain.THIS.getGame().getServiceManager().provide(clazz).get(); - } catch (Exception e2) { - PS.log("No economy service found! (EconomyLite, TotalEconomy)"); - } + if (Sponge.getServiceManager().isRegistered(EconomyService.class)) { + economyService = Sponge.getServiceManager().provide(EconomyService.class).get(); + } else { + PS.log("No economy service was registered."); } } @Override public void withdrawMoney(PlotPlayer player, double amount) { - UUID uuid = ((SpongePlayer) player).player.getUniqueId(); + Optional account = economyService.getAccount(player.getUUID()); + if (account.isPresent()) { + UniqueAccount acc = account.get(); + acc.withdraw(economyService.getDefaultCurrency(),BigDecimal.valueOf(amount), Cause.of("PlotSquared")); + } } @Override public void depositMoney(PlotPlayer player, double amount) { - UUID uuid = ((SpongePlayer) player).player.getUniqueId(); + Optional account = economyService.getAccount(player.getUUID()); + if (account.isPresent()) { + UniqueAccount acc = account.get(); + acc.deposit(economyService.getDefaultCurrency(),BigDecimal.valueOf(amount), Cause.of("PlotSquared")); + } } @Override public void depositMoney(OfflinePlotPlayer player, double amount) { - UUID uuid = player.getUUID(); - + Optional account = economyService.getAccount(player.getUUID()); + if (account.isPresent()) { + UniqueAccount acc = account.get(); + acc.withdraw(economyService.getDefaultCurrency(),BigDecimal.valueOf(amount), Cause.of("PlotSquared")); + } } @Override