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

Commit 67211e2

Browse files
NuclearfartsNuclearfarts
authored andcommitted
implement LivingAttackEvent
1 parent dd1d01b commit 67211e2

8 files changed

Lines changed: 240 additions & 5 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import net.minecraftforge.common.MinecraftForge;
2323
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
24+
import net.minecraftforge.event.entity.living.LivingAttackEvent;
2425
import net.minecraftforge.event.entity.living.LivingDeathEvent;
2526
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
2627
import net.minecraftforge.eventbus.api.Event;
@@ -58,6 +59,10 @@ public static boolean onEntityJoinWorld(Entity entity, World world) {
5859
return MinecraftForge.EVENT_BUS.post(new EntityJoinWorldEvent(entity, world));
5960
}
6061

62+
public static boolean onLivingAttack(LivingEntity entity, DamageSource src, float damage) {
63+
return MinecraftForge.EVENT_BUS.post(new LivingAttackEvent(entity, src, damage));
64+
}
65+
6166
@Override
6267
public void onInitialize() {
6368
UseItemCallback.EVENT.register((player, world, hand) -> {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2019, 2019
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 com.patchworkmc.mixin.event.entity;
21+
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import org.spongepowered.asm.mixin.injection.At;
24+
import org.spongepowered.asm.mixin.injection.Inject;
25+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
26+
27+
import net.minecraft.client.network.ClientPlayerEntity;
28+
import net.minecraft.entity.LivingEntity;
29+
import net.minecraft.entity.damage.DamageSource;
30+
31+
import com.patchworkmc.impl.event.entity.EntityEvents;
32+
33+
@Mixin(ClientPlayerEntity.class)
34+
public class MixinClientPlayerEntity {
35+
@Inject(method = "damage", at = @At("HEAD"), cancellable = true)
36+
private void hookDamage(DamageSource source, float amount, CallbackInfoReturnable<Boolean> callback) {
37+
LivingEntity entity = (LivingEntity) (Object) this;
38+
39+
if (EntityEvents.onLivingAttack(entity, source, amount)) {
40+
callback.setReturnValue(false);
41+
}
42+
}
43+
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@
2323
import org.spongepowered.asm.mixin.injection.At;
2424
import org.spongepowered.asm.mixin.injection.Inject;
2525
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
26+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
2627

2728
import net.minecraft.entity.LivingEntity;
2829
import net.minecraft.entity.damage.DamageSource;
29-
import net.minecraft.entity.player.PlayerEntity;
30-
import net.minecraft.server.network.ServerPlayerEntity;
3130

3231
import com.patchworkmc.impl.event.entity.EntityEvents;
3332

34-
// TODO: Forge bug: PlayerEntity calls its super, so this event gets fired twice on the client.
35-
@Mixin({LivingEntity.class, PlayerEntity.class, ServerPlayerEntity.class})
33+
@Mixin(LivingEntity.class)
3634
public class MixinLivingEntity {
35+
// TODO: Forge bug: PlayerEntity calls its super, so this event gets fired twice on the client.
3736
@Inject(method = "onDeath", at = @At("HEAD"), cancellable = true)
3837
private void hookDeath(DamageSource source, CallbackInfo callback) {
3938
LivingEntity entity = (LivingEntity) (Object) this;
@@ -42,4 +41,13 @@ private void hookDeath(DamageSource source, CallbackInfo callback) {
4241
callback.cancel();
4342
}
4443
}
44+
45+
@Inject(method = "damage", at = @At("HEAD"), cancellable = true)
46+
private void hookDamage(DamageSource source, float amount, CallbackInfoReturnable<Boolean> callback) {
47+
LivingEntity entity = (LivingEntity) (Object) this;
48+
49+
if (EntityEvents.onLivingAttack(entity, source, amount)) {
50+
callback.setReturnValue(false);
51+
}
52+
}
4553
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2019, 2019
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 com.patchworkmc.mixin.event.entity;
21+
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import org.spongepowered.asm.mixin.injection.At;
24+
import org.spongepowered.asm.mixin.injection.Inject;
25+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
26+
27+
import net.minecraft.client.network.OtherClientPlayerEntity;
28+
import net.minecraft.entity.LivingEntity;
29+
import net.minecraft.entity.damage.DamageSource;
30+
31+
import com.patchworkmc.impl.event.entity.EntityEvents;
32+
33+
@Mixin(OtherClientPlayerEntity.class)
34+
public class MixinOtherClientPlayerEntity {
35+
@Inject(method = "damage", at = @At("HEAD"), cancellable = true)
36+
private void hookDamage(DamageSource source, float amount, CallbackInfoReturnable<Boolean> callback) {
37+
LivingEntity entity = (LivingEntity) (Object) this;
38+
39+
if (EntityEvents.onLivingAttack(entity, source, amount)) {
40+
callback.setReturnValue(false);
41+
}
42+
}
43+
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
import org.spongepowered.asm.mixin.Mixin;
2323
import org.spongepowered.asm.mixin.injection.At;
2424
import org.spongepowered.asm.mixin.injection.Inject;
25+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
2526
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
2627

2728
import net.minecraft.entity.Entity;
29+
import net.minecraft.entity.LivingEntity;
30+
import net.minecraft.entity.damage.DamageSource;
2831
import net.minecraft.entity.player.PlayerEntity;
2932
import net.minecraft.util.ActionResult;
3033
import net.minecraft.util.Hand;
@@ -60,4 +63,22 @@ private void hookInteractEntity(Entity entity, Hand hand, CallbackInfoReturnable
6063
}
6164
}
6265
}
66+
67+
@Inject(method = "onDeath", at = @At("HEAD"), cancellable = true)
68+
private void hookDeath(DamageSource source, CallbackInfo callback) {
69+
LivingEntity entity = (LivingEntity) (Object) this;
70+
71+
if (EntityEvents.onLivingDeath(entity, source)) {
72+
callback.cancel();
73+
}
74+
}
75+
76+
@Inject(method = "damage", at = @At("HEAD"), cancellable = true)
77+
private void hookDamage(DamageSource source, float amount, CallbackInfoReturnable<Boolean> callback) {
78+
LivingEntity entity = (LivingEntity) (Object) this;
79+
80+
if (EntityEvents.onLivingAttack(entity, source, amount)) {
81+
callback.setReturnValue(false);
82+
}
83+
}
6384
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2019, 2019
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 com.patchworkmc.mixin.event.entity;
21+
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import org.spongepowered.asm.mixin.injection.At;
24+
import org.spongepowered.asm.mixin.injection.Inject;
25+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
26+
27+
import net.minecraft.entity.LivingEntity;
28+
import net.minecraft.entity.damage.DamageSource;
29+
import net.minecraft.server.network.ServerPlayerEntity;
30+
31+
import com.patchworkmc.impl.event.entity.EntityEvents;
32+
33+
@Mixin(ServerPlayerEntity.class)
34+
public class MixinServerPlayerEntity {
35+
@Inject(method = "onDeath", at = @At("HEAD"), cancellable = true)
36+
private void hookDeath(DamageSource source, CallbackInfo callback) {
37+
LivingEntity entity = (LivingEntity) (Object) this;
38+
39+
if (EntityEvents.onLivingDeath(entity, source)) {
40+
callback.cancel();
41+
}
42+
}
43+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2019, 2019
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.LivingEntity;
23+
import net.minecraft.entity.damage.DamageSource;
24+
25+
/**
26+
* LivingAttackEvent is fired when a living Entity is attacked.
27+
*
28+
* <p>This event is fired whenever a LivingEntity is attacked in
29+
* {@link LivingEntity#damage(DamageSource, float)} and
30+
* {@link net.minecraft.entity.player.PlayerEntity#damage(DamageSource, float)}.</p>
31+
*
32+
* <p>This event is fired via the {@link com.patchworkmc.impl.event.entity.EntityEvents#onLivingAttack(EntityLivingBase, DamageSource, float)}.</p>
33+
*
34+
* <p>{@link #source} contains the DamageSource of the attack.
35+
* {@link #amount} contains the amount of damage dealt to the entity.</p>
36+
*
37+
* <p>This event is cancellable.
38+
* If this event is canceled, the {@link LivingEntity} does not take attack damage.</p>
39+
*
40+
* <p>This event is fired on the {@link net.minecraftforge.common.MinecraftForge#EVENT_BUS}.</p>
41+
*/
42+
public class LivingAttackEvent extends LivingEvent {
43+
private final DamageSource source;
44+
private final float damage;
45+
46+
public LivingAttackEvent() {
47+
this.source = null;
48+
this.damage = 0f;
49+
}
50+
51+
public LivingAttackEvent(LivingEntity entity, DamageSource source, float damage) {
52+
super(entity);
53+
this.source = source;
54+
this.damage = damage;
55+
}
56+
57+
public DamageSource getSource() {
58+
return source;
59+
}
60+
61+
public float getAmount() {
62+
return damage;
63+
}
64+
65+
@Override
66+
public boolean isCancelable() {
67+
return true;
68+
}
69+
}

patchwork-events-entity/src/main/resources/patchwork-events-entity.mixins.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
"mixins": [
66
"MixinLivingEntity",
77
"MixinPlayerEntity",
8+
"MixinServerPlayerEntity",
89
"MixinServerWorld"
910
],
1011
"client": [
11-
"MixinClientWorld"
12+
"MixinClientWorld",
13+
"MixinClientPlayerEntity",
14+
"MixinOtherClientPlayerEntity"
1215
],
1316
"injectors": {
1417
"defaultRequire": 1

0 commit comments

Comments
 (0)