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

Commit 13028d2

Browse files
authored
Merge pull request #51 from PatchworkMC/feature/entity-events
3 new entity events
2 parents fa1b318 + c654efb commit 13028d2

5 files changed

Lines changed: 128 additions & 7 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package com.patchworkmc.impl.event.entity;
2121

2222
import net.minecraftforge.common.MinecraftForge;
23+
import net.minecraftforge.event.entity.EntityEvent;
2324
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
2425
import net.minecraftforge.event.entity.living.LivingAttackEvent;
2526
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent;
@@ -38,6 +39,8 @@
3839
import org.apache.logging.log4j.LogManager;
3940

4041
import net.minecraft.entity.Entity;
42+
import net.minecraft.entity.EntityDimensions;
43+
import net.minecraft.entity.EntityPose;
4144
import net.minecraft.entity.LivingEntity;
4245
import net.minecraft.entity.SpawnType;
4346
import net.minecraft.entity.damage.DamageSource;
@@ -77,6 +80,14 @@ public static boolean onEntityJoinWorld(Entity entity, World world) {
7780
return MinecraftForge.EVENT_BUS.post(new EntityJoinWorldEvent(entity, world));
7881
}
7982

83+
public static void onEntityConstruct(Entity entity) {
84+
MinecraftForge.EVENT_BUS.post(new EntityEvent.EntityConstructing(entity));
85+
}
86+
87+
public static void onEnteringChunk(Entity entity, int newChunkX, int newChunkZ, int oldChunkX, int oldChunkZ) {
88+
MinecraftForge.EVENT_BUS.post(new EntityEvent.EnteringChunk(entity, newChunkX, newChunkZ, oldChunkX, oldChunkZ));
89+
}
90+
8091
// PlayerEvents
8192
public static void onPlayerLoggedIn(ServerPlayerEntity playerEntity) {
8293
MinecraftForge.EVENT_BUS.post(new PlayerEvent.PlayerLoggedInEvent(playerEntity));
@@ -109,6 +120,12 @@ public static float onLivingDamage(LivingEntity entity, DamageSource src, float
109120
return MinecraftForge.EVENT_BUS.post(event) ? 0 : event.getAmount();
110121
}
111122

123+
public static float getEyeHeight(Entity entity, EntityPose pose, EntityDimensions size, float defaultHeight) {
124+
EntityEvent.EyeHeight event = new EntityEvent.EyeHeight(entity, pose, size, defaultHeight);
125+
MinecraftForge.EVENT_BUS.post(event);
126+
return event.getNewHeight();
127+
}
128+
112129
public static Result canEntitySpawn(MobEntity entity, IWorld world, double x, double y, double z, MobSpawnerLogic spawner, SpawnType spawnType) {
113130
if (entity == null) {
114131
return Result.DEFAULT;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.Shadow;
24+
import org.spongepowered.asm.mixin.injection.At;
25+
import org.spongepowered.asm.mixin.injection.Inject;
26+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
27+
28+
import net.minecraft.entity.Entity;
29+
import net.minecraft.entity.EntityDimensions;
30+
import net.minecraft.entity.EntityPose;
31+
import net.minecraft.entity.EntityType;
32+
import net.minecraft.world.World;
33+
34+
import com.patchworkmc.impl.event.entity.EntityEvents;
35+
36+
@Mixin(Entity.class)
37+
public abstract class MixinEntity {
38+
@Shadow
39+
private float standingEyeHeight;
40+
41+
@Shadow
42+
private EntityDimensions dimensions;
43+
44+
@Shadow
45+
protected abstract float getEyeHeight(EntityPose pose, EntityDimensions dimensions);
46+
47+
@Inject(method = "<init>", at = @At("RETURN"))
48+
public void hookConstructor(EntityType<?> type, World world, CallbackInfo ci) {
49+
Entity entity = (Entity) (Object) this;
50+
51+
this.standingEyeHeight = EntityEvents.getEyeHeight(entity, EntityPose.STANDING, dimensions, getEyeHeight(EntityPose.STANDING, dimensions));
52+
53+
EntityEvents.onEntityConstruct(entity);
54+
}
55+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.Final;
23+
import org.spongepowered.asm.mixin.Mixin;
24+
import org.spongepowered.asm.mixin.Shadow;
25+
import org.spongepowered.asm.mixin.injection.At;
26+
import org.spongepowered.asm.mixin.injection.Inject;
27+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
28+
29+
import net.minecraft.entity.Entity;
30+
import net.minecraft.util.math.ChunkPos;
31+
import net.minecraft.world.chunk.WorldChunk;
32+
33+
import com.patchworkmc.impl.event.entity.EntityEvents;
34+
35+
@Mixin(WorldChunk.class)
36+
public class MixinWorldChunk {
37+
@Shadow
38+
@Final
39+
private ChunkPos pos;
40+
41+
@Inject(method = "addEntity", at = @At(target = "Lnet/minecraft/util/math/MathHelper;floor(D)I", value = "INVOKE", ordinal = 2))
42+
public void hookAddEntity(Entity entity, CallbackInfo ci) {
43+
EntityEvents.onEnteringChunk(entity, this.pos.x, this.pos.z, entity.chunkX, entity.chunkZ);
44+
}
45+
}

patchwork-events-entity/src/main/java/net/minecraftforge/event/entity/EntityEvent.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import net.minecraftforge.eventbus.api.Event;
2323

2424
import net.minecraft.entity.Entity;
25+
import net.minecraft.entity.EntityDimensions;
26+
import net.minecraft.entity.EntityPose;
2527

2628
/**
2729
* EntityEvent is fired when an event involving any Entity occurs.
@@ -60,11 +62,11 @@ public Entity getEntity() {
6062
*
6163
* <p>This event is fired on the {@link net.minecraftforge.common.MinecraftForge#EVENT_BUS}.</p>
6264
*/
63-
/* TODO public static class EntityConstructing extends EntityEvent {
65+
public static class EntityConstructing extends EntityEvent {
6466
public EntityConstructing(Entity entity) {
6567
super(entity);
6668
}
67-
}*/
69+
}
6870

6971
/**
7072
* <b>TODO: Forge never fires this event.</b>
@@ -106,7 +108,7 @@ public void setCanUpdate(boolean canUpdate) {
106108
*
107109
* <p>This event is fired on the {@link net.minecraftforge.common.MinecraftForge#EVENT_BUS}.</p>
108110
*/
109-
/* TODO public static class EnteringChunk extends EntityEvent {
111+
public static class EnteringChunk extends EntityEvent {
110112
private int newChunkX;
111113
private int newChunkZ;
112114
private int oldChunkX;
@@ -151,7 +153,7 @@ public int getOldChunkZ() {
151153
public void setOldChunkZ(int oldChunkZ) {
152154
this.oldChunkZ = oldChunkZ;
153155
}
154-
}*/
156+
}
155157

156158
/**
157159
* EyeHeight is fired when an Entity's eye height changes.
@@ -164,7 +166,7 @@ public void setOldChunkZ(int oldChunkZ) {
164166
*
165167
* <p>This event is fired on the {@link net.minecraftforge.common.MinecraftForge#EVENT_BUS}.</p>
166168
*/
167-
/* TODO public static class EyeHeight extends EntityEvent {
169+
public static class EyeHeight extends EntityEvent {
168170
private final EntityPose pose;
169171
private final EntityDimensions size;
170172
private final float oldHeight;
@@ -197,5 +199,5 @@ public float getNewHeight() {
197199
public void setNewHeight(float newSize) {
198200
this.newHeight = newSize;
199201
}
200-
}*/
202+
}
201203
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"package": "com.patchworkmc.mixin.event.entity",
44
"compatibilityLevel": "JAVA_8",
55
"mixins": [
6+
"MixinEntity",
67
"MixinEntityType",
78
"MixinLivingEntity",
89
"MixinMobEntity",
@@ -11,7 +12,8 @@
1112
"MixinPlayerManager",
1213
"MixinServerPlayerEntity",
1314
"MixinServerWorld",
14-
"MixinSpawnHelper"
15+
"MixinSpawnHelper",
16+
"MixinWorldChunk"
1517
],
1618
"client": [
1719
"MixinClientWorld",

0 commit comments

Comments
 (0)