From 551d1d9f1a2e41cbed2bb612f239997658503a09 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Sun, 23 Aug 2020 00:13:00 +0200 Subject: [PATCH] Add NullEconHandler to avoid verbose null checks --- .../plotsquared/bukkit/BukkitPlatform.java | 4 +- .../bukkit/inject/BukkitModule.java | 4 +- .../bukkit/player/BukkitPlayer.java | 15 ++-- .../bukkit/player/BukkitPlayerManager.java | 8 +-- .../bukkit/util/BukkitEconHandler.java | 26 +++++-- .../com/plotsquared/core/PlotPlatform.java | 2 +- .../com/plotsquared/core/command/Auto.java | 2 +- .../com/plotsquared/core/command/Buy.java | 16 +++-- .../com/plotsquared/core/command/Claim.java | 4 +- .../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 | 22 +++--- .../com/plotsquared/core/command/Merge.java | 10 +-- .../components/ComponentPresetManager.java | 6 +- .../core/generator/ClassicPlotWorld.java | 2 +- .../core/player/ConsolePlayer.java | 2 +- .../plotsquared/core/player/PlotPlayer.java | 30 +------- .../plotsquared/core/util/EconHandler.java | 72 +++++++++++++++++++ 19 files changed, 146 insertions(+), 91 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java index ba454af43..ab3b2074f 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java @@ -337,9 +337,7 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl TaskManager.runTask(() -> { this.getPermissionHandler().initialize(); final EconHandler econHandler = getInjector().getInstance(EconHandler.class); - if (econHandler != null) { - econHandler.init(); - } + econHandler.init(); }); } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/inject/BukkitModule.java b/Bukkit/src/main/java/com/plotsquared/bukkit/inject/BukkitModule.java index 64c1c9720..e9fb0d92f 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/inject/BukkitModule.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/inject/BukkitModule.java @@ -107,14 +107,14 @@ public class BukkitModule extends AbstractModule { install(new FactoryModuleBuilder().build(ChunkCoordinatorBuilderFactory.class)); } - @Provides @Singleton @Nullable EconHandler provideEconHandler() { + @Provides @Singleton @Nonnull EconHandler provideEconHandler() { if (Bukkit.getPluginManager().isPluginEnabled("Vault")) { try { return new BukkitEconHandler(); } catch (final Exception ignored) { } } - return null; + return EconHandler.nullEconHandler(); } } 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 4d6e36e44..82b245631 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java @@ -36,7 +36,6 @@ import com.plotsquared.core.permissions.PermissionHandler; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.PlotWeather; import com.plotsquared.core.plot.world.PlotAreaManager; -import com.plotsquared.core.util.EconHandler; import com.plotsquared.core.util.EventDispatcher; import com.plotsquared.core.util.MathMan; import com.sk89q.worldedit.bukkit.BukkitAdapter; @@ -58,7 +57,6 @@ import org.bukkit.plugin.RegisteredListener; import javax.annotation.Nonnegative; import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.util.Arrays; import java.util.Set; import java.util.UUID; @@ -81,19 +79,18 @@ public class BukkitPlayer extends PlotPlayer { * @param plotAreaManager PlotAreaManager instance * @param eventDispatcher EventDispatcher instance * @param player Bukkit player instance - * @param econHandler EconHandler instance * @param permissionHandler PermissionHandler instance */ public BukkitPlayer(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final EventDispatcher eventDispatcher, - @Nonnull final Player player, @Nullable final EconHandler econHandler, @Nonnull final PermissionHandler permissionHandler) { - this(plotAreaManager, eventDispatcher, player, false, econHandler, permissionHandler); + @Nonnull final Player player, @Nonnull final PermissionHandler permissionHandler) { + this(plotAreaManager, eventDispatcher, player, false, permissionHandler); } public BukkitPlayer(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final - EventDispatcher eventDispatcher, @Nonnull final Player player, - final boolean realPlayer, @Nullable final EconHandler econHandler, - @Nonnull final PermissionHandler permissionHandler) { - super(plotAreaManager, eventDispatcher, econHandler, permissionHandler); + EventDispatcher eventDispatcher, @Nonnull final Player player, + final boolean realPlayer, + @Nonnull final PermissionHandler permissionHandler) { + super(plotAreaManager, eventDispatcher, permissionHandler); this.player = player; this.setupPermissionProfile(); if (realPlayer) { diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayerManager.java b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayerManager.java index 48ea59b68..a84f4f178 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayerManager.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayerManager.java @@ -29,7 +29,6 @@ import com.google.inject.Inject; import com.google.inject.Singleton; import com.plotsquared.core.permissions.PermissionHandler; import com.plotsquared.core.plot.world.PlotAreaManager; -import com.plotsquared.core.util.EconHandler; import com.plotsquared.core.util.EventDispatcher; import com.plotsquared.core.util.PlayerManager; import org.bukkit.Bukkit; @@ -46,16 +45,13 @@ import java.util.UUID; private final PlotAreaManager plotAreaManager; private final EventDispatcher eventDispatcher; - private final EconHandler econHandler; private final PermissionHandler permissionHandler; @Inject public BukkitPlayerManager(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final EventDispatcher eventDispatcher, - @Nullable final EconHandler econHandler, @Nonnull final PermissionHandler permissionHandler) { this.plotAreaManager = plotAreaManager; this.eventDispatcher = eventDispatcher; - this.econHandler = econHandler; this.permissionHandler = permissionHandler; } @@ -66,7 +62,7 @@ import java.util.UUID; try { return getPlayer(object.getUniqueId()); } catch (final NoSuchPlayerException exception) { - return new BukkitPlayer(this.plotAreaManager, this.eventDispatcher, object, false, this.econHandler, this.permissionHandler); + return new BukkitPlayer(this.plotAreaManager, this.eventDispatcher, object, false, this.permissionHandler); } } @@ -75,7 +71,7 @@ import java.util.UUID; if (player == null || !player.isOnline()) { throw new NoSuchPlayerException(uuid); } - return new BukkitPlayer(this.plotAreaManager, this.eventDispatcher, player, this.econHandler, this.permissionHandler); + return new BukkitPlayer(this.plotAreaManager, this.eventDispatcher, player, this.permissionHandler); } @Nullable @Override public BukkitOfflinePlayer getOfflinePlayer(@Nullable final UUID uuid) { 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 0a24a2657..2f33928b8 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEconHandler.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEconHandler.java @@ -30,13 +30,13 @@ import com.plotsquared.bukkit.player.BukkitOfflinePlayer; import com.plotsquared.bukkit.player.BukkitPlayer; import com.plotsquared.core.player.OfflinePlotPlayer; import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.util.EconHandler; import net.milkbowl.vault.economy.Economy; import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.plugin.RegisteredServiceProvider; -import javax.annotation.Nullable; - @Singleton public class BukkitEconHandler extends EconHandler { private Economy econ; @@ -62,25 +62,39 @@ import javax.annotation.Nullable; @Override public double getMoney(PlotPlayer player) { double bal = super.getMoney(player); if (Double.isNaN(bal)) { - return this.econ.getBalance(((BukkitPlayer) player).player); + return this.econ.getBalance(getBukkitOfflinePlayer(player)); } return bal; } @Override public void withdrawMoney(PlotPlayer player, double amount) { - this.econ.withdrawPlayer(((BukkitPlayer) player).player, amount); + this.econ.withdrawPlayer(getBukkitOfflinePlayer(player), amount); } @Override public void depositMoney(PlotPlayer player, double amount) { - this.econ.depositPlayer(((BukkitPlayer) player).player, amount); + this.econ.depositPlayer(getBukkitOfflinePlayer(player), amount); } @Override public void depositMoney(OfflinePlotPlayer player, double amount) { this.econ.depositPlayer(((BukkitOfflinePlayer) player).player, amount); } + @Override + public boolean isEnabled(PlotArea plotArea) { + return plotArea.useEconomy(); + } + + @Override + public boolean isSupported() { + return true; + } + @Override public double getBalance(PlotPlayer player) { - return this.econ.getBalance(player.getName()); + return this.econ.getBalance(getBukkitOfflinePlayer(player)); + } + + private static OfflinePlayer getBukkitOfflinePlayer(PlotPlayer plotPlayer) { + return ((BukkitPlayer) plotPlayer).player; } } diff --git a/Core/src/main/java/com/plotsquared/core/PlotPlatform.java b/Core/src/main/java/com/plotsquared/core/PlotPlatform.java index 17c8c84fd..84bf76ec2 100644 --- a/Core/src/main/java/com/plotsquared/core/PlotPlatform.java +++ b/Core/src/main/java/com/plotsquared/core/PlotPlatform.java @@ -227,7 +227,7 @@ public interface PlotPlatform

extends LocaleHolder { * * * @return Econ handler */ - @Nullable default EconHandler getEconHandler() { + @Nonnull default EconHandler getEconHandler() { return getInjector().getInstance(EconHandler.class); } 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 467757d85..6cdbdf627 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Auto.java +++ b/Core/src/main/java/com/plotsquared/core/command/Auto.java @@ -75,7 +75,7 @@ public class Auto extends SubCommand { @Inject public Auto(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final EventDispatcher eventDispatcher, - @Nullable final EconHandler econHandler, + @Nonnull final EconHandler econHandler, @Nonnull final ServicePipeline servicePipeline) { this.plotAreaManager = plotAreaManager; this.eventDispatcher = eventDispatcher; 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 f49d0d238..a4326112a 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Buy.java +++ b/Core/src/main/java/com/plotsquared/core/command/Buy.java @@ -32,8 +32,11 @@ import com.plotsquared.core.events.PlotFlagRemoveEvent; import com.plotsquared.core.events.Result; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.plot.flag.PlotFlag; import com.plotsquared.core.plot.flag.implementations.PriceFlag; +import com.plotsquared.core.synchronization.LockKey; +import com.plotsquared.core.synchronization.LockRepository; import com.plotsquared.core.util.EconHandler; import com.plotsquared.core.util.EventDispatcher; import com.plotsquared.core.util.task.RunnableVal2; @@ -56,7 +59,7 @@ public class Buy extends Command { private final EconHandler econHandler; @Inject public Buy(@Nonnull final EventDispatcher eventDispatcher, - @Nullable final EconHandler econHandler) { + @Nonnull final EconHandler econHandler) { super(MainCommand.getInstance(), true); this.eventDispatcher = eventDispatcher; this.econHandler = econHandler; @@ -67,7 +70,9 @@ public class Buy extends Command { RunnableVal3 confirm, final RunnableVal2 whenDone) { - check(this.econHandler, TranslatableCaption.of("economy.econ_disabled")); + PlotArea area = player.getPlotAreaAbs(); + check(area, TranslatableCaption.of("errors.not_in_plot_world")); + check(this.econHandler.isEnabled(area), TranslatableCaption.of("economy.econ_disabled")); final Plot plot; if (args.length != 0) { if (args.length != 1) { @@ -87,8 +92,9 @@ public class Buy extends Command { if (price <= 0) { throw new CommandException(TranslatableCaption.of("economy.not_for_sale")); } - checkTrue(player.getMoney() >= price, TranslatableCaption.of("economy.cannot_afford_plot")); - player.withdraw(price); + checkTrue(this.econHandler.getMoney(player) >= price, + TranslatableCaption.of("economy.cannot_afford_plot")); + this.econHandler.withdrawMoney(player, price); // Failure // Success confirm.run(this, () -> { @@ -117,7 +123,7 @@ public class Buy extends Command { player.sendMessage(TranslatableCaption.of("working.claimed")); whenDone.run(Buy.this, CommandResult.SUCCESS); }, () -> { - player.deposit(price); + this.econHandler.depositMoney(player, price); whenDone.run(Buy.this, CommandResult.FAILURE); }); return CompletableFuture.completedFuture(true); 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 1906c1713..50c6ebb6f 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Claim.java +++ b/Core/src/main/java/com/plotsquared/core/command/Claim.java @@ -67,7 +67,7 @@ public class Claim extends SubCommand { private final EconHandler econHandler; @Inject public Claim(@Nonnull final EventDispatcher eventDispatcher, - @Nullable final EconHandler econHandler) { + @Nonnull final EconHandler econHandler) { this.eventDispatcher = eventDispatcher; this.econHandler = econHandler; } @@ -133,7 +133,7 @@ public class Claim extends SubCommand { } } } - if ((this.econHandler != null) && area.useEconomy() && !force) { + if (this.econHandler.isEnabled(area) && !force) { Expression costExr = area.getPrices().get("claim"); double cost = costExr.evaluate((double) currentPlots); if (cost > 0d) { 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 5d0ffbc37..c7fff2776 100644 --- a/Core/src/main/java/com/plotsquared/core/command/DebugExec.java +++ b/Core/src/main/java/com/plotsquared/core/command/DebugExec.java @@ -114,7 +114,7 @@ public class DebugExec extends SubCommand { @Nullable final WorldEdit worldEdit, @Nonnull final GlobalBlockQueue blockQueue, @Nonnull final SchematicHandler schematicHandler, - @Nullable final EconHandler econHandler, + @Nonnull final EconHandler econHandler, @Nonnull final ChunkManager chunkManager, @Nonnull final WorldUtil worldUtil, @Nonnull final SetupUtils setupUtils, 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 be4774982..8bd023ef8 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Delete.java +++ b/Core/src/main/java/com/plotsquared/core/command/Delete.java @@ -58,7 +58,7 @@ public class Delete extends SubCommand { private final EconHandler econHandler; @Inject public Delete(@Nonnull final EventDispatcher eventDispatcher, - @Nullable final EconHandler econHandler) { + @Nonnull final EconHandler econHandler) { this.eventDispatcher = eventDispatcher; this.econHandler = econHandler; } @@ -100,7 +100,7 @@ public class Delete extends SubCommand { final long start = System.currentTimeMillis(); boolean result = plot.getPlotModificationManager().deletePlot(() -> { plot.removeRunning(); - if ((this.econHandler != null) && plotArea.useEconomy()) { + if (this.econHandler.isEnabled(plotArea)) { Expression valueExr = plotArea.getPrices().get("sell"); double value = plots.size() * valueExr.evaluate((double) currentPlots); if (value > 0d) { 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 d01e3ea3e..a396b010e 100644 --- a/Core/src/main/java/com/plotsquared/core/command/ListCmd.java +++ b/Core/src/main/java/com/plotsquared/core/command/ListCmd.java @@ -81,7 +81,7 @@ public class ListCmd extends SubCommand { private final EconHandler econHandler; @Inject public ListCmd(@Nonnull final PlotAreaManager plotAreaManager, - @Nullable final EconHandler econHandler) { + @Nonnull final EconHandler econHandler) { this.plotAreaManager = plotAreaManager; this.econHandler = econHandler; } @@ -279,7 +279,7 @@ public class ListCmd extends SubCommand { Templates.of("node", "plots.list.forsale")); return false; } - if (this.econHandler == null) { + if (this.econHandler.isSupported()) { break; } plotConsumer.accept(PlotQuery.newQuery().allPlots().thatPasses(plot -> plot.getFlag(PriceFlag.class) > 0)); @@ -429,7 +429,7 @@ public class ListCmd extends SubCommand { @Override public Collection tab(PlotPlayer player, String[] args, boolean space) { final List completions = new LinkedList<>(); - if (this.econHandler != null && Permissions + if (this.econHandler.isSupported() && Permissions .hasPermission(player, Permission.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 daf747bbc..b90588562 100644 --- a/Core/src/main/java/com/plotsquared/core/command/MainCommand.java +++ b/Core/src/main/java/com/plotsquared/core/command/MainCommand.java @@ -186,19 +186,17 @@ 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 != 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.getMoney(player) < price) { - if (failure != null) { - failure.run(); - } - return; + PlotArea area = player.getApplicablePlotArea(); + if (area != null && econHandler.isEnabled(area)) { + Expression priceEval = + area.getPrices().get(cmd.getFullId()); + Double price = priceEval != null ? priceEval.evaluate(0d) : 0d; + if (price != null + && econHandler.getMoney(player) < price) { + if (failure != null) { + failure.run(); } + return; } } if (success != null) { 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 12d0a5ec6..cbc59fc8f 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Merge.java +++ b/Core/src/main/java/com/plotsquared/core/command/Merge.java @@ -64,7 +64,7 @@ public class Merge extends SubCommand { private final EconHandler econHandler; @Inject public Merge(@Nonnull final EventDispatcher eventDispatcher, - @Nullable final EconHandler econHandler) { + @Nonnull final EconHandler econHandler) { this.eventDispatcher = eventDispatcher; this.econHandler = econHandler; } @@ -181,7 +181,7 @@ public class Merge extends SubCommand { return true; } if (plot.getPlotModificationManager().autoMerge(Direction.ALL, maxSize, uuid, terrain)) { - if (this.econHandler != null && plotArea.useEconomy() && price > 0d) { + if (this.econHandler.isEnabled(plotArea) && price > 0d) { this.econHandler.withdrawMoney(player, price); player.sendMessage( TranslatableCaption.of("economy.removed_balance"), @@ -202,7 +202,7 @@ public class Merge extends SubCommand { uuid = plot.getOwnerAbs(); } } - if (!force && this.econHandler != null && plotArea.useEconomy() && price > 0d + if (!force && this.econHandler.isEnabled(plotArea) && price > 0d && this.econHandler.getMoney(player) < price) { player.sendMessage( TranslatableCaption.of("economy.cannot_afford_merge"), @@ -225,7 +225,7 @@ public class Merge extends SubCommand { return true; } if (plot.getPlotModificationManager().autoMerge(direction, maxSize - size, uuid, terrain)) { - if (this.econHandler != null && plotArea.useEconomy() && price > 0d) { + if (this.econHandler.isEnabled(plotArea) && price > 0d) { this.econHandler.withdrawMoney(player, price); player.sendMessage( TranslatableCaption.of("economy.removed_balance"), @@ -265,7 +265,7 @@ public class Merge extends SubCommand { accepter.sendMessage(TranslatableCaption.of("merge.merge_not_valid")); return; } - if (this.econHandler != null && plotArea.useEconomy() && price > 0d) { + if (this.econHandler.isEnabled(plotArea) && price > 0d) { if (!force && this.econHandler.getMoney(player) < price) { player.sendMessage( TranslatableCaption.of("economy.cannot_afford_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 9143257cf..e775892da 100644 --- a/Core/src/main/java/com/plotsquared/core/components/ComponentPresetManager.java +++ b/Core/src/main/java/com/plotsquared/core/components/ComponentPresetManager.java @@ -71,7 +71,7 @@ public class ComponentPresetManager { private final EconHandler econHandler; private final InventoryUtil inventoryUtil; - @Inject public ComponentPresetManager(@Nullable final EconHandler econHandler, @Nonnull final InventoryUtil inventoryUtil) { + @Inject public ComponentPresetManager(@Nonnull final EconHandler econHandler, @Nonnull final InventoryUtil inventoryUtil) { this.econHandler = econHandler; this.inventoryUtil = inventoryUtil; final File file = new File(Objects.requireNonNull(PlotSquared.platform()).getDirectory(), "components.yml"); @@ -179,7 +179,7 @@ public class ComponentPresetManager { return false; } - if (componentPreset.getCost() > 0.0D && econHandler != null && plot.getArea().useEconomy()) { + if (componentPreset.getCost() > 0.0D && econHandler.isEnabled(plot.getArea())) { if (econHandler.getMoney(getPlayer()) < componentPreset.getCost()) { getPlayer().sendMessage(TranslatableCaption.of("preset.preset_cannot_afford")); return false; @@ -208,7 +208,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 && this.econHandler != null && plot.getArea().useEconomy()) { + if (preset.getCost() > 0 && this.econHandler.isEnabled(plot.getArea())) { lore.add(MINI_MESSAGE.serialize(MINI_MESSAGE.parse(TranslatableCaption.of("preset.preset_lore_cost").getComponent(player), Template.of("cost", String.format("%.2f", preset.getCost()))))); } diff --git a/Core/src/main/java/com/plotsquared/core/generator/ClassicPlotWorld.java b/Core/src/main/java/com/plotsquared/core/generator/ClassicPlotWorld.java index 2025661d6..5f30b424e 100644 --- a/Core/src/main/java/com/plotsquared/core/generator/ClassicPlotWorld.java +++ b/Core/src/main/java/com/plotsquared/core/generator/ClassicPlotWorld.java @@ -67,7 +67,7 @@ public abstract class ClassicPlotWorld extends SquarePlotWorld { @Nonnull final PlotId max, @WorldConfig @Nonnull final YamlConfiguration worldConfiguration, @Nonnull final GlobalBlockQueue blockQueue, - @Nullable final EconHandler econHandler) { + @Nonnull final EconHandler econHandler) { super(worldName, id, generator, min, max, worldConfiguration, blockQueue, econHandler); } diff --git a/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java b/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java index 2002bba37..a4a49a7fa 100644 --- a/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java +++ b/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java @@ -70,7 +70,7 @@ public class ConsolePlayer extends PlotPlayer { @ConsoleActor @Nonnull final Actor actor, @Nullable final EconHandler econHandler, @Nonnull final PermissionHandler permissionHandler) { - super(plotAreaManager, eventDispatcher, econHandler, permissionHandler); + super(plotAreaManager, eventDispatcher, permissionHandler); this.actor = actor; this.setupPermissionProfile(); final PlotArea[] areas = plotAreaManager.getAllPlotAreas(); 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 172e6e2a8..97f0bff81 100644 --- a/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java +++ b/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java @@ -55,7 +55,6 @@ import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.plot.world.SinglePlotArea; import com.plotsquared.core.plot.world.SinglePlotAreaManager; import com.plotsquared.core.synchronization.LockRepository; -import com.plotsquared.core.util.EconHandler; import com.plotsquared.core.util.EventDispatcher; import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.query.PlotQuery; @@ -116,16 +115,14 @@ public abstract class PlotPlayer

implements CommandCaller, OfflinePlotPlayer, private final PlotAreaManager plotAreaManager; private final EventDispatcher eventDispatcher; - private final EconHandler econHandler; private final PermissionHandler permissionHandler; // Delayed initialisation private PermissionProfile permissionProfile; - public PlotPlayer(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final EventDispatcher eventDispatcher, @Nullable final EconHandler econHandler, - @Nonnull final PermissionHandler permissionHandler) { + public PlotPlayer(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final EventDispatcher eventDispatcher, + @Nonnull final PermissionHandler permissionHandler) { this.plotAreaManager = plotAreaManager; this.eventDispatcher = eventDispatcher; - this.econHandler = econHandler; this.permissionHandler = permissionHandler; } @@ -905,29 +902,6 @@ public abstract class PlotPlayer

implements CommandCaller, OfflinePlotPlayer, */ @Nonnull public abstract Audience getAudience(); - /** - * The amount of money this Player has. - * - * @return amount of money owned by the player - */ - public double getMoney() { - return this.econHandler == null ? - 0 : - this.econHandler.getMoney(this); - } - - public void withdraw(double amount) { - if (this.econHandler != null) { - this.econHandler.withdrawMoney(this, amount); - } - } - - public void deposit(double amount) { - if (this.econHandler != null) { - this.econHandler.depositMoney(this, amount); - } - } - /** * Get this player's {@link LockRepository} * 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 4547dae06..0a30583dd 100644 --- a/Core/src/main/java/com/plotsquared/core/util/EconHandler.java +++ b/Core/src/main/java/com/plotsquared/core/util/EconHandler.java @@ -28,9 +28,26 @@ package com.plotsquared.core.util; import com.plotsquared.core.player.ConsolePlayer; import com.plotsquared.core.player.OfflinePlotPlayer; import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.PlotArea; +import com.sk89q.worldedit.EditSession; public abstract class EconHandler { + /** + * Returns an econ handler that: + *

+ * @return A null econ handler + */ + public static EconHandler nullEconHandler() { + return new NullEconHandler(); + } + public abstract boolean init(); public double getMoney(PlotPlayer player) { @@ -48,4 +65,59 @@ public abstract class EconHandler { public abstract void depositMoney(OfflinePlotPlayer player, double amount); + /** + * Returns whether economy is enabled in the given plot area or not. + * Implementations should only return true if {@link #isSupported()} returns + * true too. + * + * @param plotArea the plot area to check + * @return {@code true} if economy is enabled on the given plot area, {@code false} otherwise. + */ + public abstract boolean isEnabled(PlotArea plotArea); + + /** + * Returns whether economy is supported by the server or not. + * + * @return {@code true} if economy is supported, {@code false} otherwise. + */ + public abstract boolean isSupported(); + + private static final class NullEconHandler extends EconHandler { + + @Override + public boolean init() { + return false; + } + + @Override + public double getBalance(PlotPlayer player) { + return Double.MIN_VALUE; + } + + @Override + public void withdrawMoney(PlotPlayer player, double amount) { + + } + + @Override + public void depositMoney(PlotPlayer player, double amount) { + + } + + @Override + public void depositMoney(OfflinePlotPlayer player, double amount) { + + } + + @Override + public boolean isEnabled(PlotArea plotArea) { + return false; + } + + @Override + public boolean isSupported() { + return false; + } + } + }