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.google.inject.Inject;
022import com.google.inject.Singleton;
023import com.plotsquared.core.permissions.PermissionHandler;
024import com.plotsquared.core.plot.world.PlotAreaManager;
025import com.plotsquared.core.util.EventDispatcher;
026import com.plotsquared.core.util.PlayerManager;
027import org.bukkit.Bukkit;
028import org.bukkit.entity.Player;
029import org.checkerframework.checker.nullness.qual.NonNull;
030import org.checkerframework.checker.nullness.qual.Nullable;
031
032import java.util.UUID;
033
034/**
035 * Player manager providing {@link BukkitPlayer Bukkit players}
036 */
037@Singleton
038public class BukkitPlayerManager extends PlayerManager<BukkitPlayer, Player> {
039
040    private final PlotAreaManager plotAreaManager;
041    private final EventDispatcher eventDispatcher;
042    private final PermissionHandler permissionHandler;
043
044    @Inject
045    public BukkitPlayerManager(
046            final @NonNull PlotAreaManager plotAreaManager,
047            final @NonNull EventDispatcher eventDispatcher,
048            final @NonNull PermissionHandler permissionHandler
049    ) {
050        this.plotAreaManager = plotAreaManager;
051        this.eventDispatcher = eventDispatcher;
052        this.permissionHandler = permissionHandler;
053    }
054
055    @NonNull
056    @Override
057    public BukkitPlayer getPlayer(final @NonNull Player object) {
058        if (object.getUniqueId().version() == 2) { // not a real player
059            return new BukkitPlayer(this.plotAreaManager, this.eventDispatcher, object, false, this.permissionHandler);
060        }
061        if (!object.isOnline()) {
062            throw new NoSuchPlayerException(object.getUniqueId());
063        }
064        return getPlayer(object.getUniqueId());
065    }
066
067    @Override
068    public @NonNull BukkitPlayer createPlayer(final @NonNull UUID uuid) {
069        final Player player = Bukkit.getPlayer(uuid);
070        if (player == null || !player.isOnline()) {
071            throw new NoSuchPlayerException(uuid);
072        }
073        return new BukkitPlayer(this.plotAreaManager, this.eventDispatcher, player, false, this.permissionHandler);
074    }
075
076    @Nullable
077    @Override
078    public BukkitOfflinePlayer getOfflinePlayer(final @Nullable UUID uuid) {
079        if (uuid == null) {
080            return null;
081        }
082        return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(uuid), this.permissionHandler);
083    }
084
085    @NonNull
086    @Override
087    public BukkitOfflinePlayer getOfflinePlayer(final @NonNull String username) {
088        return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(username), this.permissionHandler);
089    }
090
091}