Files
src
build.gradle
gradle.properties
settings.gradle
Code
7 175
src/main/java/com/kodari/bloodreckoning/BloodReckoningMod.java
package com.kodari.bloodreckoning;
import java.util.Map;
import java.util.WeakHashMap;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.particle.DustParticleEffect;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.Registries;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;
public class BloodReckoningMod implements ModInitializer {
public static final String MOD_ID = "bloodreckoning";
private static final Map<PlayerEntity, Integer> HIT_COUNTS = new WeakHashMap<>();
@Override
public void onInitialize() {
AttackEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
if (!(world instanceof ServerWorld serverWorld) || !(entity instanceof LivingEntity target)) {
return ActionResult.PASS;
}
ItemStack weapon = player.getStackInHand(hand);
var enchantment = serverWorld.getRegistryManager()
.getOrThrow(RegistryKeys.ENCHANTMENT)
.getEntry(Identifier.of(MOD_ID, "blood_reckoning"))
.orElseThrow();
int level = EnchantmentHelper.getLevel(enchantment, weapon);
if (level <= 0) {
return ActionResult.PASS;
}
int hitCount = HIT_COUNTS.merge(player, 1, Integer::sum);
var damageSource = player.getDamageSources().playerAttack(player);
if (hitCount % 5 == 0) {
float siphonDamage = switch (level) {
case 1 -> 1.0F;
case 2 -> 2.0F;
default -> 4.0F;
};
float healthBefore = target.getHealth();
target.damage(serverWorld, damageSource, siphonDamage);
float healthTaken = Math.min(siphonDamage, Math.max(0.0F, healthBefore - target.getHealth()));
player.heal(healthTaken);
}
if (level >= 3 && hitCount % 10 == 0) {
target.damage(serverWorld, damageSource, 40.0F);
serverWorld.spawnParticles(
new DustParticleEffect(0x8B0000, 1.3F),
target.getX(), target.getY() + 1.0, target.getZ(),
32, 0.5, 0.7, 0.5, 0.02
);
serverWorld.playSound(
null,
target.getX(), target.getY(), target.getZ(),
Registries.SOUND_EVENT.getEntry(SoundEvents.ENTITY_WARDEN_SONIC_BOOM.id()).orElseThrow(),
SoundCategory.PLAYERS, 1.0F, 0.85F, 0L
);
}
return ActionResult.PASS;
});
}
}