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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello, I was trying to play a MOD in my preferred language, but I see that only some items are translated, and I go to debug and I get this information (the only thing that is translated is the bestiary):   [14sep.2024 17:14:36.415] [Render thread/WARN] [net.minecraft.client.resources.language.ClientLanguage/]: Skipped language file: mowziesmobs:lang/es_es.json (com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected name at line 394 column 2 path $.config.mowziesmobs.ice_crystal_attack_multiplier) [14sep.2024 17:14:36.421] [Render thread/WARN] [net.minecraft.client.resources.language.ClientLanguage/]: Skipped language file: iceandfire:lang/es_es.json (com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1349 column 4 path $.iceandfire.sound.subtitle.dragonflute)   Is that the reason why everything is not translated? , and is there any way to fix it? Thanks
    • I got my model to render from the models renderToBuffer method. But still not quite what I want. I want to render the model from my renderer's render method. I feel that having access to the renderer and its methods will open some doors for me later down the line. //EntityRendererProvider.Context pContext = ; I want this //ToaPlayerRenderer render = new ToaPlayerRenderer(pContext, false); // if I can get the above line to work, having the methods from the renderer class would be incredibly helpful down the line RenderType rendertype = model.renderType(p.getSkinTextureLocation()); // this should be something like render.getTextureLocation() VertexConsumer vertexconsumer = buffer.getBuffer(rendertype); model.renderToBuffer(stack, vertexconsumer, paLights, 1, 1, 1, 1, 1); // I don't want the render to happen here since it doesn't use the renderer //model.render(p, 1f, pTicks, stack, buffer, paLights); I want to render the model using this It is certainly getting closer though. Probably. I am still worried that even if pContext is initialized this new instance of the renderer class will still hit me with the classic and all too familiar "can't use static method in non-static context"
    • Hello, I am learning how to create Multipart Entities and I tried creating a PartEntity based on the EnderDragonPart code. However, when I tested summoning the entity in the game, the PartEntity appeared at position x 0, y 0, z 0 within the game. I tried to make it follow the main entity, and after testing again, the part entity followed the main entity but seemed to teleport back to x 0, y 0, z 0 every tick (I'm just guessing). I don't know how to fix this can someone help me? My github https://github.com/SteveKK666/Forge-NewWorld-1.20.1/tree/master/src/main/java/net/kk/newworldmod/entity/custom Illustration  https://drive.google.com/file/d/157SPvyQCE8GcsRXyQQkD4Dyhalz6LjBn/view?usp=drive_link Sorry for my English; I’m not very good at it. 
    • its still crashing with the same message
  • Topics

×
×
  • Create New...

Important Information

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