|
| 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.gui.screens.inventory.AbstractContainerScreen; |
| 11 | +import net.minecraft.client.gui.screens.inventory.CreativeModeInventoryScreen; |
| 12 | +import net.minecraft.client.gui.screens.inventory.InventoryScreen; |
| 13 | +import net.minecraft.client.player.LocalPlayer; |
| 14 | +import net.minecraft.world.InteractionHand; |
| 15 | +import net.minecraft.world.entity.Entity; |
| 16 | +import net.minecraft.world.item.ItemStack; |
| 17 | +import net.minecraft.world.item.Items; |
| 18 | +import net.minecraft.world.phys.EntityHitResult; |
| 19 | +import net.wurstclient.Category; |
| 20 | +import net.wurstclient.SearchTags; |
| 21 | +import net.wurstclient.events.HandleInputListener; |
| 22 | +import net.wurstclient.hack.Hack; |
| 23 | +import net.wurstclient.mixinterface.IKeyMapping; |
| 24 | +import net.wurstclient.mixinterface.IMultiPlayerGameMode; |
| 25 | +import net.wurstclient.settings.CheckboxSetting; |
| 26 | +import net.wurstclient.util.EntityUtils; |
| 27 | +import net.wurstclient.util.InventoryUtils; |
| 28 | +import net.wurstclient.util.ChatUtils; |
| 29 | + |
| 30 | +@SearchTags({"shield", "shield swing", "swing while blocking", |
| 31 | + "attack while blocking"}) |
| 32 | +public final class ShieldSwingHack extends Hack implements HandleInputListener |
| 33 | +{ |
| 34 | + private final CheckboxSetting autoHoldShield = |
| 35 | + new CheckboxSetting("Auto hold shield", |
| 36 | + "Automatically holds right-click while this hack is enabled and you" |
| 37 | + + " have a shield in your offhand.", |
| 38 | + false); |
| 39 | + |
| 40 | + private final CheckboxSetting autoEquipShield = |
| 41 | + new CheckboxSetting("Auto equip shield", |
| 42 | + "Automatically moves a shield from your inventory into your offhand" |
| 43 | + + " while this hack is enabled.", |
| 44 | + false); |
| 45 | + |
| 46 | + private final CheckboxSetting useNoShieldOverlay = |
| 47 | + new CheckboxSetting("Use NoShieldOverlay", |
| 48 | + "Applies NoShieldOverlay while ShieldSwing is enabled, even if" |
| 49 | + + " NoShieldOverlay itself is turned off.", |
| 50 | + false); |
| 51 | + |
| 52 | + private boolean attackKeyWasDown; |
| 53 | + private boolean forcingUseKey; |
| 54 | + private int nextTickSlot = -1; |
| 55 | + private boolean autoTotemWasEnabledBeforeShieldSwing; |
| 56 | + |
| 57 | + public ShieldSwingHack() |
| 58 | + { |
| 59 | + super("ShieldSwing"); |
| 60 | + setCategory(Category.COMBAT); |
| 61 | + addSetting(autoHoldShield); |
| 62 | + addSetting(autoEquipShield); |
| 63 | + addSetting(useNoShieldOverlay); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected void onEnable() |
| 68 | + { |
| 69 | + if(WURST.getHax().autoTotemHack.isEnabled()) |
| 70 | + { |
| 71 | + autoTotemWasEnabledBeforeShieldSwing = true; |
| 72 | + WURST.getHax().autoTotemHack.setEnabled(false); |
| 73 | + ChatUtils |
| 74 | + .warning("AutoTotem disabled because ShieldSwing is enabled."); |
| 75 | + }else |
| 76 | + { |
| 77 | + autoTotemWasEnabledBeforeShieldSwing = false; |
| 78 | + } |
| 79 | + |
| 80 | + attackKeyWasDown = false; |
| 81 | + forcingUseKey = false; |
| 82 | + nextTickSlot = -1; |
| 83 | + EVENTS.add(HandleInputListener.class, this); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + protected void onDisable() |
| 88 | + { |
| 89 | + releaseUseKey(); |
| 90 | + finishShieldSwap(); |
| 91 | + EVENTS.remove(HandleInputListener.class, this); |
| 92 | + |
| 93 | + if(autoTotemWasEnabledBeforeShieldSwing |
| 94 | + && !WURST.getHax().autoTotemHack.isEnabled()) |
| 95 | + { |
| 96 | + autoTotemWasEnabledBeforeShieldSwing = false; |
| 97 | + WURST.getHax().autoTotemHack.setEnabled(true); |
| 98 | + ChatUtils.message("AutoTotem restored."); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void onHandleInput() |
| 104 | + { |
| 105 | + if(MC.player == null || MC.gameMode == null || MC.options == null) |
| 106 | + { |
| 107 | + releaseUseKey(); |
| 108 | + attackKeyWasDown = false; |
| 109 | + nextTickSlot = -1; |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + finishShieldSwap(); |
| 114 | + tryAutoEquipShield(); |
| 115 | + tryAutoHoldShield(); |
| 116 | + |
| 117 | + if(shouldRestoreAutoTotemNow()) |
| 118 | + { |
| 119 | + setEnabled(false); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + if(MC.screen != null) |
| 124 | + { |
| 125 | + attackKeyWasDown = false; |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + boolean attackKeyDown = MC.options.keyAttack.isDown(); |
| 130 | + boolean attackKeyPressed = attackKeyDown && !attackKeyWasDown; |
| 131 | + attackKeyWasDown = attackKeyDown; |
| 132 | + if(!attackKeyPressed) |
| 133 | + return; |
| 134 | + |
| 135 | + LocalPlayer player = MC.player; |
| 136 | + if(!player.isUsingItem() || !player.getUseItem().is(Items.SHIELD)) |
| 137 | + return; |
| 138 | + |
| 139 | + if(!(MC.hitResult instanceof EntityHitResult eHitResult)) |
| 140 | + return; |
| 141 | + |
| 142 | + Entity target = eHitResult.getEntity(); |
| 143 | + if(target == null || !EntityUtils.IS_ATTACKABLE.test(target)) |
| 144 | + return; |
| 145 | + |
| 146 | + WURST.getHax().autoSwordHack.setSlot(target); |
| 147 | + MC.gameMode.attack(player, target); |
| 148 | + player.swing(InteractionHand.MAIN_HAND); |
| 149 | + } |
| 150 | + |
| 151 | + public boolean shouldUseNoShieldOverlay() |
| 152 | + { |
| 153 | + return isEnabled() && useNoShieldOverlay.isChecked(); |
| 154 | + } |
| 155 | + |
| 156 | + private void tryAutoHoldShield() |
| 157 | + { |
| 158 | + boolean shouldForce = autoHoldShield.isChecked() && MC.screen == null |
| 159 | + && hasShieldInOffhand(MC.player); |
| 160 | + |
| 161 | + if(shouldForce) |
| 162 | + { |
| 163 | + MC.options.keyUse.setDown(true); |
| 164 | + forcingUseKey = true; |
| 165 | + return; |
| 166 | + } |
| 167 | + |
| 168 | + releaseUseKey(); |
| 169 | + } |
| 170 | + |
| 171 | + private void tryAutoEquipShield() |
| 172 | + { |
| 173 | + if(!autoEquipShield.isChecked()) |
| 174 | + return; |
| 175 | + |
| 176 | + if(hasShieldInOffhand(MC.player)) |
| 177 | + return; |
| 178 | + |
| 179 | + if(MC.player.containerMenu != null |
| 180 | + && !MC.player.containerMenu.getCarried().isEmpty()) |
| 181 | + return; |
| 182 | + |
| 183 | + if(MC.screen instanceof AbstractContainerScreen |
| 184 | + && !(MC.screen instanceof InventoryScreen |
| 185 | + || MC.screen instanceof CreativeModeInventoryScreen)) |
| 186 | + return; |
| 187 | + |
| 188 | + int shieldSlot = InventoryUtils.indexOf(this::isShield, 40); |
| 189 | + if(shieldSlot < 0 || shieldSlot == 40) |
| 190 | + return; |
| 191 | + |
| 192 | + moveToOffhand(InventoryUtils.toNetworkSlot(shieldSlot)); |
| 193 | + } |
| 194 | + |
| 195 | + private void moveToOffhand(int itemSlot) |
| 196 | + { |
| 197 | + boolean offhandEmpty = MC.player.getOffhandItem().isEmpty(); |
| 198 | + IMultiPlayerGameMode interactionManager = IMC.getInteractionManager(); |
| 199 | + interactionManager.windowClick_PICKUP(itemSlot); |
| 200 | + interactionManager.windowClick_PICKUP(45); |
| 201 | + |
| 202 | + if(!offhandEmpty) |
| 203 | + nextTickSlot = itemSlot; |
| 204 | + } |
| 205 | + |
| 206 | + private void finishShieldSwap() |
| 207 | + { |
| 208 | + if(nextTickSlot == -1 || MC.player == null) |
| 209 | + return; |
| 210 | + |
| 211 | + IMultiPlayerGameMode interactionManager = IMC.getInteractionManager(); |
| 212 | + interactionManager.windowClick_PICKUP(nextTickSlot); |
| 213 | + nextTickSlot = -1; |
| 214 | + } |
| 215 | + |
| 216 | + private boolean hasShieldInOffhand(LocalPlayer player) |
| 217 | + { |
| 218 | + return isShield(player.getOffhandItem()); |
| 219 | + } |
| 220 | + |
| 221 | + private boolean isShield(ItemStack stack) |
| 222 | + { |
| 223 | + return stack != null && stack.is(Items.SHIELD); |
| 224 | + } |
| 225 | + |
| 226 | + private void releaseUseKey() |
| 227 | + { |
| 228 | + if(!forcingUseKey || MC.options == null) |
| 229 | + return; |
| 230 | + |
| 231 | + IKeyMapping.get(MC.options.keyUse).resetPressedState(); |
| 232 | + forcingUseKey = false; |
| 233 | + } |
| 234 | + |
| 235 | + private boolean shouldRestoreAutoTotemNow() |
| 236 | + { |
| 237 | + if(!autoTotemWasEnabledBeforeShieldSwing || MC.player == null) |
| 238 | + return false; |
| 239 | + |
| 240 | + if(hasShieldInOffhand(MC.player)) |
| 241 | + return false; |
| 242 | + |
| 243 | + boolean canAutoEquip = autoEquipShield.isChecked() |
| 244 | + && InventoryUtils.indexOf(this::isShield, 40, false) >= 0; |
| 245 | + return !canAutoEquip; |
| 246 | + } |
| 247 | +} |
0 commit comments