From ad5826df737eba3956fe9dcf8f9e2c8eed8608a7 Mon Sep 17 00:00:00 2001 From: sectersion Date: Sun, 11 Oct 2026 16:38:29 +0100 Subject: [PATCH] Refactor: extract MacroRender from MacroExecutor (upstream #4) Move 13 render/rotation/stopAll/packet-burst methods verbatim into MacroRender. onRender, hasRenderWork and stopAll kept as public delegates for mixin and external callers. Shared members referenced as MacroExecutor.xxx (mechanical qualification only); burst-helper call sites re-pointed across helpers. All bodies verified identical against HEAD modulo that qualification. --- .../util/macro/MacroActionRunner.java | 2 +- .../util/macro/MacroDispatcher.java | 22 +- .../util/macro/MacroExecutor.java | 256 +---------------- .../autismclient/util/macro/MacroRender.java | 262 ++++++++++++++++++ .../autismclient/util/macro/MacroRunLoop.java | 38 +-- 5 files changed, 303 insertions(+), 277 deletions(-) create mode 100644 src/main/java/autismclient/util/macro/MacroRender.java diff --git a/src/main/java/autismclient/util/macro/MacroActionRunner.java b/src/main/java/autismclient/util/macro/MacroActionRunner.java index a3bb6c7..e0f9a3e 100644 --- a/src/main/java/autismclient/util/macro/MacroActionRunner.java +++ b/src/main/java/autismclient/util/macro/MacroActionRunner.java @@ -55,7 +55,7 @@ final class MacroActionRunner { int count, Minecraft mc) { MacroExecutor.RepeatPacketContext context = MacroExecutor.REPEAT_PACKET_CONTEXT.get(); if (context == null || context.macro() == null) return new java.util.ArrayList<>(); - return MacroExecutor.preGeneratePackets(context.macro(), context.startIdx(), count, context.endIdx()); + return MacroRender.preGeneratePackets(context.macro(), context.startIdx(), count, context.endIdx()); } public static boolean isTargetWaitInteractionAction(MacroAction action) { diff --git a/src/main/java/autismclient/util/macro/MacroDispatcher.java b/src/main/java/autismclient/util/macro/MacroDispatcher.java index 1406d89..0e7f531 100644 --- a/src/main/java/autismclient/util/macro/MacroDispatcher.java +++ b/src/main/java/autismclient/util/macro/MacroDispatcher.java @@ -221,14 +221,14 @@ final class MacroDispatcher { } } else if (action instanceof LookAtBlockAction) { LookAtBlockAction la = (LookAtBlockAction) action; - LookAtBlockAction.RotationTarget rotationTarget = MacroExecutor.resolveLookAtTargetOnClient(mc, la); + LookAtBlockAction.RotationTarget rotationTarget = MacroRender.resolveLookAtTargetOnClient(mc, la); if (rotationTarget == null) { return; } if (la.smooth) { - MacroExecutor.startSmoothRotation(rotationTarget.yaw(), rotationTarget.pitch(), la.getRotationStep()); + MacroRender.startSmoothRotation(rotationTarget.yaw(), rotationTarget.pitch(), la.getRotationStep()); if (la.waitForCompletion) { - MacroExecutor.waitForSmoothRotationCompletion(); + MacroRender.waitForSmoothRotationCompletion(); } } else { mc.execute(() -> { @@ -240,9 +240,9 @@ final class MacroDispatcher { } else if (action instanceof RotateAction) { RotateAction ra = (RotateAction) action; if (ra.smooth) { - MacroExecutor.startSmoothRotation(ra.yaw, ra.pitch, ra.getRotationStep()); + MacroRender.startSmoothRotation(ra.yaw, ra.pitch, ra.getRotationStep()); if (ra.waitForCompletion) { - MacroExecutor.waitForSmoothRotationCompletion(); + MacroRender.waitForSmoothRotationCompletion(); } } else { mc.execute(() -> action.execute(mc)); @@ -419,9 +419,9 @@ final class MacroDispatcher { java.util.concurrent.locks.LockSupport.parkNanos(1_000_000L); } if (MacroExecutor.isCurrentRunActive()) { - MacroExecutor.executePreGeneratedBurst(mc, preGenerated); + MacroRender.executePreGeneratedBurst(mc, preGenerated); MacroExecutor.RepeatPacketContext context = MacroExecutor.REPEAT_PACKET_CONTEXT.get(); - if (context != null) MacroExecutor.REPEAT_PACKET_SKIP.set(MacroExecutor.countPreGeneratedActions(context.macro(), context.startIdx(), preGenerated.size(), context.endIdx())); + if (context != null) MacroExecutor.REPEAT_PACKET_SKIP.set(MacroRender.countPreGeneratedActions(context.macro(), context.startIdx(), preGenerated.size(), context.endIdx())); AutismClientMessaging.sendPrefixed("Tick sync: " + preGenerated.size() + " pkts sent"); } } else if (action instanceof RevisionSyncAction rsa) { @@ -437,9 +437,9 @@ final class MacroDispatcher { java.util.concurrent.locks.LockSupport.parkNanos(500_000L); } if (MacroExecutor.isCurrentRunActive() && mc.player != null && mc.player.containerMenu != null) { - MacroExecutor.executePreGeneratedBurst(mc, preGenerated); + MacroRender.executePreGeneratedBurst(mc, preGenerated); MacroExecutor.RepeatPacketContext context = MacroExecutor.REPEAT_PACKET_CONTEXT.get(); - if (context != null) MacroExecutor.REPEAT_PACKET_SKIP.set(MacroExecutor.countPreGeneratedActions(context.macro(), context.startIdx(), preGenerated.size(), context.endIdx())); + if (context != null) MacroExecutor.REPEAT_PACKET_SKIP.set(MacroRender.countPreGeneratedActions(context.macro(), context.startIdx(), preGenerated.size(), context.endIdx())); AutismClientMessaging.sendPrefixed("Rev sync: " + preGenerated.size() + " pkts sent"); } } else if (action instanceof ServerTickSyncAction stsa) { @@ -474,9 +474,9 @@ final class MacroDispatcher { } } if (MacroExecutor.isCurrentRunActive()) { - MacroExecutor.executePreGeneratedBurst(mc, preGenerated); + MacroRender.executePreGeneratedBurst(mc, preGenerated); MacroExecutor.RepeatPacketContext context = MacroExecutor.REPEAT_PACKET_CONTEXT.get(); - if (context != null) MacroExecutor.REPEAT_PACKET_SKIP.set(MacroExecutor.countPreGeneratedActions(context.macro(), context.startIdx(), preGenerated.size(), context.endIdx())); + if (context != null) MacroExecutor.REPEAT_PACKET_SKIP.set(MacroRender.countPreGeneratedActions(context.macro(), context.startIdx(), preGenerated.size(), context.endIdx())); long actualDelay = (System.nanoTime() - optimalTime) / 1_000_000L; AutismClientMessaging.sendPrefixed("ServerSync: " + preGenerated.size() + " pkts (+" + actualDelay + "ms)"); } diff --git a/src/main/java/autismclient/util/macro/MacroExecutor.java b/src/main/java/autismclient/util/macro/MacroExecutor.java index f959341..293e05d 100644 --- a/src/main/java/autismclient/util/macro/MacroExecutor.java +++ b/src/main/java/autismclient/util/macro/MacroExecutor.java @@ -135,12 +135,12 @@ public class MacroExecutor { private static volatile CompletableFuture activeBaritoneGoalFuture = null; static final AtomicReference BARITONE_OWNER = new AtomicReference<>(); - private static final AtomicBoolean isRotating = new AtomicBoolean(false); - private static final AtomicReference ROTATION_OWNER = new AtomicReference<>(); - private static final Object ROTATION_STATE_LOCK = new Object(); - private static volatile float targetYaw, targetPitch; - private static volatile double rotationSpeed; - private static volatile int rotationAlignedFrames; + static final AtomicBoolean isRotating = new AtomicBoolean(false); + static final AtomicReference ROTATION_OWNER = new AtomicReference<>(); + static final Object ROTATION_STATE_LOCK = new Object(); + static volatile float targetYaw, targetPitch; + static volatile double rotationSpeed; + static volatile int rotationAlignedFrames; private static final java.util.concurrent.CopyOnWriteArrayList persistentActions = new java.util.concurrent.CopyOnWriteArrayList<>(); @@ -659,7 +659,7 @@ public class MacroExecutor { PacketGateManager.clearOwnerAndFlushConfigured(run.id, mc == null ? null : mc.getConnection()); FpsLimitController.clear(run.id); PingSpoofController.clearMacro(run.id); - cancelRotation(run); + MacroRender.cancelRotation(run); MacroWorldActions.requestOwnedBaritoneStop(run, mc); Thread thread = run.macroThread; if (interruptThread && thread != null && thread != Thread.currentThread()) { @@ -1207,251 +1207,15 @@ public class MacroExecutor { } public static void onRender(float tickDelta) { - if (!isRotating.get()) return; - RunState owner = ROTATION_OWNER.get(); - if (owner != null && !owner.running.get()) { - cancelRotation(owner); - return; - } - if (owner == null && !isRunning.get()) { - synchronized (ROTATION_STATE_LOCK) { - if (ROTATION_OWNER.get() == null) isRotating.set(false); - } - return; - } - Minecraft mc = Minecraft.getInstance(); - if (mc.player == null) return; - - float currentYaw = mc.player.getYRot(); - float currentPitch = mc.player.getXRot(); - - double deltaYaw = net.minecraft.util.Mth.wrapDegrees(targetYaw - currentYaw); - double deltaPitch = targetPitch - currentPitch; - double diagonalDistance = Math.hypot(deltaYaw, deltaPitch); - if (diagonalDistance <= 0.35) { - mc.player.setYRot(targetYaw); - mc.player.setXRot(targetPitch); - rotationAlignedFrames++; - if (rotationAlignedFrames >= 2) { - synchronized (ROTATION_STATE_LOCK) { - if (ROTATION_OWNER.get() == owner) { - isRotating.set(false); - ROTATION_OWNER.set(null); - } - } - } - return; - } - rotationAlignedFrames = 0; - - double frameStep = Math.min(diagonalDistance, rotationSpeed * Math.max(tickDelta, 1.0f)); - double stepYaw = (deltaYaw / diagonalDistance) * frameStep; - double stepPitch = (deltaPitch / diagonalDistance) * frameStep; - - mc.player.setYRot(currentYaw + (float) stepYaw); - mc.player.setXRot(currentPitch + (float) stepPitch); + MacroRender.onRender(tickDelta); } public static boolean hasRenderWork() { - return isRotating.get(); - } - - static void startSmoothRotation(float yaw, float pitch, double speed) { - RunState run = CURRENT_RUN.get(); - while (run != null && run.running.get()) { - synchronized (ROTATION_STATE_LOCK) { - RunState owner = ROTATION_OWNER.get(); - if (owner == null || owner == run || !owner.running.get()) { - ROTATION_OWNER.set(run); - targetYaw = yaw; - targetPitch = pitch; - rotationSpeed = speed; - rotationAlignedFrames = 0; - isRotating.set(true); - return; - } - } - java.util.concurrent.locks.LockSupport.parkNanos(MACRO_WAIT_POLL_NANOS); - } - if (run == null) { - synchronized (ROTATION_STATE_LOCK) { - if (ROTATION_OWNER.get() != null) return; - targetYaw = yaw; - targetPitch = pitch; - rotationSpeed = speed; - rotationAlignedFrames = 0; - isRotating.set(true); - } - } - } - - static void waitForSmoothRotationCompletion() { - RunState run = CURRENT_RUN.get(); - while (isCurrentRunActive() && isRotating.get() - && (run == null || ROTATION_OWNER.get() == run)) { - java.util.concurrent.locks.LockSupport.parkNanos(1_000_000L); - } - } - - static void cancelRotation(RunState run) { - if (run == null) return; - synchronized (ROTATION_STATE_LOCK) { - if (ROTATION_OWNER.get() != run) return; - isRotating.set(false); - rotationAlignedFrames = 0; - ROTATION_OWNER.set(null); - } - } - - static LookAtBlockAction.RotationTarget resolveLookAtTargetOnClient(Minecraft mc, LookAtBlockAction action) { - if (mc == null || action == null) return null; - java.util.concurrent.atomic.AtomicReference targetRef = new java.util.concurrent.atomic.AtomicReference<>(); - java.util.concurrent.CountDownLatch latch = new java.util.concurrent.CountDownLatch(1); - mc.execute(() -> { - try { - targetRef.set(action.resolveRotationTarget(mc)); - } finally { - latch.countDown(); - } - }); - while (isCurrentRunActive()) { - try { - if (latch.await(10, java.util.concurrent.TimeUnit.MILLISECONDS)) { - break; - } - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - return targetRef.get(); + return MacroRender.hasRenderWork(); } public static void stopAll() { - stop(); + MacroRender.stopAll(); } - static void executeBurstPackets(Minecraft mc, java.util.List batch) { - if (mc.getConnection() == null) { - AutismClientMessaging.sendPrefixed("\u00a7cNo network connection for burst!"); - return; - } - - java.util.List allQueuedPackets = new java.util.ArrayList<>(); - for (SendPacketAction action : batch) { - allQueuedPackets.addAll(action.getPackets()); - } - - if (allQueuedPackets.isEmpty()) { - return; - } - - java.util.List> regeneratedPackets = new java.util.ArrayList<>(allQueuedPackets.size()); - for (autismclient.util.AutismSharedState.QueuedPacket qp : allQueuedPackets) { - if (qp.packet == null) continue; - regeneratedPackets.add(qp.packet); - } - - if (regeneratedPackets.isEmpty()) { - AutismClientMessaging.sendPrefixed("\u00a7cBurst: All packets failed to regenerate!"); - return; - } - - net.minecraft.client.multiplayer.ClientPacketListener handler = mc.getConnection(); - - for (Packet packet : regeneratedPackets) { - Packet toSend = packet; - - if (packet instanceof ServerboundContainerClickPacket) { - toSend = PacketRegenerator.regenerate(packet); - if (toSend == null) { - continue; - } - } - - handler.send(toSend); - } - - if (regeneratedPackets.size() > 1) { - AutismClientMessaging.sendPrefixed("\u00a7a Burst sent " + regeneratedPackets.size() + " packets"); - } - } - - static java.util.List> preGeneratePackets(AutismMacro macro, int startIdx, int count) { - java.util.List> result = new java.util.ArrayList<>(); - int maxIdx = count < 0 ? macro.actions.size() : Math.min(startIdx + count, macro.actions.size()); - return preGeneratePackets(macro, startIdx, count, maxIdx); - } - - static java.util.List> preGeneratePackets(AutismMacro macro, int startIdx, int count, int endIdx) { - java.util.List> result = new java.util.ArrayList<>(); - int maxIdx = Math.min(endIdx, count < 0 ? macro.actions.size() : Math.min(startIdx + count, macro.actions.size())); - - Minecraft mc = Minecraft.getInstance(); - - for (int j = startIdx; j < maxIdx; j++) { - MacroAction act = macro.actions.get(j); - if (!(act instanceof SendPacketAction)) break; - - SendPacketAction spa = (SendPacketAction) act; - for (autismclient.util.AutismSharedState.QueuedPacket qp : spa.getPackets()) { - if (qp.packet == null) continue; - - result.add(qp.packet); - } - } - - return result; - } - - static void executePreGeneratedBurst(Minecraft mc, java.util.List> packets) { - if (packets.isEmpty() || mc.getConnection() == null) return; - - net.minecraft.client.multiplayer.ClientPacketListener handler = mc.getConnection(); - - for (Packet p : packets) { - Packet toSend = p; - - if (p instanceof ServerboundContainerClickPacket) { - toSend = PacketRegenerator.regenerate(p); - if (toSend == null) { - continue; - } - } - - handler.send(toSend); - } - - AutismModule module = AutismModule.get(); - if (module.shouldForceChannelFlush()) { - try { - net.minecraft.network.Connection connection = handler.getConnection(); - if (connection != null) { - - connection.flushChannel(); - } - } catch (Exception e) { } - } - } - - static int countPreGeneratedActions(AutismMacro macro, int startIdx, int packetCount) { - return countPreGeneratedActions(macro, startIdx, packetCount, macro.actions.size()); - } - - static int countPreGeneratedActions(AutismMacro macro, int startIdx, int packetCount, int endIdx) { - int actionsToSkip = 0; - int packetsRemaining = packetCount; - - for (int j = startIdx; j < endIdx && packetsRemaining > 0; j++) { - MacroAction act = macro.actions.get(j); - if (!(act instanceof SendPacketAction)) break; - - SendPacketAction spa = (SendPacketAction) act; - int actionPackets = spa.getPackets().size(); - packetsRemaining -= actionPackets; - actionsToSkip++; - } - - return actionsToSkip; - } } diff --git a/src/main/java/autismclient/util/macro/MacroRender.java b/src/main/java/autismclient/util/macro/MacroRender.java new file mode 100644 index 0000000..550b5a5 --- /dev/null +++ b/src/main/java/autismclient/util/macro/MacroRender.java @@ -0,0 +1,262 @@ +package autismclient.util.macro; + +import autismclient.modules.AutismModule; +import autismclient.util.AutismClientMessaging; +import autismclient.util.AutismMacro; +import autismclient.util.PacketRegenerator; +import net.minecraft.client.Minecraft; +import net.minecraft.network.protocol.Packet; +import net.minecraft.network.protocol.game.ServerboundContainerClickPacket; + +final class MacroRender { + private MacroRender() { + } + public static void onRender(float tickDelta) { + if (!MacroExecutor.isRotating.get()) return; + MacroExecutor.RunState owner = MacroExecutor.ROTATION_OWNER.get(); + if (owner != null && !owner.running.get()) { + cancelRotation(owner); + return; + } + if (owner == null && !MacroExecutor.isRunning.get()) { + synchronized (MacroExecutor.ROTATION_STATE_LOCK) { + if (MacroExecutor.ROTATION_OWNER.get() == null) MacroExecutor.isRotating.set(false); + } + return; + } + Minecraft mc = Minecraft.getInstance(); + if (mc.player == null) return; + + float currentYaw = mc.player.getYRot(); + float currentPitch = mc.player.getXRot(); + + double deltaYaw = net.minecraft.util.Mth.wrapDegrees(MacroExecutor.targetYaw - currentYaw); + double deltaPitch = MacroExecutor.targetPitch - currentPitch; + double diagonalDistance = Math.hypot(deltaYaw, deltaPitch); + if (diagonalDistance <= 0.35) { + mc.player.setYRot(MacroExecutor.targetYaw); + mc.player.setXRot(MacroExecutor.targetPitch); + MacroExecutor.rotationAlignedFrames++; + if (MacroExecutor.rotationAlignedFrames >= 2) { + synchronized (MacroExecutor.ROTATION_STATE_LOCK) { + if (MacroExecutor.ROTATION_OWNER.get() == owner) { + MacroExecutor.isRotating.set(false); + MacroExecutor.ROTATION_OWNER.set(null); + } + } + } + return; + } + MacroExecutor.rotationAlignedFrames = 0; + + double frameStep = Math.min(diagonalDistance, MacroExecutor.rotationSpeed * Math.max(tickDelta, 1.0f)); + double stepYaw = (deltaYaw / diagonalDistance) * frameStep; + double stepPitch = (deltaPitch / diagonalDistance) * frameStep; + + mc.player.setYRot(currentYaw + (float) stepYaw); + mc.player.setXRot(currentPitch + (float) stepPitch); + } + + public static boolean hasRenderWork() { + return MacroExecutor.isRotating.get(); + } + + static void startSmoothRotation(float yaw, float pitch, double speed) { + MacroExecutor.RunState run = MacroExecutor.CURRENT_RUN.get(); + while (run != null && run.running.get()) { + synchronized (MacroExecutor.ROTATION_STATE_LOCK) { + MacroExecutor.RunState owner = MacroExecutor.ROTATION_OWNER.get(); + if (owner == null || owner == run || !owner.running.get()) { + MacroExecutor.ROTATION_OWNER.set(run); + MacroExecutor.targetYaw = yaw; + MacroExecutor.targetPitch = pitch; + MacroExecutor.rotationSpeed = speed; + MacroExecutor.rotationAlignedFrames = 0; + MacroExecutor.isRotating.set(true); + return; + } + } + java.util.concurrent.locks.LockSupport.parkNanos(MacroExecutor.MACRO_WAIT_POLL_NANOS); + } + if (run == null) { + synchronized (MacroExecutor.ROTATION_STATE_LOCK) { + if (MacroExecutor.ROTATION_OWNER.get() != null) return; + MacroExecutor.targetYaw = yaw; + MacroExecutor.targetPitch = pitch; + MacroExecutor.rotationSpeed = speed; + MacroExecutor.rotationAlignedFrames = 0; + MacroExecutor.isRotating.set(true); + } + } + } + + static void waitForSmoothRotationCompletion() { + MacroExecutor.RunState run = MacroExecutor.CURRENT_RUN.get(); + while (MacroExecutor.isCurrentRunActive() && MacroExecutor.isRotating.get() + && (run == null || MacroExecutor.ROTATION_OWNER.get() == run)) { + java.util.concurrent.locks.LockSupport.parkNanos(1_000_000L); + } + } + + static void cancelRotation(MacroExecutor.RunState run) { + if (run == null) return; + synchronized (MacroExecutor.ROTATION_STATE_LOCK) { + if (MacroExecutor.ROTATION_OWNER.get() != run) return; + MacroExecutor.isRotating.set(false); + MacroExecutor.rotationAlignedFrames = 0; + MacroExecutor.ROTATION_OWNER.set(null); + } + } + + static LookAtBlockAction.RotationTarget resolveLookAtTargetOnClient(Minecraft mc, LookAtBlockAction action) { + if (mc == null || action == null) return null; + java.util.concurrent.atomic.AtomicReference targetRef = new java.util.concurrent.atomic.AtomicReference<>(); + java.util.concurrent.CountDownLatch latch = new java.util.concurrent.CountDownLatch(1); + mc.execute(() -> { + try { + targetRef.set(action.resolveRotationTarget(mc)); + } finally { + latch.countDown(); + } + }); + while (MacroExecutor.isCurrentRunActive()) { + try { + if (latch.await(10, java.util.concurrent.TimeUnit.MILLISECONDS)) { + break; + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + break; + } + } + return targetRef.get(); + } + + public static void stopAll() { + MacroExecutor.stop(); + } + + static void executeBurstPackets(Minecraft mc, java.util.List batch) { + if (mc.getConnection() == null) { + AutismClientMessaging.sendPrefixed("\u00a7cNo network connection for burst!"); + return; + } + + java.util.List allQueuedPackets = new java.util.ArrayList<>(); + for (SendPacketAction action : batch) { + allQueuedPackets.addAll(action.getPackets()); + } + + if (allQueuedPackets.isEmpty()) { + return; + } + + java.util.List> regeneratedPackets = new java.util.ArrayList<>(allQueuedPackets.size()); + for (autismclient.util.AutismSharedState.QueuedPacket qp : allQueuedPackets) { + if (qp.packet == null) continue; + regeneratedPackets.add(qp.packet); + } + + if (regeneratedPackets.isEmpty()) { + AutismClientMessaging.sendPrefixed("\u00a7cBurst: All packets failed to regenerate!"); + return; + } + + net.minecraft.client.multiplayer.ClientPacketListener handler = mc.getConnection(); + + for (Packet packet : regeneratedPackets) { + Packet toSend = packet; + + if (packet instanceof ServerboundContainerClickPacket) { + toSend = PacketRegenerator.regenerate(packet); + if (toSend == null) { + continue; + } + } + + handler.send(toSend); + } + + if (regeneratedPackets.size() > 1) { + AutismClientMessaging.sendPrefixed("\u00a7a Burst sent " + regeneratedPackets.size() + " packets"); + } + } + + static java.util.List> preGeneratePackets(AutismMacro macro, int startIdx, int count) { + java.util.List> result = new java.util.ArrayList<>(); + int maxIdx = count < 0 ? macro.actions.size() : Math.min(startIdx + count, macro.actions.size()); + return preGeneratePackets(macro, startIdx, count, maxIdx); + } + + static java.util.List> preGeneratePackets(AutismMacro macro, int startIdx, int count, int endIdx) { + java.util.List> result = new java.util.ArrayList<>(); + int maxIdx = Math.min(endIdx, count < 0 ? macro.actions.size() : Math.min(startIdx + count, macro.actions.size())); + + Minecraft mc = Minecraft.getInstance(); + + for (int j = startIdx; j < maxIdx; j++) { + MacroAction act = macro.actions.get(j); + if (!(act instanceof SendPacketAction)) break; + + SendPacketAction spa = (SendPacketAction) act; + for (autismclient.util.AutismSharedState.QueuedPacket qp : spa.getPackets()) { + if (qp.packet == null) continue; + + result.add(qp.packet); + } + } + + return result; + } + + static void executePreGeneratedBurst(Minecraft mc, java.util.List> packets) { + if (packets.isEmpty() || mc.getConnection() == null) return; + + net.minecraft.client.multiplayer.ClientPacketListener handler = mc.getConnection(); + + for (Packet p : packets) { + Packet toSend = p; + + if (p instanceof ServerboundContainerClickPacket) { + toSend = PacketRegenerator.regenerate(p); + if (toSend == null) { + continue; + } + } + + handler.send(toSend); + } + + AutismModule module = AutismModule.get(); + if (module.shouldForceChannelFlush()) { + try { + net.minecraft.network.Connection connection = handler.getConnection(); + if (connection != null) { + + connection.flushChannel(); + } + } catch (Exception e) { } + } + } + + static int countPreGeneratedActions(AutismMacro macro, int startIdx, int packetCount) { + return countPreGeneratedActions(macro, startIdx, packetCount, macro.actions.size()); + } + + static int countPreGeneratedActions(AutismMacro macro, int startIdx, int packetCount, int endIdx) { + int actionsToSkip = 0; + int packetsRemaining = packetCount; + + for (int j = startIdx; j < endIdx && packetsRemaining > 0; j++) { + MacroAction act = macro.actions.get(j); + if (!(act instanceof SendPacketAction)) break; + + SendPacketAction spa = (SendPacketAction) act; + int actionPackets = spa.getPackets().size(); + packetsRemaining -= actionPackets; + actionsToSkip++; + } + + return actionsToSkip; + } +} diff --git a/src/main/java/autismclient/util/macro/MacroRunLoop.java b/src/main/java/autismclient/util/macro/MacroRunLoop.java index 4c4ec69..2c30f9c 100644 --- a/src/main/java/autismclient/util/macro/MacroRunLoop.java +++ b/src/main/java/autismclient/util/macro/MacroRunLoop.java @@ -214,7 +214,7 @@ final class MacroRunLoop { batchEnd++; } - MacroExecutor.executeBurstPackets(mc, packetBatch); + MacroRender.executeBurstPackets(mc, packetBatch); i = batchEnd - 1; continue; @@ -577,14 +577,14 @@ final class MacroRunLoop { } else if (action instanceof LookAtBlockAction) { LookAtBlockAction la = (LookAtBlockAction) action; - LookAtBlockAction.RotationTarget rotationTarget = MacroExecutor.resolveLookAtTargetOnClient(mc, la); + LookAtBlockAction.RotationTarget rotationTarget = MacroRender.resolveLookAtTargetOnClient(mc, la); if (rotationTarget == null) { continue; } if (la.smooth) { - MacroExecutor.startSmoothRotation(rotationTarget.yaw(), rotationTarget.pitch(), la.getRotationStep()); + MacroRender.startSmoothRotation(rotationTarget.yaw(), rotationTarget.pitch(), la.getRotationStep()); if (la.waitForCompletion) { - MacroExecutor.waitForSmoothRotationCompletion(); + MacroRender.waitForSmoothRotationCompletion(); } } else { mc.execute(() -> { @@ -752,7 +752,7 @@ final class MacroRunLoop { if (mc.level == null) continue; java.util.List> preGenerated = - MacroExecutor.preGeneratePackets(macro, i + 1, tsa.preGenCount); + MacroRender.preGeneratePackets(macro, i + 1, tsa.preGenCount); long targetTick = mc.level.getGameTime() + tsa.tickOffset; MacroExecutor.setCurrentStatus("Tick Sync -> " + targetTick + " (" + preGenerated.size() + " pkts)"); @@ -762,8 +762,8 @@ final class MacroRunLoop { } if (MacroExecutor.isCurrentRunActive()) { - MacroExecutor.executePreGeneratedBurst(mc, preGenerated); - i += MacroExecutor.countPreGeneratedActions(macro, i + 1, preGenerated.size()); + MacroRender.executePreGeneratedBurst(mc, preGenerated); + i += MacroRender.countPreGeneratedActions(macro, i + 1, preGenerated.size()); AutismClientMessaging.sendPrefixed("Tick sync: " + preGenerated.size() + " pkts sent"); } } @@ -773,7 +773,7 @@ final class MacroRunLoop { if (mc.player == null || mc.player.containerMenu == null) continue; java.util.List> preGenerated = - MacroExecutor.preGeneratePackets(macro, i + 1, rsa.preGenCount); + MacroRender.preGeneratePackets(macro, i + 1, rsa.preGenCount); int baseRevision = mc.player.containerMenu.getStateId(); int targetRevision = baseRevision + rsa.revisionOffset; @@ -786,8 +786,8 @@ final class MacroRunLoop { } if (MacroExecutor.isCurrentRunActive() && mc.player != null && mc.player.containerMenu != null) { - MacroExecutor.executePreGeneratedBurst(mc, preGenerated); - i += MacroExecutor.countPreGeneratedActions(macro, i + 1, preGenerated.size()); + MacroRender.executePreGeneratedBurst(mc, preGenerated); + i += MacroRender.countPreGeneratedActions(macro, i + 1, preGenerated.size()); AutismClientMessaging.sendPrefixed("Rev sync: " + preGenerated.size() + " pkts sent"); } } @@ -797,7 +797,7 @@ final class MacroRunLoop { if (mc.getConnection() == null) continue; java.util.List> preGenerated = - MacroExecutor.preGeneratePackets(macro, i + 1, stsa.preGenCount); + MacroRender.preGeneratePackets(macro, i + 1, stsa.preGenCount); int sampleWaitMs = 0; while (MacroExecutor.isCurrentRunActive() && !ServerTickTracker.isReady() && sampleWaitMs < stsa.maxWaitMs) { @@ -834,8 +834,8 @@ final class MacroRunLoop { } if (MacroExecutor.isCurrentRunActive()) { - MacroExecutor.executePreGeneratedBurst(mc, preGenerated); - i += MacroExecutor.countPreGeneratedActions(macro, i + 1, preGenerated.size()); + MacroRender.executePreGeneratedBurst(mc, preGenerated); + i += MacroRender.countPreGeneratedActions(macro, i + 1, preGenerated.size()); long actualDelay = (System.nanoTime() - optimalTime) / 1_000_000L; AutismClientMessaging.sendPrefixed("ServerSync: " + preGenerated.size() + " pkts (+" + actualDelay + "ms)"); } @@ -843,9 +843,9 @@ final class MacroRunLoop { else if (action instanceof RotateAction) { RotateAction ra = (RotateAction) action; if (ra.smooth) { - MacroExecutor.startSmoothRotation(ra.yaw, ra.pitch, ra.getRotationStep()); + MacroRender.startSmoothRotation(ra.yaw, ra.pitch, ra.getRotationStep()); if (ra.waitForCompletion) { - MacroExecutor.waitForSmoothRotationCompletion(); + MacroRender.waitForSmoothRotationCompletion(); } } else { mc.execute(() -> action.execute(mc)); @@ -855,14 +855,14 @@ final class MacroRunLoop { runPacedTpAction(mc, pacedTp); } else if (action instanceof LookAtBlockAction la) { - LookAtBlockAction.RotationTarget rotationTarget = MacroExecutor.resolveLookAtTargetOnClient(mc, la); + LookAtBlockAction.RotationTarget rotationTarget = MacroRender.resolveLookAtTargetOnClient(mc, la); if (rotationTarget == null) { continue; } if (la.smooth) { - MacroExecutor.startSmoothRotation(rotationTarget.yaw(), rotationTarget.pitch(), la.getRotationStep()); + MacroRender.startSmoothRotation(rotationTarget.yaw(), rotationTarget.pitch(), la.getRotationStep()); if (la.waitForCompletion) { - MacroExecutor.waitForSmoothRotationCompletion(); + MacroRender.waitForSmoothRotationCompletion(); } } else { mc.execute(() -> { @@ -1159,7 +1159,7 @@ final class MacroRunLoop { } PacketGateManager.clearOwnerAndFlushConfigured(runState.id, mc == null ? null : mc.getConnection()); MacroExecutor.finishCurrentRun(); - MacroExecutor.cancelRotation(runState); + MacroRender.cancelRotation(runState); if (!MacroExecutor.isRunning.get()) MacroConditionRegistry.cancelAll(); MacroExecutor.broadcastStepProgress(runState, -1, 0, "");