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.plotsquared.core.uuid.UUIDMapping;
022import com.plotsquared.core.uuid.UUIDService;
023import net.luckperms.api.LuckPerms;
024import net.luckperms.api.model.user.UserManager;
025import org.bukkit.Bukkit;
026import org.bukkit.plugin.RegisteredServiceProvider;
027import org.checkerframework.checker.nullness.qual.NonNull;
028
029import java.util.ArrayList;
030import java.util.List;
031import java.util.UUID;
032
033/**
034 * UUID service that uses the LuckPerms API
035 */
036public class LuckPermsUUIDService implements UUIDService {
037
038    private final LuckPerms luckPerms;
039
040    public LuckPermsUUIDService() {
041        final RegisteredServiceProvider<LuckPerms> provider = Bukkit.getServicesManager().getRegistration(LuckPerms.class);
042        if (provider != null) {
043            this.luckPerms = provider.getProvider();
044        } else {
045            throw new IllegalStateException("LuckPerms not available");
046        }
047    }
048
049    @Override
050    public @NonNull List<UUIDMapping> getNames(final @NonNull List<UUID> uuids) {
051        final List<UUIDMapping> mappings = new ArrayList<>(uuids.size());
052        final UserManager userManager = this.luckPerms.getUserManager();
053        for (final UUID uuid : uuids) {
054            try {
055                final String username = userManager.lookupUsername(uuid).get();
056                if (username != null) {
057                    mappings.add(new UUIDMapping(uuid, username));
058                }
059            } catch (final Exception ignored) {
060            }
061        }
062        return mappings;
063    }
064
065    @Override
066    public @NonNull List<UUIDMapping> getUUIDs(final @NonNull List<String> usernames) {
067        final List<UUIDMapping> mappings = new ArrayList<>(usernames.size());
068        final UserManager userManager = this.luckPerms.getUserManager();
069        for (final String username : usernames) {
070            try {
071                final UUID uuid = userManager.lookupUniqueId(username).get();
072                if (username != null) {
073                    mappings.add(new UUIDMapping(uuid, username));
074                }
075            } catch (final Exception ignored) {
076            }
077        }
078        return mappings;
079    }
080
081}