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);
}

View File

@ -1,5 +1,6 @@
package dev.isxander.controlify.config.gui;
import com.google.common.collect.Iterables;
import dev.isxander.controlify.Controlify;
import dev.isxander.controlify.bindings.IBind;
import dev.isxander.controlify.config.GlobalSettings;
@ -28,19 +29,12 @@ import net.minecraft.network.chat.Component;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class YACLHelper {
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 yacl = YetAnotherConfigLib.createBuilder()
@ -54,7 +48,7 @@ public class YACLHelper {
.name(Component.translatable("controlify.gui.current_controller"))
.tooltip(Component.translatable("controlify.gui.current_controller.tooltip"))
.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)
.build())
.option(Option.createBuilder(boolean.class)