Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two simple bugfixes (GUI & Scanning) #7

Open
wants to merge 1 commit into
base: 1.12.2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 60 additions & 45 deletions src/main/java/com/kuri0/rawinput/RawInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ControllerEnvironment> constructor = (Constructor<ControllerEnvironment>)
Class.forName("net.java.games.input.DefaultControllerEnvironment").getDeclaredConstructors()[0];

Expand All @@ -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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (Math.abs(px) > eps || Math.abs(y) > 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();
}
}