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.earth2me.essentials.Essentials;
022import com.earth2me.essentials.User;
023import com.plotsquared.core.uuid.UUIDMapping;
024import com.plotsquared.core.uuid.UUIDService;
025import org.checkerframework.checker.nullness.qual.NonNull;
026
027import java.util.ArrayList;
028import java.util.Collections;
029import java.util.List;
030import java.util.UUID;
031
032/**
033 * UUID service using the EssentialsX API
034 */
035public class EssentialsUUIDService implements UUIDService {
036
037    private final Essentials essentials;
038
039    public EssentialsUUIDService() {
040        this.essentials = Essentials.getPlugin(Essentials.class);
041    }
042
043    @Override
044    public @NonNull List<UUIDMapping> getNames(final @NonNull List<UUID> uuids) {
045        return Collections.emptyList();
046    }
047
048    @Override
049    public @NonNull List<UUIDMapping> getUUIDs(final @NonNull List<String> usernames) {
050        final List<UUIDMapping> mappings = new ArrayList<>(usernames.size());
051        for (final String username : usernames) {
052            try {
053                final User user = essentials.getUser(username);
054                if (user != null) {
055                    final UUID uuid = user.getConfigUUID();
056                    if (uuid != null) {
057                        mappings.add(new UUIDMapping(uuid, username));
058                    }
059                }
060            } catch (final Exception ignored) {
061            }
062        }
063        return mappings;
064    }
065
066}