-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathRawInput.java
More file actions
95 lines (84 loc) · 3.19 KB
/
Copy pathRawInput.java
File metadata and controls
95 lines (84 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.kuri0.rawinput;
import net.minecraft.client.Minecraft;
import net.minecraft.util.ChatComponentText;
import net.minecraftforge.client.ClientCommandHandler;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import java.lang.reflect.Constructor;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;
import net.java.games.input.Mouse;
@Mod(modid = RawInput.MODID, version = RawInput.VERSION)
public class RawInput
{
public static final String MODID = "rawinput";
public static final String VERSION = "1.1.1";
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)
Constructor<ControllerEnvironment> constructor = (Constructor<ControllerEnvironment>)
Class.forName("net.java.games.input.DefaultControllerEnvironment").getDeclaredConstructors()[0];
// Constructor is package private, so we have to deactivate access control checks
constructor.setAccessible(true);
// Create object with default constructor
return constructor.newInstance();
}
@EventHandler
public void init(FMLInitializationEvent event)
{
ClientCommandHandler.instance.registerCommand(new RescanCommand());
Minecraft.getMinecraft().mouseHelper = new RawMouseHelper();
Thread inputThread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (mouse == null) {
try {
Controller[] controllers;
controllers = createDefaultEnvironment().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;
// check if mouse is moving
if ((-eps < px && px < eps) || (-eps < py && py < eps)) {
mouse = (Mouse) controller;
Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("Found mouse"));
}
}
}
catch (Exception e) {
// skip to next
e.printStackTrace();
}
}
} 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();
}
}
}
});
inputThread.setName("inputThread");
inputThread.start();
}
}