Jump to content

Any way to check if the player is spawned?


freeradicalx

Recommended Posts

I'm working on a worldgen mod that spawns structures in the world in ways that are dependent on the player's X/Y/Z position, so my worldgen class gets the player's position with Minecraft.getMinecraft().thePlayer. However this is crashing my game instantly upon creation of a new world since the player doesn't even exist in the world until the first few hundred chunks are generated. Is there any way to check to see if the player is currently spawned in the world so that I can bypass using player position if they're not yet? Or any other good mechanism to override/avoid this problem? Thanks!

Link to comment
Share on other sites

you can do so by obtaining the players X, Y, and Z spawn coords. The can be found in WorldInfo. I think its something like this to obtain them...

Minecraft.getMinecraft().theWorld.getWorldInfo().getSpawnY();

 

Or at least something like that :D

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

Ah, I see that. Assuming that the spawn point is set before the chunks start generating that would be a good replacement for playerX/Y/Z until the player spawns... But I still run into the problem of needing to know when the player spawns so I can switch over to using their current position instead.

Link to comment
Share on other sites

Ah, I see that. Assuming that the spawn point is set before the chunks start generating that would be a good replacement for playerX/Y/Z until the player spawns... But I still run into the problem of needing to know when the player spawns so I can switch over to using their current position instead.

Use Forge's LivingSpawnEvent, then checkcast the EntityLiving obj to EntityPlayer.

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Link to comment
Share on other sites

Ah, I see that. Assuming that the spawn point is set before the chunks start generating that would be a good replacement for playerX/Y/Z until the player spawns... But I still run into the problem of needing to know when the player spawns so I can switch over to using their current position instead.

Use Forge's LivingSpawnEvent, then checkcast the EntityLiving obj to EntityPlayer.

 

What he said ^^

 

Also, try and avoid using Minecraft.getMinecraft()  any any SMP mod, as it is a client-side only method and will not run properly if compiled/placed on dedicated server.  (unecessary for SSP only mods, but probably a good idea to do things properly from the get-go)

 

There is also a way to get a list of all logged in player entities from MinecraftServer.... I think it is located at:

 

MinecraftServer.getServer().getConfigurationManager().playerEntityList

 

Which would allow for active/realtime polling of player positions/knowing when they are online.  (And this method is SMP safe, and could safely be called on a dedicated server).

Link to comment
Share on other sites

Ah, I see that. Assuming that the spawn point is set before the chunks start generating that would be a good replacement for playerX/Y/Z until the player spawns... But I still run into the problem of needing to know when the player spawns so I can switch over to using their current position instead.

Use Forge's LivingSpawnEvent, then checkcast the EntityLiving obj to EntityPlayer.

 

What he said ^^

 

Also, try and avoid using Minecraft.getMinecraft()  any any SMP mod, as it is a client-side only method and will not run properly if compiled/placed on dedicated server.  (unecessary for SSP only mods, but probably a good idea to do things properly from the get-go)

 

There is also a way to get a list of all logged in player entities from MinecraftServer.... I think it is located at:

 

MinecraftServer.getServer().getConfigurationManager().playerEntityList

 

Which would allow for active/realtime polling of player positions/knowing when they are online.  (And this method is SMP safe, and could safely be called on a dedicated server).

 

Minecraft.getMinecraft() should not be used in a SMP mod ... server-sided! If you are sure you're on the client-side, you can freely use this (examples are renderers). If you're not sure, then always use your proxy.

MinecraftServer.getServer() will actually not work on clients connected on a dedicated server! It can be used for the internal server or dedicated server (server-side). Here you have to make sure, you only execute this server-sided. Again, if you're not sure, use your proxy.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

Thanks for all the method call tips! I haven't adventured into setting up a proxy just yet, so I think I'm gonna keep my mod single-player while I get my feet wet. Minecraft.getMinecraft() should be just fine for now in that case, right?

 

I did try setting up an event hooks class to listen for LivingSpawnEvent.entityLiving being an instance of EntityPlayer, but it never seems to evaluate true, even after I spawn. Code in question below, any idea what I'm doing wrong here?:

 

EventHooks.class:

package freeradicalx.util;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.living.LivingSpawnEvent;

public class EventHooks {

public static boolean playerSpawned = false;

@ForgeSubscribe
public void playerSpawned(LivingSpawnEvent event){
	if (event.entityLiving instanceof EntityPlayer){
		playerSpawned = true;
	}
}

public static boolean getPlayerSpawned(){
	return playerSpawned;
}

}

 

And here's how I'm registering it in my main mod class:

@PreInit
public void preInit(FMLPreInitializationEvent event){

	GameRegistry.registerWorldGenerator(roadGen);

}

 

PS ObsequiousNewt, this is the roads mod you helped me save data with with last week. The data saving method you suggested works out so far. I'm using the player position to have the starts of roads head into unloaded chunks further away from the player as to avoid ending at already-generated chunks. Check it out (Using pink wool only to test): https://www.dropbox.com/s/vzcmvyp0i1hi56t/roads.jpg

Link to comment
Share on other sites

Thanks for all the method call tips! I haven't adventured into setting up a proxy just yet, so I think I'm gonna keep my mod single-player while I get my feet wet. Minecraft.getMinecraft() should be just fine for now in that case, right?

 

I did try setting up an event hooks class to listen for LivingSpawnEvent.entityLiving being an instance of EntityPlayer, but it never seems to evaluate true, even after I spawn. Code in question below, any idea what I'm doing wrong here?:

 

-snip-

 

PS ObsequiousNewt, this is the roads mod you helped me save data with with last week. The data saving method you suggested works out so far. I'm using the player position to have the starts of roads head into unloaded chunks further away from the player as to avoid ending at already-generated chunks. Check it out (Using pink wool only to test): https://www.dropbox.com/s/vzcmvyp0i1hi56t/roads.jpg

That's not an event handler register... This is. (yes, I took it from AtomicStryker's old tutorial, but it probably works.)

MinecraftForge.EVENT_BUS.register(new EventHooks());

 

Oh, and I like the roads! Can't wait to see the finished mod!

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Link to comment
Share on other sites

Ackph! I'm sorry, that IS what I have typed into my main mod class, but I must have been tired when trimming down the code last night to paste here so I must have cut out the wrong line. I have it typed exactly as you do inside my preInit method. I'm having EventHooks() report via System.out.println() whether the entity spawning is a player or not, and it always reports false (Not a player). My code is as typed above, including your correction. Any idea what I'm doing wrong?

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • ChemsXpress (https://chemsxpress.com): ChemsXpress is a platform specializing in the distribution of research chemicals and related products. It offers a wide range of substances for scientific and laboratory use, catering to researchers and professionals. BubatzLand (https://bubatzland.com): BubatzLand focuses on providing high-quality cannabis products. The site offers a variety of strains, edibles, and related accessories, aiming to serve the needs of cannabis enthusiasts and medicinal users. HalluShroom (https://hallushroom.com): HalluShroom specializes in the sale of psychedelic mushrooms and related products. It caters to individuals interested in exploring the benefits and experiences associated with psilocybin and other psychedelic substances. HighAsch (https://highasch.com): HighAsch offers a diverse range of cannabis products, including flowers, concentrates, and edibles. The site aims to provide premium-quality products to both recreational and medicinal users. KetaminHaus (https://ketaminhaus.com): KetaminHaus provides ketamine and related products for research and therapeutic use. The platform focuses on ensuring the availability of high-quality, safe, and effective ketamine for various applications. KokainLabor (https://kokainlabor.com): KokainLabor offers cocaine and other related substances. The site targets individuals seeking high-purity products for personal or research purposes, emphasizing quality and safety. MDPHPWelt (https://mdphpwelt.com): MDPHPWelt specializes in the distribution of MDPHP and other similar research chemicals. It aims to supply researchers with high-quality substances for scientific and experimental use. NembutaLabor (https://nembutalabor.com): NembutaLabor provides Nembutal (pentobarbital) and related products. The site focuses on supplying these substances for research and veterinary purposes, ensuring high standards of quality and safety. NembutalQuelle (https://nembutalquelle.com): NembutalQuelle is dedicated to offering Nembutal and similar barbiturates. It caters to researchers and professionals who require these substances for scientific, medical, or veterinary applications. JBSaves Ltda Brazil (https://jbsavesltdabrazil.com): JBSaves Ltda Brazil is involved in the food and agricultural industry, providing a variety of products and services related to food processing, distribution, and agricultural practices. Bello Alimentos Ltda (https://belloalimentosltda.com): Bello Alimentos Ltda offers a wide range of food products. The site focuses on high-quality, nutritious, and safe food items, catering to consumers and businesses in the food industry. São Salvador Alimentos (https://saosalvadoralimentoss.com): São Salvador Alimentos specializes in the production and distribution of food products. The site provides a variety of options aimed at meeting the dietary needs and preferences of diverse consumers. WietWereld (https://wietwereld.com): WietWereld is a platform dedicated to the cannabis community. It offers information, products, and resources related to cannabis cultivation, consumption, and culture. 14. BeuhParadise (https://beuhparadis.com): BeuhParadise offers a range of cannabis products, including flowers, edibles, and concentrates. The site aims to provide high-quality cannabis products to recreational and medicinal users, emphasizing purity and potency. 15. WietWereld (https://wietwereld.com): WietWereld is a comprehensive resource for cannabis enthusiasts. It provides products, cultivation guides, and community forums for those interested in cannabis culture and cultivation. GrasOase (https://grasoase.com): GrasOase specializes in cannabis products and accessories. The site offers a variety of strains, seeds, and related items for cannabis users, focusing on quality and customer satisfaction. https://chemsxpress.com/ https://bubatzland.com/ https://hallushroom.com/ https://highasch.com/ https://ketaminhaus.com/ https://kokainlabor.com/ https://mdphpwelt.com/ https://nembutalabor.com/ https://nembutalquelle.com/ https://jbsavesltdabrazil.com/ https://belloalimentosltda.com/ https://saosalvadoralimentoss.com/ https://wietwereld.com https://beuhparadis.com https://wietwereld.com https://grasoase.com/
    • For Example I have an Main Mod that adds new Content to Minecraft. But I'm using custom Classes etc. and I want it so that is an api integrated in the Main Mod but can be download seperately like an Module. Is this possible? If you need more Information just comment what you need to know ^^
    • Delete the config of this mod (config folder) and test it again   If there is no change, try other builds  
    • Add the full crash-report or latest.log (logs-folder) with sites like https://paste.ee/ and paste the link to it here
  • Topics

×
×
  • Create New...

Important Information

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