Skip to content

Commit 1cc25eb

Browse files
committed
Merge branch 'master' into refactor/ui
2 parents cd9818c + 611e1e9 commit 1cc25eb

File tree

88 files changed

+1075
-754
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+1075
-754
lines changed

common/src/main/java/com/lambda/mixin/MinecraftClientMixin.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.lambda.Lambda;
2121
import com.lambda.event.EventFlow;
2222
import com.lambda.event.events.ClientEvent;
23-
import com.lambda.event.events.ScreenEvent;
2423
import com.lambda.event.events.ScreenHandlerEvent;
2524
import com.lambda.event.events.TickEvent;
2625
import com.lambda.module.modules.player.Interact;
@@ -81,8 +80,6 @@ private void onScreenOpen(@Nullable Screen screen, CallbackInfo ci) {
8180
if (screen instanceof ScreenHandlerProvider<?> handledScreen) {
8281
EventFlow.post(new ScreenHandlerEvent.Open(handledScreen.getScreenHandler()));
8382
}
84-
85-
EventFlow.post(new ScreenEvent.Open<>(screen));
8683
}
8784

8885
@Inject(method = "setScreen", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/Screen;removed()V", shift = At.Shift.AFTER))
@@ -91,8 +88,6 @@ private void onScreenRemove(@Nullable Screen screen, CallbackInfo ci) {
9188
if (currentScreen instanceof ScreenHandlerProvider<?> handledScreen) {
9289
EventFlow.post(new ScreenHandlerEvent.Close(handledScreen.getScreenHandler()));
9390
}
94-
95-
EventFlow.post(new ScreenEvent.Close<>(currentScreen));
9691
}
9792

9893
@Redirect(method = "doItemUse", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerInteractionManager;isBreakingBlock()Z"))

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

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

2020
import com.lambda.event.EventFlow;
21-
import com.lambda.event.events.AttackEvent;
22-
import com.lambda.event.events.InteractionEvent;
21+
import com.lambda.event.events.PlayerEvent;
2322
import net.minecraft.client.MinecraftClient;
2423
import net.minecraft.client.network.ClientPlayerEntity;
2524
import net.minecraft.client.network.ClientPlayerInteractionManager;
@@ -29,6 +28,7 @@
2928
import net.minecraft.util.ActionResult;
3029
import net.minecraft.util.Hand;
3130
import net.minecraft.util.hit.BlockHitResult;
31+
import net.minecraft.util.hit.EntityHitResult;
3232
import net.minecraft.util.math.BlockPos;
3333
import net.minecraft.util.math.Direction;
3434
import org.spongepowered.asm.mixin.Final;
@@ -44,53 +44,55 @@ public class ClientPlayInteractionManagerMixin {
4444

4545
@Shadow
4646
public float currentBreakingProgress;
47-
@Final
48-
@Shadow
49-
private MinecraftClient client;
5047

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

57-
@Inject(method = "clickSlot", at = @At("HEAD"), cancellable = true)
58-
public void clickSlotHead(int syncId, int slotId, int button, SlotActionType actionType, PlayerEntity player, CallbackInfo ci) {
59-
if (syncId != player.currentScreenHandler.syncId) return;
60-
var click = new InteractionEvent.SlotClick(syncId, slotId, button, actionType, player.currentScreenHandler);
61-
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+
}
6260
}
6361

64-
@Inject(method = "attackEntity", at = @At("HEAD"), cancellable = true)
65-
void onAttackPre(PlayerEntity player, Entity target, CallbackInfo ci) {
66-
if (EventFlow.post(new AttackEvent.Pre(target)).isCanceled()) ci.cancel();
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+
}
6767
}
6868

69-
@Inject(method = "attackEntity", at = @At("TAIL"))
70-
void onAttackPost(PlayerEntity player, Entity target, CallbackInfo ci) {
71-
EventFlow.post(new AttackEvent.Post(target));
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();
7272
}
7373

74-
@Inject(method = "attackBlock", at = @At("HEAD"), cancellable = true)
75-
public void onAttackBlock(BlockPos pos, Direction side, CallbackInfoReturnable<Boolean> cir) {
76-
if (EventFlow.post(new InteractionEvent.BlockAttack.Pre(pos, side)).isCanceled()) cir.cancel();
74+
@Inject(method = "attackEntity", at = @At("HEAD"), cancellable = true)
75+
void onAttackPre(PlayerEntity player, Entity target, CallbackInfo ci) {
76+
if (EventFlow.post(new PlayerEvent.Attack.Entity(target)).isCanceled()) ci.cancel();
7777
}
7878

79-
@Inject(method = "attackBlock", at = @At("TAIL"))
80-
public void onAttackBlockPost(BlockPos pos, Direction side, CallbackInfoReturnable<Boolean> cir) {
81-
EventFlow.post(new InteractionEvent.BlockAttack.Post(pos, side));
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();
8284
}
8385

8486
@Inject(method = "updateBlockBreakingProgress", at = @At("HEAD"), cancellable = true)
8587
private void updateBlockBreakingProgressPre(BlockPos pos, Direction side, CallbackInfoReturnable<Boolean> cir) {
86-
var event = EventFlow.post(new InteractionEvent.BreakingProgress.Pre(pos, side, currentBreakingProgress));
87-
if (event.isCanceled()) cir.cancel();
88+
var event = EventFlow.post(new PlayerEvent.Breaking.Update(pos, side, currentBreakingProgress));
89+
if (event.isCanceled()) cir.setReturnValue(false);
8890

8991
currentBreakingProgress = event.getProgress();
9092
}
9193

92-
@Inject(method = "updateBlockBreakingProgress", at = @At("TAIL"))
93-
private void updateBlockBreakingProgressPost(BlockPos pos, Direction side, CallbackInfoReturnable<Boolean> cir) {
94-
EventFlow.post(new InteractionEvent.BreakingProgress.Post(pos, side, currentBreakingProgress));
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();
9597
}
9698
}

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.EntityEvent;
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 EntityEvent.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.EntityEvent;
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 EntityEvent.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/KeyboardMixin.java

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

2020
import com.lambda.event.EventFlow;
21-
import com.lambda.event.events.KeyPressEvent;
21+
import com.lambda.event.events.KeyboardEvent;
2222
import net.minecraft.client.Keyboard;
2323
import org.spongepowered.asm.mixin.Mixin;
2424
import org.spongepowered.asm.mixin.injection.At;
@@ -28,9 +28,19 @@
2828
@Mixin(Keyboard.class)
2929
public class KeyboardMixin {
3030
@Inject(method = "onKey", at = @At("HEAD"))
31-
void onKey(long window, int key, int scancode, int action, int modifiers, CallbackInfo ci) {
31+
private void onKey(long window, int key, int scancode, int action, int modifiers, CallbackInfo ci) {
3232
if (key <= 0) return;
33-
if (action != 1) return;
34-
EventFlow.post(new KeyPressEvent(key, scancode, action, modifiers));
33+
if (action != 1) return; // TODO: Post events on both press and release ?
34+
35+
EventFlow.post(new KeyboardEvent.Press(key, scancode, action, modifiers));
36+
}
37+
38+
@Inject(method = "onChar", at = @At("HEAD"))
39+
private void onChar(long window, int codePoint, int modifiers, CallbackInfo ci) {
40+
char[] chars = Character.toChars(codePoint);
41+
42+
for (char c : chars) {
43+
EventFlow.post(new KeyboardEvent.Char(c));
44+
}
3545
}
3646
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2024 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.mixin.input;
19+
20+
import com.lambda.event.EventFlow;
21+
import com.lambda.event.events.MouseEvent;
22+
import com.lambda.util.math.Vec2d;
23+
import net.minecraft.client.Mouse;
24+
import com.lambda.util.Mouse.Button;
25+
import com.lambda.util.Mouse.Action;
26+
import org.spongepowered.asm.mixin.Mixin;
27+
import org.spongepowered.asm.mixin.Shadow;
28+
import org.spongepowered.asm.mixin.injection.At;
29+
import org.spongepowered.asm.mixin.injection.Inject;
30+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
31+
32+
@Mixin(Mouse.class)
33+
public class MouseMixin {
34+
@Shadow private double x;
35+
36+
@Shadow private double y;
37+
38+
@Inject(method = "onMouseButton(JIII)V", at = @At("HEAD"), cancellable = true)
39+
private void onMouseButton(long window, int button, int action, int mods, CallbackInfo ci) {
40+
Vec2d position = new Vec2d(x, y);
41+
42+
if (EventFlow.post(new MouseEvent.Click(button, action, mods, position)).isCanceled()) {
43+
ci.cancel();
44+
}
45+
}
46+
47+
@Inject(method = "onMouseScroll(JDD)V", at = @At("HEAD"), cancellable = true)
48+
private void onMouseScroll(long window, double horizontal, double vertical, CallbackInfo ci) {
49+
Vec2d delta = new Vec2d(horizontal, vertical);
50+
51+
if (EventFlow.post(new MouseEvent.Scroll(delta)).isCanceled()) {
52+
ci.cancel();
53+
}
54+
}
55+
56+
@Inject(method = "onCursorPos(JDD)V", at = @At("HEAD"), cancellable = true)
57+
private void onCursorPos(long window, double x, double y, CallbackInfo ci) {
58+
if (x + y == this.x + this.y) return;
59+
60+
Vec2d position = new Vec2d(x, y);
61+
62+
if (EventFlow.post(new MouseEvent.Move(position)).isCanceled()) {
63+
ci.cancel();
64+
}
65+
}
66+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class ClientConnectionMixin {
4444

4545
@Inject(method = "send(Lnet/minecraft/network/packet/Packet;)V", at = @At("HEAD"), cancellable = true)
4646
private void sendingPacket(Packet<?> packet, final CallbackInfo callbackInfo) {
47-
if (side != NetworkSide.SERVERBOUND) return;
47+
if (side != NetworkSide.CLIENTBOUND) return;
4848

4949
if (EventFlow.post(new PacketEvent.Send.Pre((Packet<ServerPacketListener>) packet)).isCanceled()) {
5050
callbackInfo.cancel();
@@ -53,7 +53,7 @@ private void sendingPacket(Packet<?> packet, final CallbackInfo callbackInfo) {
5353

5454
@Inject(method = "send(Lnet/minecraft/network/packet/Packet;)V", at = @At("RETURN"))
5555
private void sendingPacketPost(Packet<?> packet, final CallbackInfo callbackInfo) {
56-
if (side != NetworkSide.SERVERBOUND) return;
56+
if (side != NetworkSide.CLIENTBOUND) return;
5757

5858
EventFlow.post(new PacketEvent.Send.Post((Packet<ServerPacketListener>) packet));
5959
}

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

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

2020
import com.lambda.event.EventFlow;
2121
import com.lambda.event.events.ConnectionEvent;
22-
import net.minecraft.network.packet.c2s.login.LoginHelloC2SPacket;
22+
import net.minecraft.network.PacketByteBuf;
23+
import net.minecraft.network.encryption.NetworkEncryptionException;
24+
import net.minecraft.network.packet.s2c.login.LoginHelloS2CPacket;
25+
import org.spongepowered.asm.mixin.Final;
2326
import org.spongepowered.asm.mixin.Mixin;
27+
import org.spongepowered.asm.mixin.Shadow;
2428
import org.spongepowered.asm.mixin.injection.At;
2529
import org.spongepowered.asm.mixin.injection.Inject;
2630
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
2731

28-
import java.util.UUID;
32+
import java.security.PublicKey;
2933

30-
@Mixin(LoginHelloC2SPacket.class)
31-
public class LoginHelloC2SPacketMixin {
32-
@Inject(method = "<init>(Ljava/lang/String;Ljava/util/UUID;)V", at = @At("TAIL"))
33-
private void onLoginHelloC2SPacket(String string, UUID uUID, CallbackInfo ci) {
34-
EventFlow.post(new ConnectionEvent.Connect.Login.Hello(string, uUID));
34+
@Mixin(LoginHelloS2CPacket.class)
35+
public abstract class LoginHelloC2SPacketMixin {
36+
@Shadow @Final private String serverId;
37+
38+
@Shadow @Final private byte[] nonce;
39+
40+
@Shadow public abstract PublicKey getPublicKey() throws NetworkEncryptionException;
41+
42+
@Inject(method = "<init>(Lnet/minecraft/network/PacketByteBuf;)V", at = @At("TAIL"))
43+
private void onLoginHelloC2SPacket(PacketByteBuf buf, CallbackInfo ci) throws NetworkEncryptionException {
44+
EventFlow.post(new ConnectionEvent.Connect.Login.EncryptionRequest(serverId, getPublicKey(), nonce));
3545
}
3646
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
public class LoginKeyC2SPacketMixin {
3333
@Inject(method = "<init>(Ljavax/crypto/SecretKey;Ljava/security/PublicKey;[B)V", at = @At("TAIL"))
3434
private void onLoginKeyC2SPacket(SecretKey secretKey, PublicKey publicKey, byte[] nonce, CallbackInfo ci) {
35-
// Please note this won't work if the server is offline mode because the player doesn't
36-
// fetch the server's public key.
37-
EventFlow.post(new ConnectionEvent.Connect.Login.Key(secretKey, publicKey, nonce));
35+
EventFlow.post(new ConnectionEvent.Connect.Login.EncryptionResponse(secretKey, publicKey, nonce));
3836
}
3937
}

0 commit comments

Comments
 (0)