This commit is contained in:
Alexander Söderberg
2020-05-17 17:26:48 +02:00
parent 792fa1f11d
commit 29f2863cf4
8 changed files with 292 additions and 60 deletions

View File

@ -0,0 +1,57 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* PlotSquared plot management system for Minecraft
* Copyright (C) 2020 IntellectualSites
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.plotsquared.bukkit.uuid;
import com.plotsquared.core.uuid.UUIDService;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
import java.util.UUID;
/**
* UUID service that use {@link org.bukkit.OfflinePlayer offline players}
*/
public class OfflinePlayerUUIDService implements UUIDService {
@Override @NotNull public Optional<String> get(@NotNull final UUID uuid) {
final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(uuid);
if (offlinePlayer.hasPlayedBefore()) {
return Optional.ofNullable(offlinePlayer.getName());
}
return Optional.empty();
}
@Override @NotNull public Optional<UUID> get(@NotNull final String username) {
final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(username);
if (offlinePlayer.hasPlayedBefore()) {
return Optional.of(offlinePlayer.getUniqueId());
}
return Optional.empty();
}
}

View File

@ -0,0 +1,65 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* PlotSquared plot management system for Minecraft
* Copyright (C) 2020 IntellectualSites
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.plotsquared.bukkit.uuid;
import com.destroystokyo.paper.profile.PlayerProfile;
import com.plotsquared.core.uuid.UUIDMapping;
import com.plotsquared.core.uuid.UUIDService;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* UUID service that uses the Paper profile API
*/
public class PaperUUIDService implements UUIDService {
@Override @NotNull public List<UUIDMapping> getNames(@NotNull final List<UUID> uuids) {
final List<UUIDMapping> mappings = new ArrayList<>(uuids.size());
for (final UUID uuid : uuids) {
final PlayerProfile playerProfile = Bukkit.createProfile(uuid);
if ((playerProfile.isComplete() || playerProfile.completeFromCache()) && playerProfile.getId() != null) {
mappings.add(new UUIDMapping(playerProfile.getId(), playerProfile.getName()));
}
}
return mappings;
}
@Override @NotNull public List<UUIDMapping> getUUIDs(@NotNull final List<String> usernames) {
final List<UUIDMapping> mappings = new ArrayList<>(usernames.size());
for (final String username : usernames) {
final PlayerProfile playerProfile = Bukkit.createProfile(username);
if ((playerProfile.isComplete() || playerProfile.completeFromCache()) && playerProfile.getId() != null) {
mappings.add(new UUIDMapping(playerProfile.getId(), playerProfile.getName()));
}
}
return mappings;
}
}

View File

@ -0,0 +1,90 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* PlotSquared plot management system for Minecraft
* Copyright (C) 2020 IntellectualSites
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.plotsquared.bukkit.uuid;
import com.google.common.util.concurrent.RateLimiter;
import com.plotsquared.core.uuid.UUIDMapping;
import com.plotsquared.core.uuid.UUIDService;
import com.sk89q.squirrelid.Profile;
import com.sk89q.squirrelid.resolver.HttpRepositoryService;
import com.sk89q.squirrelid.resolver.ProfileService;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* UUID service using SquirrelID
*/
@SuppressWarnings("UnstableApiUsage")
public class SquirrelIdUUIDService implements UUIDService {
private final ProfileService profileService;
private final RateLimiter rateLimiter;
/**
* Create a new SquirrelID UUID service
*
* @param rateLimit Mojangs rate limit is 600 requests per 10 minutes.
* This parameter specifies how many of those requests
* we can use before our internal rate limit kicks in.
*/
public SquirrelIdUUIDService(final int rateLimit) {
this.profileService = HttpRepositoryService.forMinecraft();
// RateLimiter uses request per seconds. The constructor
// parameter rateLimit is requests per 600 seconds
this.rateLimiter = RateLimiter.create(rateLimit / 600.0D);
}
@Override @NotNull public List<UUIDMapping> getNames(@NotNull final List<UUID> uuids) {
final List<UUIDMapping> results = new ArrayList<>(uuids.size());
this.rateLimiter.acquire(uuids.size());
try {
for (final Profile profile : this.profileService.findAllById(uuids)) {
results.add(new UUIDMapping(profile.getUniqueId(), profile.getName()));
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return results;
}
@Override @NotNull public List<UUIDMapping> getUUIDs(@NotNull final List<String> usernames) {
final List<UUIDMapping> results = new ArrayList<>(usernames.size());
this.rateLimiter.acquire(usernames.size());
try {
for (final Profile profile : this.profileService.findAllByName(usernames)) {
results.add(new UUIDMapping(profile.getUniqueId(), profile.getName()));
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return results;
}
}