Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
12 725
src/client/java/dev/kodari/offhandwheel/OffhandWheelClient.java
package dev.kodari.offhandwheel;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.mojang.serialization.JsonOps;
import dev.kodari.offhandwheel.mixin.KeyBindingAccessor;
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.fabricmc.fabric.api.client.message.v1.ClientSendMessageEvents;
import net.fabricmc.loader.api.FabricLoader;
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.registry.RegistryOps;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.Hand;
import org.lwjgl.glfw.GLFW;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Properties;
public class OffhandWheelClient implements ClientModInitializer {
static final ItemStack[] choices = {ItemStack.EMPTY, ItemStack.EMPTY, ItemStack.EMPTY, ItemStack.EMPTY};
static int mode = 2;
static int nextChoice = 0;
static boolean keepMissing;
static boolean stopBeforeSwap;
static int theme;
static int style;
static boolean mainHand;
private static int pendingSwap = -1;
private static int pendingTicks;
private static final Path SETTINGS_FILE = FabricLoader.getInstance().getConfigDir().resolve("offhandwheel-settings.properties");
private static KeyBinding openKey;
private static KeyBinding cycleKey;
private static final KeyBinding[] slotKeys = new KeyBinding[4];
private static final Path CHOICES_FILE = FabricLoader.getInstance().getConfigDir().resolve("offhandwheel-choices.json");
private static final boolean[] observed = new boolean[4];
private static final int[] missingTicks = new int[4];
private static Object loadedWorld;
@Override
public void onInitializeClient() {
loadSettings();
ClientSendMessageEvents.ALLOW_COMMAND.register(command -> {
String[] parts = command.trim().split("\\s+");
if (parts.length < 2 || !parts[0].equalsIgnoreCase("swap") || !parts[1].equalsIgnoreCase("add")) return true;
var player = MinecraftClient.getInstance().player;
if (player == null) return false;
if (parts.length != 3 || !parts[2].matches("[1-4]")) {
player.sendMessage(Text.translatable("offhandwheel.command_usage"), false);
return false;
}
ItemStack stack = player.getStackInHand(Hand.MAIN_HAND);
if (stack.isEmpty()) {
player.sendMessage(Text.translatable("offhandwheel.command_empty"), false);
return false;
}
int slot = Integer.parseInt(parts[2]);
setChoice(slot - 1, stack);
player.sendMessage(Text.translatable("offhandwheel.command_added", stack.getName(), slot), false);
return false;
});
KeyBinding.Category category = KeyBinding.Category.create(Identifier.of("offhandwheel", "general"));
openKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.offhandwheel.open", GLFW.GLFW_KEY_R, category));
cycleKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.offhandwheel.cycle", InputUtil.UNKNOWN_KEY.getCode(), category));
for (int i = 0; i < slotKeys.length; i++) {
slotKeys[i] = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.offhandwheel.slot" + (i + 1),
InputUtil.UNKNOWN_KEY.getCode(), category));
}
ClientTickEvents.END_CLIENT_TICK.register(client -> {
updateChoices(client);
processPendingSwap(client);
while (cycleKey.wasPressed()) {
if (client.player != null && client.currentScreen == null) requestSwap(client, 4);
}
for (int i = 0; i < slotKeys.length; i++) {
while (slotKeys[i].wasPressed()) {
if (client.player != null && client.currentScreen == null) requestSwap(client, i);
}
}
if (client.player == null) return;
if (client.currentScreen instanceof WheelScreen wheel) {
if (!held(client)) {
wheel.selectHovered();
client.setScreen(null);
}
} else if (client.currentScreen == null && openKey.isPressed() && held(client)) {
client.options.forwardKey.setPressed(false);
client.options.backKey.setPressed(false);
client.options.leftKey.setPressed(false);
client.options.rightKey.setPressed(false);
client.options.jumpKey.setPressed(false);
client.options.sneakKey.setPressed(false);
client.setScreen(new WheelScreen());
}
});
}
private static void requestSwap(MinecraftClient client, int slot) {
if (!stopBeforeSwap) {
if (slot == 4) cycle(client);
else swap(choices[slot]);
return;
}
pendingSwap = slot;
pendingTicks = 0;
}
private static void processPendingSwap(MinecraftClient client) {
if (pendingSwap < 0) return;
if (client.player == null || client.currentScreen != null || ++pendingTicks > 60) {
pendingSwap = -1;
return;
}
if (pendingTicks == 1 && !client.player.isOnGround()) {
client.player.setVelocity(0.0, client.player.getVelocity().y, 0.0);
return;
}
if (client.player.getVelocity().horizontalLengthSquared() > 0.0001) return;
int slot = pendingSwap;
pendingSwap = -1;
if (slot == 4) cycle(client);
else swap(choices[slot]);
}
public static boolean isStoppingForSwap() {
return pendingSwap >= 0;
}
private static void cycle(MinecraftClient client) {
ItemStack offhand = client.player.getInventory().getStack(mainHand ? client.player.getInventory().getSelectedSlot() : 40);
int current = -1;
for (int i = 0; i < mode; i++) {
if (!choices[i].isEmpty() && ItemStack.areItemsAndComponentsEqual(choices[i], offhand)) {
current = i;
break;
}
}
int start = current >= 0 ? (current + 1) % mode : 0;
for (int offset = 0; offset < mode; offset++) {
ItemStack choice = choices[(start + offset) % mode];
if (choice.isEmpty() || ItemStack.areItemsAndComponentsEqual(choice, offhand)) continue;
for (int slot = 0; slot <= 40; slot++) {
if (slot >= 36 && slot < 40) continue;
if (ItemStack.areItemsAndComponentsEqual(choice, client.player.getInventory().getStack(slot))) {
swap(choice);
return;
}
}
}
}
private static void updateChoices(MinecraftClient client) {
if (client.world == null || client.player == null) {
loadedWorld = null;
return;
}
if (loadedWorld != client.world) {
loadedWorld = client.world;
Arrays.fill(choices, ItemStack.EMPTY);
Arrays.fill(observed, false);
Arrays.fill(missingTicks, 0);
loadChoices(client);
}
if (client.currentScreen != null || !client.player.currentScreenHandler.getCursorStack().isEmpty()) return;
boolean changed = false;
for (int i = 0; i < choices.length; i++) {
if (choices[i].isEmpty()) continue;
boolean present = false;
for (int slot = 0; slot <= 40; slot++) {
if (slot >= 36 && slot < 40) continue;
if (ItemStack.areItemsAndComponentsEqual(choices[i], client.player.getInventory().getStack(slot))) {
present = true;
break;
}
}
if (present) {
observed[i] = true;
missingTicks[i] = 0;
} else if (!keepMissing && observed[i] && ++missingTicks[i] >= 20) {
choices[i] = ItemStack.EMPTY;
observed[i] = false;
missingTicks[i] = 0;
changed = true;
}
}
if (changed) saveChoices(client);
}
private static void loadSettings() {
if (!Files.isRegularFile(SETTINGS_FILE)) return;