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

Added support for getOfflinePlayer(String) with optional cache #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
34 changes: 25 additions & 9 deletions src/main/java/io/papermc/lib/PaperLib.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package io.papermc.lib;

import io.papermc.lib.environments.CraftBukkitEnvironment;
import io.papermc.lib.environments.Environment;
import io.papermc.lib.environments.PaperEnvironment;
import io.papermc.lib.environments.SpigotEnvironment;
import io.papermc.lib.features.blockstatesnapshot.BlockStateSnapshotResult;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.plugin.Plugin;

import javax.annotation.Nonnull;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
import io.papermc.lib.environments.CraftBukkitEnvironment;
import io.papermc.lib.environments.Environment;
import io.papermc.lib.environments.PaperEnvironment;
import io.papermc.lib.environments.SpigotEnvironment;
import io.papermc.lib.features.blockstatesnapshot.BlockStateSnapshotResult;

/**
* Utility methods that assist plugin developers accessing Paper features.
Expand Down Expand Up @@ -201,6 +205,18 @@ public static BlockStateSnapshotResult getBlockState(@Nonnull Block block, boole
public static CompletableFuture<Location> getBedSpawnLocationAsync(@Nonnull Player player, boolean isUrgent) {
return ENVIRONMENT.getBedSpawnLocationAsync(player, isUrgent);
}

/**
* Gets an OfflinePlayer by the given name. If the Player is not cached, an optional web request
* can be made to look up the UUID of that player. Web requests will block the current Thread though.
* @param name The name of this OfflinePlayer.
* @param makeWebRequest Whether or not a web request for UUID lookups should be made.
* @return OfflinePlayer by the given name or null if the player is not cached and no web request was made.
*/
@Nullable
public static OfflinePlayer getOfflinePlayer(@Nonnull String name, boolean makeWebRequest) {
return ENVIRONMENT.getOfflinePlayer(name, makeWebRequest);
}

/**
* Detects if the current MC version is at least the following version.
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/io/papermc/lib/environments/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,27 @@
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.offlineplayers.GetOfflinePlayer;
import io.papermc.lib.features.offlineplayers.GetOfflinePlayerSpigot;

import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

import javax.annotation.Nonnull;
import java.util.concurrent.CompletableFuture;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

@SuppressWarnings("WeakerAccess")
public abstract class Environment {

Expand All @@ -40,6 +46,7 @@ public abstract class Environment {
protected ChunkIsGenerated isGeneratedHandler = new ChunkIsGeneratedUnknown();
protected BlockStateSnapshot blockStateSnapshotHandler;
protected BedSpawnLocation bedSpawnLocationHandler = new BedSpawnLocationSync();
protected GetOfflinePlayer getOfflinePlayerHandler = new GetOfflinePlayerSpigot();

public Environment() {
Pattern versionPattern = Pattern.compile("\\(MC: (\\d)\\.(\\d+)\\.?(\\d+?)?(?: Pre-Release )?(\\d)?\\)");
Expand Down Expand Up @@ -112,6 +119,11 @@ public BlockStateSnapshotResult getBlockState(Block block, boolean useSnapshot)
public CompletableFuture<Location> getBedSpawnLocationAsync(Player player, boolean isUrgent) {
return bedSpawnLocationHandler.getBedSpawnLocationAsync(player, isUrgent);
}

@Nullable
public OfflinePlayer getOfflinePlayer(@Nonnull String name, boolean makeWebRequest) {
TheBusyBiscuit marked this conversation as resolved.
Show resolved Hide resolved
return getOfflinePlayerHandler.getOfflinePlayer(name, makeWebRequest);
}

public boolean isVersion(int minor) {
return isVersion(minor, 0);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/papermc/lib/environments/PaperEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import io.papermc.lib.features.bedspawnlocation.BedSpawnLocationPaper;
import io.papermc.lib.features.blockstatesnapshot.BlockStateSnapshotOptionalSnapshots;
import io.papermc.lib.features.chunkisgenerated.ChunkIsGeneratedApiExists;
import io.papermc.lib.features.offlineplayers.GetOfflinePlayerPaper;

import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.HumanEntity;

Expand Down Expand Up @@ -38,6 +41,13 @@ public PaperEnvironment() {
bedSpawnLocationHandler = new BedSpawnLocationPaper();
} catch (NoSuchMethodException ignored) {}
}
if (isVersion(16, 4)) {
try {
// Check for the new "getOfflinePlayerIfCached" method which was added in Paper API 1.16.4+
Server.class.getDeclaredMethod("getOfflinePlayerIfCached", String.class);
getOfflinePlayerHandler = new GetOfflinePlayerPaper();
} catch(NoSuchMethodException ignored) {}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.papermc.lib.features.offlineplayers;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.bukkit.OfflinePlayer;

public interface GetOfflinePlayer {
@Nullable
OfflinePlayer getOfflinePlayer(@Nonnull String name, boolean makeWebRequest);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.papermc.lib.features.offlineplayers;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;

public class GetOfflinePlayerPaper implements GetOfflinePlayer {
@Override
@Nullable
public OfflinePlayer getOfflinePlayer(@Nonnull String name, boolean makeWebRequest) {
if (makeWebRequest) {
return Bukkit.getOfflinePlayer(name);
} else {
return Bukkit.getOfflinePlayerIfCached(name);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.papermc.lib.features.offlineplayers;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;

public class GetOfflinePlayerSpigot implements GetOfflinePlayer {
@Override
@Nullable
public OfflinePlayer getOfflinePlayer(@Nonnull String name, boolean makeWebRequest) {
return Bukkit.getOfflinePlayer(name);
}
}