Skip to content

Commit b673c29

Browse files
committed
Added SafeWalk and improved cancellable handling
1 parent ea2c3cf commit b673c29

File tree

10 files changed

+72
-19
lines changed

10 files changed

+72
-19
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public class ClientConnectionMixin {
2121

2222
@Inject(method = "send(Lnet/minecraft/network/packet/Packet;)V", at = @At("HEAD"), cancellable = true)
2323
private void sendingPacket(Packet<?> packet, final CallbackInfo callbackInfo) {
24-
PacketEvent.Send.Pre event = new PacketEvent.Send.Pre(packet);
25-
EventFlow.post(event);
26-
if (event.isCanceled()) callbackInfo.cancel();
24+
if (EventFlow.post(new PacketEvent.Send.Pre(packet)).isCanceled()) {
25+
callbackInfo.cancel();
26+
}
2727
}
2828

2929
@Inject(method = "send(Lnet/minecraft/network/packet/Packet;)V", at = @At("RETURN"))
@@ -38,10 +38,9 @@ private void receivingPacket(
3838
CallbackInfo callbackInfo
3939
) {
4040
if (side != NetworkSide.CLIENTBOUND) return;
41-
42-
PacketEvent.Receive.Pre event = new PacketEvent.Receive.Pre(packet);
43-
EventFlow.post(event);
44-
if (event.isCanceled()) callbackInfo.cancel();
41+
if (EventFlow.post(new PacketEvent.Receive.Pre(packet)).isCanceled()) {
42+
callbackInfo.cancel();
43+
}
4544
}
4645

4746
@Inject(method = "channelRead0(Lio/netty/channel/ChannelHandlerContext;Lnet/minecraft/network/packet/Packet;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;handlePacket(Lnet/minecraft/network/packet/Packet;Lnet/minecraft/network/listener/PacketListener;)V", shift = At.Shift.AFTER))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ void onTickPost(CallbackInfo ci) {
2323

2424
@Inject(at = @At(value = "INVOKE", target = "Lorg/slf4j/Logger;info(Ljava/lang/String;)V", shift = At.Shift.AFTER, remap = false), method = "stop")
2525
private void onShutdown(CallbackInfo ci) {
26-
EventFlow.post(ClientEvent.Shutdown.INSTANCE);
26+
EventFlow.post(new ClientEvent.Shutdown());
2727
}
2828

2929
/**
3030
* Inject after the thread field is set so `ThreadExecutor#getThread` is available
3131
*/
3232
@Inject(at = @At(value = "FIELD", target = "Lnet/minecraft/client/MinecraftClient;thread:Ljava/lang/Thread;", shift = At.Shift.AFTER, ordinal = 0), method = "run")
3333
private void onStartup(CallbackInfo ci) {
34-
EventFlow.post(ClientEvent.Startup.INSTANCE);
34+
EventFlow.post(new ClientEvent.Startup());
3535
}
3636
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.lambda.mixin;
2+
3+
import com.lambda.event.EventFlow;
4+
import com.lambda.event.events.MovementEvent;
5+
import net.minecraft.entity.player.PlayerEntity;
6+
import org.spongepowered.asm.mixin.Mixin;
7+
import org.spongepowered.asm.mixin.injection.At;
8+
import org.spongepowered.asm.mixin.injection.Inject;
9+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
10+
11+
@Mixin(PlayerEntity.class)
12+
public class PlayerEntityMixin {
13+
@Inject(method = "clipAtLedge", at = @At(value = "HEAD"), cancellable = true)
14+
private void injectSafeWalk(CallbackInfoReturnable<Boolean> cir) {
15+
if (EventFlow.post(new MovementEvent.ClipAtLedge()).isCanceled()) {
16+
cir.setReturnValue(true);
17+
}
18+
}
19+
}

common/src/main/kotlin/com/lambda/config/Configuration.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ abstract class Configuration : Jsonable {
5555
configurables.find {
5656
it.name == name
5757
}?.loadFromJson(value)
58-
?: LOG.warn("No matching setting found for saved setting $name with $value in $configName config")
58+
?: LOG.warn("No matching setting found for saved setting $name with $value in ${configName.capitalize()} config")
5959
}
6060
}
6161

@@ -71,7 +71,7 @@ abstract class Configuration : Jsonable {
7171

7272
private fun load(file: File) {
7373
check(file.exists()) {
74-
"No configuration file found for $configName"
74+
"No configuration file found for ${configName.capitalize()}"
7575
}
7676

7777
loadFromJson(JsonParser.parseReader(file.reader()).asJsonObject)
@@ -82,19 +82,19 @@ abstract class Configuration : Jsonable {
8282
runCatching { load(primary) }
8383
.onSuccess { LOG.info("[IO] Config Manager: ${configName.capitalize()} config loaded.") }
8484
.onFailure {
85-
LOG.error("Failed to load $configName config, loading backup")
85+
LOG.error("Failed to load ${configName.capitalize()} config, loading backup")
8686
runCatching { load(backup) }
87-
.onSuccess { LOG.info("$configName config loaded from backup") }
88-
.onFailure { LOG.error("Failed to load $configName config from backup, unrecoverable error", it) }
87+
.onSuccess { LOG.info("${configName.capitalize()} config loaded from backup") }
88+
.onFailure { LOG.error("Failed to load ${configName.capitalize()} config from backup, unrecoverable error", it) }
8989
}
9090
}
9191
}
9292

9393
private fun trySave() {
9494
lambdaScope.launch(Dispatchers.IO) {
9595
runCatching { save() }
96-
.onSuccess { LOG.info("$configName config saved") }
97-
.onFailure { LOG.error("Failed to save $configName config", it) }
96+
.onSuccess { LOG.info("[IO] ${configName.capitalize()} config saved") }
97+
.onFailure { LOG.error("Failed to save ${configName.capitalize()} config", it) }
9898
}
9999
}
100100

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ object EventFlow {
6363
event.executeListenerSynchronous()
6464
}
6565

66+
@JvmStatic
67+
fun post(cancellable: ICancellable): ICancellable {
68+
post(cancellable as Event)
69+
return cancellable
70+
}
71+
6672
/**
6773
* Unsubscribes from both synchronous and concurrent event flows for a specific [Event] type [T].
6874
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import com.lambda.event.Event
44

55

66
abstract class ClientEvent : Event {
7-
data object Shutdown : ClientEvent()
8-
data object Startup : ClientEvent()
7+
class Shutdown : ClientEvent()
8+
class Startup : ClientEvent()
99
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.lambda.event.events
2+
3+
import com.lambda.event.Event
4+
import com.lambda.event.cancellable.Cancellable
5+
import com.lambda.event.cancellable.ICancellable
6+
7+
abstract class MovementEvent : Event {
8+
class ClipAtLedge : MovementEvent(), ICancellable by Cancellable()
9+
}

common/src/main/kotlin/com/lambda/module/Module.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ abstract class Module(
9494
) : Nameable, Muteable, Configurable(ModuleConfig) {
9595
private val isEnabledSetting = setting("Enabled", enabledByDefault, { false })
9696
private val keybindSetting = setting("Keybind", defaultKeybind)
97+
private val isVisible = setting("Visible", true)
9798
private val customTags = setting("Tags", defaultTags)
9899

99100
var isEnabled by isEnabledSetting
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.lambda.module.modules
2+
3+
import com.lambda.event.events.MovementEvent
4+
import com.lambda.event.listener.SafeListener.Companion.listener
5+
import com.lambda.module.Module
6+
import com.lambda.module.tag.ModuleTag
7+
8+
object SafeWalk : Module(
9+
name = "SafeWalk",
10+
description = "Keeps you at the edge",
11+
defaultTags = setOf(ModuleTag.MOVEMENT)
12+
) {
13+
init {
14+
listener<MovementEvent.ClipAtLedge> {
15+
it.cancel()
16+
}
17+
}
18+
}

common/src/main/resources/lambda.mixins.common.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"ChatScreenMixin",
99
"ClientConnectionMixin",
1010
"KeyboardMixin",
11-
"MinecraftClientMixin"
11+
"MinecraftClientMixin",
12+
"PlayerEntityMixin"
1213
],
1314
"injectors": {
1415
"defaultRequire": 1

0 commit comments

Comments
 (0)