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

Commit d443816

Browse files
committed
Implement LivingSetAttackTargetEvent.
1 parent 7873944 commit d443816

4 files changed

Lines changed: 103 additions & 0 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
@@ -22,6 +22,7 @@
2222
import net.minecraftforge.common.MinecraftForge;
2323
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
2424
import net.minecraftforge.event.entity.living.LivingAttackEvent;
25+
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent;
2526
import net.minecraftforge.event.entity.living.LivingDeathEvent;
2627
import net.minecraftforge.event.entity.living.LivingHurtEvent;
2728
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
@@ -82,6 +83,10 @@ public static boolean onLivingAttack(LivingEntity entity, DamageSource src, floa
8283
return MinecraftForge.EVENT_BUS.post(new LivingAttackEvent(entity, src, damage));
8384
}
8485

86+
public static void onLivingSetAttackTarget(LivingEntity entity, LivingEntity target) {
87+
MinecraftForge.EVENT_BUS.post(new LivingSetAttackTargetEvent(entity, target));
88+
}
89+
8590
public static float onLivingHurt(LivingEntity entity, DamageSource src, float damage) {
8691
LivingHurtEvent event = new LivingHurtEvent(entity, src, damage);
8792
return MinecraftForge.EVENT_BUS.post(event) ? 0 : event.getAmount();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 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.mob.MobEntity;
29+
30+
import com.patchworkmc.impl.event.entity.EntityEvents;
31+
32+
@Mixin(MobEntity.class)
33+
public class MixinMobEntity {
34+
@Inject(method = "setTarget", at = @At("TAIL"))
35+
private void hookSetTarget(LivingEntity target, CallbackInfo callback) {
36+
LivingEntity entity = (LivingEntity) (Object) this;
37+
EntityEvents.onLivingSetAttackTarget(entity, target);
38+
}
39+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.LivingEntity;
23+
import net.minecraft.entity.mob.MobEntity;
24+
25+
/**
26+
* LivingSetAttackTargetEvent is fired when an entity sets a target to attack.
27+
*
28+
* <p>This event is fired whenever a {@link MobEntity} sets a target to attack in
29+
* {@link MobEntity#setTarget(LivingEntity)}.</p>
30+
*
31+
* <p>This event is fired via {@link com.patchworkmc.impl.event.entity.EntityEvents#onLivingSetAttackTarget(LivingEntity, LivingEntity)}.</p>
32+
*
33+
* <p>{@link #target} contains the newly targeted Entity.</p>
34+
*
35+
* <p>This event is not cancellable.</p>
36+
*
37+
* <p>This event does not have a result.</p>
38+
*
39+
* <p>This event is fired on the {@link net.minecraftforge.common.MinecraftForge#EVENT_BUS}.</p>
40+
*/
41+
public class LivingSetAttackTargetEvent extends LivingEvent {
42+
private final LivingEntity target;
43+
44+
// for EventBus
45+
public LivingSetAttackTargetEvent() {
46+
super();
47+
this.target = null;
48+
}
49+
50+
public LivingSetAttackTargetEvent(LivingEntity entity, LivingEntity target) {
51+
super(entity);
52+
this.target = target;
53+
}
54+
55+
public LivingEntity getTarget() {
56+
return target;
57+
}
58+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"mixins": [
66
"MixinEntityType",
77
"MixinLivingEntity",
8+
"MixinMobEntity",
89
"MixinMobSpawnerLogic",
910
"MixinPlayerEntity",
1011
"MixinPlayerManager",

0 commit comments

Comments
 (0)