Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 4a52010

Browse files
committed
Implement LivingDamageEvent.
1 parent d443816 commit 4a52010

4 files changed

Lines changed: 89 additions & 0 deletions

File tree

patchwork-events-entity/src/main/java/com/patchworkmc/impl/event/entity/EntityEvents.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent;
2626
import net.minecraftforge.event.entity.living.LivingDeathEvent;
2727
import net.minecraftforge.event.entity.living.LivingHurtEvent;
28+
import net.minecraftforge.event.entity.living.LivingDamageEvent;
2829
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
2930
import net.minecraftforge.event.entity.living.LivingEvent;
3031
import net.minecraftforge.event.entity.player.PlayerEvent;
@@ -92,6 +93,11 @@ public static float onLivingHurt(LivingEntity entity, DamageSource src, float da
9293
return MinecraftForge.EVENT_BUS.post(event) ? 0 : event.getAmount();
9394
}
9495

96+
public static float onLivingDamage(LivingEntity entity, DamageSource src, float damage) {
97+
LivingDamageEvent event = new LivingDamageEvent(entity, src, damage);
98+
return MinecraftForge.EVENT_BUS.post(event) ? 0 : event.getAmount();
99+
}
100+
95101
public static Result canEntitySpawn(MobEntity entity, IWorld world, double x, double y, double z, MobSpawnerLogic spawner, SpawnType spawnType) {
96102
if (entity == null) {
97103
return Result.DEFAULT;

patchwork-events-entity/src/main/java/com/patchworkmc/mixin/event/entity/MixinLivingEntity.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,13 @@ private void hookApplyDamageForHurtEventCancel(DamageSource source, float damage
7575
info.cancel();
7676
}
7777
}
78+
79+
// No shift, because we are specifically not modifying the value for this function call.
80+
// TODO: Forge patches a bit later into the function here, being inconsistent with their patch for PlayerEntity. For the moment, I don't feel like finding an injection point for that, and this may be a Forge bug?
81+
@ModifyVariable(method = "applyDamage", argsOnly = true, at = @At(value = "INVOKE", target = "net/minecraft/entity/LivingEntity.setAbsorptionAmount (F)V", ordinal = 0))
82+
private float hookApplyDamageForDamageEvent(float damage, DamageSource source) {
83+
LivingEntity entity = (LivingEntity) (Object) this;
84+
85+
return EntityEvents.onLivingDamage(entity, source, damage);
86+
}
7887
}

patchwork-events-entity/src/main/java/com/patchworkmc/mixin/event/entity/MixinPlayerEntity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,12 @@ private void hookApplyDamageForHurtEventCancel(DamageSource source, float damage
9898
info.cancel();
9999
}
100100
}
101+
102+
// No shift, because we are specifically not modifying the value for this function call.
103+
@ModifyVariable(method = "applyDamage", argsOnly = true, at = @At(value = "INVOKE", target = "net/minecraft/entity/player/PlayerEntity.setAbsorptionAmount (F)V", ordinal = 0))
104+
private float hookApplyDamageForDamageEvent(float damage, DamageSource source) {
105+
LivingEntity entity = (LivingEntity) (Object) this;
106+
107+
return EntityEvents.onLivingDamage(entity, source, damage);
108+
}
101109
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.event.entity.living;
21+
22+
import net.minecraft.entity.damage.DamageSource;
23+
import net.minecraft.entity.LivingEntity;
24+
25+
/**
26+
* LivingDamageEvent is fired just before damage is applied to entity.
27+
*
28+
* <p>At this point armor, potion and absorption modifiers have already been applied to damage - this is FINAL value.</p>
29+
* <p>Also note that appropriate resources (like armor durability and absorption extra hearths) have already been consumed.</p>
30+
* <p>This event is fired whenever an Entity is damaged in
31+
* {@link LivingEntity#applyDamage(DamageSource, float)} and
32+
* {@link net.minecraft.entity.player.PlayerEntity#applyDamage(DamageSource, float)}.</p>
33+
*
34+
* <p>This event is fired via the {@link com.patchworkmc.impl.event.entity.EntityEvents#onLivingDamage(LivingEntity, DamageSource, float)}.</p>
35+
*
36+
* <p>{@link #source} contains the DamageSource that caused this Entity to be hurt. </p>
37+
* <p>{@link #amount} contains the final amount of damage that will be dealt to entity. </p>
38+
*
39+
* <p>This event is cancelable.</p>
40+
* <p>If this event is canceled, the Entity is not hurt. Used resources WILL NOT be restored.</p>
41+
*
42+
* <p>This event does not have a result.</p>
43+
* @see LivingHurtEvent
44+
*/
45+
public class LivingDamageEvent extends LivingEvent {
46+
private final DamageSource source;
47+
private float amount;
48+
49+
public LivingDamageEvent(LivingEntity entity, DamageSource source, float amount) {
50+
super(entity);
51+
this.source = source;
52+
this.amount = amount;
53+
}
54+
55+
public DamageSource getSource() {
56+
return source;
57+
}
58+
59+
public float getAmount() {
60+
return amount;
61+
}
62+
63+
public void setAmount(float amount) {
64+
this.amount = amount;
65+
}
66+
}

0 commit comments

Comments
 (0)