Jump to content

[1.16.1] Spawn new passive entity in villages


Niksilp

Recommended Posts

I'm making a mod to add new cats but I'm having trouble figuring out how to get them to spawn naturally in villages similar to the vanilla cat. I found this post that seems to be exactly what I'm looking for, but "Feature.VILLAGE.isPositionInStructure" doesn't seem to be valid in 1.16.1. I've looked at CatSpawner.class and tried replicating that, and my mod builds and the game launches with no errors but my custom entities still aren't spawning as expected. I am able to manually spawn them with the summon command.

 

I don't necessarily need my custom cat entity to spawn following the same rules as the vanilla cats -- just being able to specify their spawn weight and min/max group size would be fine, e.g. 

biome.getSpawns(EntityClassification.CREATURE).add(new Biome.SpawnListEntry(ModEntityType.NEW_CAT.get(), 100, 1, 3));

 

Here's what I have currently:

 

package morecats.world.spawner;

import java.util.List;

import morecats.MoreCats;
import morecats.entity.passive.NewCatEntity;
import morecats.init.ModEntityType;
import net.minecraft.entity.EntityClassification;
import net.minecraft.entity.ILivingEntityData;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.passive.CatEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.village.PointOfInterestManager;
import net.minecraft.village.PointOfInterestType;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = MoreCats.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class ModEntitySpawns {

	@SubscribeEvent
	public int generateNewCats(WorldEvent.PotentialSpawns event) {
		if (event.getType() == EntityClassification.CREATURE) {
          if (((ServerWorld) event.getWorld()).func_241119_a_(event.getPos(), 2)) {
              return this.func_221121_a((ServerWorld) event.getWorld(), event.getPos());
          }
		}
		return 0;
	}
	
	private int func_221121_a(ServerWorld worldIn, BlockPos p_221121_2_) {
		int i = 48;
		if (worldIn.getPointOfInterestManager().getCountInRange(PointOfInterestType.HOME.getPredicate(), p_221121_2_, 48, PointOfInterestManager.Status.IS_OCCUPIED) > 4L) {
			List<CatEntity> list = worldIn.getEntitiesWithinAABB(CatEntity.class, (new AxisAlignedBB(p_221121_2_)).grow(48.0D, 8.0D, 48.0D));
			if (list.size() < 5) {
				return this.spawnNewCat(p_221121_2_, worldIn);
			}
		}
		return 0;
	}
	
	private int spawnNewCat(BlockPos pos, World worldIn) {
		NewCatEntity newcatentity = ModEntityType.NEW_CAT.get().create(worldIn);
		if (newcatentity == null) {
			return 0;
		} else {
			newcatentity.onInitialSpawn(worldIn, worldIn.getDifficultyForLocation(pos), SpawnReason.NATURAL, (ILivingEntityData)null, (CompoundNBT)null);
			newcatentity.moveToBlockPosAndAngles(pos, 0.0F, 0.0F);
			worldIn.addEntity(newcatentity);
			return 1;
		}
	}
}

 

I'm new to modding and also (re-)learning Java, so I've been through a few tutorials on creating new entities (this tutorial was particularly helpful), I've looked at the source code for other mods that seemed like they might do something similar to what I was looking for, and I feel like I've looked through the Forge classes pretty thoroughly, but I'm struggling to figure this one out. Any help is appreciated!

 

Let me know if there's any additional code I should share.

Link to comment
Share on other sites

First of all, Code Style 4.

Second of all, copy-pasting code doesn't teach you anything. It's just a lazy-excuse of the current level of understanding you are lacking.

1 hour ago, Niksilp said:

doesn't seem to be valid in 1.16.1

Third of all, update to the latest minor. There are new ways of handling biome spawns, basically Problematic Code 11.

Fourth of all, if you can't find the method in your version, it probably means the mappings aren't updated.

Fifth of all, to negate the fourth point, you should try and add a new JigsawPiece to add an entity spawn to the village. It's all handled via nbt files nowadays and you can construct your piece to be generated along with the village. How to do this is left as an exercise for you.

2 hours ago, Niksilp said:

(this tutorial was particularly helpful)

Sixth of all, don't use him as a style guide. He promotes bad practices in Java.

2 hours ago, Niksilp said:

I'm new to modding and also (re-)learning Java,

Seventh, relearn Java first, then come back to modding. It's better to have a good foundation else you're gonna pick up bad practices.

2 hours ago, Niksilp said:

I've looked at the source code for other mods that seemed like they might do something similar to what I was looking for

Eighth, you're more or less trying to find an answer without attempting to dive into the source code of Minecraft and problem solve your way through it.

 

I'm not trying to be rude, but not understanding what you're doing is a detriment to your overall ability. This is the advice I will give you, I hope you take it to heart.

Link to comment
Share on other sites

I agree with a lot of your points Ash

21 hours ago, ChampionAsh5357 said:

you're more or less trying to find an answer without attempting to dive into the source code of Minecraft and problem solve your way through it.

But not everything has to be done by brute-forcing your way through MC source code. There's no reason to suffer if there's an existing model and it's done right. The code of popular mods worked on by large teams, are usually done right. And vanilla sometimes uses practices that mods can't access. Otherwise, yes vanilla is an invaluable tool. 

Edited by urbanxx001
Link to comment
Share on other sites

Just now, urbanxx001 said:

There's no reason to suffer if there's an existing model and it's done right.

I'm not saying you have to brute-force your way through MCs source. My argument is that their first response is to look for someone else's answer instead of trying to solve it themselves. If you're not willing to suffer and put in the effort to try and solve the problem, then you're not going to do well as a programmer. It conditions you to rely on someone else rather than adapt your knowledge to solve the problem.

 

There is nothing wrong about using an existing model, I do it all the time. However, that's only once I've gotten to the point where I've spent hours reading through documentation and testing to see if I can create a valid and correct solution to the problem. It allows me to learn about the current library I am using and how to better improve my own knowledge to complete it.

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



×
×
  • Create New...

Important Information

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