Skip to content

Commit aee4238

Browse files
committed
most stuff
1 parent ccd2d53 commit aee4238

File tree

11 files changed

+370
-32
lines changed

11 files changed

+370
-32
lines changed

src/main/kotlin/com/github/subat0m1c/rawinput/ExampleMod.kt

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.github.subat0m1c.rawinput
2+
3+
import com.github.subat0m1c.rawinput.RawInputMain.Companion.mc
4+
import com.github.subat0m1c.rawinput.RawInputMain.Companion.scope
5+
import com.github.subat0m1c.rawinput.commands.ConfigCommand.Companion.waitTime
6+
import com.github.subat0m1c.rawinput.commands.ToggleCommand.Companion.enabled
7+
import kotlinx.coroutines.Job
8+
import kotlinx.coroutines.cancel
9+
import kotlinx.coroutines.launch
10+
import net.java.games.input.Controller
11+
import net.java.games.input.ControllerEnvironment
12+
import net.java.games.input.Mouse
13+
import net.minecraft.util.ChatComponentText
14+
import net.minecraft.util.IChatComponent
15+
16+
object RawInput {
17+
var rawInputJob: Job? = null
18+
19+
var dx = 0
20+
var dy = 0
21+
var controllers: Array<Controller>? = null
22+
var mouse: Mouse? = null
23+
24+
fun closeRawInput(message: String) {
25+
rawInputJob?.cancel(message)
26+
rawInputJob = null
27+
}
28+
29+
fun relaunchRawInput(message: String) {
30+
rawInputJob?.cancel(message)
31+
controllers = ControllerEnvironment.getDefaultEnvironment().controllers
32+
rawInputJob = launchRawInput()
33+
}
34+
35+
private fun launchRawInput() = scope.launch {
36+
controllers?.let { controllers ->
37+
while (enabled) {
38+
if (mouse == null) {
39+
for (controller in controllers) {
40+
if (controller.type == Controller.Type.MOUSE) {
41+
controller.poll()
42+
val mouseController = controller as Mouse
43+
if (mouseController.x.pollData != 0f || mouseController.y.pollData != 0f) {
44+
mouse = mouseController
45+
mc.thePlayer?.addChatMessage(ChatComponentText("Mouse found: ${mouseController.name}") as IChatComponent) ?: println("Mouse found: ${mouseController.name}")
46+
break
47+
}
48+
}
49+
}
50+
}
51+
52+
mouse?.let { mouse ->
53+
mouse.poll()
54+
dx += mouse.x.pollData.toInt()
55+
dy += mouse.y.pollData.toInt()
56+
}
57+
58+
Thread.sleep(0, waitTime)
59+
}
60+
}
61+
}
62+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.github.subat0m1c.rawinput
2+
3+
import com.github.subat0m1c.rawinput.RawInput.relaunchRawInput
4+
import com.github.subat0m1c.rawinput.commands.ConfigCommand
5+
import com.github.subat0m1c.rawinput.commands.RescanCommand
6+
import com.github.subat0m1c.rawinput.commands.ToggleCommand
7+
import com.github.subat0m1c.rawinput.commands.ToggleCommand.Companion.enabled
8+
import com.github.subat0m1c.rawinput.config.ConfigManager
9+
import kotlinx.coroutines.*
10+
import net.minecraft.client.Minecraft
11+
import net.minecraftforge.client.ClientCommandHandler
12+
import net.minecraftforge.common.MinecraftForge
13+
import net.minecraftforge.fml.common.Mod
14+
import net.minecraftforge.fml.common.event.FMLInitializationEvent
15+
16+
@Mod(modid = "rawinput", useMetadata = true)
17+
class RawInputMain {
18+
@Mod.EventHandler
19+
fun init(event: FMLInitializationEvent) {
20+
MinecraftForge.EVENT_BUS.register(this)
21+
setOf(ToggleCommand(), RescanCommand(), ConfigCommand()).forEach { ClientCommandHandler.instance.registerCommand(it) }
22+
ConfigManager.awaitLoad()
23+
24+
if (enabled) {
25+
mc.mouseHelper = RawMouseHelper()
26+
relaunchRawInput("Initial rawinput launch")
27+
}
28+
}
29+
30+
companion object {
31+
val mc = Minecraft.getMinecraft()
32+
val scope = CoroutineScope(Dispatchers.Default + SupervisorJob())
33+
}
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.subat0m1c.rawinput
2+
3+
import com.github.subat0m1c.rawinput.commands.ConfigCommand.Companion.maxChange
4+
import net.minecraft.util.MouseHelper
5+
import kotlin.math.abs
6+
7+
class RawMouseHelper : MouseHelper() {
8+
override fun mouseXYChange() {
9+
if ((abs(RawInput.dx) > maxChange || abs(RawInput.dy) > maxChange) && maxChange > 0) {
10+
RawInput.dx = 0
11+
RawInput.dy = 0
12+
return
13+
}
14+
deltaX = RawInput.dx
15+
RawInput.dx = 0
16+
deltaY = -RawInput.dy
17+
RawInput.dy = 0
18+
}
19+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.github.subat0m1c.rawinput.commands
2+
3+
import com.github.subat0m1c.rawinput.RawInput.relaunchRawInput
4+
import com.github.subat0m1c.rawinput.config.Config
5+
import com.github.subat0m1c.rawinput.config.ConfigManager
6+
import net.minecraft.command.CommandBase
7+
import net.minecraft.command.ICommandSender
8+
import net.minecraft.util.ChatComponentText
9+
10+
class ConfigCommand : CommandBase() {
11+
override fun getCommandName(): String = "rawconfig"
12+
13+
override fun getCommandUsage(sender: ICommandSender?): String = "Usage: /rawconfig <waittime|maxchange|view> <int?>"
14+
15+
override fun processCommand(sender: ICommandSender?, args: Array<out String>?) {
16+
if (args == null || args.isEmpty()) {
17+
sender?.addChatMessage(ChatComponentText("Usage: /rawconfig <waittime|maxchange|view> <int?>"))
18+
return
19+
}
20+
21+
when (args.getOrNull(0)) {
22+
"waittime" -> {
23+
if (args.size < 2) {
24+
sender?.addChatMessage(ChatComponentText("Usage: /rawconfig waittime <int>"))
25+
return
26+
}
27+
args[1].toIntOrNull()?.let {
28+
waitTime = it
29+
ConfigManager.save()
30+
relaunchRawInput("Wait time updated")
31+
sender?.addChatMessage(ChatComponentText("Wait time set to $waitTime"))
32+
}
33+
}
34+
"maxchange" -> {
35+
if (args.size < 2) {
36+
sender?.addChatMessage(ChatComponentText("Usage: /rawconfig maxchange <int>"))
37+
return
38+
}
39+
args[1].toIntOrNull()?.let {
40+
maxChange = it
41+
ConfigManager.save()
42+
sender?.addChatMessage(ChatComponentText("Max change set to $maxChange"))
43+
}
44+
}
45+
"view" -> {
46+
sender?.addChatMessage(ChatComponentText("Current wait time: $waitTime"))
47+
sender?.addChatMessage(ChatComponentText("Current max change: $maxChange"))
48+
}
49+
else -> {
50+
sender?.addChatMessage(ChatComponentText("Unknown command: ${args[0]}. Usage: /rawconfig <waittime|maxchange|view> <int?>"))
51+
return
52+
}
53+
}
54+
}
55+
56+
override fun getRequiredPermissionLevel(): Int = 0
57+
58+
companion object {
59+
var waitTime by Config("waitTime", Config.intSetting(500000))
60+
var maxChange by Config("maxChange", Config.intSetting(-1))
61+
}
62+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.github.subat0m1c.rawinput.commands
2+
3+
import com.github.subat0m1c.rawinput.RawInput
4+
import com.github.subat0m1c.rawinput.RawInput.relaunchRawInput
5+
import net.java.games.input.ControllerEnvironment
6+
import net.minecraft.command.CommandBase
7+
import net.minecraft.command.ICommandSender
8+
import net.minecraft.util.ChatComponentText
9+
import net.minecraft.util.IChatComponent
10+
11+
12+
class RescanCommand : CommandBase() {
13+
override fun getCommandName(): String = "rescan"
14+
override fun getCommandUsage(sender: ICommandSender): String = "Rescans input devices: /rescan <view?|reset?>"
15+
16+
override fun processCommand(sender: ICommandSender?, args: Array<out String>?) {
17+
if (args != null && args.getOrNull(0) != null) {
18+
when (args[0]) {
19+
"view" -> {
20+
ControllerEnvironment.getDefaultEnvironment().controllers.forEach { controller ->
21+
sender?.addChatMessage(ChatComponentText("Controller: ${controller.name} (${controller.type})"))
22+
}
23+
}
24+
"reset" -> {
25+
relaunchRawInput("Reset input job command called.")
26+
}
27+
}
28+
}
29+
sender?.addChatMessage(ChatComponentText("Rescanning input devices...") as IChatComponent)
30+
RawInput.mouse = null
31+
}
32+
33+
override fun getRequiredPermissionLevel(): Int = 0
34+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.subat0m1c.rawinput.commands
2+
3+
import com.github.subat0m1c.rawinput.RawInput.closeRawInput
4+
import com.github.subat0m1c.rawinput.RawInput.relaunchRawInput
5+
import com.github.subat0m1c.rawinput.RawInputMain.Companion.mc
6+
import com.github.subat0m1c.rawinput.RawMouseHelper
7+
import com.github.subat0m1c.rawinput.config.Config
8+
import com.github.subat0m1c.rawinput.config.ConfigManager
9+
import net.minecraft.command.CommandBase
10+
import net.minecraft.command.ICommandSender
11+
import net.minecraft.util.ChatComponentText
12+
import net.minecraft.util.IChatComponent
13+
import net.minecraft.util.MouseHelper
14+
15+
16+
class ToggleCommand : CommandBase() {
17+
override fun getCommandName(): String = "rawinput"
18+
override fun getCommandUsage(sender: ICommandSender?): String = "Toggles Raw Input (/rawinput)"
19+
20+
override fun processCommand(sender: ICommandSender?, args: Array<out String>?) {
21+
enabled = !enabled
22+
ConfigManager.save()
23+
if (mc.mouseHelper is RawMouseHelper) {
24+
mc.mouseHelper = MouseHelper()
25+
closeRawInput("Toggled rawinput off")
26+
sender?.addChatMessage(ChatComponentText("Toggled OFF.") as IChatComponent)
27+
} else {
28+
mc.mouseHelper = RawMouseHelper()
29+
relaunchRawInput("Toggled rawinput on")
30+
sender?.addChatMessage(ChatComponentText("Toggled ON.") as IChatComponent)
31+
}
32+
}
33+
34+
override fun getRequiredPermissionLevel(): Int = 0
35+
36+
companion object {
37+
var enabled by Config("enabled", Config.booleanSetting(true))
38+
}
39+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.github.subat0m1c.rawinput.config
2+
3+
import com.google.gson.JsonElement
4+
import com.google.gson.JsonPrimitive
5+
import kotlin.reflect.KProperty
6+
7+
class Config <T : Any> (val name: String, private val Json: JsonManager<T>) {
8+
var value = Json.default
9+
10+
init {
11+
ConfigManager.settings.add(this)
12+
}
13+
14+
operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
15+
return value
16+
}
17+
18+
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
19+
this.value = value
20+
}
21+
22+
fun set(value: JsonElement) {
23+
this.value = Json.read(value)
24+
}
25+
26+
fun get() = Json.write(value)
27+
28+
companion object {
29+
fun booleanSetting(default: Boolean) = JsonManager(
30+
default,
31+
{ Json: JsonElement -> Json.asBoolean },
32+
{ value: Boolean -> JsonPrimitive(value) }
33+
)
34+
35+
fun intSetting(default: Int) = JsonManager(
36+
default,
37+
{ Json: JsonElement -> Json.asInt },
38+
{ value: Int -> JsonPrimitive(value) }
39+
)
40+
}
41+
}
42+
43+
data class JsonManager <T : Any> (val default: T, val read: (JsonElement) -> T, val write: (T) -> JsonElement)

0 commit comments

Comments
 (0)