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.configuration.Settings;
022import com.plotsquared.core.configuration.caption.TranslatableCaption;
023import com.plotsquared.core.permissions.Permission;
024import com.plotsquared.core.permissions.PermissionHolder;
025import com.plotsquared.core.player.PlotPlayer;
026import net.kyori.adventure.text.minimessage.Template;
027import org.checkerframework.checker.nullness.qual.NonNull;
028
029/**
030 * The Permissions class handles checking user permissions.<br>
031 * - This will respect * nodes and plots.admin and can be used to check permission ranges (e.g. plots.plot.5)<br>
032 * - Checking the PlotPlayer class directly will not take the above into account<br>
033 *
034 * @deprecated all logic that may once have been in the class lives elsewhere. We also want to do away with statically-accessed
035 *         classes
036 */
037@Deprecated(forRemoval = true, since = "6.9.3")
038public class Permissions {
039
040    /**
041     * @deprecated all logic that may once have been in the class lives elsewhere. We also want to do away with statically-accessed
042     *         classes. Use {@link PlotPlayer#hasPermission(String, boolean)}
043     */
044    @Deprecated(forRemoval = true, since = "6.9.3")
045    public static boolean hasPermission(PlotPlayer<?> player, Permission permission, boolean notify) {
046        return hasPermission(player, permission.toString(), notify);
047    }
048
049    /**
050     * Check if the owner of the profile has a given (global) permission
051     *
052     * @param caller     permission holder
053     * @param permission Permission
054     * @return {@code true} if the owner has the given permission, else {@code false}
055     * @deprecated all logic that may once have been in the class lives elsewhere. We also want to do away with statically-accessed
056     *         classes. Use {@link PermissionHolder#hasPermission(Permission)}
057     */
058    @Deprecated(forRemoval = true, since = "6.9.3")
059    public static boolean hasPermission(final @NonNull PermissionHolder caller, final @NonNull Permission permission) {
060        return caller.hasPermission(permission.toString());
061    }
062
063    /**
064     * Check if the owner of the profile has a given (global) permission. There is no guarantee that per-world permissions will
065     * be checked because unmaintained crap plugins like PEX exist.
066     *
067     * @param caller     permission holder
068     * @param permission Permission
069     * @return {@code true} if the owner has the given permission, else {@code false}
070     * @deprecated all logic that may once have been in the class lives elsewhere. We also want to do away with statically-accessed
071     *         classes. Use {@link PermissionHolder#hasPermission(String)}
072     */
073    @Deprecated(forRemoval = true, since = "6.9.3")
074    public static boolean hasPermission(final @NonNull PermissionHolder caller, final @NonNull String permission) {
075        return caller.hasPermission(permission);
076    }
077
078    /**
079     * Check if the owner of the profile has a given (global) keyed permission. Checks both {@code permission.key}
080     * and {@code permission.*}
081     *
082     * @param caller     permission holder
083     * @param permission Permission
084     * @param key        Permission "key"
085     * @return {@code true} if the owner has the given permission, else {@code false}
086     * @since 6.0.10
087     * @deprecated all logic that may once have been in the class lives elsewhere. We also want to do away with statically-accessed
088     *         classes. Use {@link PermissionHolder#hasKeyedPermission(String, String)}
089     */
090    @Deprecated(forRemoval = true, since = "6.9.3")
091    public static boolean hasKeyedPermission(
092            final @NonNull PermissionHolder caller, final @NonNull String permission,
093            final @NonNull String key
094    ) {
095        return caller.hasKeyedPermission(permission, key);
096    }
097
098    /**
099     * Checks if a PlotPlayer has a permission, and optionally send the no permission message if applicable.
100     *
101     * @param player     permission holder
102     * @param permission permission
103     * @param notify     if to notify the permission holder
104     * @return if permission is had
105     * @deprecated all logic that may once have been in the class lives elsewhere. We also want to do away with statically-accessed
106     *         classes. Use {@link PlotPlayer#hasPermission(String, boolean)}
107     */
108    @Deprecated(forRemoval = true, since = "6.9.3")
109    public static boolean hasPermission(PlotPlayer<?> player, String permission, boolean notify) {
110        if (!hasPermission(player, permission)) {
111            if (notify) {
112                player.sendMessage(
113                        TranslatableCaption.of("permission.no_permission_event"),
114                        Template.of("node", permission)
115                );
116            }
117            return false;
118        }
119        return true;
120    }
121
122    /**
123     * @deprecated all logic that may once have been in the class lives elsewhere. We also want to do away with statically-accessed
124     *         classes. Use {@link PlotPlayer#hasPermissionRange(Permission, int)}
125     */
126    @Deprecated(forRemoval = true, since = "6.9.3")
127    public static int hasPermissionRange(PlotPlayer<?> player, Permission Permission, int range) {
128        return hasPermissionRange(player, Permission.toString(), range);
129    }
130
131    /**
132     * Check the highest permission a PlotPlayer has within a specified range.<br>
133     * - Excessively high values will lag<br>
134     * - The default range that is checked is {@link Settings.Limit#MAX_PLOTS}<br>
135     *
136     * @param player Player to check for
137     * @param stub   The permission stub to check e.g. for `plots.plot.#` the stub is `plots.plot`
138     * @param range  The range to check
139     * @return The highest permission they have within that range
140     * @deprecated all logic that may once have been in the class lives elsewhere. We also want to do away with statically-accessed
141     *         classes. Use {@link PlotPlayer#hasPermissionRange(String, int)}
142     */
143    @Deprecated(forRemoval = true, since = "6.9.3")
144    public static int hasPermissionRange(PlotPlayer<?> player, String stub, int range) {
145        return player.hasPermissionRange(stub, range);
146    }
147
148}