Skip to content

Commit

Permalink
Rewrite attack and damage events
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeregorix committed Dec 30, 2024
1 parent 8a6fe45 commit a321867
Show file tree
Hide file tree
Showing 46 changed files with 1,977 additions and 1,662 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.forge.mixin.core.world.entity;

import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.entity.living.ShieldBlockEvent;
import org.spongepowered.api.event.cause.entity.damage.DamageStepTypes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.Slice;
import org.spongepowered.common.bridge.world.entity.TrackedDamageBridge;
import org.spongepowered.common.event.cause.entity.damage.SpongeDamageStep;
import org.spongepowered.common.event.cause.entity.damage.SpongeDamageTracker;
import org.spongepowered.common.item.util.ItemStackUtil;

@Mixin(LivingEntity.class)
public abstract class LivingEntityMixin_Forge_Damage implements TrackedDamageBridge {

@ModifyConstant(method = "actuallyHurt", constant = @Constant(floatValue = 0), slice = @Slice(
from = @At(value = "INVOKE", target = "Lnet/minecraftforge/common/ForgeHooks;onLivingHurt(Lnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/world/damagesource/DamageSource;F)F"),
to = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;getDamageAfterArmorAbsorb(Lnet/minecraft/world/damagesource/DamageSource;F)F")))
private float damage$doNotReturnEarly(final float constant) {
return Float.NaN;
}

@SuppressWarnings("UnstableApiUsage")
@Redirect(method = "hurtServer", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/event/ForgeEventFactory;onShieldBlock(Lnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/world/damagesource/DamageSource;F)Lnet/minecraftforge/event/entity/living/ShieldBlockEvent;"))
private ShieldBlockEvent damage$modifyBeforeAndAfterShield(final LivingEntity self, final DamageSource source, final float originalDamage) {
final SpongeDamageTracker tracker = this.damage$tracker();
if (tracker == null) {
return ForgeEventFactory.onShieldBlock(self, source, originalDamage);
}

final SpongeDamageStep step = tracker.newStep(DamageStepTypes.SHIELD, originalDamage, ItemStackUtil.snapshotOf(self.getUseItem()));
float damage = (float) step.applyModifiersBefore();
final ShieldBlockEvent event;
if (step.isSkipped()) {
event = new ShieldBlockEvent(self, source, damage);
event.setCanceled(true);
} else {
event = ForgeEventFactory.onShieldBlock(self, source, damage);
if (!event.isCanceled()) {
damage -= event.getBlockedDamage();
}
}
step.applyModifiersAfter(damage);
return event;
}

@ModifyArg(method = "actuallyHurt", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/common/ForgeHooks;onLivingDamage(Lnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/world/damagesource/DamageSource;F)F"))
private float damage$firePostEvent_Living(final float damage) {
return this.damage$firePostEvent(damage);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,29 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.vanilla.mixin.core.world.entity.player;
package org.spongepowered.forge.mixin.core.world.entity.player;

import net.minecraft.world.entity.player.Player;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import org.spongepowered.asm.mixin.injection.Slice;
import org.spongepowered.common.util.DamageEventUtil;
import org.spongepowered.forge.mixin.core.world.entity.LivingEntityMixin_Forge_Damage;

@Mixin(Player.class)
public abstract class PlayerMixin_Vanilla_Attack_Impl {
private DamageEventUtil.Attack<Player> attackImpl$attack;
public abstract class PlayerMixin_Forge_Damage extends LivingEntityMixin_Forge_Damage {

/**
* Captures the crit multiplier as a function
*/
@ModifyConstant(method = "attack", constant = @Constant(floatValue = 1.5F),
slice = @Slice(from = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;isSprinting()Z", ordinal = 1),
to = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;getKnownMovement()Lnet/minecraft/world/phys/Vec3;"))
)
public float attackImpl$critHook(final float constant) {
// if (isCritical) damage *= 1.5F;
final var bonusDamageFunc = DamageEventUtil.provideCriticalAttackFunction(this.attackImpl$attack.sourceEntity(), constant);
this.attackImpl$attack.functions().add(bonusDamageFunc);
return constant;
@ModifyConstant(method = "actuallyHurt", constant = @Constant(floatValue = 0), slice = @Slice(
from = @At(value = "INVOKE", target = "Lnet/minecraftforge/common/ForgeHooks;onLivingHurt(Lnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/world/damagesource/DamageSource;F)F"),
to = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;getDamageAfterArmorAbsorb(Lnet/minecraft/world/damagesource/DamageSource;F)F")))
private float damage$doNotReturnEarly(final float constant) {
return Float.NaN;
}

@ModifyArg(method = "actuallyHurt", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/common/ForgeHooks;onLivingDamage(Lnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/world/damagesource/DamageSource;F)F"))
private float damage$firePostEvent_Player(final float damage) {
return this.damage$firePostEvent(damage);
}
}
4 changes: 2 additions & 2 deletions forge/src/mixins/resources/mixins.spongeforge.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"server.level.ServerPlayerMixin_Forge",
"server.network.ServerGamePacketListenerImplMixin_Forge",
"world.entity.LivingEntityMixin_Forge",
"world.entity.LivingEntityMixin_Forge_Attack_Impl",
"world.entity.LivingEntityMixin_Forge_Damage",
"world.entity.item.ItemEntityMixin_Forge",
"world.entity.player.PlayerMixin_Forge_Attack_Impl",
"world.entity.player.PlayerMixin_Forge_Damage",
"world.entity.vehicle.BoatMixin_Forge",
"world.level.ServerExplosionMixin_Forge",
"world.level.block.FireBlockMixin_Forge",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"priority": 1301,
"mixins": [
"server.level.ServerEntityMixin_Shared",
"world.entity.LivingEntityMixin_Shared_Attack_Impl",
"world.entity.player.PlayerMixin_Shared_Attack_Impl",
"world.entity.LivingEntityMixin_Shared_Damage",
"world.entity.player.PlayerMixin_Shared_Damage",
"world.entity.projectile.FishingHookMixin_Shared"
],
"overwrites": {
Expand Down

This file was deleted.

Loading

0 comments on commit a321867

Please sign in to comment.