Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
7 355
src/client/java/com/kodari/multikeybind/MultiKeybindClient.java
package com.kodari.multikeybind;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import org.lwjgl.glfw.GLFW;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class MultiKeybindClient implements ClientModInitializer {
static final int LIMIT = 16;
static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
static final Path FILE = FabricLoader.getInstance().getConfigDir().resolve("multikeybind.json");
static Config config = new Config();
private static final Set<Input> activeOutputs = new HashSet<>();
private static boolean f8Down;
public static class Input {
public boolean mouse;
public int code;
public Input(boolean mouse, int code) { this.mouse = mouse; this.code = code; }
boolean valid() {
return mouse ? code >= 0 && code <= GLFW.GLFW_MOUSE_BUTTON_LAST
: code >= GLFW.GLFW_KEY_SPACE && code <= GLFW.GLFW_KEY_LAST && code != GLFW.GLFW_KEY_F8;
}
boolean down(long window) {
return valid() && (mouse ? GLFW.glfwGetMouseButton(window, code) == GLFW.GLFW_PRESS
: GLFW.glfwGetKey(window, code) == GLFW.GLFW_PRESS);
}
InputUtil.Key key() {
return (mouse ? InputUtil.Type.MOUSE : InputUtil.Type.KEYSYM).createFromCode(code);
}
String display() {
if (mouse) return switch (code) {
case 0 -> "Mouse Left";
case 1 -> "Mouse Right";
case 2 -> "Mouse Middle";
default -> "Mouse " + (code + 1);
};
String name = GLFW.glfwGetKeyName(code, 0);
return name != null ? name.toUpperCase(java.util.Locale.ROOT) : "Key " + code;
}
@Override public boolean equals(Object other) {
return other instanceof Input input && mouse == input.mouse && code == input.code;
}
@Override public int hashCode() { return 31 * Boolean.hashCode(mouse) + code; }
}
public static class Rule {
public Input trigger;
public Input[] outputs = new Input[3];
public Input toggle;
public boolean enabled = true;
transient boolean toggleDown;
}
public static class Config {
public List<Rule> rules = new ArrayList<>();
}
@Override public void onInitializeClient() {
load();
ClientTickEvents.START_CLIENT_TICK.register(MultiKeybindClient::tick);
}
static void save() {
try {
Files.createDirectories(FILE.getParent());
try (Writer writer = Files.newBufferedWriter(FILE)) { GSON.toJson(config, writer); }
} catch (Exception e) {
System.err.println("Multi Keybind: could not save configuration: " + e);
}
}
private static void load() {
if (!Files.exists(FILE)) return;
try (Reader reader = Files.newBufferedReader(FILE)) {
Config loaded = GSON.fromJson(reader, Config.class);
if (loaded == null || loaded.rules == null) return;
loaded.rules.removeIf(rule -> rule == null);
if (loaded.rules.size() > LIMIT) loaded.rules = new ArrayList<>(loaded.rules.subList(0, LIMIT));
for (Rule rule : loaded.rules) {
if (rule.outputs == null || rule.outputs.length != 3) {
Input[] fixed = new Input[3];
if (rule.outputs != null) System.arraycopy(rule.outputs, 0, fixed, 0, Math.min(3, rule.outputs.length));
rule.outputs = fixed;
}
}
config = loaded;
} catch (Exception e) {
System.err.println("Multi Keybind: could not load configuration: " + e);
}
}
private static void tick(MinecraftClient client) {
long window = client.getWindow().getHandle();
boolean playing = client.player != null && client.world != null && client.currentScreen == null && client.isWindowFocused();
boolean f8 = client.isWindowFocused() && GLFW.glfwGetKey(window, GLFW.GLFW_KEY_F8) == GLFW.GLFW_PRESS;
if (playing && f8 && !f8Down) {
client.setScreen(new BindScreen());
playing = false;
}
f8Down = f8;
if (client.currentScreen instanceof BindScreen screen && client.isWindowFocused()) screen.captureTick(window);
Set<Input> next = new HashSet<>();
for (Rule rule : config.rules) {
boolean toggle = playing && rule.toggle != null && rule.toggle.down(window);
if (toggle && !rule.toggleDown) {
rule.enabled = !rule.enabled;
save();
}
rule.toggleDown = toggle;
if (playing && rule.enabled && rule.trigger != null && rule.trigger.down(window)) {
for (Input output : rule.outputs) if (output != null && output.valid()) next.add(output);
}
}
Set<Input> affected = new HashSet<>(activeOutputs);
affected.addAll(next);
for (Input input : affected) {
boolean physical = playing && input.down(window);
boolean synthetic = next.contains(input);
if (synthetic && !activeOutputs.contains(input) && !physical) KeyBinding.onKeyPressed(input.key());
KeyBinding.setKeyPressed(input.key(), physical || synthetic);
}
activeOutputs.clear();
activeOutputs.addAll(next);
}
}