Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
10 391
src/client/java/com/kodari/dualhotbar/client/DualHotbarClient.java
package com.kodari.dualhotbar.client;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
public final class DualHotbarClient implements ClientModInitializer {
private static final int HOTBAR_SIZE = 9;
private static final KeyBinding[] SECONDARY_BINDINGS = new KeyBinding[HOTBAR_SIZE];
private static final KeyBinding.Category CATEGORY = KeyBinding.Category.create(
Identifier.of("dualhotbar", "controls")
);
@Override
public void onInitializeClient() {
DualHotbarConfig.load();
for (int slot = 0; slot < HOTBAR_SIZE; slot++) {
SECONDARY_BINDINGS[slot] = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.dualhotbar.secondary.slot_" + (slot + 1),
InputUtil.Type.KEYSYM,
InputUtil.UNKNOWN_KEY.getCode(),
CATEGORY
));
}
ClientTickEvents.END_CLIENT_TICK.register(DualHotbarClient::handleKeyPresses);
}
private static void handleKeyPresses(MinecraftClient client) {
int pressedSlot = -1;
boolean ambiguousPress = false;
for (int slot = 0; slot < HOTBAR_SIZE; slot++) {
boolean pressed = false;
while (SECONDARY_BINDINGS[slot].wasPressed()) {
pressed = true;
}
if (pressed) {
if (pressedSlot != -1) {
ambiguousPress = true;
} else {
pressedSlot = slot;
}
}
}
if (pressedSlot == -1 || ambiguousPress || !DualHotbarConfig.enabled
|| !DualHotbarConfig.secondaryEnabled[pressedSlot]) {
return;
}
KeyBinding pressedBinding = SECONDARY_BINDINGS[pressedSlot];
if (pressedBinding.isUnbound()) {
return;
}
for (int other = 0; other < HOTBAR_SIZE; other++) {
if (other != pressedSlot && pressedBinding.equals(SECONDARY_BINDINGS[other])) {
return;
}
if (pressedBinding.equals(client.options.hotbarKeys[other])) {
return;
}
}
if (client.player == null) {
return;
}
ItemStack mainHand = client.player.getMainHandStack();
if (DualHotbarConfig.condition.matches(mainHand)) {
client.player.getInventory().setSelectedSlot(pressedSlot);
}
}
}