Skip to content

Commit 4e7c4ed

Browse files
committed
Rewrite attack and damage events
1 parent c1c8907 commit 4e7c4ed

File tree

47 files changed

+1977
-1721
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1977
-1721
lines changed

SpongeAPI

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* This file is part of Sponge, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.forge.mixin.core.world.entity;
26+
27+
import net.minecraft.world.damagesource.DamageSource;
28+
import net.minecraft.world.entity.LivingEntity;
29+
import net.minecraftforge.event.ForgeEventFactory;
30+
import net.minecraftforge.event.entity.living.ShieldBlockEvent;
31+
import org.spongepowered.api.event.cause.entity.damage.DamageStepTypes;
32+
import org.spongepowered.asm.mixin.Mixin;
33+
import org.spongepowered.asm.mixin.injection.At;
34+
import org.spongepowered.asm.mixin.injection.Constant;
35+
import org.spongepowered.asm.mixin.injection.ModifyArg;
36+
import org.spongepowered.asm.mixin.injection.ModifyConstant;
37+
import org.spongepowered.asm.mixin.injection.Redirect;
38+
import org.spongepowered.asm.mixin.injection.Slice;
39+
import org.spongepowered.common.bridge.world.entity.TrackedDamageBridge;
40+
import org.spongepowered.common.event.cause.entity.damage.SpongeDamageStep;
41+
import org.spongepowered.common.event.cause.entity.damage.SpongeDamageTracker;
42+
import org.spongepowered.common.item.util.ItemStackUtil;
43+
44+
@Mixin(LivingEntity.class)
45+
public abstract class LivingEntityMixin_Forge_Damage implements TrackedDamageBridge {
46+
47+
@ModifyConstant(method = "actuallyHurt", constant = @Constant(floatValue = 0), slice = @Slice(
48+
from = @At(value = "INVOKE", target = "Lnet/minecraftforge/common/ForgeHooks;onLivingHurt(Lnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/world/damagesource/DamageSource;F)F"),
49+
to = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;getDamageAfterArmorAbsorb(Lnet/minecraft/world/damagesource/DamageSource;F)F")))
50+
private float damage$doNotReturnEarly(final float constant) {
51+
return Float.NaN;
52+
}
53+
54+
@SuppressWarnings("UnstableApiUsage")
55+
@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;"))
56+
private ShieldBlockEvent damage$modifyBeforeAndAfterShield(final LivingEntity self, final DamageSource source, final float originalDamage) {
57+
final SpongeDamageTracker tracker = this.damage$tracker();
58+
if (tracker == null) {
59+
return ForgeEventFactory.onShieldBlock(self, source, originalDamage);
60+
}
61+
62+
final SpongeDamageStep step = tracker.newStep(DamageStepTypes.SHIELD, originalDamage, ItemStackUtil.snapshotOf(self.getUseItem()));
63+
float damage = (float) step.applyModifiersBefore();
64+
final ShieldBlockEvent event;
65+
if (step.isSkipped()) {
66+
event = new ShieldBlockEvent(self, source, damage);
67+
event.setCanceled(true);
68+
} else {
69+
event = ForgeEventFactory.onShieldBlock(self, source, damage);
70+
if (!event.isCanceled()) {
71+
damage -= event.getBlockedDamage();
72+
}
73+
}
74+
step.applyModifiersAfter(damage);
75+
return event;
76+
}
77+
78+
@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"))
79+
private float damage$firePostEvent_Living(final float damage) {
80+
return this.damage$firePostEvent(damage);
81+
}
82+
}

forge/src/mixins/java/org/spongepowered/forge/mixin/core/world/entity/player/PlayerMixin_Forge_Attack_Impl.java

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,29 @@
2222
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
* THE SOFTWARE.
2424
*/
25-
package org.spongepowered.vanilla.mixin.core.world.entity.player;
25+
package org.spongepowered.forge.mixin.core.world.entity.player;
2626

2727
import net.minecraft.world.entity.player.Player;
2828
import org.spongepowered.asm.mixin.Mixin;
2929
import org.spongepowered.asm.mixin.injection.At;
3030
import org.spongepowered.asm.mixin.injection.Constant;
31+
import org.spongepowered.asm.mixin.injection.ModifyArg;
3132
import org.spongepowered.asm.mixin.injection.ModifyConstant;
3233
import org.spongepowered.asm.mixin.injection.Slice;
33-
import org.spongepowered.common.util.DamageEventUtil;
34+
import org.spongepowered.forge.mixin.core.world.entity.LivingEntityMixin_Forge_Damage;
3435

3536
@Mixin(Player.class)
36-
public abstract class PlayerMixin_Vanilla_Attack_Impl {
37-
private DamageEventUtil.Attack<Player> attackImpl$attack;
37+
public abstract class PlayerMixin_Forge_Damage extends LivingEntityMixin_Forge_Damage {
3838

39-
/**
40-
* Captures the crit multiplier as a function
41-
*/
42-
@ModifyConstant(method = "attack", constant = @Constant(floatValue = 1.5F),
43-
slice = @Slice(from = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;isSprinting()Z", ordinal = 1),
44-
to = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;getKnownMovement()Lnet/minecraft/world/phys/Vec3;"))
45-
)
46-
public float attackImpl$critHook(final float constant) {
47-
// if (isCritical) damage *= 1.5F;
48-
final var bonusDamageFunc = DamageEventUtil.provideCriticalAttackFunction(this.attackImpl$attack.sourceEntity(), constant);
49-
this.attackImpl$attack.functions().add(bonusDamageFunc);
50-
return constant;
39+
@ModifyConstant(method = "actuallyHurt", constant = @Constant(floatValue = 0), slice = @Slice(
40+
from = @At(value = "INVOKE", target = "Lnet/minecraftforge/common/ForgeHooks;onLivingHurt(Lnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/world/damagesource/DamageSource;F)F"),
41+
to = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;getDamageAfterArmorAbsorb(Lnet/minecraft/world/damagesource/DamageSource;F)F")))
42+
private float damage$doNotReturnEarly(final float constant) {
43+
return Float.NaN;
44+
}
45+
46+
@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"))
47+
private float damage$firePostEvent_Player(final float damage) {
48+
return this.damage$firePostEvent(damage);
5149
}
5250
}

forge/src/mixins/resources/mixins.spongeforge.core.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
"server.network.ServerGamePacketListenerImplMixin_Forge",
3030
"tags.TagLoaderMixin_Forge",
3131
"world.entity.LivingEntityMixin_Forge",
32-
"world.entity.LivingEntityMixin_Forge_Attack_Impl",
32+
"world.entity.LivingEntityMixin_Forge_Damage",
3333
"world.entity.item.ItemEntityMixin_Forge",
34-
"world.entity.player.PlayerMixin_Forge_Attack_Impl",
34+
"world.entity.player.PlayerMixin_Forge_Damage",
3535
"world.entity.vehicle.BoatMixin_Forge",
3636
"world.level.ServerExplosionMixin_Forge",
3737
"world.level.block.FireBlockMixin_Forge",

forge/src/mixins/resources/mixins.spongeforge.core.shared.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"priority": 1301,
66
"mixins": [
77
"server.level.ServerEntityMixin_Shared",
8-
"world.entity.LivingEntityMixin_Shared_Attack_Impl",
9-
"world.entity.player.PlayerMixin_Shared_Attack_Impl",
8+
"world.entity.LivingEntityMixin_Shared_Damage",
9+
"world.entity.player.PlayerMixin_Shared_Damage",
1010
"world.entity.projectile.FishingHookMixin_Shared"
1111
],
1212
"overwrites": {

neoforge/src/mixins/java/org/spongepowered/neoforge/mixin/core/world/entity/LivingEntityMixin_Neo_Attack_Impl.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)