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; package dev.isxander.controlify;
import com.mojang.blaze3d.Blaze3D;
import com.mojang.logging.LogUtils; import com.mojang.logging.LogUtils;
import dev.isxander.controlify.controller.Controller; import dev.isxander.controlify.controller.Controller;
import dev.isxander.controlify.controller.ControllerState; import dev.isxander.controlify.controller.ControllerState;
@ -39,6 +40,9 @@ public class Controlify {
private final Queue<Controller<?, ?>> calibrationQueue = new ArrayDeque<>(); private final Queue<Controller<?, ?>> calibrationQueue = new ArrayDeque<>();
private int consecutiveInputSwitches = 0;
private double lastInputSwitchTime = 0;
public void initializeControllers() { public void initializeControllers() {
LOGGER.info("Discovering and initializing controllers..."); LOGGER.info("Discovering and initializing controllers...");
@ -139,6 +143,18 @@ public class Controlify {
if (state.hasAnyInput()) if (state.hasAnyInput())
this.setCurrentInputMode(InputMode.CONTROLLER); 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) { if (currentController == null) {
this.setCurrentInputMode(InputMode.KEYBOARD_MOUSE); this.setCurrentInputMode(InputMode.KEYBOARD_MOUSE);
return; return;
@ -159,16 +175,22 @@ public class Controlify {
} }
public Controller<?, ?> currentController() { public Controller<?, ?> currentController() {
if (currentController == null)
return Controller.DUMMY;
return currentController; return currentController;
} }
public void setCurrentController(Controller<?, ?> controller) { public void setCurrentController(Controller<?, ?> controller) {
if (controller == null)
controller = Controller.DUMMY;
if (this.currentController == controller) return; if (this.currentController == controller) return;
this.currentController = controller; this.currentController = controller;
this.inGameInputHandler = new InGameInputHandler(this.currentController != null ? controller : Controller.DUMMY); this.inGameInputHandler = new InGameInputHandler(controller);
if (Minecraft.getInstance().player != null) { 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); 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); ControlifyEvents.INPUT_MODE_CHANGED.invoker().onInputModeChanged(currentInputMode);
} }

View File

@ -1,5 +1,6 @@
package dev.isxander.controlify.config.gui; package dev.isxander.controlify.config.gui;
import com.google.common.collect.Iterables;
import dev.isxander.controlify.Controlify; import dev.isxander.controlify.Controlify;
import dev.isxander.controlify.bindings.IBind; import dev.isxander.controlify.bindings.IBind;
import dev.isxander.controlify.config.GlobalSettings; import dev.isxander.controlify.config.GlobalSettings;
@ -28,19 +29,12 @@ import net.minecraft.network.chat.Component;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
public class YACLHelper { public class YACLHelper {
public static Screen generateConfigScreen(Screen parent) { public static Screen generateConfigScreen(Screen parent) {
if (Controlify.instance().currentController() == null) {
return new AlertScreen(
() -> Minecraft.getInstance().setScreen(parent),
Component.translatable("controlify.gui.error.title").withStyle(ChatFormatting.RED, ChatFormatting.BOLD),
Component.translatable("controlify.gui.error.message").withStyle(ChatFormatting.RED)
);
}
var controlify = Controlify.instance(); var controlify = Controlify.instance();
var yacl = YetAnotherConfigLib.createBuilder() var yacl = YetAnotherConfigLib.createBuilder()
@ -54,7 +48,7 @@ public class YACLHelper {
.name(Component.translatable("controlify.gui.current_controller")) .name(Component.translatable("controlify.gui.current_controller"))
.tooltip(Component.translatable("controlify.gui.current_controller.tooltip")) .tooltip(Component.translatable("controlify.gui.current_controller.tooltip"))
.binding(Controlify.instance().currentController(), () -> Controlify.instance().currentController(), v -> Controlify.instance().setCurrentController(v)) .binding(Controlify.instance().currentController(), () -> Controlify.instance().currentController(), v -> Controlify.instance().setCurrentController(v))
.controller(opt -> new CyclingListController<>(opt, Controller.CONTROLLERS.values(), c -> Component.literal(c.name()))) .controller(opt -> new CyclingListController<>(opt, Iterables.concat(List.of(Controller.DUMMY), Controller.CONTROLLERS.values()), c -> Component.literal(c == Controller.DUMMY ? "Disabled" : c.name())))
.instant(true) .instant(true)
.build()) .build())
.option(Option.createBuilder(boolean.class) .option(Option.createBuilder(boolean.class)

View File

@ -51,9 +51,6 @@
"controlify.gui.format.hold_toggle.toggle": "Toggle", "controlify.gui.format.hold_toggle.toggle": "Toggle",
"controlify.gui.format.open": "OPEN URL", "controlify.gui.format.open": "OPEN URL",
"controlify.gui.error.title": "Could not open Controlify settings",
"controlify.gui.error.message": "You cannot change Controlify settings when you have no controllers connected. Please connect a controller first.",
"controlify.gui.button": "Controller Settings...", "controlify.gui.button": "Controller Settings...",
"controlify.toast.vmouse_enabled.title": "Virtual Mouse Enabled", "controlify.toast.vmouse_enabled.title": "Virtual Mouse Enabled",
@ -66,6 +63,8 @@
"controlify.toast.controller_disconnected.description": "'%s' was disconnected.", "controlify.toast.controller_disconnected.description": "'%s' was disconnected.",
"controlify.toast.controller_calibration.title": "New controller detected", "controlify.toast.controller_calibration.title": "New controller detected",
"controlify.toast.controller_calibration.description": "A new controller(s) has been detected, you must calibrate before you use it!", "controlify.toast.controller_calibration.description": "A new controller(s) has been detected, you must calibrate before you use it!",
"controlify.toast.faulty_input.title": "Controller disabled",
"controlify.toast.faulty_input.description": "Your controller has been disabled because Controlify detected it was causing you problems using keyboard and mouse input. This is likely due to setting your deadzone values too low or your joystick is not mapped properly, making the controller think it is always giving input.",
"controlify.controller_theme.default": "Default", "controlify.controller_theme.default": "Default",
"controlify.controller_theme.xbox_one": "Xbox", "controlify.controller_theme.xbox_one": "Xbox",
@ -163,20 +162,18 @@
"controlify.joystick_mapping.xbox_one.axis.right_trigger.up": "Up", "controlify.joystick_mapping.xbox_one.axis.right_trigger.up": "Up",
"controlify.joystick_mapping.xbox_one.axis.right_trigger.down": "Down", "controlify.joystick_mapping.xbox_one.axis.right_trigger.down": "Down",
"controlify.joystick_mapping.xbox_one.button.a": "A", "controlify.joystick_mapping.xbox_one.button.a": "A",
"controlify.joystick_mapping.xbox_one.button.b": "B", "controlify.joystick_mapping.xbox_one.button.b": "B",
"controlify.joystick_mapping.xbox_one.button.x": "X", "controlify.joystick_mapping.xbox_one.button.x": "X",
"controlify.joystick_mapping.xbox_one.button.y": "Y", "controlify.joystick_mapping.xbox_one.button.y": "Y",
"controlify.joystick_mapping.xbox_one.button.left_bumper": "Left Bumper", "controlify.joystick_mapping.xbox_one.button.left_bumper": "Left Bumper",
"controlify.joystick_mapping.xbox_one.button.right_bumper": "Right Bumper", "controlify.joystick_mapping.xbox_one.button.right_bumper": "Right Bumper",
"controlify.joystick_mapping.xbox_one.button.left_stick": "Left Stick Press", "controlify.joystick_mapping.xbox_one.button.left_stick": "Left Stick Press",
"controlify.joystick_mapping.xbox_one.button.right_stick": "Right Stick Press", "controlify.joystick_mapping.xbox_one.button.right_stick": "Right Stick Press",
"controlify.joystick_mapping.xbox_one.button.back": "Back", "controlify.joystick_mapping.xbox_one.button.back": "Back",
"controlify.joystick_mapping.xbox_one.button.start": "Start", "controlify.joystick_mapping.xbox_one.button.start": "Start",
"controlify.joystick_mapping.xbox_one.button.guide": "Guide", "controlify.joystick_mapping.xbox_one.button.guide": "Guide",
"controlify.joystick_mapping.xbox_one.hat.dpad": "D-Pad", "controlify.joystick_mapping.xbox_one.hat.dpad": "D-Pad",
"controlify.calibration.title": "Controller Calibration for '%s'", "controlify.calibration.title": "Controller Calibration for '%s'",
"controlify.calibration.info": "This process will optimize settings for your controller to prevent stick drift. Stick drift happens in your controller thumbsticks and outputs slightly wrong values when you aren't touching them at all. Deadzones are used to prevent this.\n\nThis will only take a few seconds.", "controlify.calibration.info": "This process will optimize settings for your controller to prevent stick drift. Stick drift happens in your controller thumbsticks and outputs slightly wrong values when you aren't touching them at all. Deadzones are used to prevent this.\n\nThis will only take a few seconds.",
"controlify.calibration.wait": "Please do not touch your controller thumbsticks until the progress bar is complete. This process will only take a few seconds.", "controlify.calibration.wait": "Please do not touch your controller thumbsticks until the progress bar is complete. This process will only take a few seconds.",