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

Commit aca009f

Browse files
authored
Merge pull request #34 from kitlith/apathetic_mobs
Implement Events used by Apathetic Mobs: LivingDamageEvent, LivingSetTargetEvent, and DifficultyChangedEvent.
2 parents 7873944 + 56df3fe commit aca009f

8 files changed

Lines changed: 246 additions & 0 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
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;
28+
import net.minecraftforge.event.entity.living.LivingDamageEvent;
2729
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
2830
import net.minecraftforge.event.entity.living.LivingEvent;
2931
import net.minecraftforge.event.entity.player.PlayerEvent;
@@ -82,11 +84,20 @@ public static boolean onLivingAttack(LivingEntity entity, DamageSource src, floa
8284
return MinecraftForge.EVENT_BUS.post(new LivingAttackEvent(entity, src, damage));
8385
}
8486

87+
public static void onLivingSetAttackTarget(LivingEntity entity, LivingEntity target) {
88+
MinecraftForge.EVENT_BUS.post(new LivingSetAttackTargetEvent(entity, target));
89+
}
90+
8591
public static float onLivingHurt(LivingEntity entity, DamageSource src, float damage) {
8692
LivingHurtEvent event = new LivingHurtEvent(entity, src, damage);
8793
return MinecraftForge.EVENT_BUS.post(event) ? 0 : event.getAmount();
8894
}
8995

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+
90101
public static Result canEntitySpawn(MobEntity entity, IWorld world, double x, double y, double z, MobSpawnerLogic spawner, SpawnType spawnType) {
91102
if (entity == null) {
92103
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
}
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+
}

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+
}
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",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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;
21+
22+
import net.minecraftforge.common.MinecraftForge;
23+
import net.minecraftforge.eventbus.api.Event;
24+
25+
import net.minecraft.world.Difficulty;
26+
27+
/**
28+
* DifficultyChangeEvent is fired when difficulty is changing.
29+
*
30+
* <p>TODO: Forge bug: This event is not currently fired. See https://github.com/MinecraftForge/MinecraftForge/issues/6227</p>
31+
*
32+
* <p>This event is not cancellable.</p>
33+
*
34+
* <p>This event does not have a result.</p>
35+
*
36+
* <p>This event is fired on the {@link MinecraftForge#EVENT_BUS}.</p>
37+
*/
38+
public class DifficultyChangeEvent extends Event {
39+
private final Difficulty difficulty;
40+
private final Difficulty oldDifficulty;
41+
42+
public DifficultyChangeEvent(Difficulty difficulty, Difficulty oldDifficulty) {
43+
this.difficulty = difficulty;
44+
this.oldDifficulty = oldDifficulty;
45+
}
46+
47+
public Difficulty getDifficulty() {
48+
return difficulty;
49+
}
50+
51+
public Difficulty getOldDifficulty() {
52+
return oldDifficulty;
53+
}
54+
}

0 commit comments

Comments
 (0)