SpongePowered / Mixin

Mixin is a trait/mixin and bytecode weaving framework for Java using ASM
MIT License
1.37k stars 185 forks source link

Change code in a non-standard location in a method #634

Closed suliman03 closed 1 year ago

suliman03 commented 1 year ago

I'm new to modding and Java Mixin and I'm trying to modify some code in the following method:

protected void onCollision(HitResult hitResult) {
        super.onCollision(hitResult);
...
        entity.onLanding();
        entity.damage(this.getDamageSources().fall(), 5.0f);
...
    }

I would like to change the line entity.damage(this.getDamageSources().fall(), 5.0f); for if (bool) {entity.damage(this.getDamageSources().fall(), 5.0f)} How can I do this? With @REDIRECT?

suliman03 commented 1 year ago

Already figured it out. For those who wonder:

@Redirect(
            method = "onCollision(Lnet/minecraft/util/hit/HitResult;)V",
            at = @At(
                    value = "INVOKE",
                    target = "Lnet/minecraft/entity/Entity;damage(Lnet/minecraft/entity/damage/DamageSource;F)Z"
            )
    )
    private boolean onDamage(Entity entity, DamageSource damageSource, float damage) {
        damage = 5;
        if (entity instanceof ServerPlayerEntity) {
            ServerPlayerEntity serverPlayerEntity = (ServerPlayerEntity)entity;
            if (serverPlayerEntity.getAttributeValue(MMGAEntityAttributes.ENDERMEN_IMMUNITY) < 1) {
                entity.damage(this.getDamageSources().fall(), damage);
                return true;
            }
        }
        return false;
    }
Mumfrey commented 1 year ago

Hi, glad you figured out your question but for future questions please use the discord channel linked in the readme as this is an issue tracker.