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.player;
020
021import com.plotsquared.core.permissions.NullPermissionProfile;
022import com.plotsquared.core.permissions.PermissionHandler;
023import com.plotsquared.core.permissions.PermissionProfile;
024import com.plotsquared.core.player.OfflinePlotPlayer;
025import org.bukkit.OfflinePlayer;
026import org.checkerframework.checker.index.qual.NonNegative;
027import org.checkerframework.checker.nullness.qual.NonNull;
028import org.checkerframework.checker.nullness.qual.Nullable;
029
030import java.util.UUID;
031
032public class BukkitOfflinePlayer implements OfflinePlotPlayer {
033
034    public final OfflinePlayer player;
035    private final PermissionProfile permissionProfile;
036
037    /**
038     * Please do not use this method. Instead use BukkitUtil.getPlayer(Player),
039     * as it caches player objects.
040     *
041     * @param player            Bukkit OfflinePlayer player to convert
042     * @param permissionHandler Permission Profile to be used
043     */
044    public BukkitOfflinePlayer(
045            final @NonNull OfflinePlayer player, final @NonNull
046    PermissionHandler permissionHandler
047    ) {
048        this.player = player;
049        this.permissionProfile = permissionHandler.getPermissionProfile(this)
050                .orElse(NullPermissionProfile.INSTANCE);
051    }
052
053    @NonNull
054    @Override
055    public UUID getUUID() {
056        return this.player.getUniqueId();
057    }
058
059    @Override
060    @NonNegative
061    public long getLastPlayed() {
062        return this.player.getLastSeen();
063    }
064
065    @Override
066    public String getName() {
067        return this.player.getName();
068    }
069
070    @Override
071    public boolean hasPermission(
072            final @Nullable String world,
073            final @NonNull String permission
074    ) {
075        return this.permissionProfile.hasPermission(world, permission);
076    }
077
078    @Override
079    public boolean hasKeyedPermission(
080            final @Nullable String world,
081            final @NonNull String stub,
082            final @NonNull String key
083    ) {
084        return this.permissionProfile.hasPermission(world, stub + "." + key) || this.permissionProfile.hasPermission(
085                world,
086                stub + ".*"
087        );
088    }
089
090    @Override
091    public boolean hasPermission(@NonNull final String permission, final boolean notify) {
092        return hasPermission(permission);
093    }
094
095}