Add NullEconHandler to avoid verbose null checks

This commit is contained in:
Hannes Greule 2020-08-23 00:13:00 +02:00 committed by Alexander Söderberg
parent a5dea9e7f6
commit 551d1d9f1a
19 changed files with 146 additions and 91 deletions

View File

@ -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();
}
});
}

View File

@ -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();
}
}

View File

@ -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<Player> {
* @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,
final boolean realPlayer,
@Nonnull final PermissionHandler permissionHandler) {
super(plotAreaManager, eventDispatcher, econHandler, permissionHandler);
super(plotAreaManager, eventDispatcher, permissionHandler);
this.player = player;
this.setupPermissionProfile();
if (realPlayer) {

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -227,7 +227,7 @@ public interface PlotPlatform<P> extends LocaleHolder {
* *
* @return Econ handler
*/
@Nullable default EconHandler getEconHandler() {
@Nonnull default EconHandler getEconHandler() {
return getInjector().getInstance(EconHandler.class);
}

View File

@ -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;

View File

@ -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<Command, Runnable, Runnable> confirm,
final RunnableVal2<Command, CommandResult> 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);

View File

@ -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<Double> costExr = area.getPrices().get("claim");
double cost = costExr.evaluate((double) currentPlots);
if (cost > 0d) {

View File

@ -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,

View File

@ -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<Double> valueExr = plotArea.getPrices().get("sell");
double value = plots.size() * valueExr.evaluate((double) currentPlots);
if (value > 0d) {

View File

@ -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<Command> tab(PlotPlayer<?> player, String[] args, boolean space) {
final List<String> completions = new LinkedList<>();
if (this.econHandler != null && Permissions
if (this.econHandler.isSupported() && Permissions
.hasPermission(player, Permission.PERMISSION_LIST_FOR_SALE)) {
completions.add("forsale");
}

View File

@ -186,9 +186,8 @@ 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) {
if (area != null && econHandler.isEnabled(area)) {
Expression<Double> priceEval =
area.getPrices().get(cmd.getFullId());
Double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
@ -200,7 +199,6 @@ public class MainCommand extends Command {
return;
}
}
}
if (success != null) {
success.run();
}

View File

@ -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"),

View File

@ -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<String> 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())))));
}

View File

@ -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);
}

View File

@ -70,7 +70,7 @@ public class ConsolePlayer extends PlotPlayer<Actor> {
@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();

View File

@ -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<P> 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,
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<P> 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}
*

View File

@ -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:
* <ul>
* <li>Returns {@code false} on {@link #isEnabled(PlotArea)}</li>
* <li>Returns {@link Double#MIN_VALUE} on {@link #getBalance(PlotPlayer)}</li>
* <li>Doesn't do anything for {@link #withdrawMoney(PlotPlayer, double)},
* {@link #depositMoney(OfflinePlotPlayer, double)}
* {@link #depositMoney(PlotPlayer, double)}</li>
* </ul>
* @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;
}
}
}