Jump to content

[1.16.5] Why is my mob instantly despawning?


SapphireSky

Recommended Posts

I have two mobs. The first someone else helped me get to spawn properly the other day, but my second won't spawn at all.

Or specifically, it's for some reason removed from the world immediately after it's added to the world.

Manually spawning it with commands works fine, it's just natural spawning that doesn't work.

Both mobs are registered/spawned the same way, are extensions of CreatureEntity, and use standard AI with nothing that would trigger their instant removal.

What's going on here?

 

Entity registry:

Spoiler
public class EntityRegistry
{
	public static final HashMap<EntityType<?>, String> names = new HashMap<EntityType<?>, String>();

	public static final DeferredRegister<EntityType<?>> registry = DeferredRegister.create(ForgeRegistries.ENTITIES, Main.modid);

	public static final RegistryObject<EntityType<EntityBokoblin>> bokoblin = register("bokoblin", "Bokoblin",
			EntityType.Builder.<EntityBokoblin>of(EntityBokoblin::new, EntityClassification.CREATURE).sized(0.8f, 1.33f).clientTrackingRange(10));
  
	public static final RegistryObject<EntityType<EntityDekuBaba>> dekubaba = register("dekubaba", "Deku Baba",
			EntityType.Builder.<EntityDekuBaba>of(EntityDekuBaba::new, EntityClassification.CREATURE).sized(1f, 1.8f).clientTrackingRange(10));

	private static <T extends Entity> RegistryObject<EntityType<T>> register(String registryname, String displayname, EntityType.Builder<T> builder)
	{
		EntityType<T> entity = builder.build(new ResourceLocation(Main.modid, registryname).toString());
		names.put(entity, displayname);
		return registry.register(registryname, () -> entity);
	}

	public static void registerRenderers()
	{
		register(bokoblin.get(), RenderBokoblin::new, EntityBokoblin.createAttributes().build());
		register(dekubaba.get(), RenderDekuBaba::new, EntityDekuBaba.createAttributes().build());
	}

	@SuppressWarnings("deprecation")
	private static <T extends LivingEntity> void register(EntityType<T> entityClass, IRenderFactory<? super T> renderFactory, AttributeModifierMap map)
	{
		RenderingRegistry.registerEntityRenderingHandler(entityClass, renderFactory);
		DeferredWorkQueue.runLater(() ->
		{
			GlobalEntityTypeAttributes.put(entityClass, map);
		});
	}
}

 

Spawn registry:

Spoiler

	public static final BiPredicate<IServerWorld, BlockPos> spawnrule_hostile = (world, pos) -> world.getDifficulty() != Difficulty.PEACEFUL;
	public static final BiPredicate<IServerWorld, BlockPos> spawnrule_hostile_dark = (world, pos) -> world.getDifficulty() != Difficulty.PEACEFUL && world.getMaxLocalRawBrightness(pos) <= 5;

	public static final HashMap<Category, List<Spawners>> spawnlist = new HashMap<Category, List<Spawners>>();
	public static final HashMap<EntityType<Entity>, IPlacementPredicate<Entity>> spawnpredicates = new HashMap<EntityType<Entity>, IPlacementPredicate<Entity>>();

	public static <T extends MobEntity> void create(final FMLCommonSetupEvent event)
	{
		createSpawn(Category.BEACH, from(EntityRegistry.bokoblin, 100, 2, 4));
		createSpawn(Category.RIVER, from(EntityRegistry.bokoblin, 100, 1, 2));
		createSpawn(Category.OCEAN, from(EntityRegistry.bokoblin, 80, 2, 4));
		createSpawn(Category.PLAINS, from(EntityRegistry.bokoblin, 80, 1, 2));

		createSpawn(Category.FOREST, from(EntityRegistry.dekubaba, 100));
		createSpawn(Category.PLAINS, from(EntityRegistry.dekubaba, 65));
		createSpawn(Category.SWAMP, from(EntityRegistry.dekubaba, 50));

		event.enqueueWork(() ->
		{
			spawn(EntityRegistry.bokoblin, PlacementType.ON_GROUND, Type.WORLD_SURFACE, spawnrule_hostile);
			spawn(EntityRegistry.dekubaba, PlacementType.ON_GROUND, Type.WORLD_SURFACE, spawnrule_hostile);
		});
	}

	private static void createSpawn(Category biome, Spawners... spawners)
	{
		List<Spawners> list = spawnlist.containsKey(biome) ? spawnlist.get(biome) : new ArrayList<Spawners>();
		for (Spawners spawner : spawners)
		{
			list.add(spawner);
		}
		spawnlist.put(biome, list);
	}

	private static <T extends LivingEntity> Spawners from(RegistryObject<EntityType<T>> entity, int weight, int min, int max)
	{
		return new Spawners(entity.get(), weight, min, max);
	}

	private static <T extends LivingEntity> Spawners from(RegistryObject<EntityType<T>> entity, int weight)
	{
		return from(entity, weight, 1, 1);
	}

	private static <T extends MobEntity> void spawn(RegistryObject<EntityType<T>> entity, PlacementType placement, Heightmap.Type spawntype, BiPredicate<IServerWorld, BlockPos> condition)
	{
		EntitySpawnPlacementRegistry.register(entity.get(), placement, spawntype, (e, world, reason, pos, random) -> true);
	}

 

Events to check whether something is spawning/despawning:

Spoiler
// This tells me the mobs ARE always spawnning
	@SubscribeEvent
	public void onSpawn(EntityJoinWorldEvent event)
	{
		if (event.getEntity() instanceof FMob)
		{
			Tools.dev(event.getWorld(), "Spawning " + event.getEntity().getDisplayName().getString() + " at: " + event.getEntity().blockPosition().getX() + ", " + event.getEntity().blockPosition().getY() + ", "
					+ event.getEntity().blockPosition().getZ());
		}
	}

// This tells me that Bokoblin for some reason occasionally despawns instantly, but that Deku Baba ALWAYS despawns instantly
	@SubscribeEvent
	public void despawn(EntityLeaveWorldEvent event)
	{
		if (event.getEntity() instanceof FMob)
		{
			Tools.dev(event.getWorld(), "Deleting " + event.getEntity().getDisplayName().getString() + " from: " + event.getEntity().blockPosition().getX() + ", " + event.getEntity().blockPosition().getY() + ", "
					+ event.getEntity().blockPosition().getZ());
		}
	}

 

Entity class that DOES spawn:

Spoiler
public class EntityBokoblin extends FMob //FMob extends CreatureEntity - is an abstract class used for various mod-specific getters
{
	public EntityBokoblin(EntityType<? extends EntityBokoblin> type, World world)
	{
		super(type, world);
	}

	public static AttributeModifierMap.MutableAttribute createAttributes()
	{
		MutableAttribute attributes = MonsterEntity.createMonsterAttributes();
		attributes.add(Attributes.MAX_HEALTH, 15);
		attributes.add(Attributes.ATTACK_DAMAGE, 3);
		attributes.add(Attributes.MOVEMENT_SPEED, (double) 0.4F);
		attributes.add(Attributes.FOLLOW_RANGE, 24);
		attributes.add(Attributes.ARMOR, 1);
		return attributes;
	}

	@Override
	public void registerGoals()
	{
		this.goalSelector.addGoal(2, new MeleeAttackGoal(this, 1.0D, false));
		this.goalSelector.addGoal(6, new MoveThroughVillageGoal(this, 1.0D, true, 4, () -> false));
		this.goalSelector.addGoal(7, new WaterAvoidingRandomWalkingGoal(this, 1.0D));
		this.goalSelector.addGoal(8, new LookAtGoal(this, PlayerEntity.class, 8.0F));
		this.goalSelector.addGoal(8, new LookRandomlyGoal(this));
		this.targetSelector.addGoal(1, (new HurtByTargetGoal(this)).setAlertOthers(EntityBokoblin.class));
		this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, PlayerEntity.class, true));
		this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, AbstractVillagerEntity.class, false));
		this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, AnimalEntity.class, true));
	}

	@Override
	public boolean isHostile() // From FMob
	{
		return true;
	}

	@Override
	public void addDrops() // From FMob
	{
		droplist.add(new MobDrop(MaterialRegistry.rupeegreen.get(), 1, 100));
		droplist.add(new MobDrop(MaterialRegistry.rupeered.get(), 1, 30));
		droplist.add(new MobDrop(Items.ARROW, 3, 100, true));
		droplist.add(new MobDrop(EquipmentRegistry.dekunut.get(), 1, 20));
	}

	@Override
	public Element getMobElement() // From FMob
	{
		return Elements.None;
	}

	@Override
	public int getExp() // From FMob
	{
		return 5;
	}

	@Override
	public boolean disruptsPlayerSleep() // From FMob
	{
		return true;
	}
}

 

 

Entity class that DOESN'T spawn:

Spoiler
public class EntityDekuBaba extends FMob implements IRangedAttackMob
{ //FMob extends CreatureEntity - is an abstract class used for various mod-specific getters
  
    //Identical to vanilla skeleton's arrow shooting goal, only with pathfinding removed because mob is stationary
	private final DekuBabaShootGoal<EntityDekuBaba> rangedgoal = new DekuBabaShootGoal<>(this, 3f); 
    //Identical to vanilla melee attack goal, only with pathfinding removed because mob is stationary
	private final DekuBabaMeleeGoal meleeGoal = new DekuBabaMeleeGoal(this);
	public int breathingticks; //Only used for model animation

	public EntityDekuBaba(EntityType<? extends EntityDekuBaba> entity, World world)
	{
		super(entity, world);
	}

	public EntityDekuBaba(World world)
	{
		this(EntityRegistry.dekubaba.get(), world);
	}

	@Override
	protected void registerGoals()
	{
		this.goalSelector.addGoal(6, new LookAtGoal(this, PlayerEntity.class, 15.0F));
		this.goalSelector.addGoal(6, new LookRandomlyGoal(this));
		this.targetSelector.addGoal(1, new HurtByTargetGoal(this));
		this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, PlayerEntity.class, true));
		this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, AnimalEntity.class, true));
	}

	public static AttributeModifierMap.MutableAttribute createAttributes()
	{
		MutableAttribute attributes = MonsterEntity.createMonsterAttributes();
		attributes.add(Attributes.MAX_HEALTH, 20);
		attributes.add(Attributes.ATTACK_DAMAGE, 7);
		attributes.add(Attributes.MOVEMENT_SPEED, 0);
		attributes.add(Attributes.FOLLOW_RANGE, 16);
		attributes.add(Attributes.ARMOR, 0);
		attributes.add(Attributes.KNOCKBACK_RESISTANCE, 9999);
		return attributes;
	}

	@Override
	public boolean isHostile() // From FMob
	{
		return true;
	}

	@Override
	public void addDrops() // From FMob
	{
		droplist.add(new MobDrop(Items.STICK, 5, 500, true));
		droplist.add(new MobDrop(MiscRegistry.dekuseed.get(), 2, 250, true));
		droplist.add(new MobDrop(EquipmentRegistry.dekunut.get(), 1, 50));
	}

	@Override
	public int getExp() // From FMob
	{
		return 8;
	}

	@Override
	public boolean disruptsPlayerSleep() // From FMob
	{
		return true;
	}

	@Override
	public Element getMobElement() // From FMob
	{
		return Elements.Earth;
	}

	@Override
	public boolean canBeLeashed(PlayerEntity player)
	{
		return false;
	}

	@Override
	public boolean isPushable() // This apparently does nothing?
	{
		return false;
	}

	public boolean targetInMeleeRange()
	{
		return getTarget().distanceTo(this) <= 4;
	}

	// Melee attack is AOE spin attack where the model stretches out, so the collision box should expand when lashing out
	@Override
	public AxisAlignedBB getBoundingBox()
	{
		return swingTime > 0 ? super.getBoundingBox().inflate(2, 0, 2) : super.getBoundingBox();
	}

	@Override
	public void aiStep() //If target is close, switch to melee AI. If not close, switch to projectile AI
	{
		setGoalAsMelee(hasTarget() && targetInMeleeRange());
		super.aiStep();
	}

	@Override
	public void tick() //Update model animation & prevent mob from moving unless falling
	{
		--breathingticks;
		if (breathingticks <= -45)
		{
			breathingticks = 45;
		}

		super.tick();

		Vector3d cancelledpush = new Vector3d(0, getDeltaMovement().y, 0);
		setDeltaMovement(cancelledpush);
	}

	public void setGoalAsMelee(boolean ismelee)
	{
		if (this.level != null && !this.level.isClientSide)
		{
			this.goalSelector.removeGoal(this.meleeGoal);
			this.goalSelector.removeGoal(this.rangedgoal);
			if (ismelee)
			{
				this.goalSelector.addGoal(4, this.meleeGoal);
			}
			else
			{
				this.goalSelector.addGoal(4, this.rangedgoal);
			}
		}
	}

	@Override
	public void performRangedAttack(LivingEntity entity, float damage)
	{
		double d0 = distanceToSqr(entity);
		double d1 = entity.getX() - getX();
		double d2 = entity.getY(0.5D) - getY(0.5D);
		double d3 = entity.getZ() - getZ();
		float f = MathHelper.sqrt(MathHelper.sqrt(d0)) * 0.25F;
		EntityDekuSeed seed = new EntityDekuSeed(this, level, d1 + getRandom().nextGaussian() * (double) f, d2, d3 + getRandom().nextGaussian() * (double) f);
		seed.setPos(seed.getX(), getY(0.5D) + 0.5D, seed.getZ());
		level.addFreshEntity(seed);

		this.playSound(SoundEvents.CROSSBOW_SHOOT, 1.0F, 1.0F / (this.getRandom().nextFloat() * 0.4F + 0.8F));
	}

	@Override
	public boolean canFireProjectileWeapon(ShootableItem item) //Apparently still needed to shoot even though it has no item?
	{
		return true;
	}
}

 

 

Link to comment
Share on other sites

10 minutes ago, diesieben07 said:

Mods should be open source.

But whatever.

Great, so you won't help me because you put your opinion at higher value than my worries, even though it costs you literally nothing extra to just do it my way. Got it.

I honestly don't know why I bother asking for help on this site anymore. Just delete my account for all I care.

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.