Files
src
build.gradle
gradle.properties
README.md
settings.gradle
Code
6 287
src/client/java/com/kodari/dualsense/DualSenseControllerClient.java
package com.kodari.dualsense;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.Click;
import net.minecraft.client.gui.screen.GameMenuScreen;
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
import net.minecraft.client.input.KeyInput;
import net.minecraft.client.input.MouseInput;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.client.util.Window;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWGamepadState;
public final class DualSenseControllerClient implements ClientModInitializer {
private static final float STICK_DEADZONE = 0.18f;
private static final float TRIGGER_DEADZONE = 0.15f;
private static final float LOOK_SPEED = 3.0f;
private boolean previousStart;
private boolean previousBack;
private boolean previousHotbarPrevious;
private boolean previousHotbarNext;
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(this::tick);
}
private void tick(MinecraftClient client) {
int joystick = findGamepad();
if (joystick < 0) {
updateController(client, null);
return;
}
GLFWGamepadState state = GLFWGamepadState.create();
try {
updateController(client, GLFW.glfwGetGamepadState(joystick, state) ? state : null);
} finally {
state.free();
}
}
private static int findGamepad() {
for (int joystick = 0; joystick <= GLFW.GLFW_JOYSTICK_LAST; joystick++) {
if (GLFW.glfwJoystickPresent(joystick) && GLFW.glfwJoystickIsGamepad(joystick)) {
return joystick;
}
}
return -1;
}
private void updateController(MinecraftClient client, GLFWGamepadState state) {
boolean start = isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_START);
boolean back = isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_BACK);
boolean hotbarPrevious = isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_LEFT_BUMPER)
|| isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_DPAD_LEFT);
boolean hotbarNext = isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER)
|| isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_DPAD_RIGHT);
if (client.player != null && start && !previousStart) {
if (client.currentScreen instanceof GameMenuScreen) {
client.setScreen(null);
} else if (client.currentScreen == null) {
client.setScreen(new GameMenuScreen(true));
}
}
if (client.player != null && back && !previousBack) {
if (client.currentScreen instanceof InventoryScreen) {
client.setScreen(null);
} else if (client.currentScreen == null) {
client.setScreen(new InventoryScreen(client.player));
}
}
boolean inWorld = client.player != null && client.currentScreen == null;
if (inWorld && hotbarPrevious != hotbarNext
&& ((hotbarPrevious && !previousHotbarPrevious) || (hotbarNext && !previousHotbarNext))) {
int direction = hotbarPrevious ? -1 : 1;
int selectedSlot = client.player.getInventory().getSelectedSlot();
client.player.getInventory().setSelectedSlot(Math.floorMod(selectedSlot + direction, 9));
}
Stick leftStick = readStick(state, GLFW.GLFW_GAMEPAD_AXIS_LEFT_X, GLFW.GLFW_GAMEPAD_AXIS_LEFT_Y);
Stick rightStick = readStick(state, GLFW.GLFW_GAMEPAD_AXIS_RIGHT_X, GLFW.GLFW_GAMEPAD_AXIS_RIGHT_Y);
if (inWorld) {
client.player.setYaw(client.player.getYaw() + rightStick.x() * LOOK_SPEED);
client.player.setPitch(Math.max(-90.0f,
Math.min(90.0f, client.player.getPitch() + rightStick.y() * LOOK_SPEED)));
}
setPressed(client, client.options.forwardKey, inWorld && leftStick.y() < 0.0f);
setPressed(client, client.options.backKey, inWorld && leftStick.y() > 0.0f);
setPressed(client, client.options.leftKey, inWorld && leftStick.x() < 0.0f);
setPressed(client, client.options.rightKey, inWorld && leftStick.x() > 0.0f);
setPressed(client, client.options.jumpKey,
inWorld && isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_A));
setPressed(client, client.options.sneakKey,
inWorld && isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_B));
setPressed(client, client.options.sprintKey,
inWorld && isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_LEFT_THUMB));
setPressed(client, client.options.attackKey,
inWorld && (isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_X)
|| isTriggerDown(state, GLFW.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER)));
setPressed(client, client.options.useKey,
inWorld && (isButtonDown(state, GLFW.GLFW_GAMEPAD_BUTTON_Y)
|| isTriggerDown(state, GLFW.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER)));
previousStart = start;
previousBack = back;
previousHotbarPrevious = hotbarPrevious;
previousHotbarNext = hotbarNext;
}
private static boolean isButtonDown(GLFWGamepadState state, int button) {
return state != null && state.buttons(button) == GLFW.GLFW_PRESS;
}
private static boolean isTriggerDown(GLFWGamepadState state, int axis) {
return state != null && (state.axes(axis) + 1.0f) * 0.5f > TRIGGER_DEADZONE;
}
private static Stick readStick(GLFWGamepadState state, int xAxis, int yAxis) {
if (state == null) {
return new Stick(0.0f, 0.0f);
}
float x = state.axes(xAxis);
float y = state.axes(yAxis);
float magnitude = (float) Math.sqrt(x * x + y * y);
if (magnitude <= STICK_DEADZONE) {
return new Stick(0.0f, 0.0f);
}
float scaledMagnitude = Math.min(1.0f, (magnitude - STICK_DEADZONE) / (1.0f - STICK_DEADZONE));
float scale = scaledMagnitude / magnitude;
return new Stick(x * scale, y * scale);
}
private static void setPressed(MinecraftClient client, KeyBinding binding, boolean controllerPressed) {
binding.setPressed(controllerPressed || isPhysicallyPressed(client.getWindow(), binding));
}
private static boolean isPhysicallyPressed(Window window, KeyBinding binding) {
for (int key = 0; key <= GLFW.GLFW_KEY_LAST; key++) {
if (!InputUtil.isKeyPressed(window, key)) {
continue;
}
int scanCode = GLFW.glfwGetKeyScancode(key);
if (binding.matchesKey(new KeyInput(key, scanCode, 0))
|| (scanCode != 0 && binding.matchesKey(new KeyInput(GLFW.GLFW_KEY_UNKNOWN, scanCode, 0)))) {
return true;
}
}
for (int button = 0; button <= GLFW.GLFW_MOUSE_BUTTON_LAST; button++) {
if (GLFW.glfwGetMouseButton(window.getHandle(), button) == GLFW.GLFW_PRESS
&& binding.matchesMouse(new Click(0.0, 0.0, new MouseInput(button, 0)))) {
return true;
}
}
return false;
}
private record Stick(float x, float y) {
}
}