Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve .input command #4834

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,37 @@

import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.datafixers.util.Pair;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.commands.Command;
import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.mixin.KeyBindingAccessor;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.command.CommandSource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class InputCommand extends Command {
private static final List<KeypressHandler> activeHandlers = new ArrayList<>();

private static final Map<KeyBinding, String> keys = Map.of(
mc.options.forwardKey, "forwards",
mc.options.backKey, "backwards",
mc.options.leftKey, "left",
mc.options.rightKey, "right",
mc.options.jumpKey, "jump",
mc.options.sneakKey, "sneak",
mc.options.useKey, "use",
mc.options.attackKey, "attack"
private static final List<Pair<KeyBinding, String>> holdKeys = List.of(
new Pair<>(mc.options.forwardKey, "forwards"),
new Pair<>(mc.options.backKey, "backwards"),
new Pair<>(mc.options.leftKey, "left"),
new Pair<>(mc.options.rightKey, "right"),
new Pair<>(mc.options.jumpKey, "jump"),
new Pair<>(mc.options.sneakKey, "sneak"),
new Pair<>(mc.options.sprintKey, "sprint"),
new Pair<>(mc.options.useKey, "use"),
new Pair<>(mc.options.attackKey, "attack")
);

private static final List<Pair<KeyBinding, String>> pressKeys = List.of(
new Pair<>(mc.options.swapHandsKey, "swap"),
new Pair<>(mc.options.dropKey, "drop")
);

public InputCommand() {
Expand All @@ -38,17 +46,35 @@ public InputCommand() {

@Override
public void build(LiteralArgumentBuilder<CommandSource> builder) {
for (Map.Entry<KeyBinding, String> keyBinding : keys.entrySet()) {
builder.then(literal(keyBinding.getValue())
for (Pair<KeyBinding, String> keyBinding : holdKeys) {
builder.then(literal(keyBinding.getSecond())
.then(argument("ticks", IntegerArgumentType.integer(1))
.executes(context -> {
activeHandlers.add(new KeypressHandler(keyBinding.getKey(), context.getArgument("ticks", Integer.class)));
activeHandlers.add(new KeypressHandler(keyBinding.getFirst(), context.getArgument("ticks", Integer.class)));
return SINGLE_SUCCESS;
})
)
);
}

for (Pair<KeyBinding, String> keyBinding : pressKeys) {
builder.then(literal(keyBinding.getSecond())
.executes(context -> {
press(keyBinding.getFirst());
return SINGLE_SUCCESS;
})
);
}

for (KeyBinding keyBinding : mc.options.hotbarKeys) {
builder.then(literal(keyBinding.getTranslationKey().substring(4))
.executes(context -> {
press(keyBinding);
return SINGLE_SUCCESS;
})
);
}

builder.then(literal("clear").executes(ctx -> {
if (activeHandlers.isEmpty()) warning("No active keypress handlers.");
else {
Expand All @@ -65,7 +91,7 @@ public void build(LiteralArgumentBuilder<CommandSource> builder) {
info("Active keypress handlers: ");
for (int i = 0; i < activeHandlers.size(); i++) {
KeypressHandler handler = activeHandlers.get(i);
info("(highlight)%d(default) - (highlight)%s %d(default) ticks left out of (highlight)%d(default).", i, keys.get(handler.key), handler.ticks, handler.totalTicks);
info("(highlight)%d(default) - (highlight)%s %d(default) ticks left out of (highlight)%d(default).", i, I18n.translate(handler.key.getTranslationKey()), handler.ticks, handler.totalTicks);
}
}
return SINGLE_SUCCESS;
Expand All @@ -83,6 +109,11 @@ public void build(LiteralArgumentBuilder<CommandSource> builder) {
})));
}

private static void press(KeyBinding keyBinding) {
KeyBindingAccessor accessor = (KeyBindingAccessor) keyBinding;
accessor.meteor$setTimesPressed(accessor.meteor$getTimesPressed() + 1);
}

private static class KeypressHandler {
private final KeyBinding key;
private final int totalTicks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ public interface KeyBindingAccessor {

@Accessor("boundKey")
InputUtil.Key getKey();

@Accessor("timesPressed")
int meteor$getTimesPressed();

@Accessor("timesPressed")
void meteor$setTimesPressed(int timesPressed);
}
Loading