NaturesAura/src/main/java/de/ellpeck/naturesaura/items/ItemDeathRing.java

46 lines
1.6 KiB
Java
Raw Normal View History

2020-02-07 23:50:16 +01:00
package de.ellpeck.naturesaura.items;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.packet.PacketClient;
import de.ellpeck.naturesaura.packet.PacketHandler;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-15 16:24:53 +01:00
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.player.Player;
2024-02-03 14:56:07 +01:00
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.living.LivingDeathEvent;
import net.neoforged.bus.api.SubscribeEvent;
2020-02-07 23:50:16 +01:00
public class ItemDeathRing extends ItemImpl {
2021-12-15 16:24:53 +01:00
2020-02-07 23:50:16 +01:00
public ItemDeathRing() {
2021-12-15 16:24:53 +01:00
super("death_ring", new Properties().stacksTo(1));
2024-02-03 14:56:07 +01:00
NeoForge.EVENT_BUS.register(new Events());
2020-02-07 23:50:16 +01:00
}
public static class Events {
@SubscribeEvent
public void onDeath(LivingDeathEvent event) {
var entity = event.getEntity();
2023-07-08 12:32:27 +02:00
if (!entity.level().isClientSide && entity instanceof Player) {
var equipped = Helper.getEquippedItem(s -> s.getItem() == ModItems.DEATH_RING, (Player) entity, false);
2020-02-07 23:50:16 +01:00
if (!equipped.isEmpty()) {
entity.setHealth(entity.getMaxHealth() / 2);
2021-12-15 16:24:53 +01:00
entity.removeAllEffects();
entity.addEffect(new MobEffectInstance(MobEffects.REGENERATION, 500, 1));
2020-02-07 23:50:16 +01:00
2021-12-15 16:30:22 +01:00
var data = new CompoundTag();
2021-12-15 16:24:53 +01:00
data.putInt("id", entity.getId());
2023-07-08 12:32:27 +02:00
PacketHandler.sendToAllAround(entity.level(), entity.blockPosition(), 32, new PacketClient(1, data));
2020-02-07 23:50:16 +01:00
equipped.shrink(1);
event.setCanceled(true);
}
}
}
2020-02-07 23:50:16 +01:00
}
2020-02-07 23:50:16 +01:00
}