2020-04-16 04:52:39 +02:00
|
|
|
/*
|
|
|
|
* _____ _ _ _____ _
|
|
|
|
* | __ \| | | | / ____| | |
|
|
|
|
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
|
|
|
|
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
|
|
|
|
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
|
|
|
|
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
|
|
|
|
* | |
|
|
|
|
* |_|
|
|
|
|
* PlotSquared plot management system for Minecraft
|
|
|
|
* Copyright (C) 2020 IntellectualSites
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-04-11 02:19:18 +02:00
|
|
|
package com.plotsquared.bukkit.util;
|
2015-06-05 14:39:31 +02:00
|
|
|
|
2020-04-11 02:19:18 +02:00
|
|
|
import com.plotsquared.bukkit.player.BukkitOfflinePlayer;
|
|
|
|
import com.plotsquared.bukkit.player.BukkitPlayer;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.player.OfflinePlotPlayer;
|
|
|
|
import com.plotsquared.core.player.PlotPlayer;
|
|
|
|
import com.plotsquared.core.util.EconHandler;
|
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 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() {
|
2018-08-10 17:01:10 +02:00
|
|
|
RegisteredServiceProvider<Permission> permissionProvider =
|
|
|
|
Bukkit.getServer().getServicesManager().getRegistration(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-28 19:28:21 +02:00
|
|
|
if (Bukkit.getServer().getPluginManager().getPlugin("Vault") == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
RegisteredServiceProvider<Economy> economyProvider =
|
|
|
|
Bukkit.getServer().getServicesManager().getRegistration(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
|
|
|
|
2018-08-10 17:01:10 +02:00
|
|
|
@Override public double getMoney(PlotPlayer player) {
|
2016-03-23 02:41:37 +01:00
|
|
|
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
|
|
|
|
2018-08-10 17:01:10 +02:00
|
|
|
@Override public void withdrawMoney(PlotPlayer player, double amount) {
|
2016-03-23 02:41:37 +01:00
|
|
|
this.econ.withdrawPlayer(((BukkitPlayer) player).player, amount);
|
2015-06-05 14:39:31 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2018-08-10 17:01:10 +02:00
|
|
|
@Override public void depositMoney(PlotPlayer player, double amount) {
|
2016-03-23 02:41:37 +01:00
|
|
|
this.econ.depositPlayer(((BukkitPlayer) player).player, amount);
|
2015-06-05 14:39:31 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2018-08-10 17:01:10 +02:00
|
|
|
@Override public void depositMoney(OfflinePlotPlayer player, double amount) {
|
2016-03-23 02:41:37 +01:00
|
|
|
this.econ.depositPlayer(((BukkitOfflinePlayer) player).player, amount);
|
2015-06-05 14:39:31 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2018-08-10 17:01:10 +02:00
|
|
|
@Override public boolean hasPermission(String world, String player, String perm) {
|
2016-03-23 02:41:37 +01:00
|
|
|
return this.perms.playerHas(world, Bukkit.getOfflinePlayer(player), perm);
|
2015-08-01 22:11:28 +02:00
|
|
|
}
|
2016-03-28 19:28:21 +02:00
|
|
|
|
2018-08-10 17:01:10 +02: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
|
|
|
}
|
2016-04-19 07:32:31 +02:00
|
|
|
|
|
|
|
public void setPermission(String world, String player, String perm, boolean value) {
|
|
|
|
if (value) {
|
|
|
|
this.perms.playerAdd(world, player, perm);
|
|
|
|
} else {
|
|
|
|
this.perms.playerRemove(world, player, perm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 14:39:31 +02:00
|
|
|
}
|