From f64026af1ae80cfb262b3425c43bd69c1c265a74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Wed, 24 Jun 2020 15:26:06 +0200 Subject: [PATCH] Only initialise EconHandler when economy is enabled and stop accessing the static instance directly. --- .../bukkit/player/BukkitPlayer.java | 4 +- .../bukkit/util/BukkitEconHandler.java | 18 ++++----- .../com/plotsquared/core/PlotSquared.java | 3 +- .../com/plotsquared/core/command/Auto.java | 10 ++--- .../com/plotsquared/core/command/Buy.java | 4 +- .../com/plotsquared/core/command/Claim.java | 6 +-- .../plotsquared/core/command/DebugExec.java | 2 +- .../com/plotsquared/core/command/Delete.java | 4 +- .../com/plotsquared/core/command/ListCmd.java | 6 +-- .../plotsquared/core/command/MainCommand.java | 12 +++--- .../com/plotsquared/core/command/Merge.java | 18 ++++----- .../components/ComponentPresetManager.java | 8 ++-- .../plotsquared/core/player/PlotPlayer.java | 10 ++--- .../plotsquared/core/util/EconHandler.java | 38 +++++++++++++------ build.gradle | 2 + 15 files changed, 80 insertions(+), 65 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java index acbc31984..643039600 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java @@ -155,8 +155,8 @@ public class BukkitPlayer extends PlotPlayer { } @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); } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEconHandler.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEconHandler.java index 1efd7fea5..b8387efed 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEconHandler.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEconHandler.java @@ -48,28 +48,26 @@ public class BukkitEconHandler extends EconHandler { return this.econ != null && this.perms != null; } - private boolean setupPermissions() { + private void setupPermissions() { RegisteredServiceProvider 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 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 { diff --git a/Core/src/main/java/com/plotsquared/core/PlotSquared.java b/Core/src/main/java/com/plotsquared/core/PlotSquared.java index 3f7f35229..30d738afc 100644 --- a/Core/src/main/java/com/plotsquared/core/PlotSquared.java +++ b/Core/src/main/java/com/plotsquared/core/PlotSquared.java @@ -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) { diff --git a/Core/src/main/java/com/plotsquared/core/command/Auto.java b/Core/src/main/java/com/plotsquared/core/command/Auto.java index 9658556d9..4c75de3f8 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Auto.java +++ b/Core/src/main/java/com/plotsquared/core/command/Auto.java @@ -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 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 + ""); } } diff --git a/Core/src/main/java/com/plotsquared/core/command/Buy.java b/Core/src/main/java/com/plotsquared/core/command/Buy.java index 036e2412f..337801385 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Buy.java +++ b/Core/src/main/java/com/plotsquared/core/command/Buy.java @@ -58,7 +58,7 @@ public class Buy extends Command { RunnableVal3 confirm, final RunnableVal2 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) { diff --git a/Core/src/main/java/com/plotsquared/core/command/Claim.java b/Core/src/main/java/com/plotsquared/core/command/Claim.java index 488b2dc84..481a25bf5 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Claim.java +++ b/Core/src/main/java/com/plotsquared/core/command/Claim.java @@ -105,14 +105,14 @@ public class Claim extends SubCommand { } } } - if ((EconHandler.manager != null) && area.useEconomy() && !force) { + if ((EconHandler.getEconHandler() != null) && area.useEconomy() && !force) { Expression 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 + ""); } } diff --git a/Core/src/main/java/com/plotsquared/core/command/DebugExec.java b/Core/src/main/java/com/plotsquared/core/command/DebugExec.java index 381582c57..3d6af68f1 100644 --- a/Core/src/main/java/com/plotsquared/core/command/DebugExec.java +++ b/Core/src/main/java/com/plotsquared/core/command/DebugExec.java @@ -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); diff --git a/Core/src/main/java/com/plotsquared/core/command/Delete.java b/Core/src/main/java/com/plotsquared/core/command/Delete.java index 6d8ea243e..5f8178e06 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Delete.java +++ b/Core/src/main/java/com/plotsquared/core/command/Delete.java @@ -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 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)); } } diff --git a/Core/src/main/java/com/plotsquared/core/command/ListCmd.java b/Core/src/main/java/com/plotsquared/core/command/ListCmd.java index 811eb88a7..a08de0cd0 100644 --- a/Core/src/main/java/com/plotsquared/core/command/ListCmd.java +++ b/Core/src/main/java/com/plotsquared/core/command/ListCmd.java @@ -71,7 +71,7 @@ public class ListCmd extends SubCommand { private String[] getArgumentList(PlotPlayer player) { List 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 tab(PlotPlayer player, String[] args, boolean space) { final List completions = new LinkedList<>(); - if (EconHandler.manager != null && Permissions + if (EconHandler.getEconHandler() != null && Permissions .hasPermission(player, Captions.PERMISSION_LIST_FOR_SALE)) { completions.add("forsale"); } diff --git a/Core/src/main/java/com/plotsquared/core/command/MainCommand.java b/Core/src/main/java/com/plotsquared/core/command/MainCommand.java index 6e1151c4c..a5c42b117 100644 --- a/Core/src/main/java/com/plotsquared/core/command/MainCommand.java +++ b/Core/src/main/java/com/plotsquared/core/command/MainCommand.java @@ -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 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 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() { @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 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(); } diff --git a/Core/src/main/java/com/plotsquared/core/command/Merge.java b/Core/src/main/java/com/plotsquared/core/command/Merge.java index 962b48163..33754c4b8 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Merge.java +++ b/Core/src/main/java/com/plotsquared/core/command/Merge.java @@ -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); diff --git a/Core/src/main/java/com/plotsquared/core/components/ComponentPresetManager.java b/Core/src/main/java/com/plotsquared/core/components/ComponentPresetManager.java index 80ae1deb4..fb10eff9b 100644 --- a/Core/src/main/java/com/plotsquared/core/components/ComponentPresetManager.java +++ b/Core/src/main/java/com/plotsquared/core/components/ComponentPresetManager.java @@ -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 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()))); } diff --git a/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java b/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java index a9af02b65..13efa6bfb 100644 --- a/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java +++ b/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java @@ -756,18 +756,18 @@ public abstract class PlotPlayer

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); } } diff --git a/Core/src/main/java/com/plotsquared/core/util/EconHandler.java b/Core/src/main/java/com/plotsquared/core/util/EconHandler.java index 15180e68e..bedbda228 100644 --- a/Core/src/main/java/com/plotsquared/core/util/EconHandler.java +++ b/Core/src/main/java/com/plotsquared/core/util/EconHandler.java @@ -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); diff --git a/build.gradle b/build.gradle index 635cd7ea4..c55d34847 100644 --- a/build.gradle +++ b/build.gradle @@ -76,6 +76,8 @@ subprojects { delete("../target") } + javadoc.options.encoding = 'UTF-8' + dependencies { compile group: 'org.json', name: 'json', version: '20200518'