tinycodinglm / mc /mc-0006.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame Contribute Delete
3.13 kB
opencode trace
System You are an expert Fabric mod developer. You write idiomatic Minecraft Fabric mods using Java, Fabric API, and Yarn mappings.

You are an expert Fabric mod developer. You write idiomatic Minecraft Fabric mods using Java, Fabric API, and Yarn mappings.

User

How do I register custom entities, renderers, and add AI goals in Fabric?

Assistant
java
package com.example.mymod;

import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnGroup;
import net.minecraft.entity.attribute.DefaultAttributeContainer;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.ai.goal.*;
import net.minecraft.entity.mob.PathAwareEntity;
import net.minecraft.entity.passive.AnimalEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.world.World;

public class MyEntities {
    public static final EntityType<CustomEntity> CUSTOM = Registry.register(
        Registries.ENTITY_TYPE, Identifier.of("mymod", "custom"),
        EntityType.Builder.create(CustomEntity::new, SpawnGroup.CREATURE).dimensions(0.8f, 1.2f).build()
    );

    public static DefaultAttributeContainer.Builder createAttributes() {
        return PathAwareEntity.createMobAttributes()
            .add(EntityAttributes.MAX_HEALTH, 20.0)
            .add(EntityAttributes.MOVEMENT_SPEED, 0.25)
            .add(EntityAttributes.FOLLOW_RANGE, 32.0)
            .add(EntityAttributes.ATTACK_DAMAGE, 3.0);
    }

    public static void register() {
        Registry.register(Registries.ENTITY_TYPE, Identifier.of("mymod", "custom"), CUSTOM);
        FabricDefaultAttributeRegistry.register(CUSTOM, createAttributes());
    }
}

Custom Entity class:

java
public class CustomEntity extends AnimalEntity {
    public CustomEntity(EntityType<? extends AnimalEntity> type, World world) { super(type, world); }

    @Override
    protected void initGoals() {
        this.goalSelector.add(0, new SwimGoal(this));
        this.goalSelector.add(1, new EscapeDangerGoal(this, 1.25));
        this.goalSelector.add(2, new WanderAroundFarGoal(this, 1.0));
        this.goalSelector.add(3, new LookAtEntityGoal(this, PlayerEntity.class, 6.0f));
        this.goalSelector.add(4, new LookAroundGoal(this));
    }
}

Entity Renderer (client side):

java
public class MyEntityRenderers {
    public static void register() {
        EntityRendererRegistry.register(MyEntities.CUSTOM, CustomEntityRenderer::new);
    }
}

public class CustomEntityRenderer extends MobEntityRenderer<CustomEntity, CustomEntityModel> {
    private static final Identifier TEXTURE = Identifier.of("mymod", "textures/entity/custom.png");

    public CustomEntityRenderer(EntityRendererFactory.Context context) {
        super(context, new CustomEntityModel(context.getPart(EntityModelLayers.PLAYER)), 0.5f);
    }

    @Override
    public Identifier getTexture(CustomEntity entity) { return TEXTURE; }
}