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 Oct 20, 2024
1 parent 7367d53 commit f202f5a
Show file tree
Hide file tree
Showing 46 changed files with 1,973 additions and 1,646 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 = "hurt", 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,27 @@
*/
package org.spongepowered.forge.mixin.core.world.entity.player;

import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.event.entity.player.CriticalHitEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.common.util.DamageEventUtil;
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.forge.mixin.core.world.entity.LivingEntityMixin_Forge_Damage;

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

@Redirect(method = "attack", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/common/ForgeHooks;getCriticalHit(Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/world/entity/Entity;ZF)Lnet/minecraftforge/event/entity/player/CriticalHitEvent;"))
private CriticalHitEvent attackImpl$critHook(final Player player, final Entity target, final boolean vanillaCritical, final float damageModifier) {
final CriticalHitEvent event = ForgeHooks.getCriticalHit(player, target, vanillaCritical, damageModifier);
if (event != null) {
this.attackImpl$attack.functions().add(DamageEventUtil.provideCriticalAttackFunction(this.attackImpl$attack.sourceEntity(), event.getDamageModifier()));
}
return event;
@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 @@ -27,9 +27,9 @@
"server.network.ServerGamePacketListenerImplMixin_Forge",
"server.network.ServerLoginPacketListenerImplMixin_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.block.FireBlockMixin_Forge",
"world.level.block.entity.AbstractFurnaceBlockEntityMixin_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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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.neoforge.mixin.core.world.entity;

import net.minecraft.world.entity.LivingEntity;
import net.neoforged.neoforge.common.CommonHooks;
import net.neoforged.neoforge.common.damagesource.DamageContainer;
import net.neoforged.neoforge.event.entity.living.LivingShieldBlockEvent;
import org.spongepowered.api.event.cause.entity.damage.DamageStepTypes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
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;

import java.util.Stack;

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

@Shadow protected Stack<DamageContainer> damageContainers;

@Override
public float damage$getContainerDamage(final float damage) {
return this.damageContainers.peek().getNewDamage();
}

@Override
public void damage$setContainerDamage(final float damage) {
this.damageContainers.peek().setNewDamage(damage);
}

@SuppressWarnings("UnstableApiUsage")
@Redirect(method = "hurt", at = @At(value = "INVOKE", target = "Lnet/neoforged/neoforge/common/CommonHooks;onDamageBlock(Lnet/minecraft/world/entity/LivingEntity;Lnet/neoforged/neoforge/common/damagesource/DamageContainer;Z)Lnet/neoforged/neoforge/event/entity/living/LivingShieldBlockEvent;"))
private LivingShieldBlockEvent damage$modifyBeforeAndAfterShield(final LivingEntity self, final DamageContainer container, final boolean blocked) {
final SpongeDamageTracker tracker = this.damage$tracker();
if (tracker == null || !blocked) { // don't capture when vanilla wouldn't block
return CommonHooks.onDamageBlock(self, container, false);
}

final float originalDamage = container.getNewDamage();
final SpongeDamageStep step = tracker.newStep(DamageStepTypes.SHIELD, originalDamage, ItemStackUtil.snapshotOf(self.getUseItem()));
float damage = (float) step.applyModifiersBefore();
container.setNewDamage(damage);
final LivingShieldBlockEvent event;
if (step.isSkipped()) {
event = new LivingShieldBlockEvent(self, container, true);
event.setBlocked(true);
} else {
event = CommonHooks.onDamageBlock(self, container, true);
container.setBlockedDamage(event);
damage = container.getNewDamage();
}
step.applyModifiersAfter(damage);
return event;
}

@Redirect(method = "hurt", at = @At(value = "INVOKE", target = "Lnet/neoforged/neoforge/common/damagesource/DamageContainer;setBlockedDamage(Lnet/neoforged/neoforge/event/entity/living/LivingShieldBlockEvent;)V"))
private void damage$cancelSetBlockedDamage(final DamageContainer container, final LivingShieldBlockEvent event) {
// We already did it above
}

@ModifyVariable(method = "actuallyHurt", ordinal = 1,
at = @At(value = "INVOKE_ASSIGN", target = "Lnet/neoforged/neoforge/common/CommonHooks;onLivingDamagePre(Lnet/minecraft/world/entity/LivingEntity;Lnet/neoforged/neoforge/common/damagesource/DamageContainer;)F"))
private float damage$modifyBeforeAbsorption(final float damage) {
final SpongeDamageTracker tracker = this.damage$tracker();
return tracker == null ? damage : tracker.startStep(DamageStepTypes.ABSORPTION, damage, this);
}

@SuppressWarnings("UnstableApiUsage")
@Redirect(method = "actuallyHurt", at = @At(value = "INVOKE", target = "Lnet/neoforged/neoforge/common/damagesource/DamageContainer;setReduction(Lnet/neoforged/neoforge/common/damagesource/DamageContainer$Reduction;F)V"), slice = @Slice(
from = @At(value = "INVOKE", target = "Lnet/neoforged/neoforge/common/CommonHooks;onLivingDamagePre(Lnet/minecraft/world/entity/LivingEntity;Lnet/neoforged/neoforge/common/damagesource/DamageContainer;)F")))
private void damage$skipAbsorption(final DamageContainer container, final DamageContainer.Reduction reduction, final float absorbed) {
final SpongeDamageTracker tracker = this.damage$tracker();
if (tracker == null || !tracker.isSkipped(DamageStepTypes.ABSORPTION)) {
container.setReduction(reduction, absorbed);
}
}

@ModifyVariable(method = "actuallyHurt", at = @At("LOAD"), ordinal = 3, slice = @Slice(
from = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerPlayer;awardStat(Lnet/minecraft/resources/ResourceLocation;I)V"),
to = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;getCombatTracker()Lnet/minecraft/world/damagesource/CombatTracker;")))
private float damage$firePostEvent_Living(final float damage) {
return this.damage$firePostEvent(damage);
}
}
Loading

0 comments on commit f202f5a

Please sign in to comment.