Only initialise EconHandler when economy is enabled and stop accessing the static instance directly.

This commit is contained in:
Alexander Söderberg 2020-06-24 15:26:06 +02:00
parent efab6e92f7
commit f64026af1a
15 changed files with 80 additions and 65 deletions

View file

@ -155,8 +155,8 @@ public class BukkitPlayer extends PlotPlayer<Player> {
}
@Override public boolean hasPermission(final String permission) {
if (this.offline && EconHandler.manager != null) {
return EconHandler.manager.hasPermission(getName(), permission);
if (this.offline && EconHandler.getEconHandler() != null) {
return EconHandler.getEconHandler().hasPermission(getName(), permission);
}
return this.player.hasPermission(permission);
}

View file

@ -48,28 +48,26 @@ public class BukkitEconHandler extends EconHandler {
return this.econ != null && this.perms != null;
}
private boolean setupPermissions() {
private void setupPermissions() {
RegisteredServiceProvider<Permission> permissionProvider =
Bukkit.getServer().getServicesManager().getRegistration(Permission.class);
if (permissionProvider != null) {
this.perms = permissionProvider.getProvider();
}
return this.perms != null;
}
private boolean setupEconomy() {
private void setupEconomy() {
if (Bukkit.getServer().getPluginManager().getPlugin("Vault") == null) {
return false;
return;
}
RegisteredServiceProvider<Economy> economyProvider =
Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
if (economyProvider != null) {
this.econ = economyProvider.getProvider();
}
return this.econ != null;
}
@Override public double getMoney(PlotPlayer player) {
@Override public double getMoney(PlotPlayer<?> player) {
double bal = super.getMoney(player);
if (Double.isNaN(bal)) {
return this.econ.getBalance(((BukkitPlayer) player).player);
@ -77,11 +75,11 @@ public class BukkitEconHandler extends EconHandler {
return bal;
}
@Override public void withdrawMoney(PlotPlayer player, double amount) {
@Override public void withdrawMoney(PlotPlayer<?> player, double amount) {
this.econ.withdrawPlayer(((BukkitPlayer) player).player, amount);
}
@Override public void depositMoney(PlotPlayer player, double amount) {
@Override public void depositMoney(PlotPlayer<?> player, double amount) {
this.econ.depositPlayer(((BukkitPlayer) player).player, amount);
}
@ -93,11 +91,11 @@ public class BukkitEconHandler extends EconHandler {
return this.perms.playerHas(world, Bukkit.getOfflinePlayer(player), perm);
}
@Override public double getBalance(PlotPlayer player) {
@Override public double getBalance(PlotPlayer<?> player) {
return this.econ.getBalance(player.getName());
}
public void setPermission(String world, String player, String perm, boolean value) {
@Deprecated public void setPermission(String world, String player, String perm, boolean value) {
if (value) {
this.perms.playerAdd(world, player, perm);
} else {

View file

@ -304,8 +304,7 @@ public class PlotSquared {
}
// Economy
if (Settings.Enabled_Components.ECONOMY) {
TaskManager
.runTask(() -> EconHandler.manager = PlotSquared.this.IMP.getEconomyHandler());
TaskManager.runTask(() -> EconHandler.initializeEconHandler());
}
if (Settings.Enabled_Components.COMPONENT_PRESETS) {

View file

@ -157,9 +157,9 @@ public class Auto extends SubCommand {
@Override public boolean onCommand(final PlotPlayer<?> player, String[] args) {
PlotArea plotarea = player.getApplicablePlotArea();
if (plotarea == null) {
if (EconHandler.manager != null) {
if (EconHandler.getEconHandler() != null) {
for (PlotArea area : PlotSquared.get().getPlotAreaManager().getAllPlotAreas()) {
if (EconHandler.manager
if (EconHandler.getEconHandler()
.hasPermission(area.getWorldName(), player.getName(), "plots.auto")) {
if (plotarea != null) {
plotarea = null;
@ -253,18 +253,18 @@ public class Auto extends SubCommand {
return true;
}
}
if (EconHandler.manager != null && plotarea.useEconomy()) {
if (EconHandler.getEconHandler() != null && plotarea.useEconomy()) {
Expression<Double> costExp = plotarea.getPrices().get("claim");
double cost = costExp.evaluate((double) (Settings.Limit.GLOBAL ?
player.getPlotCount() :
player.getPlotCount(plotarea.getWorldName())));
cost = (size_x * size_z) * cost;
if (cost > 0d) {
if (!force && EconHandler.manager.getMoney(player) < cost) {
if (!force && EconHandler.getEconHandler().getMoney(player) < cost) {
sendMessage(player, Captions.CANNOT_AFFORD_PLOT, "" + cost);
return true;
}
EconHandler.manager.withdrawMoney(player, cost);
EconHandler.getEconHandler().withdrawMoney(player, cost);
sendMessage(player, Captions.REMOVED_BALANCE, cost + "");
}
}

View file

@ -58,7 +58,7 @@ public class Buy extends Command {
RunnableVal3<Command, Runnable, Runnable> confirm,
final RunnableVal2<Command, CommandResult> whenDone) {
check(EconHandler.manager, Captions.ECON_DISABLED);
check(EconHandler.getEconHandler(), Captions.ECON_DISABLED);
final Plot plot;
if (args.length != 0) {
checkTrue(args.length == 1, Captions.COMMAND_SYNTAX, getUsage());
@ -82,7 +82,7 @@ public class Buy extends Command {
confirm.run(this, () -> {
Captions.REMOVED_BALANCE.send(player, price);
EconHandler.manager.depositMoney(PlotSquared.imp().getPlayerManager().getOfflinePlayer(plot.getOwnerAbs()), price);
EconHandler.getEconHandler().depositMoney(PlotSquared.imp().getPlayerManager().getOfflinePlayer(plot.getOwnerAbs()), price);
PlotPlayer owner = PlotSquared.imp().getPlayerManager().getPlayerIfExists(plot.getOwnerAbs());
if (owner != null) {

View file

@ -105,14 +105,14 @@ public class Claim extends SubCommand {
}
}
}
if ((EconHandler.manager != null) && area.useEconomy() && !force) {
if ((EconHandler.getEconHandler() != null) && area.useEconomy() && !force) {
Expression<Double> costExr = area.getPrices().get("claim");
double cost = costExr.evaluate((double) currentPlots);
if (cost > 0d) {
if (EconHandler.manager.getMoney(player) < cost) {
if (EconHandler.getEconHandler().getMoney(player) < cost) {
return sendMessage(player, Captions.CANNOT_AFFORD_PLOT, "" + cost);
}
EconHandler.manager.withdrawMoney(player, cost);
EconHandler.getEconHandler().withdrawMoney(player, cost);
sendMessage(player, Captions.REMOVED_BALANCE, cost + "");
}
}

View file

@ -166,7 +166,7 @@ public class DebugExec extends SubCommand {
this.scope.put("BlockManager", WorldUtil.IMP);
this.scope.put("SetupUtils", SetupUtils.manager);
this.scope.put("EventUtil", PlotSquared.get().getEventDispatcher());
this.scope.put("EconHandler", EconHandler.manager);
this.scope.put("EconHandler", EconHandler.getEconHandler());
this.scope.put("DBFunc", DBFunc.dbManager);
this.scope.put("HybridUtils", HybridUtils.manager);
this.scope.put("IMP", PlotSquared.get().IMP);

View file

@ -86,11 +86,11 @@ public class Delete extends SubCommand {
final long start = System.currentTimeMillis();
boolean result = plot.deletePlot(() -> {
plot.removeRunning();
if ((EconHandler.manager != null) && plotArea.useEconomy()) {
if ((EconHandler.getEconHandler() != null) && plotArea.useEconomy()) {
Expression<Double> valueExr = plotArea.getPrices().get("sell");
double value = plots.size() * valueExr.evaluate((double) currentPlots);
if (value > 0d) {
EconHandler.manager.depositMoney(player, value);
EconHandler.getEconHandler().depositMoney(player, value);
sendMessage(player, Captions.ADDED_BALANCE, String.valueOf(value));
}
}

View file

@ -71,7 +71,7 @@ public class ListCmd extends SubCommand {
private String[] getArgumentList(PlotPlayer player) {
List<String> args = new ArrayList<>();
if (EconHandler.manager != null && Permissions
if (EconHandler.getEconHandler() != null && Permissions
.hasPermission(player, Captions.PERMISSION_LIST_FOR_SALE)) {
args.add("forsale");
}
@ -264,7 +264,7 @@ public class ListCmd extends SubCommand {
Captions.PERMISSION_LIST_FOR_SALE);
return false;
}
if (EconHandler.manager == null) {
if (EconHandler.getEconHandler() == null) {
break;
}
plotConsumer.accept(PlotQuery.newQuery().allPlots().thatPasses(plot -> plot.getFlag(PriceFlag.class) > 0));
@ -404,7 +404,7 @@ public class ListCmd extends SubCommand {
@Override public Collection<Command> tab(PlotPlayer player, String[] args, boolean space) {
final List<String> completions = new LinkedList<>();
if (EconHandler.manager != null && Permissions
if (EconHandler.getEconHandler() != null && Permissions
.hasPermission(player, Captions.PERMISSION_LIST_FOR_SALE)) {
completions.add("forsale");
}

View file

@ -164,14 +164,14 @@ public class MainCommand extends Command {
public void run(final Command cmd, final Runnable success, final Runnable failure) {
if (cmd.hasConfirmation(player)) {
CmdConfirm.addPending(player, cmd.getUsage(), () -> {
if (EconHandler.manager != null) {
if (EconHandler.getEconHandler() != null) {
PlotArea area = player.getApplicablePlotArea();
if (area != null) {
Expression<Double> priceEval =
area.getPrices().get(cmd.getFullId());
Double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
if (price != null
&& EconHandler.manager.getMoney(player) < price) {
&& EconHandler.getEconHandler().getMoney(player) < price) {
if (failure != null) {
failure.run();
}
@ -185,12 +185,12 @@ public class MainCommand extends Command {
});
return;
}
if (EconHandler.manager != null) {
if (EconHandler.getEconHandler() != null) {
PlotArea area = player.getApplicablePlotArea();
if (area != null) {
Expression<Double> priceEval = area.getPrices().get(cmd.getFullId());
Double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
if (price != 0d && EconHandler.manager.getMoney(player) < price) {
if (price != 0d && EconHandler.getEconHandler().getMoney(player) < price) {
if (failure != null) {
failure.run();
}
@ -252,14 +252,14 @@ public class MainCommand extends Command {
if ("f".equals(args[0].substring(1))) {
confirm = new RunnableVal3<Command, Runnable, Runnable>() {
@Override public void run(Command cmd, Runnable success, Runnable failure) {
if (EconHandler.manager != null) {
if (EconHandler.getEconHandler() != null) {
PlotArea area = player.getApplicablePlotArea();
if (area != null) {
Expression<Double> priceEval =
area.getPrices().get(cmd.getFullId());
Double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
if (price != 0d
&& EconHandler.manager.getMoney(player) < price) {
&& EconHandler.getEconHandler().getMoney(player) < price) {
if (failure != null) {
failure.run();
}

View file

@ -156,8 +156,8 @@ public class Merge extends SubCommand {
return true;
}
if (plot.autoMerge(Direction.ALL, maxSize, uuid, terrain)) {
if (EconHandler.manager != null && plotArea.useEconomy() && price > 0d) {
EconHandler.manager.withdrawMoney(player, price);
if (EconHandler.getEconHandler() != null && plotArea.useEconomy() && price > 0d) {
EconHandler.getEconHandler().withdrawMoney(player, price);
sendMessage(player, Captions.REMOVED_BALANCE, String.valueOf(price));
}
MainUtil.sendMessage(player, Captions.SUCCESS_MERGE);
@ -174,8 +174,8 @@ public class Merge extends SubCommand {
uuid = plot.getOwnerAbs();
}
}
if (!force && EconHandler.manager != null && plotArea.useEconomy() && price > 0d
&& EconHandler.manager.getMoney(player) < price) {
if (!force && EconHandler.getEconHandler() != null && plotArea.useEconomy() && price > 0d
&& EconHandler.getEconHandler().getMoney(player) < price) {
sendMessage(player, Captions.CANNOT_AFFORD_MERGE, String.valueOf(price));
return false;
}
@ -192,8 +192,8 @@ public class Merge extends SubCommand {
return true;
}
if (plot.autoMerge(direction, maxSize - size, uuid, terrain)) {
if (EconHandler.manager != null && plotArea.useEconomy() && price > 0d) {
EconHandler.manager.withdrawMoney(player, price);
if (EconHandler.getEconHandler() != null && plotArea.useEconomy() && price > 0d) {
EconHandler.getEconHandler().withdrawMoney(player, price);
sendMessage(player, Captions.REMOVED_BALANCE, String.valueOf(price));
}
MainUtil.sendMessage(player, Captions.SUCCESS_MERGE);
@ -226,12 +226,12 @@ public class Merge extends SubCommand {
sendMessage(accepter, Captions.MERGE_NOT_VALID);
return;
}
if (EconHandler.manager != null && plotArea.useEconomy() && price > 0d) {
if (!force && EconHandler.manager.getMoney(player) < price) {
if (EconHandler.getEconHandler() != null && plotArea.useEconomy() && price > 0d) {
if (!force && EconHandler.getEconHandler().getMoney(player) < price) {
sendMessage(player, Captions.CANNOT_AFFORD_MERGE, String.valueOf(price));
return;
}
EconHandler.manager.withdrawMoney(player, price);
EconHandler.getEconHandler().withdrawMoney(player, price);
sendMessage(player, Captions.REMOVED_BALANCE, String.valueOf(price));
}
MainUtil.sendMessage(player, Captions.SUCCESS_MERGE);

View file

@ -170,12 +170,12 @@ public class ComponentPresetManager {
return false;
}
if (componentPreset.getCost() > 0.0D && EconHandler.manager != null && plot.getArea().useEconomy()) {
if (EconHandler.manager.getMoney(player) < componentPreset.getCost()) {
if (componentPreset.getCost() > 0.0D && EconHandler.getEconHandler() != null && plot.getArea().useEconomy()) {
if (EconHandler.getEconHandler().getMoney(player) < componentPreset.getCost()) {
Captions.PRESET_CANNOT_AFFORD.send(player);
return false;
} else {
EconHandler.manager.withdrawMoney(player, componentPreset.getCost());
EconHandler.getEconHandler().withdrawMoney(player, componentPreset.getCost());
Captions.REMOVED_BALANCE.send(player, componentPreset.getCost() + "");
}
}
@ -196,7 +196,7 @@ public class ComponentPresetManager {
for (int i = 0; i < allowedPresets.size(); i++) {
final ComponentPreset preset = allowedPresets.get(i);
final List<String> lore = new ArrayList<>();
if (preset.getCost() > 0 && EconHandler.manager != null && plot.getArea().useEconomy()){
if (preset.getCost() > 0 && EconHandler.getEconHandler() != null && plot.getArea().useEconomy()){
lore.add(Captions.PRESET_LORE_COST.getTranslated().replace("%cost%",
String.format("%.2f", preset.getCost())));
}

View file

@ -756,18 +756,18 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer
* The amount of money this Player has.
*/
public double getMoney() {
return EconHandler.manager == null ? 0 : EconHandler.manager.getMoney(this);
return EconHandler.getEconHandler() == null ? 0 : EconHandler.getEconHandler().getMoney(this);
}
public void withdraw(double amount) {
if (EconHandler.manager != null) {
EconHandler.manager.withdrawMoney(this, amount);
if (EconHandler.getEconHandler() != null) {
EconHandler.getEconHandler().withdrawMoney(this, amount);
}
}
public void deposit(double amount) {
if (EconHandler.manager != null) {
EconHandler.manager.depositMoney(this, amount);
if (EconHandler.getEconHandler() != null) {
EconHandler.getEconHandler().depositMoney(this, amount);
}
}

View file

@ -25,36 +25,52 @@
*/
package com.plotsquared.core.util;
import com.plotsquared.core.IPlotMain;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.player.ConsolePlayer;
import com.plotsquared.core.player.OfflinePlotPlayer;
import com.plotsquared.core.player.PlotPlayer;
import org.jetbrains.annotations.Nullable;
public abstract class EconHandler {
public static EconHandler manager;
private static boolean initialized;
/**
* @deprecated This will be made private in the future
*/
@Deprecated public static EconHandler manager;
public static EconHandler getEconHandler() {
if (initialized) {
return manager;
/**
* Initialize the economy handler using
* {@link IPlotMain#getEconomyHandler()}
*/
public static void initializeEconHandler() {
if (manager != null) {
return;
}
initialized = true;
return manager = PlotSquared.get().IMP.getEconomyHandler();
manager = PlotSquared.get().IMP.getEconomyHandler();
}
public double getMoney(PlotPlayer player) {
/**
* Return the econ handler instance, if one exists
*
* @return Economy handler instance
*/
@Nullable public static EconHandler getEconHandler() {
return manager;
}
public double getMoney(PlotPlayer<?> player) {
if (player instanceof ConsolePlayer) {
return Double.MAX_VALUE;
}
return getBalance(player);
}
public abstract double getBalance(PlotPlayer player);
public abstract double getBalance(PlotPlayer<?> player);
public abstract void withdrawMoney(PlotPlayer player, double amount);
public abstract void withdrawMoney(PlotPlayer<?> player, double amount);
public abstract void depositMoney(PlotPlayer player, double amount);
public abstract void depositMoney(PlotPlayer<?> player, double amount);
public abstract void depositMoney(OfflinePlotPlayer player, double amount);

View file

@ -76,6 +76,8 @@ subprojects {
delete("../target")
}
javadoc.options.encoding = 'UTF-8'
dependencies {
compile group: 'org.json', name: 'json', version: '20200518'