Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit 33c8444

Browse files
committed
feat: add utility functions for Bukkit and CloudPlayer conversions
- Added `toBukkitPlayer` and `toBukkitOfflinePlayer` methods for converting `CloudPlayer` and `OfflineCloudPlayer` instances to Bukkit counterparts. - Enhanced `OfflinePlayer.toCloudOfflinePlayer` with additional javadoc comments for better clarity. - Improved code documentation for teleportation and conversion utilities.
1 parent cfec4e4 commit 33c8444

File tree

1 file changed

+42
-1
lines changed
  • surf-cloud-api/surf-cloud-api-client/surf-cloud-api-client-paper/src/main/kotlin/dev/slne/surf/cloud/api/client/paper/player

1 file changed

+42
-1
lines changed

surf-cloud-api/surf-cloud-api-client/surf-cloud-api-client-paper/src/main/kotlin/dev/slne/surf/cloud/api/client/paper/player/CloudPlayerBukkitExtensions.kt

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@ import dev.slne.surf.cloud.api.client.paper.toCloudTpFlag
55
import dev.slne.surf.cloud.api.client.paper.toCloudTpLocation
66
import dev.slne.surf.cloud.api.common.player.CloudPlayer
77
import dev.slne.surf.cloud.api.common.player.CloudPlayerManager
8+
import dev.slne.surf.cloud.api.common.player.OfflineCloudPlayer
9+
import dev.slne.surf.surfapi.bukkit.api.extensions.server
810
import io.papermc.paper.entity.TeleportFlag
911
import org.bukkit.Location
1012
import org.bukkit.OfflinePlayer
1113
import org.bukkit.event.player.PlayerTeleportEvent
1214

15+
/**
16+
* Teleports the player to the specified location with the given teleport cause and flags.
17+
*
18+
* @param location The location to teleport the player to.
19+
* @param cause The reason for the teleportation, typically a specific event or trigger. Defaults to [PlayerTeleportEvent.TeleportCause.PLUGIN].
20+
* @param flags Additional flags that define special conditions or behaviors for the teleportation.
21+
*/
1322
suspend fun CloudPlayer.teleport(
1423
location: Location,
1524
cause: PlayerTeleportEvent.TeleportCause = PlayerTeleportEvent.TeleportCause.PLUGIN,
@@ -22,4 +31,36 @@ suspend fun CloudPlayer.teleport(
2231
)
2332
}
2433

25-
fun OfflinePlayer.toCloudOfflinePlayer() = CloudPlayerManager.getOfflinePlayer(uniqueId)
34+
/**
35+
* Converts an `OfflinePlayer` instance to a `OfflineCloudPlayer` instance
36+
* by retrieving the corresponding player data from the `CloudPlayerManager` using the player's UUID.
37+
*
38+
* This function ensures that the `OfflineCloudPlayer` exists in the underlying system,
39+
* creating a placeholder entry if necessary.
40+
*
41+
* @return An `OfflineCloudPlayer` representing the player associated with the given `OfflinePlayer`.
42+
*/
43+
fun OfflinePlayer.toCloudOfflinePlayer() = CloudPlayerManager.getOfflinePlayer(uniqueId)
44+
45+
/**
46+
* Converts the current [CloudPlayer] instance into a Bukkit player instance.
47+
*
48+
* This method retrieves the corresponding Bukkit [Player] object based on the UUID of the
49+
* current [CloudPlayer]. If the player is not currently connected to the server, this will
50+
* return `null`.
51+
*
52+
* @return The Bukkit [Player] instance corresponding to the [CloudPlayer]'s UUID, or `null`
53+
* if the player is not present on the Bukkit server.
54+
*/
55+
fun CloudPlayer.toBukkitPlayer() = server.getPlayer(uuid)
56+
57+
/**
58+
* Converts an instance of [OfflineCloudPlayer] to the corresponding Bukkit offline player.
59+
*
60+
* This function utilizes the player's unique identifier (UUID) to fetch the associated
61+
* [org.bukkit.OfflinePlayer] from the server.
62+
*
63+
* @receiver The [OfflineCloudPlayer] instance to convert.
64+
* @return The corresponding [org.bukkit.OfflinePlayer] for the specified [OfflineCloudPlayer].
65+
*/
66+
fun OfflineCloudPlayer.toBukkitOfflinePlayer() = server.getOfflinePlayer(uuid)

0 commit comments

Comments
 (0)