|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2026 Wurst-Imperium and contributors. |
| 3 | + * |
| 4 | + * This source code is subject to the terms of the GNU General Public |
| 5 | + * License, version 3. If a copy of the GPL was not distributed with this |
| 6 | + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt |
| 7 | + */ |
| 8 | +package net.wurstclient.commands; |
| 9 | + |
| 10 | +import net.minecraft.core.BlockPos; |
| 11 | +import net.wurstclient.command.CmdError; |
| 12 | +import net.wurstclient.command.CmdError; |
| 13 | +import net.wurstclient.command.CmdException; |
| 14 | +import net.wurstclient.command.CmdSyntaxError; |
| 15 | +import net.wurstclient.command.Command; |
| 16 | +import net.wurstclient.util.MathUtils; |
| 17 | + |
| 18 | +public final class AutoFlyCmd extends Command |
| 19 | +{ |
| 20 | + public AutoFlyCmd() |
| 21 | + { |
| 22 | + super("autofly", "Fly to a waypoint using AutoFly.", |
| 23 | + ".autofly <x> <y> <z> [height] [speed]", |
| 24 | + ".autofly <x> <z> [height] [speed]"); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public void call(String[] args) throws CmdException |
| 29 | + { |
| 30 | + if(MC.player == null) |
| 31 | + throw new CmdError("Join a world before using .autofly."); |
| 32 | + String[] normalized = normalizeArgs(args); |
| 33 | + if(normalized.length == 1) |
| 34 | + { |
| 35 | + String cmd = normalized[0]; |
| 36 | + if(cmd.equalsIgnoreCase("next")) |
| 37 | + { |
| 38 | + ensureAutoFlyEnabled(); |
| 39 | + WURST.getHax().autoFlyHack.cycleNextWaypoint(); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if(cmd.equalsIgnoreCase("previous") || cmd.equalsIgnoreCase("prev")) |
| 44 | + { |
| 45 | + ensureAutoFlyEnabled(); |
| 46 | + WURST.getHax().autoFlyHack.cyclePreviousWaypoint(); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + if(cmd.equalsIgnoreCase("stop") || cmd.equalsIgnoreCase("off") |
| 51 | + || cmd.equalsIgnoreCase("disable")) |
| 52 | + { |
| 53 | + if(WURST.getHax().autoFlyHack.isEnabled()) |
| 54 | + WURST.getHax().autoFlyHack.setEnabled(false); |
| 55 | + return; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if(normalized.length < 2) |
| 60 | + throw new CmdSyntaxError("Invalid coordinates."); |
| 61 | + |
| 62 | + boolean hasY = normalized.length == 3 || normalized.length == 4 |
| 63 | + || normalized.length == 5; |
| 64 | + int idxHeight = hasY ? 3 : 2; |
| 65 | + |
| 66 | + if(normalized.length > idxHeight + 2) |
| 67 | + throw new CmdSyntaxError("Too many arguments."); |
| 68 | + |
| 69 | + BlockPos pos = hasY ? parseXyz(normalized) : parseXz(normalized); |
| 70 | + |
| 71 | + Double height = null; |
| 72 | + Double speed = null; |
| 73 | + if(normalized.length > idxHeight) |
| 74 | + height = parseDouble(normalized[idxHeight], "height"); |
| 75 | + if(normalized.length > idxHeight + 1) |
| 76 | + speed = parseDouble(normalized[idxHeight + 1], "speed"); |
| 77 | + |
| 78 | + WURST.getHax().autoFlyHack.setTargetFromCommand(pos, hasY, height, |
| 79 | + speed); |
| 80 | + } |
| 81 | + |
| 82 | + private BlockPos parseXyz(String[] args) throws CmdSyntaxError |
| 83 | + { |
| 84 | + int x = parseCoord(args[0], MC.player.blockPosition().getX()); |
| 85 | + int y = parseCoord(args[1], MC.player.blockPosition().getY()); |
| 86 | + int z = parseCoord(args[2], MC.player.blockPosition().getZ()); |
| 87 | + return new BlockPos(x, y, z); |
| 88 | + } |
| 89 | + |
| 90 | + private BlockPos parseXz(String[] args) throws CmdSyntaxError |
| 91 | + { |
| 92 | + int x = parseCoord(args[0], MC.player.blockPosition().getX()); |
| 93 | + int z = parseCoord(args[1], MC.player.blockPosition().getZ()); |
| 94 | + int y = MC.player.blockPosition().getY(); |
| 95 | + return new BlockPos(x, y, z); |
| 96 | + } |
| 97 | + |
| 98 | + private int parseCoord(String raw, int base) throws CmdSyntaxError |
| 99 | + { |
| 100 | + if(MathUtils.isInteger(raw)) |
| 101 | + return Integer.parseInt(raw); |
| 102 | + if(raw.equals("~")) |
| 103 | + return base; |
| 104 | + if(raw.startsWith("~") && MathUtils.isInteger(raw.substring(1))) |
| 105 | + return base + Integer.parseInt(raw.substring(1)); |
| 106 | + throw new CmdSyntaxError("Invalid coordinates."); |
| 107 | + } |
| 108 | + |
| 109 | + private Double parseDouble(String raw, String label) throws CmdSyntaxError |
| 110 | + { |
| 111 | + if(!MathUtils.isDouble(raw)) |
| 112 | + throw new CmdSyntaxError("Invalid " + label + "."); |
| 113 | + return Double.parseDouble(raw); |
| 114 | + } |
| 115 | + |
| 116 | + private String[] normalizeArgs(String[] args) |
| 117 | + { |
| 118 | + if(args == null || args.length == 0) |
| 119 | + return new String[0]; |
| 120 | + java.util.List<String> out = new java.util.ArrayList<>(); |
| 121 | + for(String arg : args) |
| 122 | + { |
| 123 | + if(arg == null) |
| 124 | + continue; |
| 125 | + String[] parts = arg.split(","); |
| 126 | + for(String p : parts) |
| 127 | + { |
| 128 | + String trimmed = p.trim(); |
| 129 | + if(!trimmed.isEmpty()) |
| 130 | + out.add(trimmed); |
| 131 | + } |
| 132 | + } |
| 133 | + return out.toArray(new String[0]); |
| 134 | + } |
| 135 | + |
| 136 | + private void ensureAutoFlyEnabled() throws CmdError |
| 137 | + { |
| 138 | + if(!WURST.getHax().autoFlyHack.isEnabled()) |
| 139 | + throw new CmdError("AutoFly must be enabled to cycle waypoints."); |
| 140 | + } |
| 141 | +} |
0 commit comments