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.util;
020
021import com.plotsquared.core.player.ConsolePlayer;
022import com.plotsquared.core.player.OfflinePlotPlayer;
023import com.plotsquared.core.player.PlotPlayer;
024import com.plotsquared.core.plot.PlotArea;
025import org.checkerframework.checker.nullness.qual.NonNull;
026
027public abstract class EconHandler {
028
029    /**
030     * Returns an econ handler that:
031     * <ul>
032     *     <li>Returns {@code false} on {@link #isEnabled(PlotArea)}</li>
033     *     <li>Returns {@link Double#MIN_VALUE} on {@link #getBalance(PlotPlayer)}</li>
034     *     <li>Doesn't do anything for {@link #withdrawMoney(PlotPlayer, double)},
035     *          {@link #depositMoney(OfflinePlotPlayer, double)}
036     *          {@link #depositMoney(PlotPlayer, double)}</li>
037     * </ul>
038     *
039     * @return A null econ handler
040     */
041    public static EconHandler nullEconHandler() {
042        return new NullEconHandler();
043    }
044
045    public abstract boolean init();
046
047    public double getMoney(PlotPlayer<?> player) {
048        if (player instanceof ConsolePlayer) {
049            return Double.MAX_VALUE;
050        }
051        return getBalance(player);
052    }
053
054    public abstract double getBalance(PlotPlayer<?> player);
055
056    public abstract void withdrawMoney(PlotPlayer<?> player, double amount);
057
058    public abstract void depositMoney(PlotPlayer<?> player, double amount);
059
060    public abstract void depositMoney(OfflinePlotPlayer player, double amount);
061
062    /**
063     * Returns whether economy is enabled in the given plot area or not.
064     * Implementations should only return true if {@link #isSupported()} returns
065     * true too.
066     *
067     * @param plotArea the plot area to check
068     * @return {@code true} if economy is enabled on the given plot area, {@code false} otherwise.
069     */
070    public abstract boolean isEnabled(PlotArea plotArea);
071
072    /**
073     * Formats the given balance into a human-readable number.
074     *
075     * @param balance the balance to format.
076     * @return the balance as formatted string.
077     */
078    public abstract @NonNull String format(double balance);
079
080    /**
081     * Returns whether economy is supported by the server or not.
082     *
083     * @return {@code true} if economy is supported, {@code false} otherwise.
084     */
085    public abstract boolean isSupported();
086
087    private static final class NullEconHandler extends EconHandler {
088
089        @Override
090        public boolean init() {
091            return false;
092        }
093
094        @Override
095        public double getBalance(PlotPlayer<?> player) {
096            return Double.MIN_VALUE;
097        }
098
099        @Override
100        public void withdrawMoney(PlotPlayer<?> player, double amount) {
101
102        }
103
104        @Override
105        public void depositMoney(PlotPlayer<?> player, double amount) {
106
107        }
108
109        @Override
110        public void depositMoney(OfflinePlotPlayer player, double amount) {
111
112        }
113
114        @Override
115        public boolean isEnabled(PlotArea plotArea) {
116            return false;
117        }
118
119        @Override
120        public @NonNull String format(double balance) {
121            return "";
122        }
123
124        @Override
125        public boolean isSupported() {
126            return false;
127        }
128
129    }
130
131}