From f4805562103c6925be0ff5395ca07a5ff919c950 Mon Sep 17 00:00:00 2001 From: egg82 Date: Mon, 8 Oct 2018 12:25:15 -0600 Subject: [PATCH 1/2] * Added PlayerProfile support to PaperLib --- src/main/java/io/papermc/lib/PaperLib.java | 32 +++++++++++++++++ .../papermc/lib/environments/Environment.java | 13 +++++++ .../lib/environments/PaperEnvironment.java | 2 ++ .../profilesupport/ProfileSupport.java | 11 ++++++ .../profilesupport/ProfileSupportPaper.java | 34 +++++++++++++++++++ .../profilesupport/ProfileSupportUnknown.java | 17 ++++++++++ 6 files changed, 109 insertions(+) create mode 100644 src/main/java/io/papermc/lib/features/profilesupport/ProfileSupport.java create mode 100644 src/main/java/io/papermc/lib/features/profilesupport/ProfileSupportPaper.java create mode 100644 src/main/java/io/papermc/lib/features/profilesupport/ProfileSupportUnknown.java diff --git a/src/main/java/io/papermc/lib/PaperLib.java b/src/main/java/io/papermc/lib/PaperLib.java index bdce87d..8468de5 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 getPlayerUUIDAsync(@Nonnull String playerName) throws IOException { + return ENVIRONMENT.getPlayerUUIDAsync(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 getPlayerNameAsync(@Nonnull UUID playerUUID) throws IOException { + return ENVIRONMENT.getPlayerNameAsync(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..a6cb206 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 getPlayerUUIDAsync(String playerName) throws IOException { + return profileSupportHandler.getPlayerUUIDAsync(playerName); + } + + public CompletableFuture getPlayerNameAsync(UUID playerUUID) throws IOException { + return profileSupportHandler.getPlayerNameAsync(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..047db0f --- /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 getPlayerUUIDAsync(String playerName) throws IOException; + + CompletableFuture getPlayerNameAsync(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..0325564 --- /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 getPlayerUUIDAsync(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 getPlayerNameAsync(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..b485605 --- /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 getPlayerUUIDAsync(String playerName) throws IOException { + return CompletableFuture.completedFuture(Bukkit.getOfflinePlayer(playerName).getUniqueId()); + } + + public CompletableFuture getPlayerNameAsync(UUID playerUUID) throws IOException { + return CompletableFuture.completedFuture(Bukkit.getOfflinePlayer(playerUUID).getName()); + } +} From a03e706ceacd258ee2140d5a5946cf4d94cd870f Mon Sep 17 00:00:00 2001 From: egg82 Date: Tue, 9 Oct 2018 12:34:25 -0600 Subject: [PATCH 2/2] * Made requested changes --- src/main/java/io/papermc/lib/PaperLib.java | 8 ++++---- .../java/io/papermc/lib/environments/Environment.java | 8 ++++---- .../lib/features/profilesupport/ProfileSupport.java | 4 ++-- .../lib/features/profilesupport/ProfileSupportPaper.java | 4 ++-- .../features/profilesupport/ProfileSupportUnknown.java | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/java/io/papermc/lib/PaperLib.java b/src/main/java/io/papermc/lib/PaperLib.java index 8468de5..a8ea6c1 100644 --- a/src/main/java/io/papermc/lib/PaperLib.java +++ b/src/main/java/io/papermc/lib/PaperLib.java @@ -160,8 +160,8 @@ public static BlockStateSnapshotResult getBlockState(@Nonnull Block block, boole * @throws IOException if there was a rate-limit or other network error */ @Nonnull - public static CompletableFuture getPlayerUUIDAsync(@Nonnull String playerName) throws IOException { - return ENVIRONMENT.getPlayerUUIDAsync(playerName); + public static CompletableFuture getPlayerUUID(@Nonnull String playerName) throws IOException { + return ENVIRONMENT.getPlayerUUID(playerName); } /** @@ -175,8 +175,8 @@ public static CompletableFuture getPlayerUUIDAsync(@Nonnull String playerN * @throws IOException if there was a rate-limit or other network error */ @Nonnull - public static CompletableFuture getPlayerNameAsync(@Nonnull UUID playerUUID) throws IOException { - return ENVIRONMENT.getPlayerNameAsync(playerUUID); + public static CompletableFuture getPlayerName(@Nonnull UUID playerUUID) throws IOException { + return ENVIRONMENT.getPlayerName(playerUUID); } /** diff --git a/src/main/java/io/papermc/lib/environments/Environment.java b/src/main/java/io/papermc/lib/environments/Environment.java index a6cb206..2ecc7fb 100644 --- a/src/main/java/io/papermc/lib/environments/Environment.java +++ b/src/main/java/io/papermc/lib/environments/Environment.java @@ -91,12 +91,12 @@ public BlockStateSnapshotResult getBlockState(Block block, boolean useSnapshot) return blockStateSnapshotHandler.getBlockState(block, useSnapshot); } - public CompletableFuture getPlayerUUIDAsync(String playerName) throws IOException { - return profileSupportHandler.getPlayerUUIDAsync(playerName); + public CompletableFuture getPlayerUUID(String playerName) throws IOException { + return profileSupportHandler.getPlayerUUID(playerName); } - public CompletableFuture getPlayerNameAsync(UUID playerUUID) throws IOException { - return profileSupportHandler.getPlayerNameAsync(playerUUID); + public CompletableFuture getPlayerName(UUID playerUUID) throws IOException { + return profileSupportHandler.getPlayerName(playerUUID); } public boolean isVersion(int minor) { diff --git a/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupport.java b/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupport.java index 047db0f..ff9efed 100644 --- a/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupport.java +++ b/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupport.java @@ -5,7 +5,7 @@ import java.util.concurrent.CompletableFuture; public interface ProfileSupport { - CompletableFuture getPlayerUUIDAsync(String playerName) throws IOException; + CompletableFuture getPlayerUUID(String playerName) throws IOException; - CompletableFuture getPlayerNameAsync(UUID playerUUID) 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 index 0325564..70376b3 100644 --- a/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupportPaper.java +++ b/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupportPaper.java @@ -8,7 +8,7 @@ import java.util.concurrent.CompletableFuture; public class ProfileSupportPaper implements ProfileSupport { - public CompletableFuture getPlayerUUIDAsync(String playerName) throws IOException { + public CompletableFuture getPlayerUUID(String playerName) throws IOException { PlayerProfile profile = Bukkit.createProfile(playerName); if (profile.isComplete() || profile.completeFromCache()) { return CompletableFuture.completedFuture(profile.getId()); @@ -20,7 +20,7 @@ public CompletableFuture getPlayerUUIDAsync(String playerName) throws IOEx }); } - public CompletableFuture getPlayerNameAsync(UUID playerUUID) throws IOException { + public CompletableFuture getPlayerName(UUID playerUUID) throws IOException { PlayerProfile profile = Bukkit.createProfile(playerUUID); if (profile.isComplete() || profile.completeFromCache()) { return CompletableFuture.completedFuture(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 index b485605..db5fe55 100644 --- a/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupportUnknown.java +++ b/src/main/java/io/papermc/lib/features/profilesupport/ProfileSupportUnknown.java @@ -7,11 +7,11 @@ import java.util.concurrent.CompletableFuture; public class ProfileSupportUnknown implements ProfileSupport { - public CompletableFuture getPlayerUUIDAsync(String playerName) throws IOException { + public CompletableFuture getPlayerUUID(String playerName) throws IOException { return CompletableFuture.completedFuture(Bukkit.getOfflinePlayer(playerName).getUniqueId()); } - public CompletableFuture getPlayerNameAsync(UUID playerUUID) throws IOException { + public CompletableFuture getPlayerName(UUID playerUUID) throws IOException { return CompletableFuture.completedFuture(Bukkit.getOfflinePlayer(playerUUID).getName()); } }