2015-10-07 08:33:33 +02:00
|
|
|
package com.plotsquared.sponge.util;
|
|
|
|
|
2016-02-19 19:52:26 +01:00
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.util.Optional;
|
2015-12-19 20:30:06 +01:00
|
|
|
|
|
|
|
import org.apache.commons.lang.NotImplementedException;
|
2016-02-19 18:18:15 +01:00
|
|
|
import org.spongepowered.api.Sponge;
|
2016-02-19 19:52:26 +01:00
|
|
|
import org.spongepowered.api.event.cause.Cause;
|
|
|
|
import org.spongepowered.api.service.economy.EconomyService;
|
|
|
|
import org.spongepowered.api.service.economy.account.UniqueAccount;
|
2015-12-19 20:30:06 +01:00
|
|
|
|
2016-02-19 20:05:47 +01:00
|
|
|
import com.intellectualcrafters.plot.PS;
|
2015-10-07 08:33:33 +02:00
|
|
|
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.object.SpongePlayer;
|
|
|
|
|
|
|
|
public class SpongeEconHandler extends EconHandler {
|
2016-02-19 19:52:26 +01:00
|
|
|
private EconomyService econ;
|
2016-02-19 20:05:47 +01:00
|
|
|
|
2015-12-19 20:30:06 +01:00
|
|
|
public SpongeEconHandler() {
|
2016-02-19 18:18:15 +01:00
|
|
|
if (Sponge.getServiceManager().isRegistered(EconomyService.class)) {
|
2016-02-19 20:05:47 +01:00
|
|
|
econ = Sponge.getServiceManager().provide(EconomyService.class).get();
|
2016-02-19 18:18:15 +01:00
|
|
|
} else {
|
|
|
|
PS.log("No economy service was registered.");
|
2015-12-19 20:30:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-07 08:33:33 +02:00
|
|
|
@Override
|
|
|
|
public void withdrawMoney(PlotPlayer player, double amount) {
|
2016-02-19 19:52:26 +01:00
|
|
|
if (econ != null) {
|
2016-02-19 20:05:47 +01:00
|
|
|
Optional<UniqueAccount> accOpt = econ.getAccount(player.getUUID());
|
|
|
|
if (accOpt.isPresent()) {
|
|
|
|
UniqueAccount acc = accOpt.get();
|
2016-02-19 19:52:26 +01:00
|
|
|
acc.withdraw(econ.getDefaultCurrency(), new BigDecimal(amount), Cause.of("PlotSquared"));
|
|
|
|
}
|
|
|
|
}
|
2015-10-07 08:33:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void depositMoney(PlotPlayer player, double amount) {
|
2016-02-19 19:52:26 +01:00
|
|
|
if (econ != null) {
|
2016-02-19 20:05:47 +01:00
|
|
|
Optional<UniqueAccount> accOpt = econ.getAccount(player.getUUID());
|
|
|
|
if (accOpt.isPresent()) {
|
|
|
|
UniqueAccount acc = accOpt.get();
|
2016-02-19 19:52:26 +01:00
|
|
|
acc.deposit(econ.getDefaultCurrency(), new BigDecimal(amount), Cause.of("PlotSquared"));
|
|
|
|
}
|
|
|
|
}
|
2015-10-07 08:33:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void depositMoney(OfflinePlotPlayer player, double amount) {
|
2016-02-19 19:52:26 +01:00
|
|
|
if (econ != null) {
|
2016-02-19 20:05:47 +01:00
|
|
|
Optional<UniqueAccount> accOpt = econ.getAccount(player.getUUID());
|
|
|
|
if (accOpt.isPresent()) {
|
|
|
|
UniqueAccount acc = accOpt.get();
|
2016-02-19 19:52:26 +01:00
|
|
|
acc.deposit(econ.getDefaultCurrency(), new BigDecimal(amount), Cause.of("PlotSquared"));
|
|
|
|
}
|
|
|
|
}
|
2015-10-07 08:33:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setPermission(String world, String player, String perm, boolean value) {
|
|
|
|
// TODO Auto-generated method stub
|
2015-12-19 20:30:06 +01:00
|
|
|
throw new NotImplementedException("TODO/WIP/NOT IMPLEMENTED!");
|
2015-10-07 08:33:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasPermission(String world, String player, String perm) {
|
|
|
|
SpongePlayer obj = (SpongePlayer) UUIDHandler.getPlayer(player);
|
|
|
|
if (obj != null) {
|
|
|
|
return obj.player.hasPermission(perm);
|
|
|
|
}
|
|
|
|
// TODO offline
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-19 19:52:26 +01:00
|
|
|
@Override
|
|
|
|
public double getBalance(PlotPlayer player) {
|
|
|
|
if (econ != null) {
|
2016-02-19 20:05:47 +01:00
|
|
|
Optional<UniqueAccount> accOpt = econ.getAccount(player.getUUID());
|
|
|
|
if (accOpt.isPresent()) {
|
|
|
|
UniqueAccount acc = accOpt.get();
|
2016-02-19 19:52:26 +01:00
|
|
|
BigDecimal balance = acc.getBalance(econ.getDefaultCurrency());
|
|
|
|
return balance.doubleValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-07 08:33:33 +02:00
|
|
|
}
|