diff --git a/README.md b/README.md index 2f12ea02..5784d07f 100644 --- a/README.md +++ b/README.md @@ -49,10 +49,10 @@ To contribute to language packs, see the [`translations/`](translations/) direct | Locale | Covered | Percentage | | --- | ---: | ---: | -| `jp_jp` | 303/770 | 39.4% | -| `pr_us` | 253/770 | 32.9% | -| `pt_pt` | 477/770 | 61.9% | -| `ru_ru` | 492/770 | 63.9% | -| `test` | 516/770 | 67.0% | -| `zh_cn` | 508/770 | 66.0% | +| `jp_jp` | 303/778 | 38.9% | +| `pr_us` | 253/778 | 32.5% | +| `pt_pt` | 477/778 | 61.3% | +| `ru_ru` | 492/778 | 63.2% | +| `test` | 516/778 | 66.3% | +| `zh_cn` | 516/778 | 66.3% | diff --git a/src/main/java/dev/aether/bootstrap/AetherBootstrapHooks.java b/src/main/java/dev/aether/bootstrap/AetherBootstrapHooks.java index 642d8e18..5ed1920d 100644 --- a/src/main/java/dev/aether/bootstrap/AetherBootstrapHooks.java +++ b/src/main/java/dev/aether/bootstrap/AetherBootstrapHooks.java @@ -200,6 +200,13 @@ default boolean areParticlesDisabled() { default void onParticlePacket(Minecraft minecraft, ClientboundLevelParticlesPacket packet) { } + default void onPlayerPositionPacket(Minecraft minecraft, + net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket packet) { + } + + default void onTeleportCommand(String command) { + } + default void onStatsPacketReceived() { } } @@ -469,6 +476,15 @@ public static void onParticlePacket(Minecraft minecraft, ClientboundLevelParticl hooks.onParticlePacket(minecraft, packet); } + public static void onPlayerPositionPacket(Minecraft minecraft, + net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket packet) { + hooks.onPlayerPositionPacket(minecraft, packet); + } + + public static void onTeleportCommand(String command) { + hooks.onTeleportCommand(command); + } + public static void onStatsPacketReceived() { hooks.onStatsPacketReceived(); } diff --git a/src/main/java/dev/aether/config/AetherConfig.java b/src/main/java/dev/aether/config/AetherConfig.java index 98e20d5a..9865c123 100644 --- a/src/main/java/dev/aether/config/AetherConfig.java +++ b/src/main/java/dev/aether/config/AetherConfig.java @@ -895,6 +895,18 @@ private static boolean readBoolean(JsonObject root, String key, boolean fallback public static final FloatEntry FAILSAFE_DIRT_CHECK_TRIGGER_DELAY_SECONDS = Config .floatVal("failsafeDirtCheckTriggerDelaySeconds", 2.0f) .range(0.0f, 10.0f); + public static final BooleanEntry FAILSAFE_TELEPORT = Config.bool("failsafeTeleport", true); + public static final BooleanEntry FAILSAFE_TELEPORT_ONLY_DURING_FARMING = Config + .bool("failsafeTeleportOnlyDuringFarming", true); + public static final FloatEntry FAILSAFE_TELEPORT_HORIZONTAL_LIMIT = Config + .floatVal("failsafeTeleportHorizontalLimit", 6.0f).range(0.0f, 64.0f); + public static final FloatEntry FAILSAFE_TELEPORT_VERTICAL_LIMIT = Config + .floatVal("failsafeTeleportVerticalLimit", 0.5f).range(0.0f, 10.0f); + public static final FloatEntry FAILSAFE_TELEPORT_SETTLE_SECONDS = Config + .floatVal("failsafeTeleportSettleSeconds", 1.5f).range(0.0f, 30.0f); + public static final StringEntry FAILSAFE_TELEPORT_ACTION = Config.string("failsafeTeleportAction", "STOP"); + public static final StringEntry FAILSAFE_TELEPORT_CUSTOM_REPLAY = Config + .string("failsafeTeleportCustomReplay", "Random"); public static final BooleanEntry FAILSAFE_ROTATION = Config.bool("failsafeRotation", true); public static final BooleanEntry FAILSAFE_WORLD_CHANGE = Config.bool("failsafeWorldChange", true); public static final IntEntry FAILSAFE_ROTATION_PITCH_THRESHOLD = Config.integer("failsafeRotationPitchThreshold", 10) diff --git a/src/main/java/dev/aether/feature/LiveAetherBootstrapHooks.java b/src/main/java/dev/aether/feature/LiveAetherBootstrapHooks.java index 30d9a9ff..c898adfe 100644 --- a/src/main/java/dev/aether/feature/LiveAetherBootstrapHooks.java +++ b/src/main/java/dev/aether/feature/LiveAetherBootstrapHooks.java @@ -390,6 +390,17 @@ public void onParticlePacket(Minecraft minecraft, ClientboundLevelParticlesPacke PestTrackerAbility.onParticlePacket(minecraft, packet); } + @Override + public void onPlayerPositionPacket(Minecraft minecraft, + net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket packet) { + FailsafeManager.onPlayerPositionPacket(minecraft, packet); + } + + @Override + public void onTeleportCommand(String command) { + FailsafeManager.onTeleportCommand(command); + } + @Override public void onStatsPacketReceived() { PingTracker.onStatsReceived(); diff --git a/src/main/java/dev/aether/mixin/MixinTeleportFailsafe.java b/src/main/java/dev/aether/mixin/MixinTeleportFailsafe.java new file mode 100644 index 00000000..139759b9 --- /dev/null +++ b/src/main/java/dev/aether/mixin/MixinTeleportFailsafe.java @@ -0,0 +1,25 @@ +package dev.aether.mixin; + +import dev.aether.bootstrap.AetherBootstrapHooks; +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientPacketListener; +import net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(ClientPacketListener.class) +public class MixinTeleportFailsafe { + @Inject(method = "handleMovePlayer", at = @At(value = "INVOKE", + target = "Lnet/minecraft/network/protocol/PacketUtils;ensureRunningOnSameThread(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketListener;Lnet/minecraft/network/PacketProcessor;)V", + shift = At.Shift.AFTER)) + private void aether$onServerPosition(ClientboundPlayerPositionPacket packet, CallbackInfo ci) { + AetherBootstrapHooks.onPlayerPositionPacket(Minecraft.getInstance(), packet); + } + + @Inject(method = "sendCommand", at = @At("HEAD")) + private void aether$onTeleportCommand(String command, CallbackInfo ci) { + AetherBootstrapHooks.onTeleportCommand(command); + } +} diff --git a/src/main/java/dev/aether/modules/failsafe/FailsafeCustomReplayManager.java b/src/main/java/dev/aether/modules/failsafe/FailsafeCustomReplayManager.java index 101a3f67..b88f5e84 100644 --- a/src/main/java/dev/aether/modules/failsafe/FailsafeCustomReplayManager.java +++ b/src/main/java/dev/aether/modules/failsafe/FailsafeCustomReplayManager.java @@ -34,6 +34,7 @@ public enum FailsafeReplayType { BPS("bps", AetherConfig.FAILSAFE_BPS_CUSTOM_REPLAY), GHOST_BLOCK("ghost_block", AetherConfig.FAILSAFE_GHOST_BLOCK_CUSTOM_REPLAY), DIRT_CHECK("dirt_check", AetherConfig.FAILSAFE_DIRT_CHECK_CUSTOM_REPLAY), + TELEPORT("teleport", AetherConfig.FAILSAFE_TELEPORT_CUSTOM_REPLAY), ROTATION("rotation", AetherConfig.FAILSAFE_ROTATION_CUSTOM_REPLAY), PEST_ROTATION("pest_rotation", AetherConfig.FAILSAFE_PEST_ROTATION_CUSTOM_REPLAY), WORLD_CHANGE("world_change", AetherConfig.FAILSAFE_WORLD_CHANGE_CUSTOM_REPLAY); diff --git a/src/main/java/dev/aether/modules/failsafe/FailsafeManager.java b/src/main/java/dev/aether/modules/failsafe/FailsafeManager.java index 0770b2c3..9194274b 100644 --- a/src/main/java/dev/aether/modules/failsafe/FailsafeManager.java +++ b/src/main/java/dev/aether/modules/failsafe/FailsafeManager.java @@ -58,6 +58,7 @@ public static void reset() { GhostBlockFailsafe.reset(); DirtFailsafe.reset(); RotationFailsafe.reset(); + TeleportFailsafe.reset(); FailsafeTestManager.reset(); } @@ -97,6 +98,17 @@ public static void tick(Minecraft client) { RotationFailsafe.tick(client); } + public static void onTeleportCommand(String command) { + TeleportFailsafe.onCommand(command); + } + + public static void onPlayerPositionPacket(Minecraft client, + net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket packet) { + if (!shouldSuspendRuntimeFailsafes()) { + TeleportFailsafe.onPositionPacket(client, packet); + } + } + public static void onBlockBreak() { if (shouldSuspendRuntimeFailsafes()) { return; diff --git a/src/main/java/dev/aether/modules/failsafe/SelfTeleportWindow.java b/src/main/java/dev/aether/modules/failsafe/SelfTeleportWindow.java new file mode 100644 index 00000000..cb937301 --- /dev/null +++ b/src/main/java/dev/aether/modules/failsafe/SelfTeleportWindow.java @@ -0,0 +1,26 @@ +package dev.aether.modules.failsafe; + +final class SelfTeleportWindow { + private static final long RESPONSE_TIMEOUT_MS = 10_000L; + private long requestedAt = Long.MIN_VALUE; + private long receivedAt = Long.MIN_VALUE; + + synchronized void reset() { + requestedAt = Long.MIN_VALUE; + receivedAt = Long.MIN_VALUE; + } + + synchronized void request(long now) { + requestedAt = now; + receivedAt = Long.MIN_VALUE; + } + + synchronized boolean onPositionPacket(long now, long settleMs) { + if (TeleportDetection.isSettling(now, requestedAt, RESPONSE_TIMEOUT_MS)) { + requestedAt = Long.MIN_VALUE; + receivedAt = now; + return true; + } + return TeleportDetection.isSettling(now, receivedAt, settleMs); + } +} diff --git a/src/main/java/dev/aether/modules/failsafe/TeleportDetection.java b/src/main/java/dev/aether/modules/failsafe/TeleportDetection.java new file mode 100644 index 00000000..e852cbcb --- /dev/null +++ b/src/main/java/dev/aether/modules/failsafe/TeleportDetection.java @@ -0,0 +1,53 @@ +package dev.aether.modules.failsafe; + +import dev.aether.macro.MacroState; +import net.minecraft.world.entity.PositionMoveRotation; +import net.minecraft.world.entity.Relative; +import net.minecraft.world.phys.Vec3; + +import java.util.Locale; +import java.util.Set; + +final class TeleportDetection { + private TeleportDetection() {} + + static boolean shouldMonitor(MacroState.State state, boolean onlyDuringFarming, + boolean farming, boolean pestBusy, boolean inGarden) { + if (state == MacroState.State.OFF || state == MacroState.State.RECOVERING) return false; + return !onlyDuringFarming || (state == MacroState.State.FARMING && farming && !pestBusy && inGarden); + } + + static Vec3 displacement(PositionMoveRotation current, PositionMoveRotation change, Set relatives) { + return PositionMoveRotation.calculateAbsolute(current, change, relatives).position().subtract(current.position()); + } + + static boolean isUnexpected(Vec3 displacement, double horizontalLimit, double verticalLimit, String heldItem) { + if (!Double.isFinite(displacement.x) || !Double.isFinite(displacement.y) || !Double.isFinite(displacement.z) + || displacement.lengthSqr() == 0.0) { + return false; + } + if (Math.abs(displacement.y) >= verticalLimit) { + return true; + } + if (Math.hypot(displacement.x, displacement.z) <= horizontalLimit) { + return false; + } + return !heldItem.contains("Aspect of the") && !heldItem.contains("Hyperion") + && !heldItem.contains("Scylla") && !heldItem.contains("Valk") && !heldItem.contains("Astr"); + } + + static boolean isTeleportCommand(String command) { + if (command == null) return false; + String normalized = command.strip().toLowerCase(Locale.ROOT); + if (normalized.startsWith("/")) normalized = normalized.substring(1); + String verb = normalized.split("\\s+", 2)[0]; + return switch (verb) { + case "warp", "warpgarden", "garden", "plottp", "tptoplot", "hub", "lobby", "l", "skyblock", "visit" -> true; + default -> false; + }; + } + + static boolean isSettling(long now, long lastTeleport, long settleMs) { + return lastTeleport != Long.MIN_VALUE && now >= lastTeleport && now - lastTeleport < settleMs; + } +} diff --git a/src/main/java/dev/aether/modules/failsafe/TeleportFailsafe.java b/src/main/java/dev/aether/modules/failsafe/TeleportFailsafe.java new file mode 100644 index 00000000..919f7ead --- /dev/null +++ b/src/main/java/dev/aether/modules/failsafe/TeleportFailsafe.java @@ -0,0 +1,72 @@ +package dev.aether.modules.failsafe; + +import dev.aether.config.AetherConfig; +import dev.aether.macro.MacroState; +import dev.aether.macro.MacroStateManager; +import dev.aether.macro.farming.FarmingMacroManager; +import dev.aether.modules.pest.helpers.PestAotvManager; +import dev.aether.modules.pest.helpers.PestDestroyer; +import dev.aether.modules.pest.helpers.PestOnTheTrackManager; +import dev.aether.modules.session.RestartManager; +import dev.aether.notification.NotificationManager; +import dev.aether.util.ClientUtils; +import net.minecraft.client.Minecraft; +import net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket; +import net.minecraft.world.entity.PositionMoveRotation; +import net.minecraft.world.phys.Vec3; + +import java.util.Locale; + +final class TeleportFailsafe { + private static final SelfTeleportWindow SELF_TELEPORT = new SelfTeleportWindow(); + + private TeleportFailsafe() {} + + static void reset() { + SELF_TELEPORT.reset(); + } + + static void onCommand(String command) { + if (TeleportDetection.isTeleportCommand(command)) { + SELF_TELEPORT.request(System.currentTimeMillis()); + } + } + + static void onPositionPacket(Minecraft client, ClientboundPlayerPositionPacket packet) { + if (client == null || client.player == null || client.level == null || packet.change() == null) { + return; + } + long settleMs = Math.round(AetherConfig.FAILSAFE_TELEPORT_SETTLE_SECONDS.get() * 1000.0f); + if (SELF_TELEPORT.onPositionPacket(System.currentTimeMillis(), settleMs)) return; + + if (!AetherConfig.FAILSAFE_TELEPORT.get() + || RestartManager.isRestartSequenceActive()) { + return; + } + boolean farming = FarmingMacroManager.isActive() + && FarmingMacroManager.getActiveMacro().isFarmingState(); + boolean pestBusy = PestDestroyer.isActive() || PestAotvManager.isPreparationAotvActive() + || PestOnTheTrackManager.getInstance().isBlockingFarming(); + if (!TeleportDetection.shouldMonitor(MacroStateManager.getCurrentState(), + AetherConfig.FAILSAFE_TELEPORT_ONLY_DURING_FARMING.get(), farming, pestBusy, + ClientUtils.getCurrentLocation() == MacroState.Location.GARDEN)) { + return; + } + Vec3 displacement = TeleportDetection.displacement( + PositionMoveRotation.of(client.player), packet.change(), packet.relatives()); + if (!TeleportDetection.isUnexpected(displacement, + AetherConfig.FAILSAFE_TELEPORT_HORIZONTAL_LIMIT.get(), + AetherConfig.FAILSAFE_TELEPORT_VERTICAL_LIMIT.get(), + client.player.getMainHandItem().getHoverName().getString())) { + return; + } + + FailsafeAction action = FailsafeAction.fromConfig(AetherConfig.FAILSAFE_TELEPORT_ACTION.get()); + String details = String.format(Locale.US, + "Unexpected teleport (horizontal %.2f blocks, vertical %.2f blocks).", + Math.hypot(displacement.x, displacement.z), Math.abs(displacement.y)); + NotificationManager.error(FailsafeManager.getNotificationTitle(action), details); + FailsafeManager.handleConfiguredAction(client, action, + FailsafeCustomReplayManager.FailsafeReplayType.TELEPORT, details, "TeleportFailsafe: " + details); + } +} diff --git a/src/main/java/dev/aether/ui/providers/failsafes/TeleportFailsafeRegistryProvider.java b/src/main/java/dev/aether/ui/providers/failsafes/TeleportFailsafeRegistryProvider.java new file mode 100644 index 00000000..ee8b218a --- /dev/null +++ b/src/main/java/dev/aether/ui/providers/failsafes/TeleportFailsafeRegistryProvider.java @@ -0,0 +1,56 @@ +package dev.aether.ui.providers.failsafes; + +import dev.aether.config.AetherConfig; +import dev.aether.modules.failsafe.FailsafeCustomReplayManager.FailsafeReplayType; +import dev.aether.ui.MainGUIRegistry; +import dev.aether.ui.providers.base.AbstractFailsafesRegistryProvider; +import dev.aether.ui.settings.ModulesTab; +import dev.aether.ui.settings.SettingGroup; +import dev.aether.ui.settings.SliderSetting; +import dev.aether.ui.settings.ToggleSetting; + +import java.util.List; + +public final class TeleportFailsafeRegistryProvider extends AbstractFailsafesRegistryProvider { + public TeleportFailsafeRegistryProvider() { + super(8); + } + + @Override + protected ModulesTab.SubTab createSubTab() { + SettingGroup group = SettingGroup.alwaysOn("Teleport Failsafe", + "Detects unexpected server position changes during macro operation; ignores requested teleports") + .add(new ToggleSetting("Only During Farming", + () -> AetherConfig.FAILSAFE_TELEPORT_ONLY_DURING_FARMING.get(), value -> { + AetherConfig.FAILSAFE_TELEPORT_ONLY_DURING_FARMING.set(value); + AetherConfig.save(); + })) + .add(FailsafeActionSettings.createActionDropdown("Teleport Action", + () -> AetherConfig.FAILSAFE_TELEPORT_ACTION.get(), + value -> AetherConfig.FAILSAFE_TELEPORT_ACTION.set(value))) + .add(FailsafeActionSettings.createCustomReplayDropdown(FailsafeReplayType.TELEPORT, + () -> AetherConfig.FAILSAFE_TELEPORT_ACTION.get())) + .add(new SliderSetting("Horizontal Ignore Limit", 0, 64, + () -> AetherConfig.FAILSAFE_TELEPORT_HORIZONTAL_LIMIT.get(), value -> { + AetherConfig.FAILSAFE_TELEPORT_HORIZONTAL_LIMIT.set(Math.round(value * 2) / 2.0f); + AetherConfig.save(); + }).withDecimals(1)) + .add(new SliderSetting("Vertical Ignore Limit", 0, 10, + () -> AetherConfig.FAILSAFE_TELEPORT_VERTICAL_LIMIT.get(), value -> { + AetherConfig.FAILSAFE_TELEPORT_VERTICAL_LIMIT.set(Math.round(value * 2) / 2.0f); + AetherConfig.save(); + }).withDecimals(1)) + .add(new SliderSetting("Teleport Settle Time", 0, 30, + () -> AetherConfig.FAILSAFE_TELEPORT_SETTLE_SECONDS.get(), value -> { + AetherConfig.FAILSAFE_TELEPORT_SETTLE_SECONDS.set(Math.round(value * 2) / 2.0f); + AetherConfig.save(); + }).withDecimals(1).withSuffix("s")); + + return MainGUIRegistry.toggleSubTab("Teleport", + "Detects unexpected server position changes during macro operation; ignores requested teleports", + () -> AetherConfig.FAILSAFE_TELEPORT.get(), value -> { + AetherConfig.FAILSAFE_TELEPORT.set(value); + AetherConfig.save(); + }, List.of(group)); + } +} diff --git a/src/main/resources/META-INF/services/dev.aether.ui.MainGUIRegistryProvider b/src/main/resources/META-INF/services/dev.aether.ui.MainGUIRegistryProvider index 035ba406..8c389a01 100644 --- a/src/main/resources/META-INF/services/dev.aether.ui.MainGUIRegistryProvider +++ b/src/main/resources/META-INF/services/dev.aether.ui.MainGUIRegistryProvider @@ -34,6 +34,7 @@ dev.aether.ui.providers.failsafes.GhostBlockFailsafeRegistryProvider dev.aether.ui.providers.failsafes.DirtFailsafeRegistryProvider dev.aether.ui.providers.failsafes.RotationFailsafeRegistryProvider dev.aether.ui.providers.failsafes.WorldChangeFailsafeRegistryProvider +dev.aether.ui.providers.failsafes.TeleportFailsafeRegistryProvider dev.aether.ui.providers.colors.HudColorsRegistryProvider dev.aether.ui.providers.colors.MenuColorsRegistryProvider dev.aether.ui.providers.settings.AccountSettingsRegistryProvider diff --git a/src/main/resources/aether.mixins.json b/src/main/resources/aether.mixins.json index 24a5d61e..a3f1a56e 100644 --- a/src/main/resources/aether.mixins.json +++ b/src/main/resources/aether.mixins.json @@ -37,6 +37,7 @@ "MixinKeyboardHandler", "MixinKeyboardInput", "MixinClientPacketListener", + "MixinTeleportFailsafe", "MixinClientPacketListenerPing", "MixinClientSuggestionProvider", "MixinConnectionChannelInitializer", diff --git a/src/main/resources/assets/aether/lang/en_us.json b/src/main/resources/assets/aether/lang/en_us.json index e71c40fe..f671954d 100644 --- a/src/main/resources/assets/aether/lang/en_us.json +++ b/src/main/resources/assets/aether/lang/en_us.json @@ -768,5 +768,13 @@ "text.aether.twilight": "Twilight", "text.aether.aurora": "Aurora", "text.aether.starfield": "Starfield", - "text.aether.animated_shader_skies": "Animated shader skies" + "text.aether.animated_shader_skies": "Animated shader skies", + "text.aether.teleport": "Teleport", + "text.aether.teleport_failsafe": "Teleport Failsafe", + "text.aether.teleport_action": "Teleport Action", + "text.aether.horizontal_ignore_limit": "Horizontal Ignore Limit", + "text.aether.vertical_ignore_limit": "Vertical Ignore Limit", + "text.aether.teleport_settle_time": "Teleport Settle Time", + "text.aether.only_during_farming": "Only During Farming", + "text.aether.detects_unexpected_server_position_changes_during_macro_operation_ignores_requested_teleports": "Detects unexpected server position changes during macro operation; ignores requested teleports" } diff --git a/src/test/java/dev/aether/modules/failsafe/SelfTeleportWindowTest.java b/src/test/java/dev/aether/modules/failsafe/SelfTeleportWindowTest.java new file mode 100644 index 00000000..002b1c82 --- /dev/null +++ b/src/test/java/dev/aether/modules/failsafe/SelfTeleportWindowTest.java @@ -0,0 +1,48 @@ +package dev.aether.modules.failsafe; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class SelfTeleportWindowTest { + @Test + void doesNotSuppressUnrequestedTeleports() { + assertFalse(new SelfTeleportWindow().onPositionPacket(1000, 1500)); + } + + @Test + void allowsSlowRewarpResponseAndTimesSettlePeriodFromArrival() { + SelfTeleportWindow window = new SelfTeleportWindow(); + window.request(1000); + assertTrue(window.onPositionPacket(4000, 1500)); + assertTrue(window.onPositionPacket(5499, 1500)); + assertFalse(window.onPositionPacket(5500, 1500)); + } + + @Test + void correctionsDoNotExtendSettlePeriodIndefinitely() { + SelfTeleportWindow window = new SelfTeleportWindow(); + window.request(1000); + assertTrue(window.onPositionPacket(1100, 1500)); + assertTrue(window.onPositionPacket(2599, 1500)); + assertFalse(window.onPositionPacket(2600, 1500)); + } + + @Test + void zeroSettleStillAllowsRequestedTeleportButNoFollowupCorrections() { + SelfTeleportWindow window = new SelfTeleportWindow(); + window.request(1000); + assertTrue(window.onPositionPacket(1100, 0)); + assertFalse(window.onPositionPacket(1101, 0)); + } + + @Test + void failedCommandAndResetDoNotLeaveProtectionDisabled() { + SelfTeleportWindow window = new SelfTeleportWindow(); + window.request(1000); + assertFalse(window.onPositionPacket(11000, 1500)); + window.request(12000); + window.reset(); + assertFalse(window.onPositionPacket(12001, 1500)); + } +} diff --git a/src/test/java/dev/aether/modules/failsafe/TeleportDetectionTest.java b/src/test/java/dev/aether/modules/failsafe/TeleportDetectionTest.java new file mode 100644 index 00000000..2e393b58 --- /dev/null +++ b/src/test/java/dev/aether/modules/failsafe/TeleportDetectionTest.java @@ -0,0 +1,108 @@ +package dev.aether.modules.failsafe; + +import dev.aether.macro.MacroState; +import net.minecraft.world.entity.PositionMoveRotation; +import net.minecraft.world.entity.Relative; +import net.minecraft.world.phys.Vec3; +import org.junit.jupiter.api.Test; + +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.*; + +class TeleportDetectionTest { + private static final String HOE = "Blessed Melon Dicer"; + + @Test + void farmingOnlyModeKeepsProtectionRestrictedToActiveFarmingInGarden() { + assertTrue(TeleportDetection.shouldMonitor(MacroState.State.FARMING, true, true, false, true)); + assertFalse(TeleportDetection.shouldMonitor(MacroState.State.FARMING, true, false, false, true)); + assertFalse(TeleportDetection.shouldMonitor(MacroState.State.FARMING, true, true, true, true)); + assertFalse(TeleportDetection.shouldMonitor(MacroState.State.FARMING, true, true, false, false)); + for (MacroState.State state : MacroState.State.values()) { + if (state != MacroState.State.FARMING) { + assertFalse(TeleportDetection.shouldMonitor(state, true, true, false, true), state.name()); + } + } + } + + @Test + void disablingFarmingOnlyModeMonitorsOtherRunningMacroPhases() { + for (MacroState.State state : MacroState.State.values()) { + boolean expected = state != MacroState.State.OFF && state != MacroState.State.RECOVERING; + assertEquals(expected, TeleportDetection.shouldMonitor(state, false, false, true, false), state.name()); + } + } + + @Test + void resolvesAbsolutePositionBeforeMeasuringDistance() { + Vec3 delta = TeleportDetection.displacement(pose(100, 70, -100), pose(120, 70, -100), Set.of()); + assertEquals(new Vec3(20, 0, 0), delta); + } + + @Test + void resolvesRelativeAxesIndependently() { + Vec3 delta = TeleportDetection.displacement(pose(100, 70, -100), pose(2, 71, -3), + Set.of(Relative.X, Relative.Z)); + assertEquals(new Vec3(2, 1, -3), delta); + } + + @Test + void doesNotMistakeFullyRelativeCoordinatesForAnAbsoluteTeleport() { + Vec3 delta = TeleportDetection.displacement(pose(200, 70, -200), pose(0.5, 0, -0.5), + Set.of(Relative.X, Relative.Y, Relative.Z)); + assertFalse(TeleportDetection.isUnexpected(delta, 6, 0.5, HOE)); + } + + @Test + void ignoresCorrectionsAtHorizontalLimitButDetectsDiagonalAndAxisAlignedTeleports() { + assertFalse(TeleportDetection.isUnexpected(new Vec3(6, 0, 0), 6, 0.5, HOE)); + assertFalse(TeleportDetection.isUnexpected(new Vec3(3, 0, 3), 6, 0.5, HOE)); + assertTrue(TeleportDetection.isUnexpected(new Vec3(6.01, 0, 0), 6, 0.5, HOE)); + assertTrue(TeleportDetection.isUnexpected(new Vec3(5, 0, 5), 6, 0.5, HOE)); + } + + @Test + void detectsVerticalMovesInBothDirectionsEvenInsideHorizontalLimit() { + assertTrue(TeleportDetection.isUnexpected(new Vec3(1, 0.5, 0), 6, 0.5, HOE)); + assertTrue(TeleportDetection.isUnexpected(new Vec3(0, -0.5, 0), 6, 0.5, HOE)); + assertFalse(TeleportDetection.isUnexpected(new Vec3(1, 0.49, 0), 6, 0.5, HOE)); + } + + @Test + void preservesFrostTeleportWeaponExceptionForHorizontalMoves() { + for (String weapon : new String[]{"Heroic Aspect of the Void", "Hyperion", "Scylla", "Valkyrie", "Astraea"}) { + assertFalse(TeleportDetection.isUnexpected(new Vec3(20, 0, 0), 6, 0.5, weapon)); + assertTrue(TeleportDetection.isUnexpected(new Vec3(20, 1, 0), 6, 0.5, weapon)); + } + } + + @Test + void rotationOnlyPacketsDoNotTriggerEvenWithZeroLimits() { + assertFalse(TeleportDetection.isUnexpected(Vec3.ZERO, 0, 0, HOE)); + assertFalse(TeleportDetection.isUnexpected(new Vec3(Double.NaN, 0, 0), 6, 0.5, HOE)); + } + + @Test + void marksOnlyCommandsThatRequestATeleport() { + for (String command : new String[]{"/warp garden", "plottp 6", " /TpToPlot 2 ", "hub", "lobby", "visit Player"}) { + assertTrue(TeleportDetection.isTeleportCommand(command), command); + } + for (String command : new String[]{"setspawn", "sethome", "warpdrive", "chat warp garden", "", " "}) { + assertFalse(TeleportDetection.isTeleportCommand(command), command); + } + assertFalse(TeleportDetection.isTeleportCommand(null)); + } + + @Test + void gracePeriodExpiresAndNeverSuppressesBeforeTheFirstCommand() { + assertFalse(TeleportDetection.isSettling(1000, Long.MIN_VALUE, 1500)); + assertTrue(TeleportDetection.isSettling(2499, 1000, 1500)); + assertFalse(TeleportDetection.isSettling(2500, 1000, 1500)); + assertFalse(TeleportDetection.isSettling(1000, 1000, 0)); + } + + private static PositionMoveRotation pose(double x, double y, double z) { + return new PositionMoveRotation(new Vec3(x, y, z), Vec3.ZERO, 0, 0); + } +} diff --git a/src/test/java/dev/aether/modules/failsafe/TeleportPacketHookTest.java b/src/test/java/dev/aether/modules/failsafe/TeleportPacketHookTest.java new file mode 100644 index 00000000..3dd364a9 --- /dev/null +++ b/src/test/java/dev/aether/modules/failsafe/TeleportPacketHookTest.java @@ -0,0 +1,49 @@ +package dev.aether.modules.failsafe; + +import org.junit.jupiter.api.Test; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; + +import java.io.InputStream; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.junit.jupiter.api.Assertions.*; + +class TeleportPacketHookTest { + @Test + void minecraftHasTheRequiredMainThreadPositionHookAndCommandMethod() throws Exception { + AtomicBoolean positionHook = new AtomicBoolean(); + AtomicBoolean commandHook = new AtomicBoolean(); + try (InputStream stream = getClass().getResourceAsStream( + "/net/minecraft/client/multiplayer/ClientPacketListener.class")) { + assertNotNull(stream); + new ClassReader(stream).accept(new ClassVisitor(Opcodes.ASM9) { + @Override + public MethodVisitor visitMethod(int access, String name, String descriptor, + String signature, String[] exceptions) { + if (name.equals("sendCommand") && descriptor.equals("(Ljava/lang/String;)V")) { + commandHook.set(true); + } + if (!name.equals("handleMovePlayer") || !descriptor.equals( + "(Lnet/minecraft/network/protocol/game/ClientboundPlayerPositionPacket;)V")) return null; + return new MethodVisitor(Opcodes.ASM9) { + @Override + public void visitMethodInsn(int opcode, String owner, String name, String descriptor, + boolean isInterface) { + if (owner.equals("net/minecraft/network/protocol/PacketUtils") + && name.equals("ensureRunningOnSameThread") + && descriptor.equals("(Lnet/minecraft/network/protocol/Packet;" + + "Lnet/minecraft/network/PacketListener;Lnet/minecraft/network/PacketProcessor;)V")) { + positionHook.set(true); + } + } + }; + } + }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES); + } + assertTrue(positionHook.get(), "Position packet injection target must match this Minecraft version"); + assertTrue(commandHook.get(), "Teleport command injection target must match this Minecraft version"); + } +} diff --git a/translations/en_us.json b/translations/en_us.json index e71c40fe..f671954d 100644 --- a/translations/en_us.json +++ b/translations/en_us.json @@ -768,5 +768,13 @@ "text.aether.twilight": "Twilight", "text.aether.aurora": "Aurora", "text.aether.starfield": "Starfield", - "text.aether.animated_shader_skies": "Animated shader skies" + "text.aether.animated_shader_skies": "Animated shader skies", + "text.aether.teleport": "Teleport", + "text.aether.teleport_failsafe": "Teleport Failsafe", + "text.aether.teleport_action": "Teleport Action", + "text.aether.horizontal_ignore_limit": "Horizontal Ignore Limit", + "text.aether.vertical_ignore_limit": "Vertical Ignore Limit", + "text.aether.teleport_settle_time": "Teleport Settle Time", + "text.aether.only_during_farming": "Only During Farming", + "text.aether.detects_unexpected_server_position_changes_during_macro_operation_ignores_requested_teleports": "Detects unexpected server position changes during macro operation; ignores requested teleports" } diff --git a/translations/jp_jp.json b/translations/jp_jp.json index f31ba670..9af129f9 100644 --- a/translations/jp_jp.json +++ b/translations/jp_jp.json @@ -768,5 +768,13 @@ "text.aether.your_aether_logo_cape_with_vanilla_movement": "Your Aether logo cape with vanilla movement", "text.aether.matcha": "Matcha", "text.aether.reverse_direction_after_rewarp": "Reverse Direction After Rewarp", - "text.aether.walk_mode": "Walk Mode" + "text.aether.walk_mode": "Walk Mode", + "text.aether.teleport": "Teleport", + "text.aether.teleport_failsafe": "Teleport Failsafe", + "text.aether.teleport_action": "Teleport Action", + "text.aether.horizontal_ignore_limit": "Horizontal Ignore Limit", + "text.aether.vertical_ignore_limit": "Vertical Ignore Limit", + "text.aether.teleport_settle_time": "Teleport Settle Time", + "text.aether.only_during_farming": "Only During Farming", + "text.aether.detects_unexpected_server_position_changes_during_macro_operation_ignores_requested_teleports": "Detects unexpected server position changes during macro operation; ignores requested teleports" } diff --git a/translations/pr_us.json b/translations/pr_us.json index 345f09c2..407a1d5e 100644 --- a/translations/pr_us.json +++ b/translations/pr_us.json @@ -768,5 +768,13 @@ "text.aether.your_aether_logo_cape_with_vanilla_movement": "Your Aether logo cape with vanilla movement", "text.aether.matcha": "Matcha", "text.aether.reverse_direction_after_rewarp": "Reverse Direction After Rewarp", - "text.aether.walk_mode": "Walk Mode" + "text.aether.walk_mode": "Walk Mode", + "text.aether.teleport": "Teleport", + "text.aether.teleport_failsafe": "Teleport Failsafe", + "text.aether.teleport_action": "Teleport Action", + "text.aether.horizontal_ignore_limit": "Horizontal Ignore Limit", + "text.aether.vertical_ignore_limit": "Vertical Ignore Limit", + "text.aether.teleport_settle_time": "Teleport Settle Time", + "text.aether.only_during_farming": "Only During Farming", + "text.aether.detects_unexpected_server_position_changes_during_macro_operation_ignores_requested_teleports": "Detects unexpected server position changes during macro operation; ignores requested teleports" } diff --git a/translations/pt_pt.json b/translations/pt_pt.json index bedd2fce..14847eb2 100644 --- a/translations/pt_pt.json +++ b/translations/pt_pt.json @@ -768,5 +768,13 @@ "text.aether.your_aether_logo_cape_with_vanilla_movement": "Your Aether logo cape with vanilla movement", "text.aether.matcha": "Matcha", "text.aether.reverse_direction_after_rewarp": "Reverse Direction After Rewarp", - "text.aether.walk_mode": "Walk Mode" + "text.aether.walk_mode": "Walk Mode", + "text.aether.teleport": "Teleport", + "text.aether.teleport_failsafe": "Teleport Failsafe", + "text.aether.teleport_action": "Teleport Action", + "text.aether.horizontal_ignore_limit": "Horizontal Ignore Limit", + "text.aether.vertical_ignore_limit": "Vertical Ignore Limit", + "text.aether.teleport_settle_time": "Teleport Settle Time", + "text.aether.only_during_farming": "Only During Farming", + "text.aether.detects_unexpected_server_position_changes_during_macro_operation_ignores_requested_teleports": "Detects unexpected server position changes during macro operation; ignores requested teleports" } diff --git a/translations/ru_ru.json b/translations/ru_ru.json index 3e8e1000..f7f97a70 100644 --- a/translations/ru_ru.json +++ b/translations/ru_ru.json @@ -768,5 +768,13 @@ "text.aether.your_aether_logo_cape_with_vanilla_movement": "Your Aether logo cape with vanilla movement", "text.aether.matcha": "Matcha", "text.aether.reverse_direction_after_rewarp": "Reverse Direction After Rewarp", - "text.aether.walk_mode": "Walk Mode" + "text.aether.walk_mode": "Walk Mode", + "text.aether.teleport": "Teleport", + "text.aether.teleport_failsafe": "Teleport Failsafe", + "text.aether.teleport_action": "Teleport Action", + "text.aether.horizontal_ignore_limit": "Horizontal Ignore Limit", + "text.aether.vertical_ignore_limit": "Vertical Ignore Limit", + "text.aether.teleport_settle_time": "Teleport Settle Time", + "text.aether.only_during_farming": "Only During Farming", + "text.aether.detects_unexpected_server_position_changes_during_macro_operation_ignores_requested_teleports": "Detects unexpected server position changes during macro operation; ignores requested teleports" } diff --git a/translations/test.json b/translations/test.json index 049d6360..411b64ac 100644 --- a/translations/test.json +++ b/translations/test.json @@ -768,5 +768,13 @@ "text.aether.your_aether_logo_cape_with_vanilla_movement": "Your Aether logo cape with vanilla movement", "text.aether.matcha": "Matcha", "text.aether.reverse_direction_after_rewarp": "Reverse Direction After Rewarp", - "text.aether.walk_mode": "Walk Mode" + "text.aether.walk_mode": "Walk Mode", + "text.aether.teleport": "Teleport", + "text.aether.teleport_failsafe": "Teleport Failsafe", + "text.aether.teleport_action": "Teleport Action", + "text.aether.horizontal_ignore_limit": "Horizontal Ignore Limit", + "text.aether.vertical_ignore_limit": "Vertical Ignore Limit", + "text.aether.teleport_settle_time": "Teleport Settle Time", + "text.aether.only_during_farming": "Only During Farming", + "text.aether.detects_unexpected_server_position_changes_during_macro_operation_ignores_requested_teleports": "Detects unexpected server position changes during macro operation; ignores requested teleports" } diff --git a/translations/zh_cn.json b/translations/zh_cn.json index 7f97341b..f28ab39e 100644 --- a/translations/zh_cn.json +++ b/translations/zh_cn.json @@ -768,5 +768,13 @@ "text.aether.your_aether_logo_cape_with_vanilla_movement": "Your Aether logo cape with vanilla movement", "text.aether.matcha": "Matcha", "text.aether.reverse_direction_after_rewarp": "Reverse Direction After Rewarp", - "text.aether.walk_mode": "Walk Mode" + "text.aether.walk_mode": "Walk Mode", + "text.aether.teleport": "传送检测", + "text.aether.teleport_failsafe": "异常传送保护", + "text.aether.teleport_action": "传送触发动作", + "text.aether.horizontal_ignore_limit": "水平忽略上限", + "text.aether.vertical_ignore_limit": "竖直忽略上限", + "text.aether.teleport_settle_time": "传送后抑制时间", + "text.aether.only_during_farming": "仅在种田期间检测", + "text.aether.detects_unexpected_server_position_changes_during_macro_operation_ignores_requested_teleports": "检测宏运行期间的服务器异常位移,排除主动请求的传送" }