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.bukkit.permissions;
020
021import com.plotsquared.bukkit.player.BukkitPlayer;
022import com.plotsquared.core.permissions.ConsolePermissionProfile;
023import com.plotsquared.core.permissions.PermissionHandler;
024import com.plotsquared.core.permissions.PermissionProfile;
025import com.plotsquared.core.player.ConsolePlayer;
026import com.plotsquared.core.player.OfflinePlotPlayer;
027import com.plotsquared.core.player.PlotPlayer;
028import org.bukkit.entity.Player;
029import org.checkerframework.checker.nullness.qual.NonNull;
030import org.checkerframework.checker.nullness.qual.Nullable;
031
032import java.lang.ref.WeakReference;
033import java.util.EnumSet;
034import java.util.Optional;
035import java.util.Set;
036
037public class BukkitPermissionHandler implements PermissionHandler {
038
039    @Override
040    public void initialize() {
041    }
042
043    @NonNull
044    @Override
045    public Optional<PermissionProfile> getPermissionProfile(
046            @NonNull PlotPlayer<?> playerPlotPlayer
047    ) {
048        if (playerPlotPlayer instanceof final BukkitPlayer bukkitPlayer) {
049            return Optional.of(new BukkitPermissionProfile(bukkitPlayer.getPlatformPlayer()));
050        } else if (playerPlotPlayer instanceof ConsolePlayer) {
051            return Optional.of(ConsolePermissionProfile.INSTANCE);
052        }
053        return Optional.empty();
054    }
055
056    @NonNull
057    @Override
058    public Optional<PermissionProfile> getPermissionProfile(
059            @NonNull OfflinePlotPlayer offlinePlotPlayer
060    ) {
061        return Optional.empty();
062    }
063
064    @NonNull
065    @Override
066    public Set<PermissionHandlerCapability> getCapabilities() {
067        return EnumSet.of(PermissionHandlerCapability.ONLINE_PERMISSIONS);
068    }
069
070
071    private static final class BukkitPermissionProfile implements PermissionProfile {
072
073        private final WeakReference<Player> playerReference;
074
075        private BukkitPermissionProfile(final @NonNull Player player) {
076            this.playerReference = new WeakReference<>(player);
077        }
078
079        @Override
080        public boolean hasPermission(
081                final @Nullable String world,
082                final @NonNull String permission
083        ) {
084            final Player player = this.playerReference.get();
085            return player != null && player.hasPermission(permission);
086        }
087
088        @Override
089        public boolean hasKeyedPermission(
090                final @Nullable String world,
091                final @NonNull String stub,
092                final @NonNull String key
093        ) {
094            final Player player = this.playerReference.get();
095            return player != null && (player.hasPermission(stub + "." + key) || player.hasPermission(stub + ".*"));
096        }
097
098    }
099
100}