fix: resolve ConcurrentModificationException in InputManager.updateBindings

- Make BINDINGS list thread-safe using Collections.synchronizedList()
- Add synchronized blocks around all BINDINGS access methods
- Prevents crash on macOS clients during launch
- Fixes race condition between main thread and controller input thread
This commit is contained in:
Elijah Stephenson
2025-09-26 20:20:51 -05:00
parent 994cd0d155
commit af4b40e88a

View File

@@ -41,7 +41,7 @@ import static eu.midnightdust.midnightcontrols.client.MidnightControlsClient.cli
*/ */
public class InputManager { public class InputManager {
public static final InputManager INPUT_MANAGER = new InputManager(); public static final InputManager INPUT_MANAGER = new InputManager();
private static final List<ButtonBinding> BINDINGS = new ArrayList<>(); private static final List<ButtonBinding> BINDINGS = Collections.synchronizedList(new ArrayList<>());
private static final List<ButtonCategory> CATEGORIES = new ArrayList<>(); private static final List<ButtonCategory> CATEGORIES = new ArrayList<>();
public static final Int2ObjectMap<ButtonState> STATES = new Int2ObjectOpenHashMap<>(); public static final Int2ObjectMap<ButtonState> STATES = new Int2ObjectOpenHashMap<>();
public static final Int2FloatMap BUTTON_VALUES = new Int2FloatOpenHashMap(); public static final Int2FloatMap BUTTON_VALUES = new Int2FloatOpenHashMap();
@@ -109,8 +109,10 @@ public class InputManager {
* @return true if the binding is registered, else false * @return true if the binding is registered, else false
*/ */
public static boolean hasBinding(@NotNull ButtonBinding binding) { public static boolean hasBinding(@NotNull ButtonBinding binding) {
synchronized (BINDINGS) {
return BINDINGS.contains(binding); return BINDINGS.contains(binding);
} }
}
/** /**
* Returns whether the specified binding is registered or not. * Returns whether the specified binding is registered or not.
@@ -119,8 +121,10 @@ public class InputManager {
* @return true if the binding is registered, else false * @return true if the binding is registered, else false
*/ */
public static boolean hasBinding(@NotNull String name) { public static boolean hasBinding(@NotNull String name) {
synchronized (BINDINGS) {
return BINDINGS.parallelStream().map(ButtonBinding::getName).anyMatch(binding -> binding.equalsIgnoreCase(name)); return BINDINGS.parallelStream().map(ButtonBinding::getName).anyMatch(binding -> binding.equalsIgnoreCase(name));
} }
}
/** /**
* Returns whether the specified binding is registered or not. * Returns whether the specified binding is registered or not.
@@ -139,18 +143,22 @@ public class InputManager {
* @return true if the binding is registered, else false * @return true if the binding is registered, else false
*/ */
public static ButtonBinding getBinding(@NotNull String name) { public static ButtonBinding getBinding(@NotNull String name) {
synchronized (BINDINGS) {
if (BINDINGS.parallelStream().map(ButtonBinding::getName).anyMatch(binding -> binding.equalsIgnoreCase(name))) if (BINDINGS.parallelStream().map(ButtonBinding::getName).anyMatch(binding -> binding.equalsIgnoreCase(name)))
BINDINGS.forEach(binding -> { BINDINGS.forEach(binding -> {
if (binding.getName().equalsIgnoreCase(name)) InputManager.tempBinding = binding; if (binding.getName().equalsIgnoreCase(name)) InputManager.tempBinding = binding;
}); });
}
return tempBinding; return tempBinding;
} }
private static List<ButtonBinding> unboundBindings; private static List<ButtonBinding> unboundBindings;
public static List<ButtonBinding> getUnboundBindings() { public static List<ButtonBinding> getUnboundBindings() {
unboundBindings = new ArrayList<>(); unboundBindings = new ArrayList<>();
synchronized (BINDINGS) {
BINDINGS.forEach(binding -> { BINDINGS.forEach(binding -> {
if (binding.isNotBound() && !MidnightControlsConfig.ignoredUnboundKeys.contains(binding.getTranslationKey())) unboundBindings.add(binding); if (binding.isNotBound() && !MidnightControlsConfig.ignoredUnboundKeys.contains(binding.getTranslationKey())) unboundBindings.add(binding);
}); });
}
unboundBindings.sort(Comparator.comparing(s -> I18n.translate(s.getTranslationKey()))); unboundBindings.sort(Comparator.comparing(s -> I18n.translate(s.getTranslationKey())));
return unboundBindings; return unboundBindings;
} }
@@ -162,9 +170,11 @@ public class InputManager {
* @return the registered binding * @return the registered binding
*/ */
public static @NotNull ButtonBinding registerBinding(@NotNull ButtonBinding binding) { public static @NotNull ButtonBinding registerBinding(@NotNull ButtonBinding binding) {
if (hasBinding(binding)) synchronized (BINDINGS) {
if (BINDINGS.contains(binding))
throw new IllegalStateException("Cannot register twice a button binding in the registry."); throw new IllegalStateException("Cannot register twice a button binding in the registry.");
BINDINGS.add(binding); BINDINGS.add(binding);
}
return binding; return binding;
} }
@@ -284,8 +294,10 @@ public class InputManager {
* @return true if the button has duplicated bindings, else false * @return true if the button has duplicated bindings, else false
*/ */
public static boolean hasDuplicatedBindings(int[] button) { public static boolean hasDuplicatedBindings(int[] button) {
synchronized (BINDINGS) {
return BINDINGS.parallelStream().filter(binding -> areButtonsEquivalent(binding.getButton(), button)).count() > 1; return BINDINGS.parallelStream().filter(binding -> areButtonsEquivalent(binding.getButton(), button)).count() > 1;
} }
}
/** /**
* Returns whether the button has duplicated bindings. * Returns whether the button has duplicated bindings.
@@ -294,8 +306,10 @@ public class InputManager {
* @return true if the button has duplicated bindings, else false * @return true if the button has duplicated bindings, else false
*/ */
public static boolean hasDuplicatedBindings(ButtonBinding binding) { public static boolean hasDuplicatedBindings(ButtonBinding binding) {
synchronized (BINDINGS) {
return BINDINGS.parallelStream().filter(other -> areButtonsEquivalent(other.getButton(), binding.getButton()) && other.filter.equals(binding.filter)).count() > 1; return BINDINGS.parallelStream().filter(other -> areButtonsEquivalent(other.getButton(), binding.getButton()) && other.filter.equals(binding.filter)).count() > 1;
} }
}
/** /**
* Returns whether the specified buttons are equivalent or not. * Returns whether the specified buttons are equivalent or not.
@@ -347,6 +361,7 @@ public class InputManager {
record ButtonStateValue(ButtonState state, float value) { record ButtonStateValue(ButtonState state, float value) {
} }
var states = new Object2ObjectOpenHashMap<ButtonBinding, ButtonStateValue>(); var states = new Object2ObjectOpenHashMap<ButtonBinding, ButtonStateValue>();
synchronized (BINDINGS) {
for (var binding : BINDINGS) { for (var binding : BINDINGS) {
var state = binding.isAvailable() ? getBindingState(binding) : ButtonState.NONE; var state = binding.isAvailable() ? getBindingState(binding) : ButtonState.NONE;
if (skipButtons.intStream().anyMatch(btn -> containsButton(binding.getButton(), btn))) { if (skipButtons.intStream().anyMatch(btn -> containsButton(binding.getButton(), btn))) {
@@ -369,6 +384,7 @@ public class InputManager {
states.put(binding, new ButtonStateValue(state, value)); states.put(binding, new ButtonStateValue(state, value));
} }
}
states.forEach((binding, state) -> { states.forEach((binding, state) -> {
if (state.state() != ButtonState.NONE) { if (state.state() != ButtonState.NONE) {
@@ -387,8 +403,10 @@ public class InputManager {
} }
public static @NotNull Stream<ButtonBinding> streamBindings() { public static @NotNull Stream<ButtonBinding> streamBindings() {
synchronized (BINDINGS) {
return BINDINGS.stream(); return BINDINGS.stream();
} }
}
public static @NotNull Stream<ButtonCategory> streamCategories() { public static @NotNull Stream<ButtonCategory> streamCategories() {
return CATEGORIES.stream(); return CATEGORIES.stream();