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.core.uuid.offline;
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.checkerframework.checker.nullness.qual.NonNull;
026
027import java.util.ArrayList;
028import java.util.Collections;
029import java.util.List;
030import java.util.Locale;
031import java.util.UUID;
032
033/**
034 * Name provider service that creates UUIDs from usernames
035 */
036public class OfflineModeUUIDService implements UUIDService {
037
038    @NonNull
039    protected final UUID getFromUsername(@NonNull String username) {
040        if (Settings.UUID.FORCE_LOWERCASE) {
041            username = username.toLowerCase(Locale.ENGLISH);
042        }
043        return UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(Charsets.UTF_8));
044    }
045
046    @Override
047    public @NonNull List<@NonNull UUIDMapping> getNames(final @NonNull List<@NonNull UUID> uuids) {
048        return Collections.emptyList();
049    }
050
051    @Override
052    public @NonNull List<@NonNull UUIDMapping> getUUIDs(@NonNull List<@NonNull String> usernames) {
053        final List<UUIDMapping> mappings = new ArrayList<>(usernames.size());
054        for (final String username : usernames) {
055            mappings.add(new UUIDMapping(getFromUsername(username), username));
056        }
057        return mappings;
058    }
059
060}