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?