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.uuid;
020
021import com.google.common.base.Charsets;
022import com.plotsquared.core.configuration.Settings;
023import com.plotsquared.core.uuid.UUIDMapping;
024import com.plotsquared.core.uuid.UUIDService;
025import org.bukkit.Bukkit;
026import org.bukkit.OfflinePlayer;
027import org.checkerframework.checker.nullness.qual.NonNull;
028
029import java.util.ArrayList;
030import java.util.Collections;
031import java.util.List;
032import java.util.UUID;
033
034/**
035 * UUID service that use {@link org.bukkit.OfflinePlayer offline players}
036 */
037public class OfflinePlayerUUIDService implements UUIDService {
038
039    @Override
040    public @NonNull List<UUIDMapping> getNames(final @NonNull List<UUID> uuids) {
041        if (Settings.UUID.FORCE_LOWERCASE || Bukkit.getWorlds().isEmpty()) {
042            return Collections.emptyList(); // This is useless now
043        }
044        final List<UUIDMapping> wrappers = new ArrayList<>(uuids.size());
045        for (final UUID uuid : uuids) {
046            final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(uuid);
047            try {
048                if (offlinePlayer.hasPlayedBefore()) {
049                    wrappers.add(new UUIDMapping(uuid, offlinePlayer.getName()));
050                }
051            } catch (final Exception ignored) {
052            } /* This can be safely ignored. If this happens, it is
053                                                    probably because it's called before the worlds have
054                                                    been loaded. This is bad, but does not break anything */
055        }
056        return wrappers;
057    }
058
059    @Override
060    public @NonNull List<UUIDMapping> getUUIDs(final @NonNull List<String> usernames) {
061        final List<UUIDMapping> wrappers = new ArrayList<>(usernames.size());
062        for (final String username : usernames) {
063            if (Settings.UUID.OFFLINE) {
064                if (Settings.UUID.FORCE_LOWERCASE) {
065                    wrappers.add(new UUIDMapping(UUID.nameUUIDFromBytes(("OfflinePlayer:" +
066                            username.toLowerCase()).getBytes(Charsets.UTF_8)), username));
067                } else {
068                    wrappers.add(new UUIDMapping(UUID.nameUUIDFromBytes(("OfflinePlayer:" +
069                            username).getBytes(Charsets.UTF_8)), username));
070                }
071            } else {
072                final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(username);
073                if (offlinePlayer.hasPlayedBefore()) {
074                    wrappers.add(new UUIDMapping(offlinePlayer.getUniqueId(), offlinePlayer.getName()));
075                }
076            }
077        }
078        return wrappers;
079    }
080
081}