Skip to content

Commit 9f9a7f2

Browse files
committed
Rework event structure, add some missing ones
1 parent d769d3e commit 9f9a7f2

File tree

25 files changed

+314
-165
lines changed

25 files changed

+314
-165
lines changed

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

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package com.lambda.mixin.entity;
1919

2020
import com.lambda.event.EventFlow;
21-
import com.lambda.event.events.LocalPlayerEvent;
21+
import com.lambda.event.events.PlayerEvent;
2222
import net.minecraft.client.MinecraftClient;
2323
import net.minecraft.client.network.ClientPlayerEntity;
2424
import net.minecraft.client.network.ClientPlayerInteractionManager;
@@ -28,6 +28,7 @@
2828
import net.minecraft.util.ActionResult;
2929
import net.minecraft.util.Hand;
3030
import net.minecraft.util.hit.BlockHitResult;
31+
import net.minecraft.util.hit.EntityHitResult;
3132
import net.minecraft.util.math.BlockPos;
3233
import net.minecraft.util.math.Direction;
3334
import org.spongepowered.asm.mixin.Final;
@@ -43,38 +44,55 @@ public class ClientPlayInteractionManagerMixin {
4344

4445
@Shadow
4546
public float currentBreakingProgress;
46-
@Final
47-
@Shadow
48-
private MinecraftClient client;
4947

50-
@Inject(method = "interactBlock", at = @At("HEAD"))
48+
@Inject(method = "interactBlock", at = @At("HEAD"), cancellable = true)
5149
public void interactBlockHead(final ClientPlayerEntity player, final Hand hand, final BlockHitResult hitResult, final CallbackInfoReturnable<ActionResult> cir) {
52-
if (client.world == null) return;
53-
EventFlow.post(new LocalPlayerEvent.BlockInteract(client.world, hitResult));
50+
if (EventFlow.post(new PlayerEvent.Interact.Block(hand, hitResult)).isCanceled()) {
51+
cir.setReturnValue(ActionResult.FAIL);
52+
}
5453
}
5554

56-
@Inject(method = "clickSlot", at = @At("HEAD"), cancellable = true)
57-
public void clickSlotHead(int syncId, int slotId, int button, SlotActionType actionType, PlayerEntity player, CallbackInfo ci) {
58-
if (syncId != player.currentScreenHandler.syncId) return;
59-
var click = new LocalPlayerEvent.SlotClick(syncId, slotId, button, actionType, player.currentScreenHandler);
60-
if (EventFlow.post(click).isCanceled()) ci.cancel();
55+
@Inject(method = "interactEntityAtLocation", at = @At("HEAD"), cancellable = true)
56+
public void interactEntityAtLocation(PlayerEntity player, Entity entity, EntityHitResult hitResult, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
57+
if (EventFlow.post(new PlayerEvent.Interact.Entity(hand, entity, hitResult)).isCanceled()) {
58+
cir.setReturnValue(ActionResult.FAIL);
59+
}
60+
}
61+
62+
@Inject(method = "interactItem", at = @At("HEAD"), cancellable = true)
63+
public void interactItemHead(PlayerEntity player, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
64+
if (EventFlow.post(new PlayerEvent.Interact.Item(hand)).isCanceled()) {
65+
cir.setReturnValue(ActionResult.FAIL);
66+
}
67+
}
68+
69+
@Inject(method = "attackBlock", at = @At("HEAD"))
70+
public void onAttackBlock(BlockPos pos, Direction side, CallbackInfoReturnable<Boolean> cir) {
71+
if (EventFlow.post(new PlayerEvent.Attack.Block(pos, side)).isCanceled()) cir.cancel();
6172
}
6273

6374
@Inject(method = "attackEntity", at = @At("HEAD"), cancellable = true)
6475
void onAttackPre(PlayerEntity player, Entity target, CallbackInfo ci) {
65-
if (EventFlow.post(new LocalPlayerEvent.EntityAttack(target)).isCanceled()) ci.cancel();
76+
if (EventFlow.post(new PlayerEvent.Attack.Entity(target)).isCanceled()) ci.cancel();
6677
}
6778

68-
@Inject(method = "attackBlock", at = @At("HEAD"), cancellable = true)
69-
public void onAttackBlock(BlockPos pos, Direction side, CallbackInfoReturnable<Boolean> cir) {
70-
if (EventFlow.post(new LocalPlayerEvent.BlockAttack(pos, side)).isCanceled()) cir.cancel();
79+
@Inject(method = "clickSlot", at = @At("HEAD"), cancellable = true)
80+
public void clickSlotHead(int syncId, int slotId, int button, SlotActionType actionType, PlayerEntity player, CallbackInfo ci) {
81+
if (syncId != player.currentScreenHandler.syncId) return;
82+
var click = new PlayerEvent.SlotClick(syncId, slotId, button, actionType, player.currentScreenHandler);
83+
if (EventFlow.post(click).isCanceled()) ci.cancel();
7184
}
7285

7386
@Inject(method = "updateBlockBreakingProgress", at = @At("HEAD"), cancellable = true)
7487
private void updateBlockBreakingProgressPre(BlockPos pos, Direction side, CallbackInfoReturnable<Boolean> cir) {
75-
var event = EventFlow.post(new LocalPlayerEvent.BreakingProgress(pos, side, currentBreakingProgress));
76-
if (event.isCanceled()) cir.cancel();
88+
var event = EventFlow.post(new PlayerEvent.Breaking.Update(pos, side, currentBreakingProgress));
89+
if (event.isCanceled()) cir.setReturnValue(false);
7790

7891
currentBreakingProgress = event.getProgress();
7992
}
93+
94+
@Inject(method = "cancelBlockBreaking", at = @At("HEAD"), cancellable = true)
95+
private void cancelBlockBreakingPre(CallbackInfo ci) {
96+
if (EventFlow.post(new PlayerEvent.Breaking.Cancel(currentBreakingProgress)).isCanceled()) ci.cancel();
97+
}
8098
}

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

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

2020
import com.lambda.Lambda;
2121
import com.lambda.event.EventFlow;
22-
import com.lambda.event.events.LocalPlayerEvent;
22+
import com.lambda.event.events.PlayerEvent;
2323
import com.lambda.event.events.MovementEvent;
2424
import com.lambda.event.events.TickEvent;
2525
import com.lambda.interaction.PlayerPacketManager;
@@ -50,9 +50,6 @@ public abstract class ClientPlayerEntityMixin extends EntityMixin {
5050
@Shadow
5151
protected abstract void autoJump(float dx, float dz);
5252

53-
@Shadow
54-
public abstract boolean isUsingItem();
55-
5653
@Inject(method = "move", at = @At("HEAD"), cancellable = true)
5754
void onMove(MovementType movementType, Vec3d movement, CallbackInfo ci) {
5855
ClientPlayerEntity self = (ClientPlayerEntity) (Object) this;
@@ -63,9 +60,9 @@ void onMove(MovementType movementType, Vec3d movement, CallbackInfo ci) {
6360
float prevX = (float) self.getX();
6461
float prevZ = (float) self.getZ();
6562

66-
EventFlow.post(new MovementEvent.Pre());
63+
EventFlow.post(new MovementEvent.Player.Pre(movementType, movement));
6764
super.move(movementType, self.getVelocity());
68-
EventFlow.post(new MovementEvent.Post());
65+
EventFlow.post(new MovementEvent.Player.Post(movementType, movement));
6966

7067
float currX = (float) self.getX();
7168
float currZ = (float) self.getZ();
@@ -125,6 +122,6 @@ float fixHeldItemPitch(ClientPlayerEntity instance) {
125122

126123
@Inject(method = "swingHand", at = @At("HEAD"), cancellable = true)
127124
void onSwingHandPre(Hand hand, CallbackInfo ci) {
128-
if (EventFlow.post(new LocalPlayerEvent.SwingHand(hand)).isCanceled()) ci.cancel();
125+
if (EventFlow.post(new PlayerEvent.SwingHand(hand)).isCanceled()) ci.cancel();
129126
}
130127
}

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

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

2020
import com.lambda.Lambda;
2121
import com.lambda.event.EventFlow;
22-
import com.lambda.event.events.LocalPlayerEvent;
22+
import com.lambda.event.events.PlayerEvent;
2323
import com.lambda.event.events.WorldEvent;
2424
import com.lambda.interaction.RotationManager;
2525
import com.lambda.util.math.Vec2d;
@@ -87,7 +87,7 @@ float fixDirectionPitch2(Entity entity) {
8787

8888
@Inject(method = "changeLookDirection", at = @At("HEAD"), cancellable = true)
8989
private void changeLookDirection(double cursorDeltaX, double cursorDeltaY, CallbackInfo ci) {
90-
if (EventFlow.post(new LocalPlayerEvent.ChangeLookDirection(cursorDeltaX, cursorDeltaY)).isCanceled()) ci.cancel();
90+
if (EventFlow.post(new PlayerEvent.ChangeLookDirection(cursorDeltaX, cursorDeltaY)).isCanceled()) ci.cancel();
9191
}
9292

9393
@Inject(method = "onTrackedDataSet(Lnet/minecraft/entity/data/TrackedData;)V", at = @At("TAIL"))

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,12 @@ void onJump(CallbackInfo ci) {
6363

6464
@Inject(method = "travel", at = @At("HEAD"), cancellable = true)
6565
void onTravelPre(Vec3d movementInput, CallbackInfo ci) {
66-
LivingEntity self = (LivingEntity) (Object) this;
67-
if (self != Lambda.getMc().player) return;
68-
69-
if (EventFlow.post(new MovementEvent.Travel.Pre()).isCanceled()) ci.cancel();
66+
if (EventFlow.post(new MovementEvent.Entity.Pre((LivingEntity) (Object) this, movementInput)).isCanceled()) ci.cancel();
7067
}
7168

7269
@Inject(method = "travel", at = @At("TAIL"))
7370
void onTravelPost(Vec3d movementInput, CallbackInfo ci) {
74-
LivingEntity self = (LivingEntity) (Object) this;
75-
if (self != Lambda.getMc().player) return;
76-
77-
EventFlow.post(new MovementEvent.Travel.Post());
71+
EventFlow.post(new MovementEvent.Entity.Post((LivingEntity) (Object) this, movementInput));
7872
}
7973

8074
@Redirect(method = "travel", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;getPitch()F"))

common/src/main/java/com/lambda/mixin/input/MouseMixin.java

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

2020
import com.lambda.event.EventFlow;
2121
import com.lambda.event.events.MouseEvent;
22+
import com.lambda.util.math.Vec2d;
2223
import net.minecraft.client.Mouse;
24+
import com.lambda.util.Mouse.Button;
25+
import com.lambda.util.Mouse.Action;
2326
import org.spongepowered.asm.mixin.Mixin;
2427
import org.spongepowered.asm.mixin.Shadow;
2528
import org.spongepowered.asm.mixin.injection.At;
@@ -34,18 +37,16 @@ public class MouseMixin {
3437

3538
@Inject(method = "onMouseButton(JIII)V", at = @At("HEAD"), cancellable = true)
3639
private void onMouseButton(long window, int button, int action, int mods, CallbackInfo ci) {
37-
com.lambda.util.Mouse.Button btn = com.lambda.util.Mouse.Button.Companion.fromMouseCode(button);
38-
com.lambda.util.Mouse.Action act = com.lambda.util.Mouse.Action.Companion.fromActionCode(action);
39-
com.lambda.util.math.Vec2d position = new com.lambda.util.math.Vec2d(x, y);
40+
Vec2d position = new Vec2d(x, y);
4041

41-
if (EventFlow.post(new MouseEvent.Click(btn, act, mods, position)).isCanceled()) {
42+
if (EventFlow.post(new MouseEvent.Click(button, action, mods, position)).isCanceled()) {
4243
ci.cancel();
4344
}
4445
}
4546

4647
@Inject(method = "onMouseScroll(JDD)V", at = @At("HEAD"), cancellable = true)
4748
private void onMouseScroll(long window, double horizontal, double vertical, CallbackInfo ci) {
48-
com.lambda.util.math.Vec2d delta = new com.lambda.util.math.Vec2d(horizontal, vertical);
49+
Vec2d delta = new Vec2d(horizontal, vertical);
4950

5051
if (EventFlow.post(new MouseEvent.Scroll(delta)).isCanceled()) {
5152
ci.cancel();
@@ -56,7 +57,7 @@ private void onMouseScroll(long window, double horizontal, double vertical, Call
5657
private void onCursorPos(long window, double x, double y, CallbackInfo ci) {
5758
if (x + y == this.x + this.y) return;
5859

59-
com.lambda.util.math.Vec2d position = new com.lambda.util.math.Vec2d(x, y);
60+
Vec2d position = new Vec2d(x, y);
6061

6162
if (EventFlow.post(new MouseEvent.Move(position)).isCanceled()) {
6263
ci.cancel();

common/src/main/kotlin/com/lambda/event/Subscriber.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class Subscriber : ConcurrentHashMap<KClass<out Event>, ConcurrentSkipListSet<Li
3535
val defaultListenerSet: ConcurrentSkipListSet<Listener<out Event>>
3636
get() = ConcurrentSkipListSet(Listener.comparator.reversed())
3737

38-
3938
/** Allows a [Listener] to start receiving a specific type of [Event] */
4039
inline fun <reified T : Event> subscribe(listener: Listener<T>) =
4140
getOrPut(T::class) { defaultListenerSet }.add(listener)

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

Lines changed: 0 additions & 90 deletions
This file was deleted.

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ sealed class MouseEvent {
3636
val action: Mouse.Action,
3737
val modifiers: Int,
3838
val position: Vec2d
39-
) : ICancellable by Cancellable()
39+
) : ICancellable by Cancellable() {
40+
constructor(button: Int, action: Int, modifiers: Int, position: Vec2d) : this(
41+
Mouse.Button.fromMouseCode(button),
42+
Mouse.Action.fromActionCode(action),
43+
modifiers,
44+
position
45+
)
46+
}
4047

4148
/**
4249
* Represents a mouse scroll event

0 commit comments

Comments
 (0)