Step 1 ‐ Create a basic entity - Jissee/PILib GitHub Wiki

所有代码基于 1.19.2 版本,不同版本可能有所不同
All the codes are based on version 1.19.2, and methods or classes may be different between each versions
圆周率库的渲染器只适用于实体,如生物、矿车、船、掉落物等。你需要先创建实体。以下是创建实体的简单方法
The renderers of PiLib is only for entities like mobs, minecarts, boats and drops. Here is a simple guide to create your own entity

// 1. 创建实体类,需要从任意现有类(如 "LivingEntity")中继承
//    Create the entity class, which should extend any existing class like "LivingEntity".
public class MyEntity extends LivingEntity {
    
    // 2. 创建构造函数
    //    Create a constructor
    public MyEntity(EntityType<? extends LivingEntity> pEntityType, Level pLevel) {
        super(pEntityType, pLevel);
    }
    
    // 3. 创建实体相关属性
    //    Create attribute for entity
    public static AttributeSupplier.Builder prepareAttribute() {
        return LivingEntity.createLivingAttributes()
                .add(Attributes.MOVEMENT_SPEED,1)
                .add(Attributes.SPAWN_REINFORCEMENTS_CHANCE)
                .add(Attributes.FOLLOW_RANGE, 16);
    }

    // 4. 创建或重写其他你需要使用的类方法
    //    Create or override any other methods
}
// 5. 创建注册类,注册我们的实体
//    Create registry class and register our entity
public class EntityRegistry {
    public static final DeferredRegister<EntityType<?>> ENTITIES = 
            DeferredRegister.create(ForgeRegistries.ENTITY_TYPES, MODID);

    public static final RegistryObject<EntityType<MyEntity>> MY_ENTITY = ENTITIES.register("my_entity",
            () -> EntityType.Builder.of(MyEntity::new, MobCategory.CREATURE)
                    .sized(1f,1.8f)
                    .build(new ResourceLocation(MODID,"my_entity").toString())
    );
}
// 6. 监听事件,注册实体属性
      Listen to event and register entity attributes
public class EventHandler {
    @SubscribeEvent
    public static void onCreateAttribute(EntityAttributeCreationEvent event){
        event.put(EntityRegistry.MY_ENTITY.get(), MyEntity.prepareAttribute().build());
    }
}
// 7. 注册到模组事件总线
//    Register to mod event bus
public class MyMod {
    
    public MyMod() {
        IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
        EntityRegistry.ENTITIES.register(bus);
        
        // 以下两句二选一 Choose one of two following statements
        MinecraftForge.EVENT_BUS.register(EventHandler.class);
        MinecraftForge.EVENT_BUS.addListener(EventHandler::onCreateAttribute);
    }
    
}

⚠️ **GitHub.com Fallback** ⚠️