Files
src
build.gradle
gradle.properties
settings.gradle
Code
8 368
src/main/java/br/com/sobrenatural/Sobrenatural.java
package br.com.sobrenatural;
import com.mojang.serialization.Codec;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
import net.fabricmc.fabric.api.attachment.v1.AttachmentSyncPredicate;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.Identifier;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
public class Sobrenatural implements ModInitializer {
public static final AttachmentType<String> RACA = AttachmentRegistry.create(
Identifier.fromNamespaceAndPath("sobrenatural", "raca"),
builder -> builder.persistent(Codec.STRING).copyOnDeath().syncWith(ByteBufCodecs.fromCodec(Codec.STRING), AttachmentSyncPredicate.all())
);
public static final int SANGUE_MAXIMO = 100;
public static final AttachmentType<Integer> SANGUE = AttachmentRegistry.create(
Identifier.fromNamespaceAndPath("sobrenatural", "sangue"),
builder -> builder.persistent(Codec.INT).initializer(() -> SANGUE_MAXIMO).copyOnDeath().syncWith(ByteBufCodecs.fromCodec(Codec.INT), AttachmentSyncPredicate.targetOnly())
);
private static final AttachmentType<Integer> EXPERIENCIA = AttachmentRegistry.create(
Identifier.fromNamespaceAndPath("sobrenatural", "experiencia"),
builder -> builder.persistent(Codec.INT).initializer(() -> 0).copyOnDeath()
);
private static final AttachmentType<Integer> ABATES = AttachmentRegistry.create(
Identifier.fromNamespaceAndPath("sobrenatural", "abates"),
builder -> builder.persistent(Codec.INT).initializer(() -> 0).copyOnDeath()
);
private static final Map<UUID, Pedido> PEDIDOS = new HashMap<>();
private static final Map<UUID, Long> RECARGAS = new HashMap<>();
private int ticksSangue;
private record Pedido(UUID autor, long expiraEm) {}
public record HabilidadePayload() implements CustomPacketPayload {
public static final Type<HabilidadePayload> TYPE = new Type<>(Identifier.fromNamespaceAndPath("sobrenatural", "habilidade"));
public static final StreamCodec<RegistryFriendlyByteBuf, HabilidadePayload> CODEC = StreamCodec.unit(new HabilidadePayload());
@Override
public Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}
@Override
public void onInitialize() {
PayloadTypeRegistry.serverboundPlay().register(HabilidadePayload.TYPE, HabilidadePayload.CODEC);
ServerPlayNetworking.registerGlobalReceiver(HabilidadePayload.TYPE, (payload, context) -> usarHabilidade(context.player()));
ServerTickEvents.END_SERVER_TICK.register(server -> {
boolean consumir = ++ticksSangue >= 1200;
if (consumir) ticksSangue = 0;
for (ServerPlayer jogador : server.getPlayerList().getPlayers()) {
String raca = jogador.getAttachedOrSet(RACA, "humano");
int sangue = jogador.getAttachedOrCreate(SANGUE);
if (consumir && ("vampiro".equals(raca) || "hibrido".equals(raca)) && sangue > 0) {
jogador.setAttached(SANGUE, sangue - 1);
}
}
});
ServerLivingEntityEvents.AFTER_DEATH.register((entidade, dano) -> {
if (entidade instanceof ServerPlayer || !(dano.getEntity() instanceof ServerPlayer jogador)) {
return;
}
String raca = jogador.getAttachedOrSet(RACA, "humano");
if ("vampiro".equals(raca) || "hibrido".equals(raca)) {
jogador.setAttached(SANGUE, Math.min(SANGUE_MAXIMO, jogador.getAttachedOrCreate(SANGUE) + 15));
}
int abates = jogador.getAttachedOrCreate(ABATES) + 1;
int experiencia = jogador.getAttachedOrCreate(EXPERIENCIA) + 5;
jogador.setAttached(ABATES, abates);
if (abates % 10 == 0) {
experiencia += 50;
jogador.sendSystemMessage(Component.literal("Missão concluída: derrote 10 criaturas. Recompensa: 50 de experiência sobrenatural."));
}
jogador.setAttached(EXPERIENCIA, experiencia);
});
CommandRegistrationCallback.EVENT.register((dispatcher, context, selection) -> {
var raiz = Commands.literal("sobrenatural")
.then(Commands.literal("perfil").executes(ctx -> {
ServerPlayer jogador = ctx.getSource().getPlayerOrException();
String raca = jogador.getAttachedOrSet(RACA, "humano");
int experiencia = jogador.getAttachedOrCreate(EXPERIENCIA);
int abates = jogador.getAttachedOrCreate(ABATES);
int sangue = jogador.getAttachedOrCreate(SANGUE);
ctx.getSource().sendSuccess(() -> Component.literal("Raça: " + raca + " | Rank: " + (experiencia / 100 + 1) + " | Experiência: " + experiencia + " | Sangue: " + sangue + "/" + SANGUE_MAXIMO + " | Missão: " + (abates % 10) + "/10 criaturas"), false);
return 1;
}))
.then(Commands.literal("transformar")
.then(Commands.argument("jogador", EntityArgument.player()).executes(ctx -> {
ServerPlayer autor = ctx.getSource().getPlayerOrException();
ServerPlayer alvo = EntityArgument.getPlayer(ctx, "jogador");
if (!"vampiro".equals(autor.getAttachedOrSet(RACA, "humano"))) {
ctx.getSource().sendFailure(Component.literal("Somente vampiros podem oferecer a transformação."));
return 0;
}
if (autor.getUUID().equals(alvo.getUUID())) {
ctx.getSource().sendFailure(Component.literal("Você não pode transformar a si mesmo."));
return 0;
}
if (!"humano".equals(alvo.getAttachedOrSet(RACA, "humano"))) {
ctx.getSource().sendFailure(Component.literal("Somente humanos podem receber esta transformação."));
return 0;
}
PEDIDOS.put(alvo.getUUID(), new Pedido(autor.getUUID(), System.currentTimeMillis() + 60000));
alvo.sendSystemMessage(Component.literal("Um vampiro ofereceu transformar você. Use /sobrenatural aceitar ou /sobrenatural recusar em até 60 segundos."));
ctx.getSource().sendSuccess(() -> Component.literal("Pedido de transformação enviado."), false);
return 1;
})))
.then(Commands.literal("aceitar").executes(ctx -> {
ServerPlayer jogador = ctx.getSource().getPlayerOrException();
Pedido pedido = PEDIDOS.remove(jogador.getUUID());
if (pedido == null || pedido.expiraEm() < System.currentTimeMillis()) {
ctx.getSource().sendFailure(Component.literal("Você não tem um pedido válido."));
return 0;
}
if (!"humano".equals(jogador.getAttachedOrSet(RACA, "humano"))) {
ctx.getSource().sendFailure(Component.literal("Apenas humanos podem aceitar esta transformação."));
return 0;
}
ServerPlayer autor = ctx.getSource().getServer().getPlayerList().getPlayer(pedido.autor());
if (autor == null || !"vampiro".equals(autor.getAttachedOrSet(RACA, "humano"))) {
ctx.getSource().sendFailure(Component.literal("O vampiro que fez a oferta não está mais disponível."));
return 0;
}
jogador.setAttached(RACA, "vampiro");
ctx.getSource().sendSuccess(() -> Component.literal("Você aceitou a transformação e agora é vampiro."), false);
return 1;
}))
.then(Commands.literal("recusar").executes(ctx -> {
ServerPlayer jogador = ctx.getSource().getPlayerOrException();
if (PEDIDOS.remove(jogador.getUUID()) == null) {
ctx.getSource().sendFailure(Component.literal("Você não tem um pedido de transformação."));
return 0;
}
ctx.getSource().sendSuccess(() -> Component.literal("Transformação recusada."), false);
return 1;
}));
var escolher = Commands.literal("escolher");
for (String raca : new String[] {"vampiro", "lobisomem", "bruxa", "hibrido", "cacador", "demonio"}) {
escolher.then(Commands.literal(raca).executes(ctx -> {
ServerPlayer jogador = ctx.getSource().getPlayerOrException();
if (jogador.getAttached(RACA) != null && !"humano".equals(jogador.getAttached(RACA))) {
ctx.getSource().sendFailure(Component.literal("Você já escolheu uma raça."));
return 0;
}
jogador.setAttached(RACA, raca);
PEDIDOS.remove(jogador.getUUID());
ctx.getSource().sendSuccess(() -> Component.literal("Sua raça agora é: " + raca), false);
return 1;
}));
}
dispatcher.register(raiz.then(escolher));
});
}
private static void usarHabilidade(ServerPlayer jogador) {
String raca = jogador.getAttachedOrSet(RACA, "humano");
long agora = System.currentTimeMillis();
long restante = RECARGAS.getOrDefault(jogador.getUUID(), 0L) - agora;
if (restante > 0) {
jogador.sendOverlayMessage(Component.literal("Habilidade em recarga: " + ((restante + 999) / 1000) + " s"));
return;
}
boolean usaSangue = "vampiro".equals(raca) || "hibrido".equals(raca);
int sangue = jogador.getAttachedOrCreate(SANGUE);
if (usaSangue && sangue < 20) {
jogador.sendOverlayMessage(Component.literal("Você precisa de 20 de sangue."));
return;
}
String habilidade;
switch (raca) {
case "vampiro" -> {
habilidade = "Ímpeto vampírico";
jogador.addEffect(new MobEffectInstance(MobEffects.SPEED, 300, 1));
jogador.addEffect(new MobEffectInstance(MobEffects.NIGHT_VISION, 400, 0));
}
case "lobisomem" -> {
habilidade = "Fúria lupina";
jogador.addEffect(new MobEffectInstance(MobEffects.STRENGTH, 300, 0));
jogador.addEffect(new MobEffectInstance(MobEffects.SPEED, 300, 0));