Jump to content

Any way to check if the player is spawned?


freeradicalx
 Share

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
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.

 Share



×
×
  • Create New...

Important Information

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