Jump to content

[1.16.2] How do I teleport a player to their spawnpoint


naelsun

Recommended Posts

2 hours ago, naelsun said:

I wanted to teleport a player to their spawnpoint (bed, respawn anchor or if those two are unavailable the world spawn), but i can't find anything, could you help please?

The code to get the respawn point is kind of confusing, but I think the function func_241140_K_(), in the ServerPlayerEntity class returns a BlockPos which represents the latest used bed/respawn anchor. This can however be null if the player has not used a bed/respawn anchor. Therefore this BlockPos value should be used as parameter to the method func_242374_a() in PlayerEntity, which return an optional with a potential respawn point.

 

You can get an idea of how vanilla does it by following the call hierarchy of the event PlayerEvent.Clone, which is called when a player respawns.

 

P.S. Just out of curiosity, does this have anything to do with the golden chorus fruit you were working on? :)

Edited by vemerion
Add P.S.
Link to comment
Share on other sites

    BlockPos bed = PlayerEntity.func_242374_a();
    double px = bed.getX();
    double py = bed.getY();
    double pz = bed.getZ();
    Minecraft.getInstance().player.sendChatMessage( "/tp " + px + " " + py + " " + pz );

 

Well this definetly seems too complex to me, that is what i have after some head scratching and i doubt i'm even close to it, since the func is asking for 5 arguments that i have no clue what might be, and even though i think i understand how the PlayerEvent.Clone event works i have no idea how to implement it. So what i'm asking is: am i close to getting it? Thank you again for helping btw!

Edited by naelsun
Link to comment
Share on other sites

53 minutes ago, naelsun said:

    BlockPos bed = PlayerEntity.func_242374_a();
    double px = bed.getX();
    double py = bed.getY();
    double pz = bed.getZ();
    Minecraft.getInstance().player.sendChatMessage( "/tp " + px + " " + py + " " + pz );

 

Well this definetly seems too complex to me, that is what i have after some head scratching and i doubt i'm even close to it, since the func is asking for 5 arguments that i have no clue what might be, and even though i think i understand how the PlayerEvent.Clone event works i have no idea how to implement it. So what i'm asking is: am i close to getting it? Thank you again for helping btw!

You are on the right track, and I agree that the vanilla respawn code is complex. Here is how I solved it: (Make sure to put this after a !world.isRemote check to avoid reaching across sides)

ServerPlayerEntity player = (ServerPlayerEntity) event.player; // Get the player somehow (from a parameter or similar)
ServerWorld respawnWorld = player.server.getWorld(player.func_241141_L_());
BlockPos spawn = player.func_241140_K_();
float spawnAngle = player.func_242109_L();
boolean forcedSpawn = false;
boolean preserveAnchorCharges = true;
Optional<Vector3d> maybeSpawn = PlayerEntity.func_242374_a(respawnWorld, spawn, spawnAngle,
  forcedSpawn, preserveAnchorCharges);

Then the maybeSpawn will contain the respawn position of the player, and the respawnWorld will be the world in which the player will respawn. Of course, the maybeSpawn can be empty, and then you have to get the respawn position from the world.

  • Thanks 1
Link to comment
Share on other sites

Thank you so much for all your help, but i still couldn't make it work, is it because im using the LivingEntity parameter instead of the Player one?

 

@SubscribeEvent
public static void onFoodEaten(LivingEntityUseItemEvent.Finish event) {
    if (event.getItem().getItem() == RegistryHandler.GOLDEN_CHORUS_FRUIT.get()) {
        if (!event.getEntityLiving().getEntityWorld().isRemote) {
            ServerPlayerEntity player = (ServerPlayerEntity) event.getEntityLiving();
            ServerWorld respawnWorld = player.server.getWorld(player.func_241141_L_());
            BlockPos spawn = player.func_241140_K_();
            float spawnAngle = player.func_242109_L();
            boolean forcedSpawn = false;
            boolean preserveAnchorCharges = true;
            Optional<Vector3d> maybeSpawn = PlayerEntity.func_242374_a(respawnWorld, spawn, spawnAngle,
                    forcedSpawn, preserveAnchorCharges);
        }
    }
}
Link to comment
Share on other sites

Quote

Then the maybeSpawn will contain the respawn position of the player, and the respawnWorld will be the world in which the player will respawn. Of course, the maybeSpawn can be empty, and then you have to get the respawn position from the world.

The code Vemerion gave you is not complete. You now got the position of the spawnpoint, but you've never moved the player.

Link to comment
Share on other sites

17 minutes ago, poopoodice said:

The code Vemerion gave you is not complete. You now got the position of the spawnpoint, but you've never moved the player.

I’m really sorry, i should have reaserched more before trying to make a mod, every answer i get there is something i don’t understand, like what do you mean by moving the player? Sorry for bothering so much

Link to comment
Share on other sites

8 hours ago, naelsun said:

I’m really sorry, i should have reaserched more before trying to make a mod, every answer i get there is something i don’t understand, like what do you mean by moving the player? Sorry for bothering so much

To be honest, the most fun and exciting part of modding (and programming in general) is the problem solving part. The joy and excitement you feel when you solve a difficult problem and the whole mod falls into place. I don't want to rob you of that feeling by handing any more code to you. I would rather see that you solve this last part by inspecting vanilla code, looking at online tutorials, and of course by using your own ingenuity.

 

(This is not to say that you should never ask for help on the forums, but I firmly believe that you are smart enough to solve this problem yourself)

  • Like 1
Link to comment
Share on other sites

2 minutes ago, vemerion said:

To be honest, the most fun and exciting part of modding (and programming in general) is the problem solving part. The joy and excitement you feel when you solve a difficult problem and the whole mod falls into place. I don't want to rob you of that feeling by handing any more code to you. I would rather see that you solve this last part by inspecting vanilla code, looking at online tutorials, and of course by using your own ingenuity.

 

(This is not to say that you should never ask for help on the forums, but I firmly believe that you are smart enough to solve this problem yourself)

Well i agree that solving is the best part, i think it's just that i tried to do too much too fast, i just woke up now and i have been now looking at libraries trying to understand what's left to do for the last hour, also i have to thank you for not just giving me the solution, i just didn't realise because i misread, i really want to learn, i guess i'll just have to brainstorm a lot, so thank you again, i'll give you an update if i can find anything

 

15 hours ago, vemerion said:

P.S. Just out of curiosity, does this have anything to do with the golden chorus fruit you were working on? :)

(i'm a bit late now but as you saw, yes, yes it does) 

  • Like 1
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.