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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I want to download Forge 1.20.6, but when I press download, it redirects me to Adfocus and does not provide any download. I have never encountered such a problem when I downloaded Forge 1.18.2 at least 12 months ago.
    • did you find any solutions? i have the same problem and also that mahito doesnt transform
    • Here's a simple example to solve this particular issue: public class ModArmorMaterials { public static final DeferredRegister<ArmorMaterial> ARMORS = DeferredRegister.create(Registries.ARMOR_MATERIAL, MyModName.MOD_ID); public static final RegistryObject<ArmorMaterial> COPPER = registerArmor("copper", Util.make(new EnumMap<>(ArmorItem.Type.class), enumMap -> { enumMap.put(ArmorItem.Type.BOOTS, 1); enumMap.put(ArmorItem.Type.LEGGINGS, 4); enumMap.put(ArmorItem.Type.CHESTPLATE, 5); enumMap.put(ArmorItem.Type.HELMET, 2); enumMap.put(ArmorItem.Type.BODY, 4); }), 12, SoundEvents.ARMOR_EQUIP_IRON, 0.0F, 0.0F, () -> Ingredient.of(Items.COPPER_INGOT)); private static RegistryObject<ArmorMaterial> registerArmor(String pGroup, EnumMap<ArmorItem.Type, Integer> pEnumMap, int pEnchantmentValue, Holder<SoundEvent> pEquipSound, float pToughness, float pKnockbackResistance, Supplier<Ingredient> pRepairIngredient) { List<ArmorMaterial.Layer> pLayerList = List.of(new ArmorMaterial.Layer(new ResourceLocation(pGroup))); return registerArmor(pGroup, pEnumMap, pEnchantmentValue, pEquipSound, pToughness, pKnockbackResistance, pRepairIngredient, pLayerList); } private static RegistryObject<ArmorMaterial> registerArmor( String pGroup, EnumMap<ArmorItem.Type, Integer> pEnumMap, int pEnchantmentValue, Holder<SoundEvent> pEquipSound, float pToughness, float pKnockbackResistance, Supplier<Ingredient> pRepairIngredient, List<ArmorMaterial.Layer> pLayerList) { return ARMORS.register(pGroup, () -> new ArmorMaterial(pEnumMap, pEnchantmentValue, pEquipSound, pRepairIngredient, pLayerList, pToughness, pKnockbackResistance)); } public static void register(IEventBus eventBus) { ARMORS.register(eventBus); } } Essentially, the TierSortingRegistry has been removed and so you now need to set up your own DeferredRegister. Please see the minecraftforge GitHub for more information; I found this solution through their issues (https://github.com/MinecraftForge/MinecraftForge/issues/9961)
    • How did I even leave that in there? I’ll try it in a bit, that very well could be it.
  • Topics

×
×
  • Create New...

Important Information

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