Skip to content

Commit 70706f2

Browse files
committed
Update GhostHand to prevent it from interacting every tick
closes #5605 closes #5297
1 parent 45f6f3f commit 70706f2

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
/**
11+
* Some of our other injections coming from {@link net.minecraft.client.MinecraftClient#doItemUse()}
12+
* (e.g. InteractItemEvent) are called twice because the method loops over the Mainhand and the Offhand. This event is
13+
* only called once, before any interaction logic is called.
14+
*/
15+
public class DoItemUseEvent extends Cancellable {
16+
private static final DoItemUseEvent INSTANCE = new DoItemUseEvent();
17+
18+
public static DoItemUseEvent get() {
19+
return INSTANCE;
20+
}
21+
}

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

Lines changed: 7 additions & 5 deletions
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.DoItemUseEvent;
1516
import meteordevelopment.meteorclient.events.entity.player.ItemUseCrosshairTargetEvent;
1617
import meteordevelopment.meteorclient.events.game.GameLeftEvent;
1718
import meteordevelopment.meteorclient.events.game.OpenScreenEvent;
@@ -44,7 +45,6 @@
4445
import net.minecraft.entity.Entity;
4546
import net.minecraft.entity.player.PlayerEntity;
4647
import net.minecraft.item.ItemStack;
47-
import net.minecraft.resource.ReloadableResourceManagerImpl;
4848
import net.minecraft.util.hit.HitResult;
4949
import net.minecraft.util.profiler.Profilers;
5050
import org.jetbrains.annotations.Nullable;
@@ -84,10 +84,6 @@ public abstract class MinecraftClientMixin implements IMinecraftClient {
8484
@Nullable
8585
public ClientPlayerEntity player;
8686

87-
@Shadow
88-
@Final
89-
private ReloadableResourceManagerImpl resourceManager;
90-
9187
@Shadow
9288
@Final
9389
@Mutable
@@ -182,6 +178,11 @@ private void onDoItemUseHand(CallbackInfo ci, @Local ItemStack itemStack) {
182178
}
183179
}
184180

181+
@Inject(method = "doItemUse", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/Hand;values()[Lnet/minecraft/util/Hand;"), cancellable = true)
182+
private void onDoItemUseBeforeHands(CallbackInfo ci) {
183+
if (MeteorClient.EVENT_BUS.post(DoItemUseEvent.get()).isCancelled()) ci.cancel();
184+
}
185+
185186
@ModifyExpressionValue(method = "doItemUse", at = @At(value = "FIELD", target = "Lnet/minecraft/client/MinecraftClient;crosshairTarget:Lnet/minecraft/util/hit/HitResult;", ordinal = 1))
186187
private HitResult doItemUseMinecraftClientCrosshairTargetProxy(HitResult original) {
187188
return MeteorClient.EVENT_BUS.post(ItemUseCrosshairTargetEvent.get(original)).target;
@@ -260,6 +261,7 @@ private boolean handleInputEventsModifyIsUsingItem(boolean original) {
260261
private void handleInputEventsInjectStopUsingItem(CallbackInfo info) {
261262
if (Modules.get().get(Multitask.class).attackingEntities() && player.isUsingItem()) {
262263
if (!options.useKey.isPressed() && HB$stopUsingItem()) interactionManager.stopUsingItem(player);
264+
//noinspection StatementWithEmptyBody
263265
while (options.useKey.wasPressed());
264266
}
265267
}

src/main/java/meteordevelopment/meteorclient/systems/modules/player/GhostHand.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
package meteordevelopment.meteorclient.systems.modules.player;
77

88
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
9-
import meteordevelopment.meteorclient.events.world.TickEvent;
9+
import meteordevelopment.meteorclient.events.entity.player.DoItemUseEvent;
1010
import meteordevelopment.meteorclient.systems.modules.Categories;
1111
import meteordevelopment.meteorclient.systems.modules.Module;
1212
import meteordevelopment.orbit.EventHandler;
13+
import net.minecraft.util.ActionResult;
1314
import net.minecraft.util.Hand;
1415
import net.minecraft.util.hit.BlockHitResult;
1516
import net.minecraft.util.math.BlockPos;
@@ -26,7 +27,7 @@ public GhostHand() {
2627
}
2728

2829
@EventHandler
29-
private void onTick(TickEvent.Pre event) {
30+
private void onTick(DoItemUseEvent event) {
3031
if (!mc.options.useKey.isPressed() || mc.player.isSneaking()) return;
3132

3233
if (mc.world.getBlockState(BlockPos.ofFloored(mc.player.raycast(mc.player.getBlockInteractionRange(), mc.getRenderTickCounter().getTickProgress(true), false).getPos())).hasBlockEntity()) return;
@@ -44,8 +45,14 @@ private void onTick(TickEvent.Pre event) {
4445
posList.add(pos);
4546

4647
if (mc.world.getBlockState(pos).hasBlockEntity()) {
47-
mc.interactionManager.interactBlock(mc.player, Hand.MAIN_HAND, new BlockHitResult(new Vec3d(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5), Direction.UP, pos, true));
48-
return;
48+
for (Hand hand : Hand.values()) {
49+
ActionResult result = mc.interactionManager.interactBlock(mc.player, hand, new BlockHitResult(new Vec3d(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5), Direction.UP, pos, true));
50+
if (result instanceof ActionResult.Success || result instanceof ActionResult.Fail) {
51+
mc.player.swingHand(hand);
52+
event.cancel();
53+
return;
54+
}
55+
}
4956
}
5057
}
5158
}

0 commit comments

Comments
 (0)