From f373b3fc27f2926df144f1178f0d6a74786c1734 Mon Sep 17 00:00:00 2001 From: isXander Date: Sun, 4 Jun 2023 12:35:53 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20crashing=20with=20some=20j?= =?UTF-8?q?oysticks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/joystick/JoystickState.java | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/main/java/dev/isxander/controlify/controller/joystick/JoystickState.java b/src/main/java/dev/isxander/controlify/controller/joystick/JoystickState.java index e2cf962..bc5e65a 100644 --- a/src/main/java/dev/isxander/controlify/controller/joystick/JoystickState.java +++ b/src/main/java/dev/isxander/controlify/controller/joystick/JoystickState.java @@ -12,10 +12,7 @@ import org.lwjgl.glfw.GLFW; import java.nio.ByteBuffer; import java.nio.FloatBuffer; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; +import java.util.*; import java.util.stream.IntStream; public class JoystickState implements ControllerState { @@ -73,32 +70,32 @@ public class JoystickState implements ControllerState { } public static JoystickState fromJoystick(JoystickController joystick, int joystickId) { - FloatBuffer axesBuffer = GLFW.glfwGetJoystickAxes(joystickId); - float[] inAxes = new float[axesBuffer.limit()]; + Optional axesBuffer = Optional.ofNullable(GLFW.glfwGetJoystickAxes(joystickId)); + float[] inAxes = new float[axesBuffer.map(FloatBuffer::limit).orElse(0)]; { int i = 0; - while (axesBuffer.hasRemaining()) { - inAxes[i] = axesBuffer.get(); + while (axesBuffer.isPresent() && axesBuffer.get().hasRemaining()) { + inAxes[i] = axesBuffer.get().get(); i++; } } - ByteBuffer buttonBuffer = GLFW.glfwGetJoystickButtons(joystickId); - boolean[] inButtons = new boolean[buttonBuffer.limit()]; + Optional buttonBuffer = Optional.ofNullable(GLFW.glfwGetJoystickButtons(joystickId)); + boolean[] inButtons = new boolean[axesBuffer.map(FloatBuffer::limit).orElse(0)]; { int i = 0; - while (buttonBuffer.hasRemaining()) { - inButtons[i] = buttonBuffer.get() == GLFW.GLFW_PRESS; + while (buttonBuffer.isPresent() && buttonBuffer.get().hasRemaining()) { + inButtons[i] = buttonBuffer.get().get() == GLFW.GLFW_PRESS; i++; } } - ByteBuffer hatBuffer = GLFW.glfwGetJoystickHats(joystickId); - HatState[] inHats = new HatState[hatBuffer.limit()]; + Optional hatBuffer = Optional.ofNullable(GLFW.glfwGetJoystickHats(joystickId)); + HatState[] inHats = new HatState[hatBuffer.map(ByteBuffer::limit).orElse(0)]; { int i = 0; - while (hatBuffer.hasRemaining()) { - var state = switch (hatBuffer.get()) { + while (hatBuffer.isPresent() && hatBuffer.get().hasRemaining()) { + var state = switch (hatBuffer.get().get()) { case GLFW.GLFW_HAT_CENTERED -> HatState.CENTERED; case GLFW.GLFW_HAT_UP -> HatState.UP; case GLFW.GLFW_HAT_RIGHT -> HatState.RIGHT;