| 1 | /*
|
| 2 | * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
|
| 3 | * Copyright (c) Meteor Development.
|
| 4 | */
|
| 5 |
|
| 6 | package meteordevelopment.meteorclient.systems.modules.world;
|
| 7 |
|
| 8 | import meteordevelopment.meteorclient.events.game.OpenScreenEvent;
|
| 9 | import meteordevelopment.meteorclient.events.packets.PacketEvent;
|
| 10 | import meteordevelopment.meteorclient.events.world.TickEvent;
|
| 11 | import meteordevelopment.meteorclient.mixin.AbstractSignEditScreenAccessor;
|
| 12 | import meteordevelopment.meteorclient.settings.IntSetting;
|
| 13 | import meteordevelopment.meteorclient.settings.Setting;
|
| 14 | import meteordevelopment.meteorclient.settings.SettingGroup;
|
| 15 | import meteordevelopment.meteorclient.systems.modules.Categories;
|
| 16 | import meteordevelopment.meteorclient.systems.modules.Module;
|
| 17 | import meteordevelopment.orbit.EventHandler;
|
| 18 | import net.minecraft.client.gui.screens.inventory.AbstractSignEditScreen;
|
| 19 | import net.minecraft.network.protocol.game.ServerboundSignUpdatePacket;
|
| 20 | import net.minecraft.world.level.block.entity.SignBlockEntity;
|
| 21 |
|
| 22 | import java.util.ArrayDeque;
|
| 23 | import java.util.Queue;
|
| 24 |
|
| 25 | public class AutoSign extends Module {
|
| 26 | private final SettingGroup sgGeneral = settings.getDefaultGroup();
|
| 27 |
|
| 28 | private final Setting<Integer> delay = sgGeneral.add(new IntSetting.Builder()
|
| 29 | .name("delay")
|
| 30 | .description("The tick delay between sign update packets.")
|
| 31 | .defaultValue(10)
|
| 32 | .range(0, 100)
|
| 33 | .sliderRange(0, 100)
|
| 34 | .build()
|
| 35 | );
|
| 36 |
|
| 37 | private String[] text;
|
| 38 |
|
| 39 | // Some servers (e.g., 2b2t) don't like the sign packet being sent too soon after the swing or block click packets, so queue them.
|
| 40 | // Delaying by sleeping in the event handler may be fine for a single sign, but would visibly lag the UI at a larger scale.
|
| 41 | private final Queue<ServerboundSignUpdatePacket> queue = new ArrayDeque<>();
|
| 42 | private int timer = 0;
|
| 43 |
|
| 44 | public AutoSign() {
|
| 45 | super(Categories.World, "auto-sign", "Automatically writes signs. The first sign's text will be used.");
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public void onDeactivate() {
|
| 50 | text = null;
|
| 51 | }
|
| 52 |
|
| 53 | @EventHandler
|
| 54 | private void onTick(TickEvent.Post event) {
|
| 55 | // Adding a new packet with the timer close to the threshold could lead to it being sent too fast relative to
|
| 56 | // the swing/click packets, but only if there isn't another packet ahead of it to reset the timer, so always
|
| 57 | // keep it reset if the queue is empty.
|
| 58 | if (mc.player == null || queue.peek() == null) {
|
| 59 | timer = 0;
|
| 60 | return;
|
| 61 | }
|
| 62 |
|
| 63 | if (timer < delay.get()) {
|
| 64 | timer++;
|
| 65 | return;
|
| 66 | }
|
| 67 |
|
| 68 | mc.player.connection.send(queue.poll());
|
| 69 |
|
| 70 | timer = 0;
|
| 71 | }
|
| 72 |
|
| 73 | @EventHandler
|
| 74 | private void onSendPacket(PacketEvent.Send event) {
|
| 75 | if (!(event.packet instanceof ServerboundSignUpdatePacket)) return;
|
| 76 |
|
| 77 | text = ((ServerboundSignUpdatePacket) event.packet).getLines();
|
| 78 | }
|
| 79 |
|
| 80 | @EventHandler
|
| 81 | private void onOpenScreen(OpenScreenEvent event) {
|
| 82 | if (!(event.screen instanceof AbstractSignEditScreen) || text == null) return;
|
| 83 |
|
| 84 | SignBlockEntity sign = ((AbstractSignEditScreenAccessor) event.screen).meteor$getSign();
|
| 85 |
|
| 86 | queue.add(new ServerboundSignUpdatePacket(sign.getBlockPos(), true, text[0], text[1], text[2], text[3]));
|
| 87 |
|
| 88 | event.cancel();
|
| 89 | }
|
| 90 | }
|
| 91 |
|