From 63cd7c8b97fae946e88933cdd4939d736d8eee82 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 19 Feb 2016 12:18:15 -0500 Subject: [PATCH] Fix SpongeEconHandler --- .../sponge/util/SpongeEconHandler.java | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) 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