PlotSquared/src/main/java/com/plotsquared/bukkit/util/BukkitEconHandler.java

91 lines
2.8 KiB
Java
Raw Normal View History

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