diff --git a/src/main/java/io/papermc/lib/PaperLib.java b/src/main/java/io/papermc/lib/PaperLib.java index bdce87d..a8ea6c1 100644 --- a/src/main/java/io/papermc/lib/PaperLib.java +++ b/src/main/java/io/papermc/lib/PaperLib.java @@ -13,6 +13,8 @@ import org.bukkit.plugin.Plugin; import javax.annotation.Nonnull; +import java.io.IOException; +import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.logging.Logger; @@ -147,6 +149,36 @@ public static BlockStateSnapshotResult getBlockState(@Nonnull Block block, boole return ENVIRONMENT.getBlockState(block, useSnapshot); } + /** + * Returns the UUID of a player found by name, or null + * if a player was not found by the name provided. + * If there was a rate-limit or other network error, + * an IOException will be thrown. + * + * @param playerName The name of the player to fetch + * @return The UUID of the specified player, or null if not found + * @throws IOException if there was a rate-limit or other network error + */ + @Nonnull + public static CompletableFuture getPlayerUUID(@Nonnull String playerName) throws IOException { + return ENVIRONMENT.getPlayerUUID(playerName); + } + + /** + * Returns the name of a player found by UUID, or null + * if a player was not found by the UUID provided. + * If there was a rate-limit or other network error, + * an IOException will be thrown. + * + * @param playerUUID The UUID of the player to fetch + * @return The name of the specified player, or null if not found + * @throws IOException if there was a rate-limit or other network error + */ + @Nonnull + public static CompletableFuture getPlayerName(@Nonnull UUID playerUUID) throws IOException { + return ENVIRONMENT.getPlayerName(playerUUID); + } + /** * Detects if the current MC version is at least the following version. * diff --git a/src/main/java/io/papermc/lib/environments/Environment.java b/src/main/java/io/papermc/lib/environments/Environment.java index 434c1fb..2ecc7fb 100644 --- a/src/main/java/io/papermc/lib/environments/Environment.java +++ b/src/main/java/io/papermc/lib/environments/Environment.java @@ -11,6 +11,8 @@ import io.papermc.lib.features.chunkisgenerated.ChunkIsGenerated; import io.papermc.lib.features.chunkisgenerated.ChunkIsGeneratedApiExists; import io.papermc.lib.features.chunkisgenerated.ChunkIsGeneratedUnknown; +import io.papermc.lib.features.profilesupport.ProfileSupport; +import io.papermc.lib.features.profilesupport.ProfileSupportUnknown; import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.Location; @@ -18,6 +20,8 @@ import org.bukkit.block.Block; import org.bukkit.entity.Entity; +import java.io.IOException; +import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.regex.MatchResult; import java.util.regex.Matcher; @@ -32,6 +36,7 @@ public abstract class Environment { protected AsyncChunks asyncChunksHandler = new AsyncChunksSync(); protected AsyncTeleport asyncTeleportHandler = new AsyncTeleportSync(); protected ChunkIsGenerated isGeneratedHandler = new ChunkIsGeneratedUnknown(); + protected ProfileSupport profileSupportHandler = new ProfileSupportUnknown(); protected BlockStateSnapshot blockStateSnapshotHandler; public Environment() { @@ -86,6 +91,14 @@ public BlockStateSnapshotResult getBlockState(Block block, boolean useSnapshot) return blockStateSnapshotHandler.getBlockState(block, useSnapshot); } + public CompletableFuture getPlayerUUID(String playerName) throws IOException { + return profileSupportHandler.getPlayerUUID(playerName); + } + + public CompletableFuture getPlayerName(UUID playerUUID) throws IOException { + return profileSupportHandler.getPlayerName(playerUUID); + } + public boolean isVersion(int minor) { return isVersion(minor, 0); } diff --git a/src/main/java/io/papermc/lib/environments/PaperEnvironment.java b/src/main/java/io/papermc/lib/environments/PaperEnvironment.java index cbd2e89..bbf27df 100644 --- a/src/main/java/io/papermc/lib/environments/PaperEnvironment.java +++ b/src/main/java/io/papermc/lib/environments/PaperEnvironment.java @@ -5,6 +5,7 @@ import io.papermc.lib.features.asyncteleport.AsyncTeleportPaper; import io.papermc.lib.features.blockstatesnapshot.BlockStateSnapshotOptionalSnapshots; import io.papermc.lib.features.chunkisgenerated.ChunkIsGeneratedApiExists; +import io.papermc.lib.features.profilesupport.ProfileSupportPaper; public class PaperEnvironment extends SpigotEnvironment { @@ -22,6 +23,7 @@ public PaperEnvironment() { // Paper added this API in 1.12 with same signature spigot did in 1.13 isGeneratedHandler = new ChunkIsGeneratedApiExists(); blockStateSnapshotHandler = new BlockStateSnapshotOptionalSnapshots(); + profileSupportHandler = new ProfileSupportPaper(); } } diff --git a/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupport.java b/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupport.java new file mode 100644 index 0000000..ff9efed --- /dev/null +++ b/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupport.java @@ -0,0 +1,11 @@ +package io.papermc.lib.features.profilesupport; + +import java.io.IOException; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; + +public interface ProfileSupport { + CompletableFuture getPlayerUUID(String playerName) throws IOException; + + CompletableFuture getPlayerName(UUID playerUUID) throws IOException; +} diff --git a/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupportPaper.java b/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupportPaper.java new file mode 100644 index 0000000..70376b3 --- /dev/null +++ b/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupportPaper.java @@ -0,0 +1,34 @@ +package io.papermc.lib.features.profilesupport; + +import com.destroystokyo.paper.profile.PlayerProfile; +import org.bukkit.Bukkit; + +import java.io.IOException; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; + +public class ProfileSupportPaper implements ProfileSupport { + public CompletableFuture getPlayerUUID(String playerName) throws IOException { + PlayerProfile profile = Bukkit.createProfile(playerName); + if (profile.isComplete() || profile.completeFromCache()) { + return CompletableFuture.completedFuture(profile.getId()); + } + + return CompletableFuture.supplyAsync(() -> { + profile.complete(false); + return profile.getId(); + }); + } + + public CompletableFuture getPlayerName(UUID playerUUID) throws IOException { + PlayerProfile profile = Bukkit.createProfile(playerUUID); + if (profile.isComplete() || profile.completeFromCache()) { + return CompletableFuture.completedFuture(profile.getName()); + } + + return CompletableFuture.supplyAsync(() -> { + profile.complete(false); + return profile.getName(); + }); + } +} diff --git a/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupportUnknown.java b/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupportUnknown.java new file mode 100644 index 0000000..db5fe55 --- /dev/null +++ b/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupportUnknown.java @@ -0,0 +1,17 @@ +package io.papermc.lib.features.profilesupport; + +import org.bukkit.Bukkit; + +import java.io.IOException; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; + +public class ProfileSupportUnknown implements ProfileSupport { + public CompletableFuture getPlayerUUID(String playerName) throws IOException { + return CompletableFuture.completedFuture(Bukkit.getOfflinePlayer(playerName).getUniqueId()); + } + + public CompletableFuture getPlayerName(UUID playerUUID) throws IOException { + return CompletableFuture.completedFuture(Bukkit.getOfflinePlayer(playerUUID).getName()); + } +}