1
0
forked from Clones/Controlify

broken controller input detection (close #28)

This commit is contained in:
isXander
2023-02-17 01:17:32 +00:00
parent 8e31472c07
commit 7e53fac611
3 changed files with 46 additions and 26 deletions

View File

@ -1,5 +1,6 @@
package dev.isxander.controlify;
import com.mojang.blaze3d.Blaze3D;
import com.mojang.logging.LogUtils;
import dev.isxander.controlify.controller.Controller;
import dev.isxander.controlify.controller.ControllerState;
@ -39,6 +40,9 @@ public class Controlify {
private final Queue<Controller<?, ?>> calibrationQueue = new ArrayDeque<>();
private int consecutiveInputSwitches = 0;
private double lastInputSwitchTime = 0;
public void initializeControllers() {
LOGGER.info("Discovering and initializing controllers...");
@ -139,6 +143,18 @@ public class Controlify {
if (state.hasAnyInput())
this.setCurrentInputMode(InputMode.CONTROLLER);
if (consecutiveInputSwitches > 20) {
LOGGER.warn("Controlify detected current controller to be constantly giving input and has been disabled.");
minecraft.getToasts().addToast(SystemToast.multiline(
minecraft,
SystemToast.SystemToastIds.PERIODIC_NOTIFICATION,
Component.translatable("controlify.toast.faulty_input.title"),
Component.translatable("controlify.toast.faulty_input.description")
));
this.setCurrentController(null);
consecutiveInputSwitches = 0;
}
if (currentController == null) {
this.setCurrentInputMode(InputMode.KEYBOARD_MOUSE);
return;
@ -159,16 +175,22 @@ public class Controlify {
}
public Controller<?, ?> currentController() {
if (currentController == null)
return Controller.DUMMY;
return currentController;
}
public void setCurrentController(Controller<?, ?> controller) {
if (controller == null)
controller = Controller.DUMMY;
if (this.currentController == controller) return;
this.currentController = controller;
this.inGameInputHandler = new InGameInputHandler(this.currentController != null ? controller : Controller.DUMMY);
this.inGameInputHandler = new InGameInputHandler(controller);
if (Minecraft.getInstance().player != null) {
this.inGameButtonGuide = new InGameButtonGuide(this.currentController != null ? controller : Controller.DUMMY, Minecraft.getInstance().player);
this.inGameButtonGuide = new InGameButtonGuide(controller, Minecraft.getInstance().player);
}
}
@ -209,6 +231,13 @@ public class Controlify {
this.inGameButtonGuide = new InGameButtonGuide(this.currentController != null ? currentController : Controller.DUMMY, Minecraft.getInstance().player);
}
if (Blaze3D.getTime() - lastInputSwitchTime < 20) {
consecutiveInputSwitches++;
} else {
consecutiveInputSwitches = 0;
}
lastInputSwitchTime = Blaze3D.getTime();
ControlifyEvents.INPUT_MODE_CHANGED.invoker().onInputModeChanged(currentInputMode);
}