Skip to content

Commit 9134e2d

Browse files
committed
Add EntityEvent and enhance behavior handling
Introduced a new `EntityEvent` class to unify and encapsulate entity-specific events. Adjusted various modules, such as `AutoDisconnect` and `BreakBlock`, to leverage the new event structure for improved behavior handling and clarity. Enhanced damage detection and refined tracking for item entities during block-breaking operations.
1 parent 2c48a93 commit 9134e2d

File tree

19 files changed

+285
-66
lines changed

19 files changed

+285
-66
lines changed

common/src/main/java/com/lambda/mixin/client/sound/SoundSystemMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
@Mixin(SoundSystem.class)
3030
public class SoundSystemMixin {
31-
@Inject(method = "play(Lnet/minecraft/client/sound/SoundInstance;)V", at = @At(value = "HEAD"), cancellable = true)
31+
@Inject(method = "play(Lnet/minecraft/client/sound/SoundInstance;)V", at = @At("HEAD"), cancellable = true)
3232
public void onPlay(SoundInstance sound, CallbackInfo ci) {
3333
if (EventFlow.post(new ClientEvent.Sound(sound)).isCanceled()) {
3434
ci.cancel();

common/src/main/java/com/lambda/mixin/entity/ClientPlayerEntityMixin.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.lambda.Lambda;
2121
import com.lambda.event.EventFlow;
22+
import com.lambda.event.events.EntityEvent;
2223
import com.lambda.event.events.PlayerEvent;
2324
import com.lambda.event.events.MovementEvent;
2425
import com.lambda.event.events.TickEvent;
@@ -27,6 +28,7 @@
2728
import net.minecraft.client.input.Input;
2829
import net.minecraft.client.network.ClientPlayerEntity;
2930
import net.minecraft.entity.MovementType;
31+
import net.minecraft.entity.damage.DamageSource;
3032
import net.minecraft.util.Hand;
3133
import net.minecraft.util.math.Vec3d;
3234
import org.spongepowered.asm.mixin.Mixin;
@@ -124,4 +126,11 @@ float fixHeldItemPitch(ClientPlayerEntity instance) {
124126
void onSwingHandPre(Hand hand, CallbackInfo ci) {
125127
if (EventFlow.post(new PlayerEvent.SwingHand(hand)).isCanceled()) ci.cancel();
126128
}
129+
130+
@Inject(method = "damage", at = @At("HEAD"), cancellable = true)
131+
public void damage(DamageSource source, float amount, CallbackInfoReturnable<Boolean> cir) {
132+
if (EventFlow.post(new PlayerEvent.Damage(source, amount)).isCanceled()) {
133+
cir.setReturnValue(false);
134+
}
135+
}
127136
}

common/src/main/java/com/lambda/mixin/entity/EntityMixin.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919

2020
import com.lambda.Lambda;
2121
import com.lambda.event.EventFlow;
22+
import com.lambda.event.events.EntityEvent;
2223
import com.lambda.event.events.PlayerEvent;
2324
import com.lambda.event.events.WorldEvent;
2425
import com.lambda.interaction.RotationManager;
2526
import com.lambda.util.math.Vec2d;
2627
import net.minecraft.entity.Entity;
2728
import net.minecraft.entity.MovementType;
29+
import net.minecraft.entity.damage.DamageSource;
2830
import net.minecraft.entity.data.TrackedData;
2931
import net.minecraft.util.math.Vec3d;
3032
import org.spongepowered.asm.mixin.Mixin;
@@ -33,6 +35,7 @@
3335
import org.spongepowered.asm.mixin.injection.Inject;
3436
import org.spongepowered.asm.mixin.injection.Redirect;
3537
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
38+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
3639

3740
@Mixin(Entity.class)
3841
public abstract class EntityMixin {
@@ -94,6 +97,16 @@ private void changeLookDirection(double cursorDeltaX, double cursorDeltaY, Callb
9497
public void onTrackedDataSet(TrackedData<?> data, CallbackInfo ci) {
9598
Entity entity = (Entity) (Object) this;
9699

97-
EventFlow.post(new WorldEvent.EntityUpdate(entity, data));
100+
EventFlow.post(new EntityEvent.EntityUpdate(entity, data));
101+
}
102+
103+
// ToDo: Does not trigger for some reason.
104+
@Inject(method = "damage", at = @At("HEAD"), cancellable = true)
105+
public void damage(DamageSource source, float amount, CallbackInfoReturnable<Boolean> cir) {
106+
Entity entity = (Entity) (Object) this;
107+
108+
if (EventFlow.post(new EntityEvent.Damage(entity, source, amount)).isCanceled()) {
109+
cir.setReturnValue(false);
110+
}
98111
}
99112
}

common/src/main/java/com/lambda/mixin/items/BarrierBlockMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
@Mixin(BarrierBlock.class)
3131
public class BarrierBlockMixin {
3232

33-
@Inject(method = "getRenderType", at = @At(value = "RETURN"), cancellable = true)
33+
@Inject(method = "getRenderType", at = @At("RETURN"), cancellable = true)
3434
private void getRenderType(BlockState state, CallbackInfoReturnable<BlockRenderType> cir) {
3535
if (BlockESP.INSTANCE.isEnabled()
3636
&& BlockESP.getBarrier()

common/src/main/java/com/lambda/mixin/network/ClientPlayNetworkHandlerMixin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929

3030
@Mixin(ClientPlayNetworkHandler.class)
3131
public class ClientPlayNetworkHandlerMixin {
32-
@Inject(method = "onUpdateSelectedSlot", at = @At(value = "TAIL"))
32+
@Inject(method = "onUpdateSelectedSlot", at = @At("TAIL"))
3333
private void onUpdateSelectedSlot(UpdateSelectedSlotS2CPacket packet, CallbackInfo ci) {
3434
EventFlow.post(new InventoryEvent.SelectedHotbarSlotUpdate(packet.getSlot()));
3535
}
3636

37-
@Inject(method = "onScreenHandlerSlotUpdate", at = @At(value = "TAIL"))
37+
@Inject(method = "onScreenHandlerSlotUpdate", at = @At("TAIL"))
3838
private void onScreenHandlerSlotUpdate(ScreenHandlerSlotUpdateS2CPacket packet, CallbackInfo ci) {
3939
EventFlow.post(new InventoryEvent.SlotUpdate(packet.getSyncId(), packet.getRevision(), packet.getSlot(), packet.getStack()));
4040
}

common/src/main/java/com/lambda/mixin/render/DebugHudMixin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929

3030
@Mixin(DebugHud.class)
3131
public class DebugHudMixin {
32-
@Inject(method = "getRightText", at = @At(value = "TAIL"))
32+
@Inject(method = "getRightText", at = @At("TAIL"))
3333
private void onGetRightText(CallbackInfoReturnable<List<String>> cir) {
3434
DebugInfoHud.addDebugInfo(cir.getReturnValue());
3535
}
3636

37-
@Inject(method = "getLeftText", at = @At(value = "TAIL"))
37+
@Inject(method = "getLeftText", at = @At("TAIL"))
3838
private void onGetLeftText(CallbackInfoReturnable<List<String>> cir) {
3939
cir.getReturnValue().addAll(List.of(TaskFlow.INSTANCE.toString().split("\n")));
4040
}

common/src/main/java/com/lambda/mixin/world/ClientWorldMixin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.lambda.mixin.world;
1919

2020
import com.lambda.event.EventFlow;
21+
import com.lambda.event.events.EntityEvent;
2122
import com.lambda.event.events.WorldEvent;
2223
import com.lambda.module.modules.render.WorldColors;
2324
import com.lambda.util.math.ColorKt;
@@ -36,7 +37,7 @@
3637
public class ClientWorldMixin {
3738
@Inject(method = "addEntity", at = @At("HEAD"), cancellable = true)
3839
private void addEntity(Entity entity, CallbackInfo ci) {
39-
if (EventFlow.post(new WorldEvent.EntitySpawn(entity)).isCanceled()) ci.cancel();
40+
if (EventFlow.post(new EntityEvent.EntitySpawn(entity)).isCanceled()) ci.cancel();
4041
}
4142

4243
@Inject(method = "getCloudsColor", at = @At("HEAD"), cancellable = true)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2025 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.event.events
19+
20+
import com.lambda.event.Event
21+
import com.lambda.event.callback.Cancellable
22+
import com.lambda.event.callback.ICancellable
23+
import net.minecraft.entity.Entity
24+
import net.minecraft.entity.damage.DamageSource
25+
import net.minecraft.entity.data.TrackedData
26+
27+
/**
28+
* A sealed class representing different types of events involving entities.
29+
*/
30+
sealed class EntityEvent {
31+
/**
32+
* Represents an event where damage is inflicted on an entity.
33+
*
34+
* This event is cancellable, allowing handlers to prevent the damage from being applied to the entity.
35+
*
36+
* @property entity The entity receiving the damage.
37+
* @property source The source of the damage (e.g., weapon, environment, or magic).
38+
* @property amount The amount of damage being inflicted.
39+
*/
40+
data class Damage(
41+
val entity: Entity,
42+
val source: DamageSource,
43+
val amount: Float,
44+
) : ICancellable by Cancellable()
45+
46+
/**
47+
* Represents an event triggered when an entity is spawned in the game world.
48+
*
49+
* This event is cancellable, allowing handlers to prevent the entity from spawning.
50+
*
51+
* @property entity The entity that is being spawned.
52+
*/
53+
data class EntitySpawn(
54+
val entity: Entity
55+
) : ICancellable by Cancellable()
56+
57+
/**
58+
* Represents an event triggered when an entity's tracked data is updated.
59+
*
60+
* This event is cancellable, allowing handlers to prevent the propagation
61+
* of the tracked data update if deemed necessary.
62+
*
63+
* @property entity The entity whose tracked data is being updated.
64+
* @property data The tracked data being updated.
65+
*/
66+
data class EntityUpdate(
67+
val entity: Entity,
68+
val data: TrackedData<*>,
69+
) : Event
70+
}

common/src/main/kotlin/com/lambda/event/events/InventoryEvent.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ sealed class InventoryEvent {
3838
*
3939
* @property screenHandler The screen handler associated with the opened inventory.
4040
*/
41-
class Open(val screenHandler: ScreenHandler) : Event
41+
data class Open(val screenHandler: ScreenHandler) : Event
4242

4343
/**
4444
* Represents an event triggered when an inventory or screen is closed.
@@ -49,7 +49,7 @@ sealed class InventoryEvent {
4949
*
5050
* @property screenHandler The screen handler associated with the closed inventory.
5151
*/
52-
class Close(val screenHandler: ScreenHandler) : Event
52+
data class Close(val screenHandler: ScreenHandler) : Event
5353

5454
/**
5555
* Represents an update event for an inventory, typically triggered when inventory contents or states
@@ -73,7 +73,7 @@ sealed class InventoryEvent {
7373
* @property slot The index of the updated inventory slot.
7474
* @property stack The new item stack in the updated slot.
7575
*/
76-
class SlotUpdate(
76+
data class SlotUpdate(
7777
val syncId: Int,
7878
val revision: Int,
7979
val slot: Int,
@@ -85,5 +85,5 @@ sealed class InventoryEvent {
8585
*
8686
* @property slot The index of the newly selected hotbar slot.
8787
*/
88-
class SelectedHotbarSlotUpdate(val slot: Int) : Event
88+
data class SelectedHotbarSlotUpdate(val slot: Int) : Event
8989
}

common/src/main/kotlin/com/lambda/event/events/MovementEvent.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ sealed class MovementEvent {
4040
* @property movementType The type of movement.
4141
* @property movement The movement vector.
4242
*/
43-
class Pre(
43+
data class Pre(
4444
override val movementType: MovementType,
4545
override val movement: Vec3d,
4646
) : Player(), ICancellable by Cancellable()
@@ -51,7 +51,7 @@ sealed class MovementEvent {
5151
* @property movementType The type of movement.
5252
* @property movement The movement vector.
5353
*/
54-
class Post(
54+
data class Post(
5555
override val movementType: MovementType,
5656
override val movement: Vec3d,
5757
) : Player(), Event
@@ -71,7 +71,7 @@ sealed class MovementEvent {
7171
* @property entity The entity involved in the movement event.
7272
* @property movementInput The movement input vector for the entity.
7373
*/
74-
class Pre(
74+
data class Pre(
7575
override val entity: LivingEntity,
7676
override val movementInput: Vec3d,
7777
) : Entity(), ICancellable by Cancellable()
@@ -82,7 +82,7 @@ sealed class MovementEvent {
8282
* @property entity The entity involved in the movement event.
8383
* @property movementInput The movement input vector for the entity.
8484
*/
85-
class Post(
85+
data class Post(
8686
override val entity: LivingEntity,
8787
override val movementInput: Vec3d,
8888
) : Entity(), Event

0 commit comments

Comments
 (0)