Topic category: Help with Minecraft modding (Java Edition)
So I'm trying to make an biped entity that will fight mobs. It tries to attack the mobs it runs up to them and is colliding with them, but they wont play there attack animation and ultimately wont hit the mobs. When i switched the entity base to a zombie it would play its attack animation, but then it would act to much like a zombie like burning in sunlight and attacking villagers. I was just hoping that somebody would know what I'm missing to allow them to be able to attack. P.s I'm very new to coding so i don't know a lot yet.
Code for my entity
package net.mcreator.biomes.entity;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.common.registry.EntityRegistry;
import net.minecraftforge.fml.common.registry.EntityEntryBuilder;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.client.registry.RenderingRegistry;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.World;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.DamageSource;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Item;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.init.Items;
import net.minecraft.entity.passive.EntityVillager;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAINearestAttackableTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAIFollow;
import net.minecraft.entity.ai.EntityAIAttackMelee;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.EnumCreatureType;
import net.minecraft.entity.EnumCreatureAttribute;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.EntityCreature;
import net.minecraft.entity.Entity;
import net.minecraft.client.renderer.entity.RenderBiped;
import net.minecraft.client.model.ModelBiped;
import net.mcreator.biomes.procedure.ProcedureBarbarianThisEntityKillsAnotherOne;
import net.mcreator.biomes.ElementsBiomes;
import java.util.Iterator;
import java.util.ArrayList;
@ElementsBiomes.ModElement.Tag
public class EntityBarbarian extends ElementsBiomes.ModElement {
public static final int ENTITYID = 5;
public static final int ENTITYID_RANGED = 6;
public EntityBarbarian(ElementsBiomes instance) {
super(instance, 34);
}
@Override
public void initElements() {
elements.entities.add(() -> EntityEntryBuilder.create().entity(EntityCustom.class).id(new ResourceLocation("biomes", "barbarian"), ENTITYID)
.name("barbarian").tracker(64, 3, true).egg(-11062009, -16777216).build());
}
@Override
public void init(FMLInitializationEvent event) {
Biome[] spawnBiomes = allbiomes(Biome.REGISTRY);
EntityRegistry.addSpawn(EntityCustom.class, 20, 3, 10, EnumCreatureType.CREATURE, spawnBiomes);
}
private Biome[] allbiomes(net.minecraft.util.registry.RegistryNamespaced<ResourceLocation, Biome> in) {
Iterator<Biome> itr = in.iterator();
ArrayList<Biome> ls = new ArrayList<Biome>();
while (itr.hasNext())
ls.add(itr.next());
return ls.toArray(new Biome[ls.size()]);
}
@SideOnly(Side.CLIENT)
@Override
public void preInit(FMLPreInitializationEvent event) {
RenderingRegistry.registerEntityRenderingHandler(EntityCustom.class, renderManager -> {
RenderBiped customRender = new RenderBiped(renderManager, new ModelBiped(), 0.5f) {
protected ResourceLocation getEntityTexture(Entity entity) {
return new ResourceLocation("biomes:textures/barb.png");
}
};
customRender.addLayer(new net.minecraft.client.renderer.entity.layers.LayerBipedArmor(customRender) {
protected void initArmor() {
this.modelLeggings = new ModelBiped(0.5f);
this.modelArmor = new ModelBiped(1);
}
});
return customRender;
});
}
public static class EntityCustom extends EntityCreature {
public EntityCustom(World world) {
super(world);
setSize(0.6f, 1.8f);
experienceValue = 5;
this.isImmuneToFire = false;
setNoAI(!true);
this.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(Items.IRON_SWORD, (int) (1)));
}
@Override
protected void initEntityAI() {
super.initEntityAI();
this.tasks.addTask(1, new EntityAIWander(this, 1));
this.tasks.addTask(2, new EntityAILookIdle(this));
this.tasks.addTask(3, new EntityAISwimming(this));
this.targetTasks.addTask(4, new EntityAIHurtByTarget(this, false));
this.targetTasks.addTask(5, new EntityAINearestAttackableTarget(this, EntityMob.class, false, false));
this.tasks.addTask(6, new EntityAIAttackMelee(this, 1.2, true));
this.tasks.addTask(7, new EntityAIFollow(this, (float) 1, 10, 5));
this.tasks.addTask(8, new EntityAIWatchClosest(this, EntityVillager.class, (float) 6));
}
@Override
public EnumCreatureAttribute getCreatureAttribute() {
return EnumCreatureAttribute.UNDEFINED;
}
@Override
protected Item getDropItem() {
return null;
}
@Override
public net.minecraft.util.SoundEvent getAmbientSound() {
return (net.minecraft.util.SoundEvent) net.minecraft.util.SoundEvent.REGISTRY.getObject(new ResourceLocation(""));
}
@Override
public net.minecraft.util.SoundEvent getHurtSound(DamageSource ds) {
return (net.minecraft.util.SoundEvent) net.minecraft.util.SoundEvent.REGISTRY.getObject(new ResourceLocation("entity.generic.hurt"));
}
@Override
public net.minecraft.util.SoundEvent getDeathSound() {
return (net.minecraft.util.SoundEvent) net.minecraft.util.SoundEvent.REGISTRY.getObject(new ResourceLocation("entity.generic.death"));
}
@Override
protected float getSoundVolume() {
return 1.0F;
}
@Override
public boolean attackEntityFrom(DamageSource source, float amount) {
if (source == DamageSource.FALL)
return false;
return super.attackEntityFrom(source, amount);
}
@Override
public void onKillEntity(EntityLivingBase entity) {
super.onKillEntity(entity);
int x = (int) this.posX;
int y = (int) this.posY;
int z = (int) this.posZ;
{
java.util.HashMap<String, Object> $_dependencies = new java.util.HashMap<>();
$_dependencies.put("entity", entity);
ProcedureBarbarianThisEntityKillsAnotherOne.executeProcedure($_dependencies);
}
}
@Override
protected void applyEntityAttributes() {
super.applyEntityAttributes();
if (this.getEntityAttribute(SharedMonsterAttributes.ARMOR) != null)
this.getEntityAttribute(SharedMonsterAttributes.ARMOR).setBaseValue(0D);
if (this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED) != null)
this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.3D);
if (this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH) != null)
this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(10D);
if (this.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE) != null)
this.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).setBaseValue(3D);
}
}
}
because you're defining your entity as a creature. creatures made in mcreator for 1.12.2 won't actually attack and do damage unless you edit code.
Thank you kind sir I fixed that issue, my question now is what would the code be for me to make it so the mob doesn't attack itself i have it set to attack all marked as mobs but now they kill each other.
are you comfortable editing code? if not, try adding code block "Avoid entity in radius{r} near speed{n} far speed{f} of type {Barbarian}" before everything else. i don't know what will happen though, be funny to see the results when you spawn a bunch of them in close proximity.
if you are comfy editing code, replace the line "this.targetTasks.addTask(5, new EntityAINearestAttackableTarget(...)" with the following:
Hi, i try what you told. as i have same problem where i want my mob attack everything but i dont want it to attack their own kind.
so i try to copy that code while also do some little adjustment for latest version of mcreator.
@Override
protected void registerGoals() {
super.registerGoals();
this.goalSelector.addGoal(1, new MeleeAttackGoal(this, 1, true));
this.targetSelector.addGoal(2, new HurtByTargetGoal(this).setCallsForHelp(this.getClass()));
this.targetSelector.addGoal(5, new NearestAttackableTargetGoal(this, MobEntity.class, 10, false, false, new Predicate<Entity>() {
public boolean apply(@Nullable Entity p_apply_1_) {
return !(p_apply_1_ instanceof EntityThaw.EntityCustom);
}
}));
this.goalSelector.addGoal(4, new LookAtGoal(this, MobEntity.class, (float) 10));
this.goalSelector.addGoal(5, new RandomWalkingGoal(this, 1));
this.goalSelector.addGoal(6, new LookRandomlyGoal(this));
this.goalSelector.addGoal(7, new SwimGoal(this));
}
however there is one little problem:
Executing
Gradle task: build
Build
info: MCreator 2021.1.18117, forge-1.16.5, 64-bit, 3526 MB, Windows 10,
JVM 1.8.0_275, JAVA_HOME: D:\Pylo\MCreator\jdk
>
Task :compileJava
C:\Users\******\Outer
Realm Backup
1.16.4\src\main\java\net\mcreator\outerrealm\entity\ThawEntity.java:124:
error: cannot find symbol
this.targetSelector.addGoal(5, new
NearestAttackableTargetGoal(this, MobEntity.class, 10, false, false, new
Predicate<Entity>() {
^
symbol: class Predicate
location: class CustomEntity
1
error
>
Task :compileJava FAILED
FAILURE:
Build failed with an exception.
*
What went wrong:
Execution
failed for task ':compileJava'.
>
Compilation failed; see the compiler error output for details.
*
Try:
Run
with --stacktrace option to get the stack trace. Run with --info or
--debug option to get more log output. Run with --scan to get full
insights.
*
Get more help at https://help.gradle.org
BUILD
FAILED in 13s
1
actionable task: 1 executed
BUILD
FAILED
Task
completed in 16 seconds
its "predicate", what should i do?
Nevermind, it worked now. Although need some tweaking for the code for newer mcreator version
yeah you need to import the Predicate library
Can you created a tutorial, i feel like this going to help a lot of people