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.plot.comment;
020
021import com.plotsquared.core.database.DBFunc;
022import com.plotsquared.core.player.PlotPlayer;
023import com.plotsquared.core.plot.Plot;
024import com.plotsquared.core.util.task.RunnableVal;
025
026import java.util.List;
027
028public abstract class CommentInbox {
029
030    @Override
031    public abstract String toString();
032
033    /**
034     * @param plot   the plot's inbox to read
035     * @param player the player trying to read the comment
036     * @return the inbox, otherwise {@code false} false
037     */
038    public boolean canRead(Plot plot, PlotPlayer<?> player) {
039        if (player.hasPermission("plots.inbox.read." + this, true)) {
040            return plot.isOwner(player.getUUID()) || player.hasPermission("plots.inbox.read." + this + ".other", true);
041        }
042        return false;
043    }
044
045    /**
046     * @param plot   the plot's inbox to write to
047     * @param player the player trying to write the comment
048     * @return {@code true} if the player can write a comment on the plot
049     */
050    public boolean canWrite(Plot plot, PlotPlayer<?> player) {
051        if (plot == null) {
052            return player.hasPermission("plots.inbox.write." + this, true);
053        }
054        return player.hasPermission("plots.inbox.write." + this, true) && (
055                plot.isOwner(player.getUUID()) || player.hasPermission("plots.inbox.write." + this + ".other", true));
056    }
057
058    /**
059     * @param plot   the plot's inbox to write to
060     * @param player the player trying to modify the inbox
061     * @return {@code true} if the player can write a comment on the plot
062     */
063    @SuppressWarnings({"BooleanMethodIsAlwaysInverted"})
064    public boolean canModify(Plot plot, PlotPlayer<?> player) {
065        if (player.hasPermission("plots.inbox.modify." + this, true)) {
066            return plot.isOwner(player.getUUID()) || player.hasPermission("plots.inbox.modify." + this + ".other", true);
067        }
068        return false;
069    }
070
071    /**
072     * <br>
073     * The `whenDone` parameter should be executed when it's done fetching the comments.
074     * The value should be set to List of comments
075     *
076     * @param plot     plot
077     * @param whenDone task to run when comments are obtained
078     * @return success or not
079     */
080    public abstract boolean getComments(Plot plot, RunnableVal<List<PlotComment>> whenDone);
081
082    /**
083     * @param plot    plot
084     * @param comment the comment to add
085     * @return success or not
086     */
087    public abstract boolean addComment(Plot plot, PlotComment comment);
088
089    /**
090     * @param plot    plot
091     * @param comment the comment to remove
092     */
093    public void removeComment(Plot plot, PlotComment comment) {
094        DBFunc.removeComment(plot, comment);
095    }
096
097    /**
098     * @param plot plot
099     */
100    public void clearInbox(Plot plot) {
101        DBFunc.clearInbox(plot, toString());
102    }
103
104}