javaminecraftminecraft-fabric

Is there a way to get a player's respawn coordinates with a PlayerEntity object (Fabricmc)


I made a use override on the Block class

public TypedActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand) {
   // code in here
}

is there a way to use the playerEntity variable to get the player's respawn coorindates?


Solution

  • A little late, but what you need, to get the spawn point of a player, is either a ServerWorld object or a ServerPlayerEntity object. Because ServerPlayerEntity and ServerWorld are subclasses of PlayerEntity and World, you can just perform a runtime type-check using instanceof and then cast the objects to their subclass.

    ServerPlayerEntity has a getter called getSpawnPointPosition (which returns the spawn position of the player, e.g. the bed position)

    ServerWorld has a method called getSpawnPos (which returns the worlds spawn point)

    The findRespawnPosition method is used to get a position near the given point, that is not obstructed. So you'd for example provide the players bed spawn to findRespawnPosition and the method returns a position, that's not inside a block and tall enough for the player to stand. You would use that method if you want to teleport a player to their respawn point.