Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
12 297
src/client/java/com/kodari/wheelhitplace/WheelHitPlaceClient.java
package com.kodari.wheelhitplace;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.fabricmc.fabric.api.client.screen.v1.ScreenMouseEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.option.KeybindsScreen;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.util.Identifier;
import com.kodari.wheelhitplace.mixin.KeybindsScreenAccessor;
public final class WheelHitPlaceClient implements ClientModInitializer {
private static final int WHEEL_UP_CODE = 100;
private static final int WHEEL_DOWN_CODE = 101;
private static final InputUtil.Key WHEEL_UP = InputUtil.Type.MOUSE.createFromCode(WHEEL_UP_CODE);
private static final InputUtil.Key WHEEL_DOWN = InputUtil.Type.MOUSE.createFromCode(WHEEL_DOWN_CODE);
private static KeyBinding extraAttackKey;
private static KeyBinding extraUseKey;
private static KeyBinding bypassKey;
@Override
public void onInitializeClient() {
WheelHitPlaceConfig.load();
KeyBinding.Category category = KeyBinding.Category.create(Identifier.of("wheelhitplace", "controls"));
extraAttackKey = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.wheelhitplace.attack", InputUtil.Type.KEYSYM, -1, category));
extraUseKey = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.wheelhitplace.use", InputUtil.Type.KEYSYM, -1, category));
bypassKey = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.wheelhitplace.bypass", InputUtil.Type.KEYSYM, 67, category));
ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
if (screen instanceof KeybindsScreen keybindsScreen) {
ScreenMouseEvents.allowMouseScroll(screen).register((target, mouseX, mouseY, horizontalAmount, verticalAmount) -> {
KeyBinding selected = keybindsScreen.selectedKeyBinding;
if (verticalAmount == 0.0 || (selected != extraAttackKey && selected != extraUseKey)) {
return true;
}
selected.setBoundKey(wheelKey(verticalAmount));
keybindsScreen.selectedKeyBinding = null;
((KeybindsScreenAccessor) keybindsScreen).wheelhitplace$getControlsList().update();
return false;
});
}
});
}
public static boolean handleMouseScroll(double verticalAmount) {
MinecraftClient client = MinecraftClient.getInstance();
if (verticalAmount == 0.0 || client.currentScreen != null || client.player == null || bypassKey == null || bypassKey.isPressed()) {
return false;
}
InputUtil.Key wheelKey = wheelKey(verticalAmount);
String wheelTranslationKey = wheelKey.getTranslationKey();
boolean attackBound = extraAttackKey != null && extraAttackKey.getBoundKeyTranslationKey().equals(wheelTranslationKey);
boolean useBound = extraUseKey != null && extraUseKey.getBoundKeyTranslationKey().equals(wheelTranslationKey);
if (!attackBound && !useBound) {
return false;
}
for (int press = 0; press < WheelHitPlaceConfig.multiplier(); press++) {
if (attackBound) {
KeyBinding.onKeyPressed(InputUtil.fromTranslationKey(client.options.attackKey.getBoundKeyTranslationKey()));
}
if (useBound) {
KeyBinding.onKeyPressed(InputUtil.fromTranslationKey(client.options.useKey.getBoundKeyTranslationKey()));
}
}
return true;
}
private static InputUtil.Key wheelKey(double verticalAmount) {
return verticalAmount > 0.0 ? WHEEL_UP : WHEEL_DOWN;
}
}