Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 322
src/main/java/com/kodari/cursedarts/CursedArts.java
package com.kodari.cursedarts;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.entity.event.v1.ServerEntityCombatEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import java.util.Comparator;
import java.util.List;
public final class CursedArts implements ModInitializer {
private static final String MOD_ID = "cursedarts";
private static final AttachmentType<CurseData> PLAYER_DATA = AttachmentRegistry.createPersistent(
Identifier.fromNamespaceAndPath(MOD_ID, "player_data"), CurseData.CODEC
);
@Override
public void onInitialize() {
ServerTickEvents.END_SERVER_TICK.register(server -> server.getPlayerList().getPlayers().forEach(player -> {
CurseData current = data(player);
CurseData updated = current.tick();
if (!current.equals(updated)) {
player.setAttached(PLAYER_DATA, updated);
}
}));
ServerEntityCombatEvents.AFTER_KILLED_OTHER_ENTITY.register((level, killer, killed, damageSource) -> {
if (killer instanceof ServerPlayer player) {
CurseData current = data(player);
player.setAttached(PLAYER_DATA, current.gainKill());
}
});
CommandRegistrationCallback.EVENT.register((dispatcher, buildContext, selection) -> registerCommands(dispatcher));
}
private static void registerCommands(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("curse")
.then(Commands.literal("status").executes(context -> status(context.getSource())))
.then(Commands.literal("choose")
.then(Commands.literal("rift").executes(context -> choose(context.getSource(), "rift")))
.then(Commands.literal("resonance").executes(context -> choose(context.getSource(), "resonance")))
.then(Commands.literal("slipstep").executes(context -> choose(context.getSource(), "slipstep"))))
.then(Commands.literal("cast").executes(context -> cast(context.getSource()))));
}
private static int status(CommandSourceStack source) throws CommandSyntaxException {
CurseData state = data(source.getPlayerOrException());
source.sendSuccess(() -> Component.literal("Technique: " + state.technique() + " | Rank: " + state.rank() + " | Progress: " + state.experience() + " | Cursed energy: " + state.energy() + "/" + state.maxEnergy()), false);
return 1;
}
private static int choose(CommandSourceStack source, String technique) throws CommandSyntaxException {
ServerPlayer player = source.getPlayerOrException();
player.setAttached(PLAYER_DATA, data(player).choose(technique));
source.sendSuccess(() -> Component.literal("Selected technique: " + technique + ". Use /curse cast to activate it."), false);
return 1;
}
private static int cast(CommandSourceStack source) throws CommandSyntaxException {
ServerPlayer player = source.getPlayerOrException();
CurseData state = data(player);
if (state.technique().equals("none")) {
source.sendFailure(Component.literal("Choose a technique first: /curse choose <rift|resonance|slipstep>"));
return 0;
}
if (state.cooldownTicks() > 0) {
source.sendFailure(Component.literal("Your technique is recharging."));
return 0;
}
ServerLevel level = player.level();
List<LivingEntity> targets;
int cost;
int cooldown;
switch (state.technique()) {
case "rift" -> {
cost = 30;
cooldown = 40;
targets = nearby(player, 4.5);
if (targets.isEmpty()) {
source.sendFailure(Component.literal("No targets are close enough for Rift Burst."));
return 0;
}
if (!canSpend(source, state, cost)) return 0;
player.setAttached(PLAYER_DATA, state.spend(cost, cooldown));
float damage = 4.0F + state.rank() * 0.75F;
for (LivingEntity target : targets) {
target.hurtServer(level, level.damageSources().indirectMagic(player, player), damage);
}
String result = "Rift Burst struck " + targets.size() + " nearby target(s).";
source.sendSuccess(() -> Component.literal(result), false);
}
case "resonance" -> {
cost = 25;
cooldown = 60;
targets = nearby(player, 8.0);
if (targets.isEmpty()) {
source.sendFailure(Component.literal("No targets are close enough for Resonance."));
return 0;
}
if (!canSpend(source, state, cost)) return 0;
player.setAttached(PLAYER_DATA, state.spend(cost, cooldown));
int amplifier = Math.min(2, (state.rank() - 1) / 4);
for (LivingEntity target : targets) {
target.addEffect(new MobEffectInstance(MobEffects.SLOWNESS, 80, amplifier), player);
}
String result = "Resonance hindered " + targets.size() + " nearby target(s).";
source.sendSuccess(() -> Component.literal(result), false);
}
case "slipstep" -> {
cost = 20;
cooldown = 60;
targets = nearby(player, 12.0);
LivingEntity nearest = targets.stream().min(Comparator.comparingDouble(target -> target.position().distanceToSqr(player.position()))).orElse(null);
if (nearest == null) {
source.sendFailure(Component.literal("No target is close enough for Slipstep."));
return 0;
}
if (!canSpend(source, state, cost)) return 0;
player.setAttached(PLAYER_DATA, state.spend(cost, cooldown));
Vec3 destination = nearest.position();
player.teleportTo(destination.x, destination.y + 1.0, destination.z);
source.sendSuccess(() -> Component.literal("Slipstep carried you to your nearest target."), false);
}
default -> {
source.sendFailure(Component.literal("Choose a valid technique first."));
return 0;
}
}
return 1;
}
private static boolean canSpend(CommandSourceStack source, CurseData state, int cost) {
if (state.energy() >= cost) {
return true;
}
source.sendFailure(Component.literal("Not enough cursed energy: " + state.energy() + "/" + cost + "."));
return false;
}
private static List<LivingEntity> nearby(ServerPlayer player, double radius) {
AABB bounds = player.getBoundingBox().inflate(radius);
return player.level().getEntitiesOfClass(LivingEntity.class, bounds, target -> target != player && target.isAlive());
}
private static CurseData data(ServerPlayer player) {
CurseData state = player.getAttached(PLAYER_DATA);
if (state == null) {
state = CurseData.fresh();
player.setAttached(PLAYER_DATA, state);
}
return state;
}
}