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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Baba  Serege [[+27-73 590 8989]] has experience of 27 years in helping and guiding many people from all over the world. His psychic abilities may help you answer and resolve many unanswered questions. He specialize in helping women and men from all walks of life.. 1) – Bring back lost lover. even if lost for a long time. 2) – My lover is abusing alcohol, partying and cheating on me I urgently need help” 3) – Divorce or court issues. 4) – Is your love falling apart? 5) – Do you want your love to grow stronger? 6) – Is your partner losing interest in you? 7) – Do you want to catch your partner cheating on you? – We help to keep your partner faithful and loyal to you. 9) – We recover love and happiness when relationship breaks down. 10) – Making your partner loves you alone. 11) – We create loyalty and everlasting love between couples. 12) – Get a divorce settlement quickly from your ex-partner. 13) – We create everlasting love between couples. 14) – We help you look for the best suitable partner. 15) – We bring back lost lover even if lost for a long time. 16) – We strengthen bonds in all love relationship and marriages 17) – Are you an herbalist who wants to get more powers? 18) – Buy a house or car of your dream. 19) – Unfinished jobs by other doctors come to me. 20) – I help those seeking employment. 21) – Pensioners free treatment. 22) – Win business tenders and contracts. 23) – Do you need to recover your lost property? 24) – Promotion at work and better pay. 25) – Do you want to be protected from bad spirits and nightmares? 26) – Financial problems. 27) – Why you can’t keep money or lovers? 28) – Why you have a lot of enemies? 29) – Why you are fired regularly on jobs? 30) – Speed up money claim spell, delayed payments, pension and accident funds 31) – I help students pass their exams/interviews. 33) – Removal of bad luck and debts. 34) – Are struggling to sleep because of a spiritual wife or husband. 35- ) Recover stolen property
    • OLXTOTO adalah situs bandar togel online resmi terbesar dan terpercaya di Indonesia. Bergabunglah dengan OLXTOTO dan nikmati pengalaman bermain togel yang aman dan terjamin. Koleksi toto 4D dan togel toto terlengkap di OLXTOTO membuat para member memiliki pilihan taruhan yang lebih banyak. Sebagai situs togel terpercaya, OLXTOTO menjaga keamanan dan kenyamanan para membernya dengan sistem keamanan terbaik dan enkripsi data. Transaksi yang cepat, aman, dan terpercaya merupakan jaminan dari OLXTOTO. Nikmati layanan situs toto terbaik dari OLXTOTO dengan tampilan yang user-friendly dan mudah digunakan. Layanan pelanggan tersedia 24/7 untuk membantu para member. Bergabunglah dengan OLXTOTO sekarang untuk merasakan pengalaman bermain togel yang menyenangkan dan menguntungkan.
    • Baba  Serege [[+27-73 590 8989]] has experience of 27 years in helping and guiding many people from all over the world. His psychic abilities may help you answer and resolve many unanswered questions. He specialize in helping women and men from all walks of life.. 1) – Bring back lost lover. even if lost for a long time. 2) – My lover is abusing alcohol, partying and cheating on me I urgently need help” 3) – Divorce or court issues. 4) – Is your love falling apart? 5) – Do you want your love to grow stronger? 6) – Is your partner losing interest in you? 7) – Do you want to catch your partner cheating on you? – We help to keep your partner faithful and loyal to you. 9) – We recover love and happiness when relationship breaks down. 10) – Making your partner loves you alone. 11) – We create loyalty and everlasting love between couples. 12) – Get a divorce settlement quickly from your ex-partner. 13) – We create everlasting love between couples. 14) – We help you look for the best suitable partner. 15) – We bring back lost lover even if lost for a long time. 16) – We strengthen bonds in all love relationship and marriages 17) – Are you an herbalist who wants to get more powers? 18) – Buy a house or car of your dream. 19) – Unfinished jobs by other doctors come to me. 20) – I help those seeking employment. 21) – Pensioners free treatment. 22) – Win business tenders and contracts. 23) – Do you need to recover your lost property? 24) – Promotion at work and better pay. 25) – Do you want to be protected from bad spirits and nightmares? 26) – Financial problems. 27) – Why you can’t keep money or lovers? 28) – Why you have a lot of enemies? 29) – Why you are fired regularly on jobs? 30) – Speed up money claim spell, delayed payments, pension and accident funds 31) – I help students pass their exams/interviews. 33) – Removal of bad luck and debts. 34) – Are struggling to sleep because of a spiritual wife or husband. 35- ) Recover stolen property
    • BD303 merupakan salah satu situs slot mudah scatter paling populer dan digemari oleh kalangan slot online di tahun 2024 mainkan sekarang dengan kesempatan yang mudah menang jackpot jutaan rupiah.
  • Topics

×
×
  • Create New...

Important Information

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