Files
src
build.gradle
gradle.properties
Code
4 152
src/main/java/com/kodari/lazosvivos/LazosVivos.java
package com.kodari.lazosvivos;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
public class LazosVivos implements ModInitializer {
private static final String GENDER_PREFIX = "kodari_gender_";
private static final String MOOD_PREFIX = "kodari_mood_";
private static final String TRAIT_PREFIX = "kodari_trait_";
private static final String FRIEND_PREFIX = "kodari_friend_";
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
dispatcher.register(Commands.literal("vinculo")
.then(Commands.literal("genero")
.then(Commands.literal("mujer").executes(context -> setValue(context.getSource(), GENDER_PREFIX, "mujer", "Género elegido: mujer.")))
.then(Commands.literal("hombre").executes(context -> setValue(context.getSource(), GENDER_PREFIX, "hombre", "Género elegido: hombre.")))
.then(Commands.literal("no_binario").executes(context -> setValue(context.getSource(), GENDER_PREFIX, "no_binario", "Género elegido: no binario."))))
.then(Commands.literal("animo")
.then(Commands.literal("feliz").executes(context -> setValue(context.getSource(), MOOD_PREFIX, "feliz", "Ánimo actualizado: feliz.")))
.then(Commands.literal("triste").executes(context -> setValue(context.getSource(), MOOD_PREFIX, "triste", "Ánimo actualizado: triste.")))
.then(Commands.literal("enojado").executes(context -> setValue(context.getSource(), MOOD_PREFIX, "enojado", "Ánimo actualizado: enojado.")))
.then(Commands.literal("tranquilo").executes(context -> setValue(context.getSource(), MOOD_PREFIX, "tranquilo", "Ánimo actualizado: tranquilo."))))
.then(Commands.literal("rasgo")
.then(Commands.literal("amable").executes(context -> setValue(context.getSource(), TRAIT_PREFIX, "amable", "Rasgo elegido: amable.")))
.then(Commands.literal("curioso").executes(context -> setValue(context.getSource(), TRAIT_PREFIX, "curioso", "Rasgo elegido: curioso.")))
.then(Commands.literal("valiente").executes(context -> setValue(context.getSource(), TRAIT_PREFIX, "valiente", "Rasgo elegido: valiente."))))
.then(Commands.literal("amistad")
.then(Commands.argument("objetivo", EntityArgument.entity())
.executes(context -> createFriendship(context.getSource(), EntityArgument.getEntity(context, "objetivo")))))
.then(Commands.literal("perfil").executes(context -> showProfile(context.getSource())))
)
);
}
private static int setValue(CommandSourceStack source, String prefix, String value, String message) throws CommandSyntaxException {
ServerPlayer player = source.getPlayerOrException();
replaceTag(player, prefix, value);
source.sendSuccess(() -> Component.literal(message), false);
return 1;
}
private static int createFriendship(CommandSourceStack source, Entity target) throws CommandSyntaxException {
ServerPlayer player = source.getPlayerOrException();
if (target.getUUID().equals(player.getUUID())) {
source.sendSuccess(() -> Component.literal("Elige otra entidad para formar una amistad."), false);
return 0;
}
boolean added = target.addTag(FRIEND_PREFIX + player.getUUID());
source.sendSuccess(
() -> Component.literal(added ? "Has formado un vínculo de amistad." : "Ya tienes una amistad registrada con esa entidad."),
false
);
return added ? 1 : 0;
}
private static int showProfile(CommandSourceStack source) throws CommandSyntaxException {
ServerPlayer player = source.getPlayerOrException();
String gender = readValue(player, GENDER_PREFIX);
String mood = readValue(player, MOOD_PREFIX);
String trait = readValue(player, TRAIT_PREFIX);
source.sendSuccess(
() -> Component.literal("Perfil — género: " + gender + ", ánimo: " + mood + ", rasgo: " + trait + "."),
false
);
return 1;
}
private static void replaceTag(Entity entity, String prefix, String value) {
entity.entityTags().stream()
.filter(tag -> tag.startsWith(prefix))
.toList()
.forEach(entity::removeTag);
entity.addTag(prefix + value);
}
private static String readValue(Entity entity, String prefix) {
return entity.entityTags().stream()
.filter(tag -> tag.startsWith(prefix))
.findFirst()
.map(tag -> tag.substring(prefix.length()))
.orElse("sin elegir");
}
}