Skip to content

Commit 49480f6

Browse files
committed
Add a setting to AttributeSwap to allow swapping without hitting an entity
Useful for quickly using the lunge enchantment. Closes #6012
1 parent ba2ccba commit 49480f6

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
3+
* Copyright (c) Meteor Development.
4+
*/
5+
6+
package meteordevelopment.meteorclient.events.entity.player;
7+
8+
import meteordevelopment.meteorclient.events.Cancellable;
9+
10+
public class DoAttackEvent extends Cancellable {
11+
private static final DoAttackEvent INSTANCE = new DoAttackEvent();
12+
13+
public static DoAttackEvent get() {
14+
return INSTANCE;
15+
}
16+
}

src/main/java/meteordevelopment/meteorclient/mixin/MinecraftClientMixin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
1313
import com.llamalad7.mixinextras.sugar.Local;
1414
import meteordevelopment.meteorclient.MeteorClient;
15+
import meteordevelopment.meteorclient.events.entity.player.DoAttackEvent;
1516
import meteordevelopment.meteorclient.events.entity.player.DoItemUseEvent;
1617
import meteordevelopment.meteorclient.events.entity.player.ItemUseCrosshairTargetEvent;
1718
import meteordevelopment.meteorclient.events.game.GameLeftEvent;
@@ -116,9 +117,10 @@ private void onTick(CallbackInfo info) {
116117
Profilers.get().pop();
117118
}
118119

119-
@Inject(method = "doAttack", at = @At("HEAD"))
120+
@Inject(method = "doAttack", at = @At("HEAD"), cancellable = true)
120121
private void onAttack(CallbackInfoReturnable<Boolean> cir) {
121122
CPSUtils.onAttack();
123+
if (MeteorClient.EVENT_BUS.post(DoAttackEvent.get()).isCancelled()) cir.cancel();
122124
}
123125

124126
@Inject(method = "doItemUse", at = @At("HEAD"))

src/main/java/meteordevelopment/meteorclient/systems/modules/combat/AttributeSwap.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package meteordevelopment.meteorclient.systems.modules.combat;
77

88
import meteordevelopment.meteorclient.events.entity.player.AttackEntityEvent;
9+
import meteordevelopment.meteorclient.events.entity.player.DoAttackEvent;
910
import meteordevelopment.meteorclient.events.world.TickEvent;
1011
import meteordevelopment.meteorclient.settings.*;
1112
import meteordevelopment.meteorclient.systems.modules.Categories;
@@ -27,6 +28,7 @@
2728
import net.minecraft.item.TridentItem;
2829
import net.minecraft.registry.tag.EntityTypeTags;
2930
import net.minecraft.registry.tag.ItemTags;
31+
import net.minecraft.util.hit.HitResult;
3032

3133
public class AttributeSwap extends Module {
3234
private final SettingGroup sgGeneral = settings.getDefaultGroup();
@@ -47,8 +49,15 @@ public class AttributeSwap extends Module {
4749
.name("target-slot")
4850
.description("Hotbar slot to swap to (1-9).")
4951
.defaultValue(1)
50-
.min(1)
51-
.sliderRange(1, 9)
52+
.range(1, 9)
53+
.visible(() -> mode.get() == Mode.Simple)
54+
.build()
55+
);
56+
57+
private final Setting<Boolean> swapOnMiss = sgGeneral.add(new BoolSetting.Builder()
58+
.name("swap-on-miss")
59+
.description("Whether to swap on a missed attack. Useful for quickly lunging with spears.")
60+
.defaultValue(false)
5261
.visible(() -> mode.get() == Mode.Simple)
5362
.build()
5463
);
@@ -276,21 +285,31 @@ public void onDeactivate() {
276285
}
277286

278287
@EventHandler
279-
private void onAttack(AttackEntityEvent event) {
280-
if (!canSwapByWeapon()) return;
288+
private void onAttack(DoAttackEvent event) {
289+
if (!canSwapByWeapon() || mode.get() == Mode.Smart || !swapOnMiss.get()) return;
290+
if (mc.crosshairTarget.getType() == HitResult.Type.BLOCK) return;
291+
292+
doSwap(targetSlot.get() - 1);
293+
}
294+
295+
@EventHandler
296+
private void onAttackEntity(AttackEntityEvent event) {
297+
if (!canSwapByWeapon() || (mode.get() == Mode.Simple && swapOnMiss.get())) return;
281298
performSwap(event.entity);
282299
}
283300

284301
private void performSwap(Entity target) {
285302
if (awaitingBack) return;
286303

287-
int slotIndex;
288-
289304
if (mode.get() == Mode.Simple) {
290-
slotIndex = targetSlot.get() - 1;
305+
doSwap(targetSlot.get() - 1);
291306
} else {
292-
slotIndex = getSmartSlot(target);
307+
doSwap(getSmartSlot(target));
293308
}
309+
}
310+
311+
private void doSwap(int slotIndex) {
312+
if (awaitingBack) return;
294313

295314
if (slotIndex < 0 || slotIndex > 8) return;
296315
if (slotIndex == mc.player.getInventory().getSelectedSlot()) return;

0 commit comments

Comments
 (0)