Jump to content

[SOLVED] Is there any way to tell generically if a Mob is hostile


hugo_the_dwarf

Recommended Posts

With my mod I have attributes and other capabilites attached to hostile mobs, but not passive ones (Villagers, pigs, etc) this is so I can make anything that counts as an enemy have random strengths.

 

Now I thought it'd be as easy as just checking if it implements IMob however testing with another mod (that adds many more creatures) some of them did not get my new stats.

 

Is there a way where I can check "If(EntityLivingBase.CanAttackOthers)" rather "If(EntityLivingBase instanceof IMob)"

 

Now things like a Pig which never ever attacks anyone or anything will never get it, but something like the Iron Golem where it's mostly passive but can attack if something is near or if a player hits it should get it.

Link to comment
Share on other sites

With my mod I have attributes and other capabilites attached to hostile mobs, but not passive ones (Villagers, pigs, etc) this is so I can make anything that counts as an enemy have random strengths.

 

Now I thought it'd be as easy as just checking if it implements IMob however testing with another mod (that adds many more creatures) some of them did not get my new stats.

 

Is there a way where I can check "If(EntityLivingBase.CanAttackOthers)" rather "If(EntityLivingBase instanceof IMob)"

 

Now things like a Pig which never ever attacks anyone or anything will never get it, but something like the Iron Golem where it's mostly passive but can attack if something is near or if a player hits it should get it.

You could look for SharedMonsterAttributes.ATTACK_DAMAGE and see if they have it and if it is greater than zero. This is assuming they used the vanillas system though.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Checked that, there seems to be some issue tho with a few of the other person's mod.

 

Some mobs have that Attribute, but it's 0. But they are hostile, Some that are passive have it and it's 1. not good.

 

I will at least keep my check for "instanceof IMob" as it covers a large blanket of the correct mobs. I've noticed that Iron Golems don't have the attribute. But that's fine as since they are Vanilla I can just toss in "instanceof EntityIronGolem" no big deal (was going to custom cater their stats anyways so that's a double bonus)

 

Is there anyway to see or check if they have an AI task of attacking? or attacking back?

 

EDIT:

Your suggestion was very helpful, but it seems some of the mobs added are not following a generic path

Link to comment
Share on other sites

Checked that, there seems to be some issue tho with a few of the other person's mod.

 

Some mobs have that Attribute, but it's 0. But they are hostile, Some that are passive have it and it's 1. not good.

 

I will at least keep my check for "instanceof IMob" as it covers a large blanket of the correct mobs. I've noticed that Iron Golems don't have the attribute. But that's fine as since they are Vanilla I can just toss in "instanceof EntityIronGolem" no big deal (was going to custom cater their stats anyways so that's a double bonus)

 

Is there anyway to see or check if they have an AI task of attacking? or attacking back?

 

EDIT:

Your suggestion was very helpful, but it seems some of the mobs added are not following a generic path

You can loop through the tasks field and check for certain AIs such as EntityAINearestAttackableTarget, EntityAIAttackMelee, EntityAIOwnerHurtByTarget, etc.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

I'm getting this Error, when trying to use my check for the AI tasks

 

 

[00:28:07] [server thread/ERROR] [FML]: Exception caught during firing event net.minecraftforge.event.AttachCapabilitiesEvent$Entity@3b2219ae:

java.lang.NullPointerException

at htd.rot.events.EventAddCapabilities.entityCanAttack(EventAddCapabilities.java:66) ~[EventAddCapabilities.class:?]

at htd.rot.events.EventAddCapabilities.onAddCapabilitiesEntity(EventAddCapabilities.java:39) ~[EventAddCapabilities.class:?]

at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_137_EventAddCapabilities_onAddCapabilitiesEntity_AttachCapabilitiesEvent.invoke(.dynamic) ~[?:?]

at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) ~[ASMEventHandler.class:?]

at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:185) [EventBus.class:?]

at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:535) [ForgeEventFactory.class:?]

at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:520) [ForgeEventFactory.class:?]

at net.minecraft.entity.Entity.<init>(Entity.java:252) [Entity.class:?]

at net.minecraft.entity.EntityLivingBase.<init>(EntityLivingBase.java:192) [EntityLivingBase.class:?]

at net.minecraft.entity.EntityLiving.<init>(EntityLiving.java:100) [EntityLiving.class:?]

at net.minecraft.entity.passive.EntityAmbientCreature.<init>(EntityAmbientCreature.java:11) [EntityAmbientCreature.class:?]

at net.minecraft.entity.passive.EntityBat.<init>(EntityBat.java:32) [EntityBat.class:?]

at sun.reflect.GeneratedConstructorAccessor52.newInstance(Unknown Source) [?:1.8.0_77]

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [?:1.8.0_77]

at java.lang.reflect.Constructor.newInstance(Constructor.java:423) [?:1.8.0_77]

at net.minecraft.world.WorldEntitySpawner.findChunksForSpawning(WorldEntitySpawner.java:144) [WorldEntitySpawner.class:?]

at net.minecraft.world.WorldServer.tick(WorldServer.java:206) [WorldServer.class:?]

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:772) [MinecraftServer.class:?]

at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:687) [MinecraftServer.class:?]

at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156) [integratedServer.class:?]

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:536) [MinecraftServer.class:?]

at java.lang.Thread.run(Thread.java:745) [?:1.8.0_77]

[00:28:07] [server thread/ERROR] [FML]: Index: 3 Listeners:

[00:28:07] [server thread/ERROR] [FML]: 0: NORMAL

[00:28:07] [server thread/ERROR] [FML]: 1: ASM: bettercombat.mod.main.BetterCombatEventHandler@30d72c00 onEntityConstruct(Lnet/minecraftforge/event/AttachCapabilitiesEvent$Entity;)V

[00:28:07] [server thread/ERROR] [FML]: 2: ASM: lycanite.lycanitesmobs.EventListener@1a874ebf onAttachCapabilities(Lnet/minecraftforge/event/AttachCapabilitiesEvent$Entity;)V

[00:28:07] [server thread/ERROR] [FML]: 3: ASM: htd.rot.events.EventAddCapabilities@774c8880 onAddCapabilitiesEntity(Lnet/minecraftforge/event/AttachCapabilitiesEvent;)V

[00:28:07] [server thread/ERROR] [FML]: 4: ASM: htd.rot.events.EventAddCapabilities@774c8880 onAddCapabilitiesItemStack(Lnet/minecraftforge/event/AttachCapabilitiesEvent;)V

[00:28:07] [server thread/INFO] [sTDERR]: [net.minecraft.world.WorldEntitySpawner:findChunksForSpawning:148]: java.lang.reflect.InvocationTargetException

[00:28:07] [server thread/INFO] [sTDERR]: [net.minecraft.world.WorldEntitySpawner:findChunksForSpawning:148]: at sun.reflect.GeneratedConstructorAccessor52.newInstance(Unknown Source)

[00:28:07] [server thread/INFO] [sTDERR]: [net.minecraft.world.WorldEntitySpawner:findChunksForSpawning:148]: at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

[00:28:07] [server thread/INFO] [sTDERR]: [net.minecraft.world.WorldEntitySpawner:findChunksForSpawning:148]: at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

[00:28:07] [server thread/INFO] [sTDERR]: [net.minecraft.world.WorldEntitySpawner:findChunksForSpawning:148]: at net.minecraft.world.WorldEntitySpawner.findChunksForSpawning(WorldEntitySpawner.java:144)

[00:28:07] [server thread/INFO] [sTDERR]: [net.minecraft.world.WorldEntitySpawner:findChunksForSpawning:148]: at net.minecraft.world.WorldServer.tick(WorldServer.java:206)

[00:28:07] [server thread/INFO] [sTDERR]: [net.minecraft.world.WorldEntitySpawner:findChunksForSpawning:148]: at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:772)

[00:28:07] [server thread/INFO] [sTDERR]: [net.minecraft.world.WorldEntitySpawner:findChunksForSpawning:148]: at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:687)

[00:28:07] [server thread/INFO] [sTDERR]: [net.minecraft.world.WorldEntitySpawner:findChunksForSpawning:148]: at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156)

[00:28:07] [server thread/INFO] [sTDERR]: [net.minecraft.world.WorldEntitySpawner:findChunksForSpawning:148]: at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:536)

[00:28:07] [server thread/INFO] [sTDERR]: [net.minecraft.world.WorldEntitySpawner:findChunksForSpawning:148]: at java.lang.Thread.run(Thread.java:745)

[00:28:07] [server thread/INFO] [sTDERR]: [java.lang.Throwable:printStackTrace:634]: Caused by: java.lang.NullPointerException

[00:28:07] [server thread/INFO] [sTDERR]: [java.lang.Throwable:printStackTrace:634]: at htd.rot.events.EventAddCapabilities.entityCanAttack(EventAddCapabilities.java:66)

[00:28:07] [server thread/INFO] [sTDERR]: [java.lang.Throwable:printStackTrace:634]: at htd.rot.events.EventAddCapabilities.onAddCapabilitiesEntity(EventAddCapabilities.java:39)

[00:28:07] [server thread/INFO] [sTDERR]: [java.lang.Throwable:printStackTrace:634]: at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_137_EventAddCapabilities_onAddCapabilitiesEntity_AttachCapabilitiesEvent.invoke(.dynamic)

[00:28:07] [server thread/INFO] [sTDERR]: [java.lang.Throwable:printStackTrace:634]: at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90)

[00:28:07] [server thread/INFO] [sTDERR]: [java.lang.Throwable:printStackTrace:634]: at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:185)

[00:28:07] [server thread/INFO] [sTDERR]: [java.lang.Throwable:printStackTrace:634]: at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:535)

[00:28:07] [server thread/INFO] [sTDERR]: [java.lang.Throwable:printStackTrace:634]: at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:520)

[00:28:07] [server thread/INFO] [sTDERR]: [java.lang.Throwable:printStackTrace:634]: at net.minecraft.entity.Entity.<init>(Entity.java:252)

[00:28:07] [server thread/INFO] [sTDERR]: [java.lang.Throwable:printStackTrace:634]: at net.minecraft.entity.EntityLivingBase.<init>(EntityLivingBase.java:192)

[00:28:07] [server thread/INFO] [sTDERR]: [java.lang.Throwable:printStackTrace:634]: at net.minecraft.entity.EntityLiving.<init>(EntityLiving.java:100)

[00:28:07] [server thread/INFO] [sTDERR]: [java.lang.Throwable:printStackTrace:634]: at net.minecraft.entity.passive.EntityAmbientCreature.<init>(EntityAmbientCreature.java:11)

[00:28:07] [server thread/INFO] [sTDERR]: [java.lang.Throwable:printStackTrace:634]: at net.minecraft.entity.passive.EntityBat.<init>(EntityBat.java:32)

[00:28:07] [server thread/INFO] [sTDERR]: [java.lang.Throwable:printStackTrace:634]: ... 10 more

 

 

Code in question:

 

@SubscribeEvent
public void onAddCapabilitiesEntity(AttachCapabilitiesEvent<Entity> e)
{
	if (canHaveAttributes(e.getObject()))
	{
		EntityLivingBase ent = (EntityLivingBase) e.getObject();

		if (ent instanceof EntityPlayer) e.addCapability(CapPlayerExtraProvider.KEY, new CapPlayerExtraProvider(ent));
		else if (ent instanceof IMob || entityCanAttack(ent)) e.addCapability(CapMobExtraProvider.KEY, new CapMobExtraProvider(ent));

		e.addCapability(CapAttributeProvider.KEY, new CapAttributeProvider(ent));
	}
}

........

public static boolean entityCanAttack(Entity entity)
{
	if (entity instanceof EntityLiving)
	{
		EntityLiving ent = (EntityLiving) entity;
		for (EntityAITaskEntry task : ent.tasks.taskEntries)
		{
			Console.out().println(ent.getName());
			Console.out().println(task.action.getClass().getName());
			if (task.action instanceof EntityAIAttackMelee || task.action instanceof EntityAIAttackRanged || task.action instanceof EntityAIAttackRangedBow
					|| task.action instanceof EntityAINearestAttackableTarget || task.action instanceof EntityAIZombieAttack || task.action instanceof EntityAIOwnerHurtByTarget)
				return true;
		}
	}
	return false;
}

 

Mainly it's erroring on when I'm trying to loop through the tasks.

 

EDIT line 66 is "for (EntityAITaskEntry task : ent.tasks.taskEntries)" in "public static boolean entityCanAttack(Entity entity)"

Link to comment
Share on other sites

Yup was me just being dumb.

 

Changed it so everything that is not a player or a villager and is EntityLivingBase to get my capability.

 

But where the information is updated (stat rolling etc) which is in the LivingEvent.LivingUpdateEvent I check to see if the targetTasks is not empty

Thanks for the help guys

Link to comment
Share on other sites

I don't recommend using any instanceof checks as it can cause problems with various mods including my own. I have several mobs that are hostile but can be tamed/summoned by players no longer being hostile but still use the same class, therefore instanceof wont work.

 

I've been using the vanilla method: isCreatureType(EnumCreatureType type, boolean forSpawnCount) to check for hostile mobs, where you can use it like this:

isCreatureType(EnumCreatureType.monster, false), it works great for vanilla mobs and can be overridden by modders like myself to add logic to hostility checks, in my case I can check if a mob is owned by a player (not hostile) or wild (hostile). It also has the forSpawnCount boolean which is handy for other spawner mods, etc too!

  • Like 1
Link to comment
Share on other sites

I don't recommend using any instanceof checks as it can cause problems with various mods including my own. I have several mobs that are hostile but can be tamed/summoned by players no longer being hostile but still use the same class, therefore instanceof wont work.

 

I've been using the vanilla method: isCreatureType(EnumCreatureType type, boolean forSpawnCount) to check for hostile mobs, where you can use it like this:

isCreatureType(EnumCreatureType.monster, false), it works great for vanilla mobs and can be overridden by modders like myself to add logic to hostility checks, in my case I can check if a mob is owned by a player (not hostile) or wild (hostile). It also has the forSpawnCount boolean which is handy for other spawner mods, etc too!

 

Interesting, I'll keep those in mind. However I'm more or less interested in "If the Mob can attack, in any way" which by the looks of it checking the targetTasks list is the best way. If the mob cannot attack I don't need to add my custom stats to them and they can safely ignore any new of my new damage calculations.

 

Which by the way some of your mobs gave me quite the surprise as some of them attack twice (normal hit, and a unblock-able hit) this made me go "How did that one hit me, when I have a Def Rating of 337?" but helped me cover more bases of what can and will attack and when to apply new calculations.

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.