goldodemon/Minecraft-JavaSet / LivingEntityMixin.java
goldodemon's picture
download
raw
1.47 kB
package com.example.examplemod.mixin;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
/**
* Mixin into LivingEntity to add a "lucky dodge" chance that cancels damage.
* Mixins are version-sensitive at the bytecode level but this mixin target
* (damage(DamageSource, float)) has been stable across 1.21.7 - 1.21.11.
*
* Register in examplemod.mixins.json under "mixins": ["LivingEntityMixin"].
*/
@Mixin(LivingEntity.class)
public abstract class LivingEntityMixin {
// Inject at HEAD of damage(), cancel the whole method if our condition is met.
@Inject(method = "damage", at = @At("HEAD"), cancellable = true)
private void examplemod$onDamage(DamageSource source, float amount,
CallbackInfoReturnable<Boolean> cir) {
LivingEntity self = (LivingEntity) (Object) this;
// 5% chance to completely dodge any damage
if (self.getRandom().nextFloat() < 0.05f) {
if (!self.getWorld().isClient) {
self.sendMessage(Text.literal("You dodged the attack!"));
}
cir.setReturnValue(false); // cancel damage, "false" = no damage applied
}
}
}

Xet Storage Details

Size:
1.47 kB
·
Xet hash:
972dab4453e45722ed711b038cbfc558d753a03d558acb73ed311e76699e595a

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.