Topic category: Help with Minecraft modding (Java Edition)
I tried looking through procedures but found nothing that'll make mobs attack my modded mob, unless someone figured out how to make a procedure for it.
(EDIT) Okay So I figured out how to make a villager-like mob that has the same mobs that attack it and have the nitwit behavior so players have a bit more freedom with what they can do, So basically you have to make a procedure that repeats or on spawn to make sure they don't claim a jobsite. Use the command procedure and use this:
For World Tick
/data modify entity @e[type=village_and_pillage_roamers:villager_nitwit,limit=1,sort=random] VillagerData set value {profession: "minecraft:nitwit", level: 1, type: "minecraft:plains"}
For Initial Spawn
-Use the Execute command at X Y Z procedure
/data modify entity @e[type=village_and_pillage_roamers:villager_nitwit,limit=1,sort=nearest] VillagerData set value {profession: "minecraft:nitwit", level: 1, type: "minecraft:plains"}
For more than one entity use tags and insert tag="your name" into the @e[] portion
New >> Living entity, add model, attributes, spawn egg, etc. In the AI and goals tab, check >> can tame, then add:
change values as needed.
I did not figure it out. It even seems to me, that I am not able to set up a procedure or source codes that makes e.g. a zombie attacks my mob. The point is, that mobs, who attacks other mobs have defined targets in their .class file who they should attack. Those definitions are written in a class file from the vanilla or forge game file. Example for what I say:
Open your Mod folder that was created as Mcreator Workspace. Klick the search function and tip e.g. "Vindicator" in there. Underneath, you find a "Vindicator" as class and as .java file. And the .java file you can open it to see, what is written there.
One Example is against Villagers: this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, AbstractVillager.class, true)); So goals like this you can define them for your own mob. So, if e.g. the vindicator should attack your mob, he need a goal for an attack your mob. The vidicator attack your mob only, if mob is based on the villager base type.
Its not that easy, to tip this goal in to the vindicator, because 1st, it is not written in the .class file so far 2nd even if you did, it is written in the forge snapshot of Mcreator. So if you use an external forge, the classfile of the vidicator doesn't have the goal to attack your mob.
The only way, I can imagine to make vanilla mobs attack the own custom mob is, if I could have access to the vanilla mobs in forge and access on their Ai. Just like the way I have access to both ways for my own created mobs. For example, I need to be able to choice the Forge/Vanilla vindicator who is nautrally given in minecraft, not create an own can use the existing mob instead, within his Ai. Either I can now tip in an "@Override" Code in code editor and can add "this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, NameOfYourMobEntity.class, true));" in or I can open the Block editor, where I can create a mob within seconds and add this block with this goal there!
It stays the same. I need to access the vanilla mobs, to add an attack goal for them. As long as I cant, there is a 2nd way I create an equivalent monster type. e.g. Zombie (you better use an other name for him), choose on "Ai and targets" section the base model "Zombie" I have a duplicate of the Zombie then and I can give him this additional attack goal. The problem is, I have 2 Zombie types. The forge or vanilla version, who leave your own created mob alone and the edited Zombie who will attack your own created mob or creature because the 2nd Zombie has the added goal to attack your custom mob.
I think a procedure which will test for normal Zombies can help. If a normal Zombie is detected, the procedure shall despawn it and spawn your customized Zombie instead of the vanilla Zombie. But I don't know if that is possible and if so, how it is possible.
I'm afraid there is no other way for now how monsters can programmed to attack custom mobs. I need definitively a oppurtunity to access the vanilla mobs and their Ai. So I can add this important goal to the vanilla mobs!
also my modded mob is a passive entity and shouldn't be able to attack back like a villager would. But it seems that I need to code in the behavior. If anyone can help me with the code link me to it.
If your question is about adding entities that attack your mob. The procedure above has everything you need. It is for a passive mob, line #7 states (this line defines the target): Attack in sight (yes/no), nearby only (yes/no), entities of type (select one, for more than one duplicate the line), and condition (double-click it for more options). Line #8 states (your mob will fight back any attacker): Fight attacker mobs back, call for help (yes/no), and condition (double-click it for more options). For my experience, no additional coding is needed. I did a mod with those options several months ago using MCreator (no issues). https://www.planetminecraft.com/mod/just-everything-mod-java-1-16-4/
If you want vanilla mobs that attack your mob, then MD91 already explained it.
You can also, leave tame unchecked, remove lines #3 and #4, and it will be an agressive (that will also attack any player) or passive mob that will attack the assigned target(s) and fight back it provocked.
This answer is for LostZombie. Villager themselves are passive as well and I never seen a villager attacking a mob in vanilla. I had not tested, wheter my mob fight back, when she's under attack or she attack itself targets, if I would tell her. My own mob is a monster (and gets attacked by ironGolem) but have the passive villager Ai. So I did not test that so far. This could might go wrong.
This is, why coding is also my construction site now and I'm a beginner. I had seen tutorials about this nowhere, searched amost 3 weeks for it. If I ask questions or someone else did, like you, I did not seen an answer from anyone in most cases. The only reference point I can find, are the given AIs on vanilla Mobs. You can search e.g. for villagers or whatever type of mob you are interested in, and if you found it's .java file, you see the way, the codes are written. But here ends my competence. I coded on the one side by giving a custom villager a sleep sound, if she is sleeping. But it is still a long way from a breakthrough to code anything, whatever I want to code!
Yesterday I tried to change the voice pitch to 1 only, by add a code set voice pitch min and set voice pitch max. I had seen the correct code in soundengine.java, if I rember right. I imported that needed classfile but that was everything thogh. I had written that pitch with @Override and became 2 errors. The biggest challenge is I do not know what I am doing wrong.
I think the only thing I could try now, is to ask Toma400 about this. If he maybe is ready to make a tutorial about this, So you, me and everyone after us can first steps to learn that minecraft coding. We stay not the only one, who wants to learn that. And by all these mods which were uploaded in this community I can't imagine, that noone knows how to code entitys. But if I find no one for this. I can only do try and error, with possibly chaotic results in doubt cases.
Remains now, that I need to know how to code that right. I already written my first experiences in my previous comment. So this shows me on more time, I am on the right path to code e.g. a skeleton shall attack my villager girl. But it is still the question how to modify the vanilla skeleton. As long as I don't have a way how to do this, I look for an entity vanilla despawn procedure and a direct spawn of a modified skeleton entity, who have the additional goal.
Right now its unimportant, because the base AI from the villagers, I can use for custom entitys, is sufficient for my purpose and works fine for me today. But it remains, that I need to add some more goals for attacking certain entitys, but now I am more interested, to define a certain sleeping and working time. Because I have night active villagers as well and their night activity have a good reason.
To MD91. Yeah I figured I'd had to write down some code cuz I tried doing what _SK_ is referring to but I cant make passive mobs attack because it cancels the action of attacking. I tried using vanilla villager NBT tags in Conditions with Villager AI in the "Ai Controls" but they claims jobs which I don't want.
I had written a new topic in "Help with modding (Java Edition)" It is a summary about my struggles. And because it is about the base Ai of villagers I need to edit. 2 profession groups of them may not claim a job. So I this is a topic for me as well. Although my custom villager girl claimed a farmer job, in code editor, I did not found an entry of a profession. Toma400 want to take a look at my problems, if he finds time for this within the next days. If so and I learn some lessons in coding, maybe I will be able to solve the job claiming. I could imagine to tip an @Override order which set the profession to 'nitwit' so the custom entity won't claim a job at any time.
I am waiting for this moment if I hear from Toma or not. If I know some more which will help, I post it here as well.
Easy