mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2024-11-21 20:56:45 +01:00
Add events for plot buying (#4291)
* cancelable event results are nullable * chore: add javadocs to CancellablePlotEvent# * feat: events for plot buy process
This commit is contained in:
parent
ae941e67a4
commit
951f08bc8b
@ -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);
|
||||
|
@ -18,17 +18,34 @@
|
||||
*/
|
||||
package com.plotsquared.core.events;
|
||||
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
/**
|
||||
* PlotSquared event with {@link Result} to cancel, force, or allow.
|
||||
*/
|
||||
public interface CancellablePlotEvent {
|
||||
|
||||
Result getEventResult();
|
||||
/**
|
||||
* The currently set {@link Result} for this event (as set by potential previous event listeners).
|
||||
*
|
||||
* @return the current result.
|
||||
*/
|
||||
@Nullable Result getEventResult();
|
||||
|
||||
void setEventResult(Result eventResult);
|
||||
/**
|
||||
* Set the {@link Result} for this event.
|
||||
*
|
||||
* @param eventResult the new result.
|
||||
*/
|
||||
void setEventResult(@Nullable Result eventResult);
|
||||
|
||||
/**
|
||||
* @deprecated No usage and not null-safe
|
||||
*/
|
||||
@Deprecated(since = "TODO")
|
||||
default int getEventResultRaw() {
|
||||
return getEventResult().getValue();
|
||||
return getEventResult() != null ? getEventResult().getValue() : -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* PlotSquared, a land and world management plugin for Minecraft.
|
||||
* Copyright (C) IntellectualSites <https://intellectualsites.com>
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* PlotSquared, a land and world management plugin for Minecraft.
|
||||
* Copyright (C) IntellectualSites <https://intellectualsites.com>
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -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
|
||||
|
@ -125,6 +125,7 @@
|
||||
"economy.added_balance": "<prefix><gold><money> </gold><gray>has been added to your balance.</gray>",
|
||||
"economy.removed_balance": "<prefix><gold><money> </gold><gray>has been taken from your balance.</gray>",
|
||||
"economy.removed_granted_plot": "<prefix><gray>You used <used_grants> plot grant(s), you've got </gray><gold><remaining_grants></gold> <gray>left.</gray>",
|
||||
"economy.cannot_buy_blocked": "<prefix><red>You are not allowed to buy this plot.</red>",
|
||||
"setup.choose_generator": "<gold>What generator do you want?</gold>",
|
||||
"setup.setup_not_started": "<prefix><gold>No setup started.</gold>",
|
||||
"setup.setup_init": "<prefix><gold>Usage: </gold><gray>/plot setup <value></gray>",
|
||||
|
Loading…
Reference in New Issue
Block a user