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 49222b66d..020d5de6a 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Buy.java +++ b/Core/src/main/java/com/plotsquared/core/command/Buy.java @@ -21,8 +21,9 @@ package com.plotsquared.core.command; import com.google.inject.Inject; import com.plotsquared.core.PlotSquared; import com.plotsquared.core.configuration.caption.TranslatableCaption; -import com.plotsquared.core.events.PlotFlagRemoveEvent; +import com.plotsquared.core.events.PlayerBuyPlotEvent; import com.plotsquared.core.events.Result; +import com.plotsquared.core.player.OfflinePlotPlayer; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.PlotArea; @@ -88,22 +89,30 @@ public class Buy extends Command { TranslatableCaption.of("permission.cant_claim_more_plots"), TagResolver.resolver("amount", Tag.inserting(Component.text(player.getAllowedPlots()))) ); - double price = plot.getFlag(PriceFlag.class); - if (price <= 0) { + double priceFlag = plot.getFlag(PriceFlag.class); + if (priceFlag <= 0) { throw new CommandException(TranslatableCaption.of("economy.not_for_sale")); } checkTrue( this.econHandler.isSupported(), TranslatableCaption.of("economy.vault_or_consumer_null") ); - checkTrue( - this.econHandler.getMoney(player) >= price, - TranslatableCaption.of("economy.cannot_afford_plot"), - TagResolver.builder() - .tag("money", Tag.inserting(Component.text(this.econHandler.format(price)))) - .tag("balance", Tag.inserting(Component.text(this.econHandler.format(this.econHandler.getMoney(player))))) - .build() - ); + + PlayerBuyPlotEvent event = this.eventDispatcher.callPlayerBuyPlot(player, plot, priceFlag); + if (event.getEventResult() == Result.DENY) { + throw new CommandException(TranslatableCaption.of("economy.cannot_buy_blocked")); + } + + double price = event.getEventResult() == Result.FORCE ? 0 : event.price(); + if (this.econHandler.getMoney(player) < price) { + throw new CommandException( + TranslatableCaption.of("economy.cannot_afford_plot"), + TagResolver.builder() + .tag("money", Tag.inserting(Component.text(this.econHandler.format(price)))) + .tag("balance", Tag.inserting(Component.text(this.econHandler.format(this.econHandler.getMoney(player))))) + .build() + ); + } this.econHandler.withdrawMoney(player, price); // Failure // Success @@ -113,7 +122,8 @@ public class Buy extends Command { TagResolver.resolver("money", Tag.inserting(Component.text(this.econHandler.format(price)))) ); - this.econHandler.depositMoney(PlotSquared.platform().playerManager().getOfflinePlayer(plot.getOwnerAbs()), price); + OfflinePlotPlayer previousOwner = PlotSquared.platform().playerManager().getOfflinePlayer(plot.getOwnerAbs()); + this.econHandler.depositMoney(previousOwner, price); PlotPlayer owner = PlotSquared.platform().playerManager().getPlayerIfExists(plot.getOwnerAbs()); if (owner != null) { @@ -127,9 +137,8 @@ public class Buy extends Command { ); } PlotFlag plotFlag = plot.getFlagContainer().getFlag(PriceFlag.class); - PlotFlagRemoveEvent event = this.eventDispatcher.callFlagRemove(plotFlag, plot); - if (event.getEventResult() != Result.DENY) { - plot.removeFlag(event.getFlag()); + if (this.eventDispatcher.callFlagRemove(plotFlag, plot).getEventResult() != Result.DENY) { + plot.removeFlag(plotFlag); } plot.setOwner(player.getUUID()); plot.getPlotModificationManager().setSign(player.getName()); @@ -137,6 +146,7 @@ public class Buy extends Command { TranslatableCaption.of("working.claimed"), TagResolver.resolver("plot", Tag.inserting(Component.text(plot.getId().toString()))) ); + this.eventDispatcher.callPostPlayerBuyPlot(player, previousOwner, plot, price); whenDone.run(Buy.this, CommandResult.SUCCESS); }, () -> { this.econHandler.depositMoney(player, price); diff --git a/Core/src/main/java/com/plotsquared/core/events/PlayerBuyPlotEvent.java b/Core/src/main/java/com/plotsquared/core/events/PlayerBuyPlotEvent.java new file mode 100644 index 000000000..ecc46b3fb --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/events/PlayerBuyPlotEvent.java @@ -0,0 +1,88 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * Copyright (C) IntellectualSites team and contributors + * + * 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 . + */ +package com.plotsquared.core.events; + +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import org.checkerframework.checker.index.qual.NonNegative; +import org.checkerframework.checker.nullness.qual.Nullable; + +/** + * Called when a user attempts to buy a plot. + *

+ * Setting the {@link #setEventResult(Result) Result} to {@link Result#FORCE} ignores the price and players account balance and does not charge the + * player anything. {@link Result#DENY} blocks the purchase completely, {@link Result#ACCEPT} and {@code null} do not modify + * the behaviour. + *

+ * Setting the {@link #setPrice(double) price} to {@code 0} makes the plot practically free. + * + * @since TODO + */ +public class PlayerBuyPlotEvent extends PlotPlayerEvent implements CancellablePlotEvent { + + private Result result; + private double price; + + public PlayerBuyPlotEvent(final PlotPlayer plotPlayer, final Plot plot, @NonNegative final double price) { + super(plotPlayer, plot); + this.price = price; + } + + + /** + * Sets the price required to buy the plot. + * + * @param price the new price. + * @since TODO + */ + public void setPrice(@NonNegative final double price) { + //noinspection ConstantValue - the annotation does not ensure a non-negative runtime value + if (price < 0) { + throw new IllegalArgumentException("price must be non-negative"); + } + this.price = price; + } + + /** + * Returns the currently set price required to buy the plot. + * + * @return the price. + * @since TODO + */ + public @NonNegative double price() { + return price; + } + + /** + * {@inheritDoc} + */ + @Override + public void setEventResult(@Nullable final Result eventResult) { + this.result = eventResult; + } + + /** + * {@inheritDoc} + */ + @Override + public @Nullable Result getEventResult() { + return this.result; + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/events/post/PostPlayerBuyPlotEvent.java b/Core/src/main/java/com/plotsquared/core/events/post/PostPlayerBuyPlotEvent.java new file mode 100644 index 000000000..e9d6f17bb --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/events/post/PostPlayerBuyPlotEvent.java @@ -0,0 +1,64 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * Copyright (C) IntellectualSites team and contributors + * + * 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 . + */ +package com.plotsquared.core.events.post; + +import com.plotsquared.core.events.PlotPlayerEvent; +import com.plotsquared.core.player.OfflinePlotPlayer; +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import org.checkerframework.checker.index.qual.NonNegative; + +/** + * Called after a player has successfully bought a plot. + * + * @since TODO + */ +public class PostPlayerBuyPlotEvent extends PlotPlayerEvent { + + private final OfflinePlotPlayer previousOwner; + private final double price; + + public PostPlayerBuyPlotEvent( + final PlotPlayer plotPlayer, final OfflinePlotPlayer previousOwner, final Plot plot, + @NonNegative final double price + ) { + super(plotPlayer, plot); + this.previousOwner = previousOwner; + this.price = price; + } + + /** + * The previous owner of the bought plot. + * + * @return the previous owner. + */ + public OfflinePlotPlayer previousOwner() { + return previousOwner; + } + + /** + * Returns the price after potential modifications by {@link com.plotsquared.core.events.PlayerBuyPlotEvent}. + * + * @return the price the player had to pay to buy the plot. + */ + public double price() { + return price; + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java index e71d17443..a01e72564 100644 --- a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java +++ b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java @@ -25,6 +25,7 @@ import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.events.PlayerAutoPlotEvent; import com.plotsquared.core.events.PlayerAutoPlotsChosenEvent; +import com.plotsquared.core.events.PlayerBuyPlotEvent; import com.plotsquared.core.events.PlayerClaimPlotEvent; import com.plotsquared.core.events.PlayerEnterPlotEvent; import com.plotsquared.core.events.PlayerLeavePlotEvent; @@ -49,6 +50,7 @@ import com.plotsquared.core.events.PlotUnlinkEvent; import com.plotsquared.core.events.RemoveRoadEntityEvent; import com.plotsquared.core.events.TeleportCause; import com.plotsquared.core.events.post.PostPlayerAutoPlotEvent; +import com.plotsquared.core.events.post.PostPlayerBuyPlotEvent; import com.plotsquared.core.events.post.PostPlotChangeOwnerEvent; import com.plotsquared.core.events.post.PostPlotDeleteEvent; import com.plotsquared.core.events.post.PostPlotMergeEvent; @@ -57,6 +59,7 @@ import com.plotsquared.core.listener.PlayerBlockEventType; import com.plotsquared.core.location.Direction; import com.plotsquared.core.location.Location; import com.plotsquared.core.permissions.Permission; +import com.plotsquared.core.player.OfflinePlotPlayer; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.PlotArea; @@ -315,6 +318,17 @@ public class EventDispatcher { return event; } + public PlayerBuyPlotEvent callPlayerBuyPlot(PlotPlayer player, Plot plot, double price) { + PlayerBuyPlotEvent event = new PlayerBuyPlotEvent(player, plot, price); + eventBus.post(event); + return event; + } + + public void callPostPlayerBuyPlot(PlotPlayer player, OfflinePlotPlayer previousOwner, Plot plot, + double price) { + eventBus.post(new PostPlayerBuyPlotEvent(player, previousOwner, plot, price)); + } + public void doJoinTask(final PlotPlayer player) { if (player == null) { return; //possible future warning message to figure out where we are retrieving null diff --git a/Core/src/main/resources/lang/messages_en.json b/Core/src/main/resources/lang/messages_en.json index 1887fbd7e..2e9f85b6d 100644 --- a/Core/src/main/resources/lang/messages_en.json +++ b/Core/src/main/resources/lang/messages_en.json @@ -125,6 +125,7 @@ "economy.added_balance": " has been added to your balance.", "economy.removed_balance": " has been taken from your balance.", "economy.removed_granted_plot": "You used plot grant(s), you've got left.", + "economy.cannot_buy_blocked": "You are not allowed to buy this plot.", "setup.choose_generator": "What generator do you want?", "setup.setup_not_started": "No setup started.", "setup.setup_init": "Usage: /plot setup ",