|
| 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.hacks; |
| 9 | + |
| 10 | +import net.minecraft.client.KeyMapping; |
| 11 | +import net.minecraft.client.player.LocalPlayer; |
| 12 | +import net.wurstclient.events.ChatInputListener; |
| 13 | +import net.wurstclient.events.PlayerAttacksEntityListener; |
| 14 | +import net.wurstclient.events.UpdateListener; |
| 15 | +import net.wurstclient.hack.Hack; |
| 16 | +import net.minecraft.network.chat.Component; |
| 17 | + |
| 18 | +/** |
| 19 | + * Navigator-only helper that briefly toggles Towny PvP while you attack: |
| 20 | + * enable on attack, disable when you stop attacking. |
| 21 | + * |
| 22 | + * Implementation notes: |
| 23 | + * - Does not set a Category, so it shows up only in the Navigator/commands. |
| 24 | + * - Uses PlayerAttacksEntity event to detect the start of an attack. |
| 25 | + * - Disables again once the attack key is released (with a 1-tick grace). |
| 26 | + */ |
| 27 | +public final class TownyHack extends Hack |
| 28 | + implements PlayerAttacksEntityListener, UpdateListener, ChatInputListener |
| 29 | +{ |
| 30 | + private enum PvPState |
| 31 | + { |
| 32 | + UNKNOWN, |
| 33 | + ENABLED, |
| 34 | + DISABLED |
| 35 | + } |
| 36 | + |
| 37 | + private boolean desiredEnabled; |
| 38 | + private PvPState knownState; |
| 39 | + private boolean awaitingConfirm; |
| 40 | + private int ticksSinceCommand; |
| 41 | + private int ticksSinceAttack; |
| 42 | + private int retryCount; |
| 43 | + |
| 44 | + private enum SendMode |
| 45 | + { |
| 46 | + NONE, |
| 47 | + COMMAND, |
| 48 | + CHAT |
| 49 | + } |
| 50 | + |
| 51 | + private SendMode lastSendMode = SendMode.NONE; |
| 52 | + private boolean didFallback; |
| 53 | + |
| 54 | + // Anti-spam timings (ticks) |
| 55 | + private static final int MIN_INTERVAL_TICKS = 40; // 2s between sends |
| 56 | + private static final int CONFIRM_TIMEOUT_TICKS = 100; // 5s to wait |
| 57 | + |
| 58 | + public TownyHack() |
| 59 | + { |
| 60 | + super("Towny"); |
| 61 | + // No category -> Navigator-only |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected void onEnable() |
| 66 | + { |
| 67 | + desiredEnabled = false; |
| 68 | + knownState = PvPState.UNKNOWN; |
| 69 | + awaitingConfirm = false; |
| 70 | + ticksSinceCommand = 0; |
| 71 | + ticksSinceAttack = 0; |
| 72 | + retryCount = 0; |
| 73 | + lastSendMode = SendMode.NONE; |
| 74 | + didFallback = false; |
| 75 | + EVENTS.add(PlayerAttacksEntityListener.class, this); |
| 76 | + EVENTS.add(UpdateListener.class, this); |
| 77 | + EVENTS.add(ChatInputListener.class, this); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected void onDisable() |
| 82 | + { |
| 83 | + EVENTS.remove(PlayerAttacksEntityListener.class, this); |
| 84 | + EVENTS.remove(UpdateListener.class, this); |
| 85 | + EVENTS.remove(ChatInputListener.class, this); |
| 86 | + desiredEnabled = false; |
| 87 | + knownState = PvPState.UNKNOWN; |
| 88 | + awaitingConfirm = false; |
| 89 | + ticksSinceCommand = 0; |
| 90 | + ticksSinceAttack = 0; |
| 91 | + retryCount = 0; |
| 92 | + lastSendMode = SendMode.NONE; |
| 93 | + didFallback = false; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void onPlayerAttacksEntity(net.minecraft.world.entity.Entity target) |
| 98 | + { |
| 99 | + if(MC == null || MC.player == null) |
| 100 | + return; |
| 101 | + |
| 102 | + // We want PvP enabled while attacking. |
| 103 | + desiredEnabled = true; |
| 104 | + ticksSinceAttack = 0; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void onUpdate() |
| 109 | + { |
| 110 | + LocalPlayer player = MC.player; |
| 111 | + if(player == null) |
| 112 | + return; |
| 113 | + |
| 114 | + ticksSinceAttack++; |
| 115 | + |
| 116 | + // When the attack key is no longer held (and at least 1 tick passed), |
| 117 | + // we no longer desire PvP. |
| 118 | + KeyMapping attackKey = MC.options.keyAttack; |
| 119 | + boolean attackDown = attackKey != null && attackKey.isDown(); |
| 120 | + if(!attackDown && ticksSinceAttack >= 1) |
| 121 | + desiredEnabled = false; |
| 122 | + |
| 123 | + // Handle timing for anti-spam |
| 124 | + if(ticksSinceCommand < Integer.MAX_VALUE) |
| 125 | + ticksSinceCommand++; |
| 126 | + |
| 127 | + if(awaitingConfirm) |
| 128 | + { |
| 129 | + if(ticksSinceCommand > CONFIRM_TIMEOUT_TICKS) |
| 130 | + { |
| 131 | + // Give up or retry once after timeout |
| 132 | + awaitingConfirm = false; |
| 133 | + if(retryCount < 1) |
| 134 | + { |
| 135 | + // Only retry if still needed and min interval elapsed |
| 136 | + maybeSendToggle(); |
| 137 | + retryCount++; |
| 138 | + } |
| 139 | + } |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + // Not awaiting confirmation: check if desired differs from known. |
| 144 | + boolean isKnownEnabled = (knownState == PvPState.ENABLED); |
| 145 | + if(desiredEnabled != isKnownEnabled) |
| 146 | + maybeSendToggle(); |
| 147 | + } |
| 148 | + |
| 149 | + private void maybeSendToggle() |
| 150 | + { |
| 151 | + if(ticksSinceCommand < MIN_INTERVAL_TICKS) |
| 152 | + return; |
| 153 | + |
| 154 | + sendTownyToggleCommand(); |
| 155 | + awaitingConfirm = true; |
| 156 | + ticksSinceCommand = 0; |
| 157 | + } |
| 158 | + |
| 159 | + private void sendTownyToggleCommand() |
| 160 | + { |
| 161 | + try |
| 162 | + { |
| 163 | + if(MC != null && MC.getConnection() != null) |
| 164 | + { |
| 165 | + // Preferred: command packet without leading slash. |
| 166 | + MC.getConnection().sendCommand("t toggle pvp"); |
| 167 | + lastSendMode = SendMode.COMMAND; |
| 168 | + } |
| 169 | + }catch(Throwable ignored) |
| 170 | + { |
| 171 | + // Best-effort: ignore failures sending command |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + private void sendTownyToggleChat() |
| 176 | + { |
| 177 | + try |
| 178 | + { |
| 179 | + if(MC != null && MC.getConnection() != null) |
| 180 | + { |
| 181 | + // Fallback: send as literal chat with a slash. |
| 182 | + MC.getConnection().sendChat("/t toggle pvp"); |
| 183 | + lastSendMode = SendMode.CHAT; |
| 184 | + } |
| 185 | + }catch(Throwable ignored) |
| 186 | + { |
| 187 | + // ignore |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public void onReceivedMessage(ChatInputListener.ChatInputEvent event) |
| 193 | + { |
| 194 | + Component comp = event.getComponent(); |
| 195 | + if(comp == null) |
| 196 | + return; |
| 197 | + |
| 198 | + String msg = comp.getString(); |
| 199 | + if(msg == null) |
| 200 | + return; |
| 201 | + String m = msg.trim().toLowerCase(); |
| 202 | + // Fallback trigger: unknown command error for our string |
| 203 | + if(m.contains("unknown or incomplete command") |
| 204 | + && m.contains("t toggle pvp") && lastSendMode == SendMode.COMMAND |
| 205 | + && !didFallback) |
| 206 | + { |
| 207 | + // Try one-time fallback via chat with slash |
| 208 | + if(ticksSinceCommand >= MIN_INTERVAL_TICKS) |
| 209 | + { |
| 210 | + sendTownyToggleChat(); |
| 211 | + awaitingConfirm = true; |
| 212 | + ticksSinceCommand = 0; |
| 213 | + didFallback = true; |
| 214 | + } |
| 215 | + return; |
| 216 | + } |
| 217 | + |
| 218 | + if(!m.contains("pvp")) |
| 219 | + return; |
| 220 | + |
| 221 | + // Heuristic parse; match common Towny toggle outputs. |
| 222 | + boolean enable = (m.contains("enabled") || m.contains("on")) |
| 223 | + && !m.contains("disable"); |
| 224 | + boolean disable = (m.contains("disabled") || m.contains("off")) |
| 225 | + && !m.contains("enable "); |
| 226 | + |
| 227 | + if(enable == disable) |
| 228 | + return; // ambiguous |
| 229 | + |
| 230 | + knownState = enable ? PvPState.ENABLED : PvPState.DISABLED; |
| 231 | + awaitingConfirm = false; |
| 232 | + retryCount = 0; |
| 233 | + didFallback = false; |
| 234 | + } |
| 235 | +} |
0 commit comments