Skip to content

Commit 927cd47

Browse files
committed
Actually got PacketEvents working
1 parent bae01c5 commit 927cd47

File tree

6 files changed

+84
-42
lines changed

6 files changed

+84
-42
lines changed

src/main/java/adris/altoclef/AltoClef.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@
1111
import adris.altoclef.altomenu.managers.ModuleManager;
1212
import adris.altoclef.commandsystem.CommandExecutor;
1313
import adris.altoclef.eventbus.ClefEventBus;
14+
import adris.altoclef.eventbus.events.*;
1415
import adris.altoclef.scripting.LuaScriptEngine;
1516
import adris.altoclef.control.InputControls;
1617
import adris.altoclef.control.PlayerExtraController;
1718
import adris.altoclef.control.SlotHandler;
18-
import adris.altoclef.eventbus.events.ClientRenderEvent;
19-
import adris.altoclef.eventbus.events.ClientTickEvent;
20-
import adris.altoclef.eventbus.events.SendChatEvent;
21-
import adris.altoclef.eventbus.events.TitleScreenEntryEvent;
2219
import adris.altoclef.tasksystem.Task;
2320
import adris.altoclef.tasksystem.TaskRunner;
2421
import adris.altoclef.trackers.*;
@@ -32,6 +29,7 @@
3229
import baritone.altoclef.AltoClefSettings;
3330
import baritone.api.BaritoneAPI;
3431
import baritone.api.Settings;
32+
import com.google.common.eventbus.Subscribe;
3533
import net.fabricmc.api.ModInitializer;
3634
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
3735
import net.fabricmc.loader.api.FabricLoader;
@@ -319,6 +317,10 @@ public void onInitializeLoad() {
319317
// Render
320318
ClefEventBus.subscribe(ClientRenderEvent.class, evt -> onClientRenderOverlay(evt.stack));
321319

320+
// Packet
321+
ClefEventBus.subscribe(PacketEvent.class, evt -> {
322+
});
323+
322324
// Playground
323325
Playground.IDLE_TEST_INIT_FUNCTION(this);
324326

@@ -751,4 +753,5 @@ public void onTick() {
751753
}
752754
}
753755
}
756+
754757
}

src/main/java/adris/altoclef/altomenu/modules/Player/AntiHunger.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,49 @@
22

33
import adris.altoclef.altomenu.Mod;
44
import adris.altoclef.altomenu.settings.BooleanSetting;
5-
import adris.altoclef.altomenu.settings.ModeSetting;
65
import adris.altoclef.eventbus.EventHandler;
76
import adris.altoclef.eventbus.events.PacketEvent;
87
import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket;
9-
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket;
10-
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket;
11-
12-
import java.util.Objects;
138

149
public class AntiHunger extends Mod {
1510

16-
//todo:
17-
// Add Sprint Spoof (Packet & Legit) (Legit cancels Sprint Press Client Side) (Packet cancels Sprint Packet on Send)
18-
// Add Ground Spoof (Packet) (Send onGround Packet Every So Many Ticks)
19-
// Attempt to find a way to lower hunger usage when spamming jump button under a block that doesn't require Constant Packet Spoof (@ChiefWarCry can you attempt it)
20-
2111
public AntiHunger() {
2212
super("AntiHunger", "Prevents hunger from decreasing.", Mod.Category.PLAYER);
13+
14+
// Automatically hook packet handling globally
15+
PacketEvent.addGlobalListener(this::onPacket);
2316
}
2417

2518
BooleanSetting spoofValue = new BooleanSetting("SpoofValue", true);
2619
BooleanSetting packetSpoof = new BooleanSetting("Packet", true);
2720

28-
2921
@EventHandler
3022
public boolean onShitTick() {
3123
if (mc.player == null) return true;
24+
3225
if (spoofValue.isEnabled() && mc.player.getHungerManager().getFoodLevel() != 20) {
3326
mc.player.getHungerManager().setFoodLevel(20);
3427
}
3528

3629
return false;
3730
}
38-
}
31+
32+
private void onPacket(PacketEvent evt) {
33+
// Only care about outgoing packets
34+
if (evt.direction != PacketEvent.Direction.SEND) return;
35+
if (!packetSpoof.isEnabled()) return;
36+
37+
if (!AntiHunger.super.isEnabled()) return;
38+
39+
// Cancel sprint packets
40+
if (evt.packet instanceof ClientCommandC2SPacket cmd) {
41+
ClientCommandC2SPacket.Mode mode = cmd.getMode();
42+
if (mode == ClientCommandC2SPacket.Mode.START_SPRINTING ||
43+
mode == ClientCommandC2SPacket.Mode.STOP_SPRINTING) {
44+
45+
evt.cancel(); // stops sprint packet
46+
System.out.println("Cancelled sprint packet: " + mode);
47+
}
48+
}
49+
}
50+
}

src/main/java/adris/altoclef/eventbus/events/PacketEvent.java

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,36 @@
22

33
import net.minecraft.network.packet.Packet;
44

5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.function.Consumer;
8+
59
public class PacketEvent extends Cancellable {
610

7-
public static PacketEvent.Receive Receive;
811
public Packet<?> packet;
12+
public Direction direction;
913

10-
public static class Receive extends PacketEvent {
11-
private static final Receive INSTANCE = new Receive();
14+
public enum Direction { SEND, RECEIVE }
1215

13-
public static Receive get(Packet<?> packet) {
14-
INSTANCE.setCancelled(false);
15-
INSTANCE.packet = packet;
16-
return INSTANCE;
17-
}
16+
public PacketEvent(Packet<?> packet, Direction direction) {
17+
this.packet = packet;
18+
this.direction = direction;
1819
}
1920

20-
public static class Send extends PacketEvent {
21-
private static final Send INSTANCE = new Send();
21+
// Global packet listeners
22+
private static final List<Consumer<PacketEvent>> GLOBAL_LISTENERS = new ArrayList<>();
2223

23-
public static Send get(Packet<?> packet) {
24-
INSTANCE.setCancelled(false);
25-
INSTANCE.packet = packet;
26-
return INSTANCE;
27-
}
24+
public static void addGlobalListener(Consumer<PacketEvent> listener) {
25+
GLOBAL_LISTENERS.add(listener);
2826
}
2927

30-
public static class Sent extends PacketEvent {
31-
private static final Sent INSTANCE = new Sent();
28+
public static void removeGlobalListener(Consumer<PacketEvent> listener) {
29+
GLOBAL_LISTENERS.remove(listener);
30+
}
3231

33-
public static Sent get(Packet<?> packet) {
34-
INSTANCE.packet = packet;
35-
return INSTANCE;
32+
public void callGlobal() {
33+
for (Consumer<PacketEvent> l : GLOBAL_LISTENERS) {
34+
l.accept(this);
3635
}
3736
}
38-
39-
40-
}
37+
}
Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package adris.altoclef.mixins;
22

3+
4+
import adris.altoclef.eventbus.ClefEventBus;
35
import adris.altoclef.eventbus.events.PacketEvent;
6+
import io.netty.channel.ChannelHandlerContext;
47
import net.minecraft.network.ClientConnection;
58
import net.minecraft.network.packet.Packet;
69
import org.spongepowered.asm.mixin.Mixin;
@@ -11,9 +14,32 @@
1114
@Mixin(ClientConnection.class)
1215
public class ClientConnectionMixin {
1316

17+
@Inject(
18+
method = "send(Lnet/minecraft/network/packet/Packet;)V",
19+
at = @At("HEAD"),
20+
cancellable = true
21+
)
22+
private void onSend(Packet<?> packet, CallbackInfo ci) {
23+
PacketEvent evt = new PacketEvent(packet, PacketEvent.Direction.SEND);
24+
ClefEventBus.publish(evt);
25+
evt.callGlobal();
26+
if (evt.isCancelled()) ci.cancel();
27+
}
28+
29+
@Inject(
30+
method = "channelRead0(Lio/netty/channel/ChannelHandlerContext;Lnet/minecraft/network/packet/Packet;)V",
31+
at = @At("HEAD"),
32+
cancellable = true
33+
)
34+
private void onIncoming(ChannelHandlerContext ctx, Packet<?> packet, CallbackInfo ci) {
35+
// Wrap packet in your PacketEvent
36+
PacketEvent evt = new PacketEvent(packet, PacketEvent.Direction.RECEIVE);
37+
ClefEventBus.publish(evt);
38+
evt.callGlobal();
1439

15-
@Inject(at = @At("HEAD"), method = "send(Lnet/minecraft/network/packet/Packet;)V", remap = false, cancellable = true)
16-
private void onSendPacketHead(Packet<?> packet, CallbackInfo info) {
17-
if (PacketEvent.Send.get(packet).isCancelled()) info.cancel();
40+
// Cancel if any module cancelled it
41+
if (evt.isCancelled()) {
42+
ci.cancel();
43+
}
1844
}
1945
}

src/main/java/adris/altoclef/mixins/ClientPlayNetworkHandlerMixin.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import adris.altoclef.altomenu.managers.CommandManager;
77
import adris.altoclef.altomenu.managers.ChatHandler;
88
import adris.altoclef.altomenu.modules.Player.Velocity;
9+
import adris.altoclef.eventbus.ClefEventBus;
10+
import adris.altoclef.eventbus.events.PacketEvent;
911
import net.minecraft.client.network.ClientPlayNetworkHandler;
12+
import net.minecraft.network.packet.Packet;
1013
import net.minecraft.network.packet.s2c.play.*;
1114
import org.spongepowered.asm.mixin.Mixin;
1215
import org.spongepowered.asm.mixin.Shadow;

src/main/resources/altoclef.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"BlockModifiedByPlayerMixin",
99
"ClientBlockBreakAccessor",
1010
"ClientConnectionAccessor",
11+
"ClientConnectionMixin",
1112
"ClientInteractWithBlockMixin",
1213
"ClientPlayerInteractionAccessor",
1314
"ClientUIMixin",

0 commit comments

Comments
 (0)