Jump to content

get player's respawn coordinates?


Recommended Posts

Hi, how can I get the coordinates of the player's spawn point? I want to do this in the onLivingHurt event. This is what I have now:
 

        @SubscribeEvent
        public static void onLivingHurt(LivingHurtEvent event){
            if(event.getEntity() instanceof Player player){
                player.getCapability(PlayerZonesProvider.PLAYER_ZONES).ifPresent(zones->{
                    if(event.getSource()!=player.damageSources().fall()){
                        double distanceFromSpawn=Math.sqrt(Math.pow(player.getX(),2)+Math.pow(player.getZ(),2));//i want to replace this with the true respawn position
                        
                        BlockPos spawnPosition=((ServerPlayer) player).getRespawnPosition();//this always ends up being null
                        //if(spawnPosition != null)
                            player.sendSystemMessage(Component.literal(Integer.toString(spawnPosition.getX())));
                        int currentZone=zones.getCurrentZone(distanceFromSpawn);
                        player.hurt(player.damageSources().fall(),event.getAmount()*(currentZone+1));
                    }
                });
            }
        }

However, it always crashes immediately because spawnPosition is null: Caused by: java.lang.NullPointerException: Cannot invoke "net.minecraft.core.BlockPos.getX()" because "spawnPosition" is null

What am I doing wrong?

Link to comment
Share on other sites

The player doesn’t have a respawn point because you haven’t used a bed or respawn anchor or something. When the players respawn point is null and the player wants to respawn, the server calculates when the player should respawn. If getRespawnPosition is null, get the world spawn from the ServerLevel

  • Thanks 1
Link to comment
Share on other sites

9 minutes ago, Xoroshio said:

The player doesn’t have a respawn point because you haven’t used a bed or respawn anchor or something. When the players respawn point is null and the player wants to respawn, the server calculates when the player should respawn. If getRespawnPosition is null, get the world spawn from the ServerLevel

Thank you so much

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.