From dc14759147c8ff233b2949cf99a715c7028a7ce3 Mon Sep 17 00:00:00 2001 From: Yuki Date: Mon, 21 Mar 2022 10:11:21 +0000 Subject: [PATCH] Two simple bugfixes (GUI & Scanning) Fix so erroring doesn't cancel the entire scan Delta is no longer calculated while in a GUI. --- .../java/com/kuri0/rawinput/RawInput.java | 105 ++++++++++-------- 1 file changed, 60 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/kuri0/rawinput/RawInput.java b/src/main/java/com/kuri0/rawinput/RawInput.java index d52eabd..5cbc6ee 100644 --- a/src/main/java/com/kuri0/rawinput/RawInput.java +++ b/src/main/java/com/kuri0/rawinput/RawInput.java @@ -15,16 +15,15 @@ public class RawInput { public static final String MODID = "rawinput"; - public static final String VERSION = "1.1.1"; - + public static final String VERSION = "1.1.2"; + public static Mouse mouse; // Delta for mouse public static int dx = 0; public static int dy = 0; - + @SuppressWarnings("unchecked") - private static ControllerEnvironment createDefaultEnvironment() throws ReflectiveOperationException { - // Find constructor (class is package private, so we can't access it directly) + private static ControllerEnvironment createDefaultEnvironment() throws ReflectiveOperationException { // Find constructor (class is package private, so we can't access it directly) Constructor constructor = (Constructor) Class.forName("net.java.games.input.DefaultControllerEnvironment").getDeclaredConstructors()[0]; @@ -37,47 +36,63 @@ private static ControllerEnvironment createDefaultEnvironment() throws Reflectiv @EventHandler public void init(FMLInitializationEvent event) { - ClientCommandHandler.instance.registerCommand(new RescanCommand()); - Minecraft.getMinecraft().mouseHelper = new RawMouseHelper(); + ClientCommandHandler.instance.registerCommand(new RescanCommand()); + Minecraft.getMinecraft().mouseHelper = new RawMouseHelper(); + + Thread inputThread = new Thread(new Runnable() { + @Override + public void run() { + ControllerEnvironment enviro = null; + while (true) { + if (enviro == null) { + try { + enviro = createDefaultEnvironment(); + } catch(Exception e) { + e.printStackTrace(); + } + } else if (mouse == null) { + try { + Controller[] controllers = enviro.getControllers(); + for (Controller controller : controllers) { + try { + if (controller.getType() == Controller.Type.MOUSE) { + controller.poll(); + float px = ((Mouse) controller).getX().getPollData(); + float py = ((Mouse) controller).getY().getPollData(); + float eps = 0.1f; - Thread inputThread = new Thread(new Runnable() { - @Override - public void run() { - while (true) { - if (mouse == null) { - try { - Controller[] controllers; - controllers = createDefaultEnvironment().getControllers(); - for (int i = 0; i < controllers.length; i++) { - if (controllers[i].getType() == Controller.Type.MOUSE) { - controllers[i].poll(); - if (((Mouse)controllers[i]).getX().getPollData() != 0.0 || ((Mouse)controllers[i]).getY().getPollData() != 0.0) { - mouse = (Mouse)controllers[i]; - Minecraft.getMinecraft().player.sendMessage(new TextComponentString("Found mouse")); - } - } - } - } catch (ReflectiveOperationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } else { - mouse.poll(); - - dx += (int)mouse.getX().getPollData(); - dy += (int)mouse.getY().getPollData(); - } - try { - Thread.sleep(1); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } + // check if mouse is moving + if (px < -eps || px > eps || py < -eps || py > eps) { + mouse = (Mouse) controller; + Minecraft.getMinecraft().player.sendMessage(new TextComponentString("Found mouse")); + } + } + } catch (Exception e) { + // skip to next + e.printStackTrace(); + } + } + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } else { + mouse.poll(); + if (Minecraft.getMinecraft().currentScreen == null) { + dx += (int)mouse.getX().getPollData(); + dy += (int)mouse.getY().getPollData(); + } } - }); - inputThread.setName("inputThread"); - inputThread.start(); + try { + Thread.sleep(1); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + }); + inputThread.setName("inputThread"); + inputThread.start(); } } -