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    @SuppressWarnings("deprecation")
058    public BukkitPlayer getPlayer(final @NonNull Player object) {
059        if (object.getUniqueId().version() == 2) { // not a real player
060            return new BukkitPlayer(this.plotAreaManager, this.eventDispatcher, object, false, this.permissionHandler);
061        }
062        if (!object.isOnline()) {
063            throw new NoSuchPlayerException(object.getUniqueId());
064        }
065        return getPlayer(object.getUniqueId());
066    }
067
068    @Override
069    @SuppressWarnings("deprecation")
070    public @NonNull BukkitPlayer createPlayer(final @NonNull UUID uuid) {
071        final Player player = Bukkit.getPlayer(uuid);
072        if (player == null || !player.isOnline()) {
073            throw new NoSuchPlayerException(uuid);
074        }
075        return new BukkitPlayer(this.plotAreaManager, this.eventDispatcher, player, this.permissionHandler);
076    }
077
078    @Nullable
079    @Override
080    public BukkitOfflinePlayer getOfflinePlayer(final @Nullable UUID uuid) {
081        if (uuid == null) {
082            return null;
083        }
084        return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(uuid), this.permissionHandler);
085    }
086
087    @NonNull
088    @Override
089    public BukkitOfflinePlayer getOfflinePlayer(final @NonNull String username) {
090        return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(username), this.permissionHandler);
091    }
092
093}