2015-07-30 19:24:01 +02:00
|
|
|
package com.plotsquared.bukkit.util;
|
2015-06-05 14:39:31 +02:00
|
|
|
|
2015-07-27 19:50:04 +02:00
|
|
|
import com.intellectualcrafters.plot.object.OfflinePlotPlayer;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotPlayer;
|
|
|
|
import com.intellectualcrafters.plot.util.EconHandler;
|
|
|
|
import com.plotsquared.bukkit.object.BukkitOfflinePlayer;
|
2016-02-10 19:59:51 +01:00
|
|
|
import com.plotsquared.bukkit.object.BukkitPlayer;
|
2016-03-23 02:41:37 +01:00
|
|
|
import net.milkbowl.vault.economy.Economy;
|
|
|
|
import net.milkbowl.vault.permission.Permission;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
2015-06-05 16:08:16 +02:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public class BukkitEconHandler extends EconHandler {
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-06-05 14:39:31 +02:00
|
|
|
private Economy econ;
|
|
|
|
private Permission perms;
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public Economy getEconomy() {
|
2015-08-07 18:40:41 +02:00
|
|
|
init();
|
2016-03-23 02:41:37 +01:00
|
|
|
return this.econ;
|
2015-08-07 18:40:41 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public Permission getPermissions() {
|
2015-08-07 18:40:41 +02:00
|
|
|
init();
|
2016-03-23 02:41:37 +01:00
|
|
|
return this.perms;
|
2015-08-07 18:40:41 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public boolean init() {
|
2016-03-23 02:41:37 +01:00
|
|
|
if (this.econ == null || this.perms == null) {
|
2015-06-05 14:39:31 +02:00
|
|
|
setupPermissions();
|
|
|
|
setupEconomy();
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
return this.econ != null && this.perms != null;
|
2015-06-05 14:39:31 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
private boolean setupPermissions() {
|
2016-03-23 02:41:37 +01:00
|
|
|
RegisteredServiceProvider<Permission> permissionProvider =
|
2016-02-14 02:01:18 +01:00
|
|
|
Bukkit.getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
|
2015-09-13 06:04:31 +02:00
|
|
|
if (permissionProvider != null) {
|
2016-03-23 02:41:37 +01:00
|
|
|
this.perms = permissionProvider.getProvider();
|
2015-06-05 14:39:31 +02:00
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
return this.perms != null;
|
2015-06-05 14:39:31 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
private boolean setupEconomy() {
|
2016-03-23 02:41:37 +01:00
|
|
|
RegisteredServiceProvider<Economy> economyProvider =
|
2016-02-14 02:01:18 +01:00
|
|
|
Bukkit.getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
|
2015-09-13 06:04:31 +02:00
|
|
|
if (economyProvider != null) {
|
2016-03-23 02:41:37 +01:00
|
|
|
this.econ = economyProvider.getProvider();
|
2015-06-05 14:39:31 +02:00
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
return this.econ != null;
|
2015-06-05 14:39:31 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-06-05 14:39:31 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public double getMoney(PlotPlayer player) {
|
|
|
|
double bal = super.getMoney(player);
|
2015-09-13 06:04:31 +02:00
|
|
|
if (Double.isNaN(bal)) {
|
2016-03-23 02:41:37 +01:00
|
|
|
return this.econ.getBalance(((BukkitPlayer) player).player);
|
2015-09-13 06:04:31 +02:00
|
|
|
}
|
2015-07-27 20:06:20 +02:00
|
|
|
return bal;
|
2015-06-05 14:39:31 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-06-05 14:39:31 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public void withdrawMoney(PlotPlayer player, double amount) {
|
|
|
|
this.econ.withdrawPlayer(((BukkitPlayer) player).player, amount);
|
2015-06-05 14:39:31 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-06-05 14:39:31 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public void depositMoney(PlotPlayer player, double amount) {
|
|
|
|
this.econ.depositPlayer(((BukkitPlayer) player).player, amount);
|
2015-06-05 14:39:31 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-06-05 14:39:31 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public void depositMoney(OfflinePlotPlayer player, double amount) {
|
|
|
|
this.econ.depositPlayer(((BukkitOfflinePlayer) player).player, amount);
|
2015-06-05 14:39:31 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-08-01 22:11:28 +02:00
|
|
|
@Override
|
2016-03-23 02:41:37 +01:00
|
|
|
public boolean hasPermission(String world, String player, String perm) {
|
|
|
|
return this.perms.playerHas(world, Bukkit.getOfflinePlayer(player), perm);
|
2015-08-01 22:11:28 +02:00
|
|
|
}
|
2016-02-19 19:42:06 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getBalance(PlotPlayer player) {
|
2016-03-23 02:41:37 +01:00
|
|
|
return this.econ.getBalance(player.getName());
|
2016-02-19 19:42:06 +01:00
|
|
|
}
|
2015-06-05 14:39:31 +02:00
|
|
|
}
|