Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Add PlayerProfile support to PaperLib #4

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/main/java/io/papermc/lib/PaperLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -147,6 +149,36 @@ public static BlockStateSnapshotResult getBlockState(@Nonnull Block block, boole
return ENVIRONMENT.getBlockState(block, useSnapshot);
}

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These API's weren't added to be async. They were added to more efficiently look up a players UUID than loading the entire offline player.

Nothing would prevent you loading the offline player async too, but I'm not sure this lib needs to handle that.

Lets just drop the async and keep them sync, and plugins can call the method async themselves.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, fair. I guess I was just kinda going based off the current naming scheme. Though you can totally load them async it's not necessarily a requirement.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You only changed the name. Need to remove the futures themselves and the async part too.

* 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<UUID> 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<String> getPlayerName(@Nonnull UUID playerUUID) throws IOException {
return ENVIRONMENT.getPlayerName(playerUUID);
}

/**
* Detects if the current MC version is at least the following version.
*
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/io/papermc/lib/environments/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
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;
import org.bukkit.World;
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;
Expand All @@ -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() {
Expand Down Expand Up @@ -86,6 +91,14 @@ public BlockStateSnapshotResult getBlockState(Block block, boolean useSnapshot)
return blockStateSnapshotHandler.getBlockState(block, useSnapshot);
}

public CompletableFuture<UUID> getPlayerUUID(String playerName) throws IOException {
return profileSupportHandler.getPlayerUUID(playerName);
}

public CompletableFuture<String> getPlayerName(UUID playerUUID) throws IOException {
return profileSupportHandler.getPlayerName(playerUUID);
}

public boolean isVersion(int minor) {
return isVersion(minor, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<UUID> getPlayerUUID(String playerName) throws IOException;

CompletableFuture<String> getPlayerName(UUID playerUUID) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -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<UUID> 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<String> 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();
});
}
}
Original file line number Diff line number Diff line change
@@ -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<UUID> getPlayerUUID(String playerName) throws IOException {
return CompletableFuture.completedFuture(Bukkit.getOfflinePlayer(playerName).getUniqueId());
}

public CompletableFuture<String> getPlayerName(UUID playerUUID) throws IOException {
return CompletableFuture.completedFuture(Bukkit.getOfflinePlayer(playerUUID).getName());
}
}