Jump to content

Simple Mob Spawning Question


ldodds

Recommended Posts

Hi,

 

I'm trying out minecraft forge for a new mod. However I'm having trouble getting mob spawning to work. I'm not using a custom mob, just trying to get a sheep to spawn in the world. However when I do that the mob doesn't seem to get properly spawned: it doesn't move, react and there's no collisions.

 

I have the following, very simple code that executes in an object that implements the ITickHandler interface, specifically the tickStart method. The tick handler is registered with the TickFactory using Side.SERVER.

 

 

							  Minecraft minecraft = Minecraft.getMinecraft();
						  World world = minecraft.theWorld;
						  EntityClientPlayerMP player = minecraft.thePlayer;
						  
						  ChunkCoordinates coords = player.getPlayerCoordinates();							  
						  EntitySheep sheep = new EntitySheep( world );
						  sheep.setLocationAndAngles(coords.posX, coords.posY, coords.posZ, 0F, 0F);
						  sheep.initCreature();
						  world.spawnEntityInWorld(sheep);

 

 

The sheep appears, but doesn't do anything. I've looked over other code in the source and examples elsewhere and this seems like it should work.

 

I've seen references to checking world.isRemote but that now seems to always return true. I've also tried checking FMLCommonHandler.instance().getEffectiveSide() and have confirmed that is Side.SERVER when the code is running. So as far as I can tell the sheep should be spawning on the server side.

 

I've seen other people have similar problems, so I'm guessing I'm overlooking something simple. Anyone have any ideas?

 

Update: I've noticed that the World object returned by Minecraft.theWorld is an instance of WorldClient. I'm guessing this might be the issue. Assuming that is the problem, how do I reliably get access to the server world?

Link to comment
Share on other sites

OK, I've figured out my issue, so am replying to my own question :)

 

It would seem that the following code always returns objects from the client:

 

 

Minecraft minecraft = Minecraft.getMinecraft();
EntityClientPlayerMP player = minecraft.thePlayer;
//this is actually an instance of WorldClient
World world = player.worldObj;

 

 

The clue is there in the class name of the player. Obvious in hindsight. To properly spawn mobs you need to spawn them on the server. So you need a reference to an instance of EntityPlayerMP and from that you can get a WorldServer instance.

 

In my case I have a ITickHandler instance registered on Side.SERVER and a tick() method that returns the following:

 

 

		  public EnumSet<TickType> ticks() {
		return EnumSet.of(TickType.PLAYER);
	  }

 

 

When the tickStart method is called on the handler the first item in the tickData array is a reference to the player and is an instance of EntityPlayerMP. I needed to use this object to retrieve a reference to the players world.

 

Spawning the entity then works as expected.

 

Hope that helps someone else struggling with similar issues.

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.