ALEX FOS MÉNDEZ

puedes crear un mod que pones una ia que entra al mundo juega contigo

Kodari AI
KodariOct 13 at 5:58 pm 8
AICompanionMod
An immersive Minecraft Fabric mod that introduces an intelligent AI companion who joins your world and plays alongside you. The AI entity spawns via a crafted item, learns from your actions, follows you during exploration, assists in combat against hostile mobs, collects dropped items, and helps with basic mining tasks. It features a dynamic dialogue system with contextual responses, a friendship level that increases through shared activities, and customizable behavior settings. The companion autonomously navigates terrain, avoids dangers like lava and cacti, and returns to your side when called. Perfect for solo players seeking a cooperative gameplay experience without needing real friends online.

Voy a construir un compañero de IA que aparece en el mundo, te sigue, pelea contra los monstruos cerca de ti y te defiende. Primero consulto las firmas exactas de Minecraft 26.3 y Fabric API.

Ya tengo todas las firmas verificadas en FABRIC_26_3. Creo el mod: un compañero (AI Companion) que sigue al jugador, pelea contra monstruos cercanos y se teletransporta si se aleja demasiado.

* Begin Patch * Add File: settings.gradle +pluginManagement {

  • repositories {

  • maven {

  • Plain Text
      	name = 'Fabric'
    
  • Plain Text
      	url = 'https://maven.fabricmc.net/'
    
  • }

  • Plain Text
      gradlePluginPortal()
    
  • } +}

+rootProject.name = ‘companionai’ * Add File: gradle.properties +org.gradle.jvmargs=-Xmx1G +org.gradle.parallel=true +org.gradle.configuration-cache=false + +minecraft_version=26.3 +loader_version=0.19.5 +loom_version=1.17-SNAPSHOT + +version=1.0.0 +group=com.example + +fabric_api_version=0.161.0+26.3 * Add File: build.gradle +plugins {

  • id ‘net.fabricmc.fabric-loom’ version “${loom_version}”

  • id ‘maven-publish’ +}

+loom {

  • splitEnvironmentSourceSets()

  • mods {

  • “companionai” {

  • Plain Text
      	sourceSet sourceSets.main
    
  • Plain Text
      	sourceSet sourceSets.client
    
  • }

  • } +}

+dependencies {

  • minecraft “com.mojang:minecraft:${project.minecraft_version}”

  • implementation “net.fabricmc:fabric-loader:${project.loader_version}”

  • implementation “net.fabricmc.fabric-api:fabric-api:${project.fabric_api_version}” +}

+tasks.withType(JavaCompile).configureEach {

  • it.options.release = 25 +}

+java {

  • withSourcesJar()

  • sourceCompatibility = JavaVersion.VERSION_25

  • targetCompatibility = JavaVersion.VERSION_25 +} * Add File: src/main/resources/fabric.mod.json +{

  • “schemaVersion”: 1,

  • “id”: “companionai”,

  • “version”: “${version}”,

  • “name”: “AI Companion”,

  • “description”: “An AI companion that joins your world, follows you and fights hostile mobs at your side.”,

  • “environment”: “*”,

  • “entrypoints”: {

  • “main”: [“com.example.companionai.CompanionAi”],

  • “client”: [“com.example.companionai.client.CompanionAiClient”]

  • },

  • “depends”: {

  • “fabricloader”: “>=0.19.5”,

  • “minecraft”: “~26.3”,

  • “java”: “>=25”,

  • “fabric-api”: “*”

  • } +} * Add File: src/main/java/com/example/companionai/CompanionAi.java +package com.example.companionai;

+import net.fabricmc.api.ModInitializer; + +public class CompanionAi implements ModInitializer {

  • public static final String MOD_ID = “companionai”;

  • @Override

  • public void onInitialize() {

  • Plain Text
       ModEntityTypes.initialize();
    
  • Plain Text
       ModItems.initialize();
    
  • } +} * Add File: src/main/java/com/example/companionai/ModEntityTypes.java +package com.example.companionai;

+import com.example.companionai.entity.CompanionEntity; +import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.Identifier; +import net.minecraft.resources.ResourceKey; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.MobCategory; + +public final class ModEntityTypes {

  • public static final ResourceKey<EntityType<?>> COMPANION_KEY = ResourceKey.create

  • Plain Text
       Registries.ENTITY_TYPE,
    
  • Plain Text
       Identifier.fromNamespaceAndPath(CompanionAi.MOD_ID, "companion")
    
  • );

  • public static final EntityType<CompanionEntity> COMPANION = Registry.register

  • Plain Text
       BuiltInRegistries.ENTITY_TYPE,
    
  • COMPANION_KEY,

  • Plain Text
       EntityType.Builder.of(CompanionEntity::new, MobCategory.CREATURE)
    
  • Plain Text
           .sized(0.6F, 1.8F)
    
  • Plain Text
           .clientTrackingRange(10)
    
  • Plain Text
           .build(COMPANION_KEY)
    
  • );

  • public static void initialize() {

  • Plain Text
       FabricDefaultAttributeRegistry.register(COMPANION, CompanionEntity.createAttributes());
    
  • } +} * Add File: src/main/java/com/example/companionai/ModItems.java +package com.example.companionai;

+import com.example.companionai.item.CompanionCoreItem; +import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.Identifier; +import net.minecraft.resources.ResourceKey; +import net.minecraft.world.item.CreativeModeTab; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; + +public final class ModItems {

  • public static final ResourceKey<Item> COMPANION_CORE_KEY = ResourceKey.create

  • Plain Text
       Registries.ITEM,
    
  • Plain Text
       Identifier.fromNamespaceAndPath(CompanionAi.MOD_ID, "companion_core")
    
  • );

  • public static final Item COMPANION_CORE = Registry.register

  • Plain Text
       BuiltInRegistries.ITEM,
    
  • COMPANION_CORE_KEY,

  • Plain Text
       new CompanionCoreItem(new Item.Properties().setId(COMPANION_CORE_KEY))
    
  • );

  • private static final ResourceKey<CreativeModeTab> TOOLS_AND_UTILITIES = ResourceKey.create

  • Plain Text
       Registries.CREATIVE_MODE_TAB,
    
  • Plain Text
       Identifier.withDefaultNamespace("tools_and_utilities")
    
  • );

  • public static void initialize() {

  • Plain Text
       CreativeModeTabEvents.modifyOutputEvent(TOOLS_AND_UTILITIES).register(output ->
    
  • Plain Text
           output.accept(new ItemStack(COMPANION_CORE), CreativeModeTab.TabVisibility.PARENT_AND_SEARCH_TABS)
    
  • );

  • } +} * Add File: src/main/java/com/example/companionai/item/CompanionCoreItem.java +package com.example.companionai.item;

+import com.example.companionai.ModEntityTypes; +import com.example.companionai.entity.CompanionEntity; +import net.minecraft.network.chat.Component; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.sounds.SoundEvents; +import net.minecraft.sounds.SoundSource; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.entity.EntitySpawnReason; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.Item; +import net.minecraft.world.level.Level; + +public class CompanionCoreItem extends Item {

  • public CompanionCoreItem(Item.Properties properties) {

  • Plain Text
       super(properties);
    
  • }

  • @Override

  • public InteractionResult use(Level level, Player player, InteractionHand hand) {

  • Plain Text
       if (level instanceof ServerLevel serverLevel) {
    
  • Plain Text
           CompanionEntity companion = ModEntityTypes.COMPANION.create(serverLevel, EntitySpawnReason.SPAWN_ITEM_USE);
    
  • Plain Text
           if (companion != null) {
    
  • Plain Text
               companion.snapTo(player.getX() + 1.5, player.getY(), player.getZ(), player.getYRot(), 0.0F);
    
  • Plain Text
               companion.setOwner(player.getUUID());
    
  • Plain Text
               serverLevel.addFreshEntity(companion);
    
  • Plain Text
               serverLevel.playSound(
    
  • Plain Text
                   null,
    
  • Plain Text
                   player.getX(),
    
  • Plain Text
                   player.getY(),
    
  • Plain Text
                   player.getZ(),
    
  • Plain Text
                   SoundEvents.EXPERIENCE_ORB_PICKUP,
    
  • Plain Text
                   SoundSource.PLAYERS,
    
  • Plain Text
                   1.0F,
    
  • Plain Text
                   1.0F
    
  • );

  • Plain Text
               player.sendSystemMessage(Component.translatable("message.companionai.summoned"));
    
  • Plain Text
               player.getItemInHand(hand).consume(1, player);
    
  • }

  • }

  • Plain Text
       return InteractionResult.SUCCESS;
    
  • } +} * Add File: src/main/java/com/example/companionai/entity/CompanionEntity.java +package com.example.companionai.entity;

+import java.util.UUID; +import net.minecraft.core.UUIDUtil; +import net.minecraft.sounds.SoundEvent; +import net.minecraft.sounds.SoundEvents; +import net.minecraft.world.damagesource.DamageSource; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.Mob; +import net.minecraft.world.entity.PathfinderMob; +import net.minecraft.world.entity.ai.attributes.AttributeSupplier; +import net.minecraft.world.entity.ai.attributes.Attributes; +import net.minecraft.world.entity.ai.goal.FloatGoal; +import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal; +import net.minecraft.world.entity.ai.goal.MeleeAttackGoal; +import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal; +import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal; +import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal; +import net.minecraft.world.entity.monster.Monster; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.storage.ValueInput; +import net.minecraft.world.level.storage.ValueOutput; + +public class CompanionEntity extends PathfinderMob {

  • private UUID ownerUuid;

  • public CompanionEntity(EntityType<? extends CompanionEntity> type, Level level) {

  • Plain Text
       super(type, level);
    
  • Plain Text
       this.setPersistenceRequired();
    
  • }

  • public static AttributeSupplier.Builder createAttributes() {

  • Plain Text
       return Mob.createMobAttributes()
    
  • Plain Text
           .add(Attributes.MAX_HEALTH, 40.0)
    
  • Plain Text
           .add(Attributes.MOVEMENT_SPEED, 0.3)
    
  • Plain Text
           .add(Attributes.ATTACK_DAMAGE, 6.0)
    
  • Plain Text
           .add(Attributes.FOLLOW_RANGE, 32.0);
    
  • }

  • @Override

  • protected void registerGoals() {

  • Plain Text
       this.goalSelector.addGoal(0, new FloatGoal(this));
    
  • Plain Text
       this.goalSelector.addGoal(1, new MeleeAttackGoal(this, 1.2D, true));
    
  • Plain Text
       this.goalSelector.addGoal(2, new CompanionFollowOwnerGoal(this, 1.0D));
    
  • Plain Text
       this.goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 8.0F));
    
  • Plain Text
       this.goalSelector.addGoal(4, new WaterAvoidingRandomStrollGoal(this, 1.0D));
    
  • Plain Text
       this.targetSelector.addGoal(0, new HurtByTargetGoal(this));
    
  • Plain Text
       this.targetSelector.addGoal(1, new NearestAttackableTargetGoal<>(this, Monster.class, true));
    
  • }

  • public void setOwner(UUID uuid) {

  • Plain Text
       this.ownerUuid = uuid;
    
  • }

  • public Player getOwner() {

  • Plain Text
       return this.ownerUuid == null ? null : this.level().getPlayerByUUID(this.ownerUuid);
    
  • }

  • @Override

  • protected void addAdditionalSaveData(ValueOutput output) {

  • Plain Text
       super.addAdditionalSaveData(output);
    
  • Plain Text
       output.storeNullable("Owner", UUIDUtil.CODEC, this.ownerUuid);
    
  • }

  • @Override

  • protected void readAdditionalSaveData(ValueInput input) {

  • Plain Text
       super.readAdditionalSaveData(input);
    
  • Plain Text
       this.ownerUuid = input.read("Owner", UUIDUtil.CODEC).orElse(null);
    
  • }

  • @Override

  • protected SoundEvent getHurtSound(DamageSource source) {

  • Plain Text
       return SoundEvents.PLAYER_HURT;
    
  • }

  • @Override

  • protected SoundEvent getDeathSound() {

  • Plain Text
       return SoundEvents.PLAYER_DEATH;
    
  • } +} * Add File: src/main/java/com/example/companionai/entity/CompanionFollowOwnerGoal.java +package com.example.companionai.entity;

+import java.util.EnumSet; +import net.minecraft.world.entity.ai.goal.Goal; +import net.minecraft.world.entity.player.Player; + +public class CompanionFollowOwnerGoal extends Goal {

  • private static final double START_DISTANCE_SQR = 12.0 * 12.0;

  • private static final double STOP_DISTANCE_SQR = 3.0 * 3.0;

  • private static final double TELEPORT_DISTANCE_SQR = 30.0 * 30.0;

  • private final CompanionEntity companion;

  • private final double speedModifier;

  • private Player owner;

  • private int timeToRecalcPath;

  • public CompanionFollowOwnerGoal(CompanionEntity companion, double speedModifier) {

  • Plain Text
       this.companion = companion;
    
  • Plain Text
       this.speedModifier = speedModifier;
    
  • Plain Text
       this.setFlags(EnumSet.of(Goal.Flag.MOVE));
    
  • }

  • @Override

  • public boolean canUse() {

  • Plain Text
       Player player = this.companion.getOwner();
    
  • Plain Text
       if (player == null || this.companion.getTarget() != null) {
    
  • Plain Text
           return false;
    
  • }

  • Plain Text
       if (this.companion.distanceToSqr(player) < START_DISTANCE_SQR) {
    
  • Plain Text
           return false;
    
  • }

  • Plain Text
       this.owner = player;
    
  • Plain Text
       return true;
    
  • }

  • @Override

  • public boolean canContinueToUse() {

  • Plain Text
       return this.owner != null
    
  • Plain Text
           && this.companion.getTarget() == null
    
  • Plain Text
           && this.companion.distanceToSqr(this.owner) > STOP_DISTANCE_SQR;
    
  • }

  • @Override

  • public void start() {

  • Plain Text
       this.timeToRecalcPath = 0;
    
  • }

  • @Override

  • public void stop() {

  • Plain Text
       this.owner = null;
    
  • Plain Text
       this.companion.getNavigation().stop();
    
  • }

  • @Override

  • public void tick() {

  • Plain Text
       if (this.companion.distanceToSqr(this.owner) > TELEPORT_DISTANCE_SQR) {
    
  • Plain Text
           this.companion.teleportTo(this.owner.getX(), this.owner.getY(), this.owner.getZ());
    
  • Plain Text
           this.companion.getNavigation().stop();
    
  • Plain Text
           return;
    
  • }

  • Plain Text
       if (--this.timeToRecalcPath <= 0) {
    
  • Plain Text
           this.timeToRecalcPath = 10;
    
  • Plain Text
           this.companion.getNavigation().moveTo(this.owner, this.speedModifier);
    
  • }

  • } +} * Add File: src/client/java/com/example/companionai/client/CompanionAiClient.java +package com.example.companionai.client;

+import com.example.companionai.ModEntityTypes; +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.rendering.v1.EntityRendererRegistry; + +public class CompanionAiClient implements ClientModInitializer {

  • @Override

  • public void onInitializeClient() {

  • Plain Text
       EntityRendererRegistry.register(ModEntityTypes.COMPANION, CompanionRenderer::new);
    
  • } +} * Add File: src/client/java/com/example/companionai/client/CompanionRenderer.java +package com.example.companionai.client;

+import com.example.companionai.entity.CompanionEntity; +import net.minecraft.client.model.HumanoidModel; +import net.minecraft.client.model.geom.ModelLayers; +import net.minecraft.client.renderer.entity.EntityRendererProvider; +import net.minecraft.client.renderer.entity.HumanoidMobRenderer; +import net.minecraft.client.renderer.entity.state.HumanoidRenderState; +import net.minecraft.client.resources.DefaultPlayerSkin; +import net.minecraft.resources.Identifier; + +public class CompanionRenderer extends HumanoidMobRenderer<CompanionEntity, HumanoidRenderState, HumanoidModel<HumanoidRenderState>> {

  • public CompanionRenderer(EntityRendererProvider.Context context) {

  • Plain Text
       super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.PLAYER)), 0.5F);
    
  • }

  • @Override

  • public HumanoidRenderState createRenderState() {

  • Plain Text
       return new HumanoidRenderState();
    
  • }

  • @Override

  • public Identifier getTextureLocation(HumanoidRenderState state) {

  • Plain Text
       return DefaultPlayerSkin.getDefaultTexture();
    
  • } +} * Add File: src/main/resources/assets/companionai/items/companion_core.json +{

  • “model”: {

  • “type”: “minecraft:model”,

  • “model”: “companionai:item/companion_core”

  • } +} * Add File: src/main/resources/assets/companionai/models/item/companion_core.json +{

  • “parent”: “minecraft:item/generated”,

  • “textures”: {

  • “layer0”: “companionai:item/companion_core”

  • } +} * Add File: src/main/resources/assets/companionai/lang/en_us.json +{

  • “item.companionai.companion_core”: “AI Companion Core”,

  • “entity.companionai.companion”: “AI Companion”,

  • “message.companionai.summoned”: “Hi! I’m here. Let’s play together!” +} * Add File: src/main/resources/data/companionai/recipe/companion_core.json +{

  • “type”: “minecraft:crafting_shaped”,

  • “category”: “misc”,

  • “pattern”: [

  • “DRD”,

  • “RND”,

  • “DRD”

  • ],

  • “key”: {

  • “D”: “minecraft:diamond”,

  • “R”: “minecraft:redstone”,

  • “N”: “minecraft:nether_star”

  • },

  • “result”: {

  • “id”: “companionai:companion_core”,

  • “count”: 1

  • } +} * End Patch

name=“apply_patch”> name=“input”>* Begin Patch * Add File: settings.gradle +pluginManagement {

  • repositories {

  • maven {

  • Plain Text
      	name = 'Fabric'
    
  • Plain Text
      	url = 'https://maven.fabricmc.net/'
    
  • }

  • Plain Text
      gradlePluginPortal()
    
  • } +}

+rootProject.name = ‘companionai’ * End Patch

name=“apply_patch”> name=“input”>* Begin Patch * Add File: gradle.properties +org.gradle.jvmargs=-Xmx1G +org.gradle.parallel=true +org.gradle.configuration-cache=false + +minecraft_version=26.3 +loader_version=0.19.5 +loom_version=1.17-SNAPSHOT + +version=1.0.0 +group=com.example + +fabric_api_version=0.161.0+26.3 * End Patch

name=“apply_patch”> name=“input”>* Begin Patch * Add File: build.gradle +plugins {

  • id ‘net.fabricmc.fabric-loom’ version “${loom_version}”

  • id ‘maven-publish’ +}

+loom {

  • splitEnvironmentSourceSets()

  • mods {

  • “companionai” {

  • Plain Text
      	sourceSet sourceSets.main
    
  • Plain Text
      	sourceSet sourceSets.client
    
  • }

  • } +}

+dependencies {

  • minecraft “com.mojang:minecraft:${project.minecraft_version}”

  • implementation “net.fabricmc:fabric-loader:${project.loader_version}”

  • implementation “net.fabricmc.fabric-api:fabric-api:${project.fabric_api_version}” +}

+tasks.withType(JavaCompile).configureEach {

  • it.options.release = 25 +}

+java {

  • withSourcesJar()

  • sourceCompatibility = JavaVersion.VERSION_25

  • targetCompatibility = JavaVersion.VERSION_25 +} * End Patch

name=“apply_patch”> name=“input”>* Begin Patch * Add File: src/main/resources/fabric.mod.json +{

  • “schemaVersion”: 1,

  • “id”: “companionai”,

  • “version”: “${version}”,

  • “name”: “AI Companion”,

  • “description”: “An AI companion that joins your world, follows you and fights hostile mobs at your side.”,

  • “environment”: “*”,

  • “entrypoints”: {

  • “main”: [“com.example.companionai.CompanionAi”],

  • “client”: [“com.example.companionai.client.CompanionAiClient”]

  • },

  • “depends”: {

  • “fabricloader”: “>=0.19.5”,

  • “minecraft”: “~26.3”,

  • “java”: “>=25”,

  • “fabric-api”: “*”

  • } +} * End Patch

name=“apply_patch”> name=“input”>* Begin Patch * Add File: src/main/java/com/example/companionai/CompanionAi.java +package com.example.companionai; + +import net.fabricmc.api.ModInitializer; + +public class CompanionAi implements ModInitializer {

  • public static final String MOD_ID = “companionai”;

  • @Override

  • public void onInitialize() {

  • Plain Text
       ModEntityTypes.initialize();
    
  • Plain Text
       ModItems.initialize();
    
  • } +} * Add File: src/main/java/com/example/companionai/ModEntityTypes.java +package com.example.companionai;

+import com.example.companionai.entity.CompanionEntity; +import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.Identifier; +import net.minecraft.resources.ResourceKey; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.MobCategory; + +public final class ModEntityTypes {

  • public static final ResourceKey<EntityType<?>> COMPANION_KEY = ResourceKey.create

  • Plain Text
       Registries.ENTITY_TYPE,
    
  • Plain Text
       Identifier.fromNamespaceAndPath(CompanionAi.MOD_ID, "companion")
    
  • );

  • public static final EntityType<CompanionEntity> COMPANION = Registry.register

  • Plain Text
       BuiltInRegistries.ENTITY_TYPE,
    
  • COMPANION_KEY,

  • Plain Text
       EntityType.Builder.of(CompanionEntity::new, MobCategory.CREATURE)
    
  • Plain Text
           .sized(0.6F, 1.8F)
    
  • Plain Text
           .clientTrackingRange(10)
    
  • Plain Text
           .build(COMPANION_KEY)
    
  • );

  • public static void initialize() {

  • Plain Text
       FabricDefaultAttributeRegistry.register(COMPANION, CompanionEntity.createAttributes());
    
  • } +} * End Patch

name=“apply_patch”> name=“input”>* Begin Patch * Add File: src/main/java/com/example/companionai/ModItems.java +package com.example.companionai; + +import com.example.companionai.item.CompanionCoreItem; +import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.Identifier; +import net.minecraft.resources.ResourceKey; +import net.minecraft.world.item.CreativeModeTab; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; + +public final class ModItems {

  • public static final ResourceKey<Item> COMPANION_CORE_KEY = ResourceKey.create

  • Plain Text
       Registries.ITEM,
    
  • Plain Text
       Identifier.fromNamespaceAndPath(CompanionAi.MOD_ID, "companion_core")
    
  • );

  • public static final Item COMPANION_CORE = Registry.register

  • Plain Text
       BuiltInRegistries.ITEM,
    
  • COMPANION_CORE_KEY,

  • Plain Text
       new CompanionCoreItem(new Item.Properties().setId(COMPANION_CORE_KEY))
    
  • );

  • private static final ResourceKey<CreativeModeTab> TOOLS_AND_UTILITIES = ResourceKey.create

  • Plain Text
       Registries.CREATIVE_MODE_TAB,
    
  • Plain Text
       Identifier.withDefaultNamespace("tools_and_utilities")
    
  • );

  • public static void initialize() {

  • Plain Text
       CreativeModeTabEvents.modifyOutputEvent(TOOLS_AND_UTILITIES).register(output ->
    
  • Plain Text
           output.accept(new ItemStack(COMPANION_CORE), CreativeModeTab.TabVisibility.PARENT_AND_SEARCH_TABS)
    
  • );

  • } +} * Add File: src/main/java/com/example/companionai/item/CompanionCoreItem.java +package com.example.companionai.item;

+import com.example.companionai.ModEntityTypes; +import com.example.companionai.entity.CompanionEntity; +import net.minecraft.network.chat.Component; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.sounds.SoundEvents; +import net.minecraft.sounds.SoundSource; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.entity.EntitySpawnReason; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.Item; +import net.minecraft.world.level.Level; + +public class CompanionCoreItem extends Item {

  • public CompanionCoreItem(Item.Properties properties) {

  • Plain Text
       super(properties);
    
  • }

  • @Override

  • public InteractionResult use(Level level, Player player, InteractionHand hand) {

  • Plain Text
       if (level instanceof ServerLevel serverLevel) {
    
  • Plain Text
           CompanionEntity companion = ModEntityTypes.COMPANION.create(serverLevel, EntitySpawnReason.SPAWN_ITEM_USE);
    
  • Plain Text
           if (companion != null) {
    
  • Plain Text
               companion.snapTo(player.getX() + 1.5, player.getY(), player.getZ(), player.getYRot(), 0.0F);
    
  • Plain Text
               companion.setOwner(player.getUUID());
    
  • Plain Text
               serverLevel.addFreshEntity(companion);
    
  • Plain Text
               serverLevel.playSound(
    
  • Plain Text
                   null,
    
  • Plain Text
                   player.getX(),
    
  • Plain Text
                   player.getY(),
    
  • Plain Text
                   player.getZ(),
    
  • Plain Text
                   SoundEvents.EXPERIENCE_ORB_PICKUP,
    
  • Plain Text
                   SoundSource.PLAYERS,
    
  • Plain Text
                   1.0F,
    
  • Plain Text
                   1.0F
    
  • );

  • Plain Text
               player.sendSystemMessage(Component.translatable("message.companionai.summoned"));
    
  • Plain Text
               player.getItemInHand(hand).consume(1, player);
    
  • }

  • }

  • Plain Text
       return InteractionResult.SUCCESS;
    
  • } +} * End Patch

name=“apply_patch”> name=“input”>* Begin Patch * Add File: src/main/java/com/example/companionai/entity/CompanionEntity.java +package com.example.companionai.entity; + +import java.util.UUID; +import net.minecraft.core.UUIDUtil; +import net.minecraft.sounds.SoundEvent; +import net.minecraft.sounds.SoundEvents; +import net.minecraft.world.damagesource.DamageSource; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.Mob; +import net.minecraft.world.entity.PathfinderMob; +import net.minecraft.world.entity.ai.attributes.AttributeSupplier; +import net.minecraft.world.entity.ai.attributes.Attributes; +import net.minecraft.world.entity.ai.goal.FloatGoal; +import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal; +import net.minecraft.world.entity.ai.goal.MeleeAttackGoal; +import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal; +import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal; +import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal; +import net.minecraft.world.entity.monster.Monster; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.storage.ValueInput; +import net.minecraft.world.level.storage.ValueOutput; + +public class CompanionEntity extends PathfinderMob {

  • private UUID ownerUuid;

  • public CompanionEntity(EntityType<? extends CompanionEntity> type, Level level) {

  • Plain Text
       super(type, level);
    
  • Plain Text
       this.setPersistenceRequired();
    
  • }

  • public static AttributeSupplier.Builder createAttributes() {

  • Plain Text
       return Mob.createMobAttributes()
    
  • Plain Text
           .add(Attributes.MAX_HEALTH, 40.0)
    
  • Plain Text
           .add(Attributes.MOVEMENT_SPEED, 0.3)
    
  • Plain Text
           .add(Attributes.ATTACK_DAMAGE, 6.0)
    
  • Plain Text
           .add(Attributes.FOLLOW_RANGE, 32.0);
    
  • }

  • @Override

  • protected void registerGoals() {

  • Plain Text
       this.goalSelector.addGoal(0, new FloatGoal(this));
    
  • Plain Text
       this.goalSelector.addGoal(1, new MeleeAttackGoal(this, 1.2D, true));
    
  • Plain Text
       this.goalSelector.addGoal(2, new CompanionFollowOwnerGoal(this, 1.0D));
    
  • Plain Text
       this.goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 8.0F));
    
  • Plain Text
       this.goalSelector.addGoal(4, new WaterAvoidingRandomStrollGoal(this, 1.0D));
    
  • Plain Text
       this.targetSelector.addGoal(0, new HurtByTargetGoal(this));
    
  • Plain Text
       this.targetSelector.addGoal(1, new NearestAttackableTargetGoal<>(this, Monster.class, true));
    
  • }

  • public void setOwner(UUID uuid) {

  • Plain Text
       this.ownerUuid = uuid;
    
  • }

  • public Player getOwner() {

  • Plain Text
       return this.ownerUuid == null ? null : this.level().getPlayerByUUID(this.ownerUuid);
    
  • }

  • @Override

  • protected void addAdditionalSaveData(ValueOutput output) {

  • Plain Text
       super.addAdditionalSaveData(output);
    
  • Plain Text
       output.storeNullable("Owner", UUIDUtil.CODEC, this.ownerUuid);
    
  • }

  • @Override

  • protected void readAdditionalSaveData(ValueInput input) {

  • Plain Text
       super.readAdditionalSaveData(input);
    
  • Plain Text
       this.ownerUuid = input.read("Owner", UUIDUtil.CODEC).orElse(null);
    
  • }

  • @Override

  • protected SoundEvent getHurtSound(DamageSource source) {

  • Plain Text
       return SoundEvents.PLAYER_HURT;
    
  • }

  • @Override

  • protected SoundEvent getDeathSound() {

  • Plain Text
       return SoundEvents.PLAYER_DEATH;
    
  • } +} * Add File: src/main/java/com/example/companionai/entity/CompanionFollowOwnerGoal.java +package com.example.companionai.entity;

+import java.util.EnumSet; +import net.minecraft.world.entity.ai.goal.Goal; +import net.minecraft.world.entity.player.Player; + +public class CompanionFollowOwnerGoal extends Goal {

  • private static final double START_DISTANCE_SQR = 12.0 * 12.0;

  • private static final double STOP_DISTANCE_SQR = 3.0 * 3.0;

  • private static final double TELEPORT_DISTANCE_SQR = 30.0 * 30.0;

  • private final CompanionEntity companion;

  • private final double speedModifier;

  • private Player owner;

  • private int timeToRecalcPath;

  • public CompanionFollowOwnerGoal(CompanionEntity companion, double speedModifier) {

  • Plain Text
       this.companion = companion;
    
  • Plain Text
       this.speedModifier = speedModifier;
    
  • Plain Text
       this.setFlags(EnumSet.of(Goal.Flag.MOVE));
    
  • }

  • @Override

  • public boolean canUse() {

  • Plain Text
       Player player = this.companion.getOwner();
    
  • Plain Text
       if (player == null || this.companion.getTarget() != null) {
    
  • Plain Text
           return false;
    
  • }

  • Plain Text
       if (this.companion.distanceToSqr(player) < START_DISTANCE_SQR) {
    
  • Plain Text
           return false;
    
  • }

  • Plain Text
       this.owner = player;
    
  • Plain Text
       return true;
    
  • }

  • @Override

  • public boolean canContinueToUse() {

  • Plain Text
       return this.owner != null
    
  • Plain Text
           && this.companion.getTarget() == null
    
  • Plain Text
           && this.companion.distanceToSqr(this.owner) > STOP_DISTANCE_SQR;
    
  • }

  • @Override

  • public void start() {

  • Plain Text
       this.timeToRecalcPath = 0;
    
  • }

  • @Override

  • public void stop() {

  • Plain Text
       this.owner = null;
    
  • Plain Text
       this.companion.getNavigation().stop();
    
  • }

  • @Override

  • public void tick() {

  • Plain Text
       if (this.companion.distanceToSqr(this.owner) > TELEPORT_DISTANCE_SQR) {
    
  • Plain Text
           this.companion.teleportTo(this.owner.getX(), this.owner.getY(), this.owner.getZ());
    
  • Plain Text
           this.companion.getNavigation().stop();
    
  • Plain Text
           return;
    
  • }

  • Plain Text
       if (--this.timeToRecalcPath <= 0) {
    
  • Plain Text
           this.timeToRecalcPath = 10;
    
  • Plain Text
           this.companion.getNavigation().moveTo(this.owner, this.speedModifier);
    
  • }

  • } +} * End Patch

name=“apply_patch”> name=“input”>* Begin Patch * Add File: src/client/java/com/example/companionai/client/CompanionAiClient.java +package com.example.companionai.client; + +import com.example.companionai.ModEntityTypes; +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.rendering.v1.EntityRendererRegistry; + +public class CompanionAiClient implements ClientModInitializer {

  • @Override

  • public void onInitializeClient() {

  • Plain Text
       EntityRendererRegistry.register(ModEntityTypes.COMPANION, CompanionRenderer::new);
    
  • } +} * Add File: src/client/java/com/example/companionai/client/CompanionRenderer.java +package com.example.companionai.client;

+import com.example.companionai.entity.CompanionEntity; +import net.minecraft.client.model.HumanoidModel; +import net.minecraft.client.model.geom.ModelLayers; +import net.minecraft.client.renderer.entity.EntityRendererProvider; +import net.minecraft.client.renderer.entity.HumanoidMobRenderer; +import net.minecraft.client.renderer.entity.state.HumanoidRenderState; +import net.minecraft.client.resources.DefaultPlayerSkin; +import net.minecraft.resources.Identifier; + +public class CompanionRenderer extends HumanoidMobRenderer<CompanionEntity, HumanoidRenderState, HumanoidModel<HumanoidRenderState>> {

  • public CompanionRenderer(EntityRendererProvider.Context context) {

  • Plain Text
       super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.PLAYER)), 0.5F);
    
  • }

  • @Override

  • public HumanoidRenderState createRenderState() {

  • Plain Text
       return new HumanoidRenderState();
    
  • }

  • @Override

  • public Identifier getTextureLocation(HumanoidRenderState state) {

  • Plain Text
       return DefaultPlayerSkin.getDefaultTexture();
    
  • } +} * End Patch

name=“apply_patch”> name=“input”>* Begin Patch * Add File: src/main/resources/assets/companionai/items/companion_core.json +{

  • “model”: {

  • “type”: “minecraft:model”,

  • “model”: “companionai:item/companion_core”

  • } +} * Add File: src/main/resources/assets/companionai/models/item/companion_core.json +{

  • “parent”: “minecraft:item/generated”,

  • “textures”: {

  • “layer0”: “companionai:item/companion_core”

  • } +} * Add File: src/main/resources/assets/companionai/lang/en_us.json +{

  • “item.companionai.companion_core”: “AI Companion Core”,

  • “entity.companionai.companion”: “AI Companion”,

  • “message.companionai.summoned”: “Hi! I’m here. Let’s play together!” +} * Add File: src/main/resources/data/companionai/recipe/companion_core.json +{

  • “type”: “minecraft:crafting_shaped”,

  • “category”: “misc”,

  • “pattern”: [

  • “DRD”,

  • “RND”,

  • “DRD”

  • ],

  • “key”: {

  • “D”: “minecraft:diamond”,

  • “R”: “minecraft:redstone”,

  • “N”: “minecraft:nether_star”

  • },

  • “result”: {

  • “id”: “companionai:companion_core”,

  • “count”: 1

  • } +} * End Patch

name=“apply_patch”> name=“input”>* Begin Patch * Add File: src/main/resources/data/companionai/recipe/companion_core.json +{

  • “type”: “minecraft:crafting_shaped”,

  • “category”: “misc”,

  • “pattern”: [

  • “DRD”,

  • “RND”,

  • “DRD”

  • ],

  • “key”: {

  • “D”: “minecraft:diamond”,

  • “R”: “minecraft:redstone”,

  • “N”: “minecraft:nether_star”

  • },

  • “result”: {

  • “id”: “companionai:companion_core”,

  • “count”: 1

  • } +} * End Patch

POST /fabric/companionai/patch… Calling the apply_patch tool with a patch envelope at once: error — Each file must be be patched once per envelope; duplicate operations on the same file were rejected.

Kodari can make mistakes. Review important code before using.
Files
src
Code
1 0