001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.events;
020
021import com.plotsquared.core.player.PlotPlayer;
022import com.plotsquared.core.plot.Plot;
023import org.checkerframework.checker.index.qual.NonNegative;
024import org.checkerframework.checker.nullness.qual.Nullable;
025
026/**
027 * Called when a user attempts to buy a plot.
028 * <p>
029 * Setting the {@link #setEventResult(Result) Result} to {@link Result#FORCE} ignores the price and players account balance and does not charge the
030 * player anything. {@link Result#DENY} blocks the purchase completely, {@link Result#ACCEPT} and {@code null} do not modify
031 * the behaviour.
032 * <p>
033 * Setting the {@link #setPrice(double) price} to {@code 0} makes the plot practically free.
034 *
035 * @since 7.3.2
036 */
037public class PlayerBuyPlotEvent extends PlotPlayerEvent implements CancellablePlotEvent {
038
039    private Result result;
040    private double price;
041
042    public PlayerBuyPlotEvent(final PlotPlayer<?> plotPlayer, final Plot plot, @NonNegative final double price) {
043        super(plotPlayer, plot);
044        this.price = price;
045    }
046
047
048    /**
049     * Sets the price required to buy the plot.
050     *
051     * @param price the new price.
052     * @since 7.3.2
053     */
054    public void setPrice(@NonNegative final double price) {
055        //noinspection ConstantValue - the annotation does not ensure a non-negative runtime value
056        if (price < 0) {
057            throw new IllegalArgumentException("price must be non-negative");
058        }
059        this.price = price;
060    }
061
062    /**
063     * Returns the currently set price required to buy the plot.
064     *
065     * @return the price.
066     * @since 7.3.2
067     */
068    public @NonNegative double price() {
069        return price;
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    public void setEventResult(@Nullable final Result eventResult) {
077        this.result = eventResult;
078    }
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    public @Nullable Result getEventResult() {
085        return this.result;
086    }
087
088}