Files
src
build.gradle
settings.gradle
Code
8 145
src/main/java/com/kodari/heartsteal/HeartstealMod.java
package com.kodari.heartsteal;
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.player.PlayerEntity;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
public class HeartstealMod implements ModInitializer {
private static final RegistryKey<Enchantment> HEARTSTEAL = RegistryKey.of(
RegistryKeys.ENCHANTMENT,
Identifier.of("heartsteal", "heartsteal")
);
@Override
public void onInitialize() {
AttackEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
if (world.isClient()) {
return ActionResult.PASS;
}
Registry<Enchantment> enchantments = world.getRegistryManager().getOrThrow(RegistryKeys.ENCHANTMENT);
Enchantment enchantment = enchantments.get(HEARTSTEAL);
if (enchantment == null) {
return ActionResult.PASS;
}
int level = EnchantmentHelper.getLevel(enchantments.getEntry(enchantment), player.getMainHandStack());
int chance = switch (level) {
case 1 -> 10;
case 2 -> 30;
case 3 -> 50;
default -> 0;
};
if (chance > 0 && player.getRandom().nextInt(100) < chance) {
player.heal(2.0F);
}
return ActionResult.PASS;
});
}
}