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

    • OLXTOTO - Bandar Togel Online Dan Slot Terbesar Di Indonesia OLXTOTO telah lama dikenal sebagai salah satu bandar online terkemuka di Indonesia, terutama dalam pasar togel dan slot. Dengan reputasi yang solid dan pengalaman bertahun-tahun, OLXTOTO menawarkan platform yang aman dan andal bagi para penggemar perjudian daring. DAFTAR OLXTOTO DISINI DAFTAR OLXTOTO DISINI DAFTAR OLXTOTO DISINI Beragam Permainan Togel Sebagai bandar online terbesar di Indonesia, OLXTOTO menawarkan berbagai macam permainan togel. Mulai dari togel Singapura, togel Hongkong, hingga togel Sidney, pemain memiliki banyak pilihan untuk mencoba keberuntungan mereka. Dengan sistem yang transparan dan hasil yang adil, OLXTOTO memastikan bahwa setiap taruhan diproses dengan cepat dan tanpa keadaan. Slot Online Berkualitas Selain togel, OLXTOTO juga menawarkan berbagai permainan slot online yang menarik. Dari slot klasik hingga slot video modern, pemain dapat menemukan berbagai opsi permainan yang sesuai dengan preferensi mereka. Dengan grafis yang memukau dan fitur bonus yang menggiurkan, pengalaman bermain slot di OLXTOTO tidak akan pernah membosankan. Keamanan dan Kepuasan Pelanggan Terjamin Keamanan dan kepuasan pelanggan merupakan prioritas utama di OLXTOTO. Mereka menggunakan teknologi enkripsi terbaru untuk melindungi data pribadi dan keuangan para pemain. Tim dukungan pelanggan yang ramah dan responsif siap membantu pemain dengan setiap pertanyaan atau masalah yang mereka hadapi. Promosi dan Bonus Menarik OLXTOTO sering menawarkan promosi dan bonus menarik kepada para pemainnya. Mulai dari bonus selamat datang hingga bonus deposit, pemain memiliki kesempatan untuk meningkatkan kemenangan mereka dengan memanfaatkan berbagai penawaran yang tersedia. Penutup Dengan reputasi yang solid, beragam permainan berkualitas, dan komitmen terhadap keamanan dan kepuasan pelanggan, OLXTOTO tetap menjadi salah satu pilihan utama bagi para pecinta judi online di Indonesia. Jika Anda mencari pengalaman berjudi yang menyenangkan dan terpercaya, OLXTOTO layak dipertimbangkan.
    • I have been having a problem with minecraft forge. Any version. Everytime I try to launch it it always comes back with error code 1. I have tried launching from curseforge, from the minecraft launcher. I have also tried resetting my computer to see if that would help. It works on my other computer but that one is too old to run it properly. I have tried with and without mods aswell. Fabric works, optifine works, and MultiMC works aswell but i want to use forge. If you can help with this issue please DM on discord my # is Haole_Dawg#6676
    • Add the latest.log (logs-folder) with sites like https://paste.ee/ and paste the link to it here  
    • I have no idea how a UI mod crashed a whole world but HUGE props to you man, just saved me +2 months of progress!  
    • So i know for a fact this has been asked before but Render stuff troubles me a little and i didnt find any answer for recent version. I have a custom nausea effect. Currently i add both my nausea effect and the vanilla one for the effect. But the problem is that when I open the inventory, both are listed, while I'd only want mine to show up (both in the inv and on the GUI)   I've arrived to the GameRender (on joined/net/minecraft/client) and also found shaders on client-extra/assets/minecraft/shaders/post and client-extra/assets/minecraft/shaders/program but I'm lost. I understand that its like a regular screen, where I'd render stuff "over" the game depending on data on the server, but If someone could point to the right client and server classes that i can read to see how i can manage this or any tip would be apreciated
  • Topics

×
×
  • Create New...

Important Information

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