MinecraftScripterAI
Discord ↗
Link Copied to Clipboard
×
"create a npc companion"
/* First, create a custom entity class extending AgeableEntity (or another suitable base class) and implement the IAngerable interface if you want the NPC to be able to get angry */ public class NPCCompanionEntity extends AgeableEntity implements IAngerable { private static final RangedInteger ANGER_TIME_RANGE = TickRangeConverter.rangeOfSeconds(20, 39); private int angerTime; public NPCCompanionEntity(EntityType<? extends AgeableEntity> entityType, World world) { super(entityType, world); } /* Implement the necessary methods for IAngerable */ @Override public int getRemainingPersistentAngerTime() { return angerTime; } @Override public void setRemainingPersistentAngerTime(int time) { angerTime = time; } @Nullable @Override public UUID getPersistentAngerTarget() { return this.getLastHurtByMobUUID(); } @Override public void setPersistentAngerTarget(@Nullable UUID target) { this.setLastHurtByMobUUID(target); } @Override public void startPersistentAngerTimer() { this.setRemainingPersistentAngerTime(ANGER_TIME_RANGE.randomValue(this.random)); } /* Register the NPC Companion entity */ public static EntityType<NPCCompanionEntity> register(String id, EntityType.Builder<NPCCompanionEntity> builder) { return Registry.register(Registry.ENTITY_TYPE, id, builder.build(id)); } } /* Then, in your mod's main class, register the entity */ public class MyMod { public static final EntityType<NPCCompanionEntity> NPC_COMPANION = NPCCompanionEntity.register("npc_companion", EntityType.Builder.of(NPCCompanionEntity::new, EntityClassification.CREATURE).sized(0.6F, 1.8F)); public MyMod() { // Register the entity IEventBus bus = MinecraftForge.EVENT_BUS; bus.addListener(this::onCommonSetup); } private void onCommonSetup(FMLCommonSetupEvent event) { event.enqueueWork(() -> { GlobalEntityTypeAttributes.put(NPC_COMPANION, NPCCompanionEntity.createAttributes().build()); }); } }