thegooningpro

compile this

Kodari AI
KodariSep 24 at 11:31 am 1

I can’t run Gradle or Maven directly here. Compilation is checked automatically after code changes; share any build errors and I’ll fix them.

thegooningpro

compile it

Kodari AI
KodariSep 24 at 11:31 am 1

I can’t start a build from this interface. Compilation runs automatically after code changes; paste the build errors here and I’ll fix them.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
README.md
settings.gradle
Code
15 597
src/main/java/com/neuralnpc/NeuralNpcMod.java
package com.neuralnpc;

import com.neuralnpc.entity.NeuralNpcEntity;
import com.neuralnpc.chat.ChatMemory;
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.server.command.CommandManager;
import net.minecraft.command.argument.StringArgumentType;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnGroup;
import net.minecraft.item.Item;
import net.minecraft.item.SpawnEggItem;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;

public class NeuralNpcMod implements ModInitializer {
    public static final String MOD_ID = "neuralnpc";

    public static final EntityType<NeuralNpcEntity> NEURAL_NPC = Registry.register(
            Registries.ENTITY_TYPE,
            Identifier.of(MOD_ID, "neural_npc"),
            EntityType.Builder.create(NeuralNpcEntity::new, SpawnGroup.CREATURE)
                    .dimensions(0.6f, 1.8f)
                    .maxTrackingRange(64)
                    .build("neuralnpc:neural_npc")
    );

    public static final Item NEURAL_NPC_SPAWN_EGG = Registry.register(
            Registries.ITEM,
            Identifier.of(MOD_ID, "neural_npc_spawn_egg"),
            new SpawnEggItem(NEURAL_NPC, new Item.Settings().maxCount(64))
    );

    @Override
    public void onInitialize() {
        CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
                dispatcher.register(CommandManager.literal("neuralnpc")
                        .then(CommandManager.literal("sound")
                                .then(CommandManager.argument("sound", StringArgumentType.word())
                                        .suggests((context, builder) -> {
                                            builder.suggest("hello"); builder.suggest("bell"); builder.suggest("note");
                                            builder.suggest("levelup"); builder.suggest("burp"); builder.suggest("cat");
                                            builder.suggest("cow"); builder.suggest("chicken"); builder.suggest("pig");
                                            builder.suggest("zombie"); builder.suggest("creeper"); builder.suggest("anvil");
                                            builder.suggest("door"); builder.suggest("water");
                                            return builder.buildFuture();
                                        })
                                        .executes(context -> {
                                            var source = context.getSource();
                                            var player = source.getPlayerOrThrow();
                                            var name = StringArgumentType.getString(context, "sound");
                                            var npcs = player.getServerWorld().getEntitiesByType(NEURAL_NPC,
                                                    e -> e.squaredDistanceTo(player) <= 24.0 * 24.0);
                                            int count = 0;
                                            for (NeuralNpcEntity npc : npcs) if (npc.makeSound(name)) count++;
                                            source.sendFeedback(() -> net.minecraft.text.Text.literal(
                                                    count == 0 ? "No nearby Neural NPC could play that sound."
                                                               : "Neural NPC sound played: " + name), false);
                                            return count;
                                        }))));
        ServerMessageEvents.CHAT_MESSAGE.register((message, sender, params) -> {
            String content = message.getContent().getString();
            ChatMemory.add(sender.getName().getString(), content, sender.getServerWorld().getTime());

            // Every nearby Neural NPC gets the message as sensory input. Only addressed NPCs reply.
            sender.getServer().getWorlds().forEach(world ->
                    world.getEntitiesByType(NEURAL_NPC, e -> e.squaredDistanceTo(sender) <= 20.0 * 20.0)
                            .forEach(e -> e.receiveChat(sender.getName().getString(), content)));
        });
        ItemGroupEvents.modifyEntriesEvent(net.minecraft.item.ItemGroups.SPAWN_EGGS)
                .register(entries -> entries.addAfter(Items.ARMADILLO_SPAWN_EGG, NEURAL_NPC_SPAWN_EGG));
    }
}