Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 303
src/main/java/com/kodari/npcatelier/NpcAtelier.java
package com.kodari.npcatelier;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.passive.VillagerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.village.TradeOffer;
public class NpcAtelier implements ModInitializer {
private static final String NPC_TAG = "npcatelier_npc";
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(
CommandManager.literal("npc")
.executes(context -> {
feedback(context.getSource(), "NPC commands: create, come, goto, stop, say, talk, trade.");
return 1;
})
.then(CommandManager.literal("create")
.then(CommandManager.argument("name", StringArgumentType.word())
.executes(context -> {
ServerCommandSource source = context.getSource();
ServerPlayerEntity player = source.getPlayerOrThrow();
String name = StringArgumentType.getString(context, "name");
VillagerEntity npc = EntityType.VILLAGER.create(source.getWorld());
if (npc == null) {
feedback(source, "Could not create that NPC.");
return 0;
}
npc.refreshPositionAndAngles(player.getX(), player.getY(), player.getZ(), player.getYaw(), player.getPitch());
npc.setCustomName(Text.literal(name));
npc.setCustomNameVisible(true);
npc.addCommandTag(NPC_TAG);
npc.setPersistent();
source.getWorld().spawnEntity(npc);
feedback(source, "Created NPC " + name + ".");
return 1;
})))
.then(CommandManager.literal("come")
.then(CommandManager.argument("name", StringArgumentType.word())
.executes(context -> {
ServerCommandSource source = context.getSource();
ServerPlayerEntity player = source.getPlayerOrThrow();
VillagerEntity npc = findNpc(source.getWorld(), player, StringArgumentType.getString(context, "name"));
if (npc == null) {
feedback(source, "No matching NPC within 64 blocks.");
return 0;
}
npc.getNavigation().startMovingTo(player, 1.0);
feedback(source, npc.getCustomName().getString() + " is coming to you.");
return 1;
})))
.then(CommandManager.literal("goto")
.then(CommandManager.argument("name", StringArgumentType.word())
.then(CommandManager.argument("x", StringArgumentType.word())
.then(CommandManager.argument("y", StringArgumentType.word())
.then(CommandManager.argument("z", StringArgumentType.word())
.executes(context -> {
ServerCommandSource source = context.getSource();
ServerPlayerEntity player = source.getPlayerOrThrow();
VillagerEntity npc = findNpc(source.getWorld(), player, StringArgumentType.getString(context, "name"));
if (npc == null) {
feedback(source, "No matching NPC within 64 blocks.");
return 0;
}
try {
double x = parseCoordinate(StringArgumentType.getString(context, "x"));
double y = parseCoordinate(StringArgumentType.getString(context, "y"));
double z = parseCoordinate(StringArgumentType.getString(context, "z"));
npc.getNavigation().startMovingTo(x, y, z, 1.0);
feedback(source, npc.getCustomName().getString() + " is heading to " + x + ", " + y + ", " + z + ".");
return 1;
} catch (IllegalArgumentException exception) {
feedback(source, "Coordinates must be finite numbers within 30,000,000 blocks.");
return 0;
}
}))))))
.then(CommandManager.literal("stop")
.then(CommandManager.argument("name", StringArgumentType.word())
.executes(context -> {
ServerCommandSource source = context.getSource();
ServerPlayerEntity player = source.getPlayerOrThrow();
VillagerEntity npc = findNpc(source.getWorld(), player, StringArgumentType.getString(context, "name"));
if (npc == null) {
feedback(source, "No matching NPC within 64 blocks.");
return 0;
}
npc.getNavigation().stop();
feedback(source, npc.getCustomName().getString() + " stopped.");
return 1;
})))
.then(CommandManager.literal("say")
.then(CommandManager.argument("name", StringArgumentType.word())
.then(CommandManager.argument("message", StringArgumentType.greedyString())
.executes(context -> {
ServerCommandSource source = context.getSource();
ServerPlayerEntity player = source.getPlayerOrThrow();
VillagerEntity npc = findNpc(source.getWorld(), player, StringArgumentType.getString(context, "name"));
if (npc == null) {
feedback(source, "No matching NPC within 64 blocks.");
return 0;
}
feedback(source, "<" + npc.getCustomName().getString() + "> " + StringArgumentType.getString(context, "message"));
return 1;
}))))
.then(CommandManager.literal("talk")
.then(CommandManager.argument("name", StringArgumentType.word())
.then(CommandManager.argument("message", StringArgumentType.greedyString())
.executes(context -> {
ServerCommandSource source = context.getSource();
ServerPlayerEntity player = source.getPlayerOrThrow();
VillagerEntity npc = findNpc(source.getWorld(), player, StringArgumentType.getString(context, "name"));
if (npc == null) {
feedback(source, "No matching NPC within 64 blocks.");
return 0;
}
String response = replyTo(StringArgumentType.getString(context, "message"));
feedback(source, "<" + npc.getCustomName().getString() + "> " + response);
return 1;
}))))
.then(CommandManager.literal("trade")
.then(CommandManager.argument("name", StringArgumentType.word())
.then(CommandManager.argument("buy_item", StringArgumentType.word())
.then(CommandManager.argument("buy_count", StringArgumentType.word())
.then(CommandManager.argument("sell_item", StringArgumentType.word())
.then(CommandManager.argument("sell_count", StringArgumentType.word())
.executes(context -> {
ServerCommandSource source = context.getSource();
ServerPlayerEntity player = source.getPlayerOrThrow();
VillagerEntity npc = findNpc(source.getWorld(), player, StringArgumentType.getString(context, "name"));
if (npc == null) {
feedback(source, "No matching NPC within 64 blocks.");
return 0;
}
Item buyItem = resolveItem(StringArgumentType.getString(context, "buy_item"));
Item sellItem = resolveItem(StringArgumentType.getString(context, "sell_item"));
int buyCount;
int sellCount;
try {
buyCount = Integer.parseInt(StringArgumentType.getString(context, "buy_count"));
sellCount = Integer.parseInt(StringArgumentType.getString(context, "sell_count"));
} catch (NumberFormatException exception) {
feedback(source, "Trade counts must be whole numbers from 1 to 64.");
return 0;
}
if (buyItem == null || sellItem == null) {
feedback(source, "Unknown item ID. Use a registry ID such as minecraft:emerald.");
return 0;
}
if (buyCount < 1 || buyCount > 64 || sellCount < 1 || sellCount > 64) {
feedback(source, "Trade counts must be from 1 to 64.");
return 0;
}
npc.getOffers().add(new TradeOffer(
new ItemStack(buyItem, buyCount),
new ItemStack(sellItem, sellCount),
12,
0,
0.05f));
feedback(source, "Added trade to " + npc.getCustomName().getString() + ".");
return 1;
})))))))
));
}
private static VillagerEntity findNpc(ServerWorld world, ServerPlayerEntity player, String name) {
return world.getEntitiesByClass(
VillagerEntity.class,
player.getBoundingBox().expand(64.0),
npc -> npc.getCommandTags().contains(NPC_TAG)
&& npc.getCustomName() != null
&& npc.getCustomName().getString().equalsIgnoreCase(name))
.stream()
.findFirst()
.orElse(null);
}
private static Item resolveItem(String itemId) {
try {
Item item = Registries.ITEM.get(new Identifier(itemId));
return item == Items.AIR ? null : item;
} catch (IllegalArgumentException exception) {
return null;
}