mirror of
https://github.com/TeamMidnightDust/MidnightControls.git
synced 2025-12-13 23:25:10 +01:00
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:
@@ -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,7 +109,9 @@ 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) {
|
||||||
return BINDINGS.contains(binding);
|
synchronized (BINDINGS) {
|
||||||
|
return BINDINGS.contains(binding);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -119,7 +121,9 @@ 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) {
|
||||||
return BINDINGS.parallelStream().map(ButtonBinding::getName).anyMatch(binding -> binding.equalsIgnoreCase(name));
|
synchronized (BINDINGS) {
|
||||||
|
return BINDINGS.parallelStream().map(ButtonBinding::getName).anyMatch(binding -> binding.equalsIgnoreCase(name));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -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) {
|
||||||
if (BINDINGS.parallelStream().map(ButtonBinding::getName).anyMatch(binding -> binding.equalsIgnoreCase(name)))
|
synchronized (BINDINGS) {
|
||||||
BINDINGS.forEach(binding -> {
|
if (BINDINGS.parallelStream().map(ButtonBinding::getName).anyMatch(binding -> binding.equalsIgnoreCase(name)))
|
||||||
if (binding.getName().equalsIgnoreCase(name)) InputManager.tempBinding = binding;
|
BINDINGS.forEach(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<>();
|
||||||
BINDINGS.forEach(binding -> {
|
synchronized (BINDINGS) {
|
||||||
if (binding.isNotBound() && !MidnightControlsConfig.ignoredUnboundKeys.contains(binding.getTranslationKey())) unboundBindings.add(binding);
|
BINDINGS.forEach(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) {
|
||||||
throw new IllegalStateException("Cannot register twice a button binding in the registry.");
|
if (BINDINGS.contains(binding))
|
||||||
BINDINGS.add(binding);
|
throw new IllegalStateException("Cannot register twice a button binding in the registry.");
|
||||||
|
BINDINGS.add(binding);
|
||||||
|
}
|
||||||
return binding;
|
return binding;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -284,7 +294,9 @@ 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) {
|
||||||
return BINDINGS.parallelStream().filter(binding -> areButtonsEquivalent(binding.getButton(), button)).count() > 1;
|
synchronized (BINDINGS) {
|
||||||
|
return BINDINGS.parallelStream().filter(binding -> areButtonsEquivalent(binding.getButton(), button)).count() > 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -294,7 +306,9 @@ 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) {
|
||||||
return BINDINGS.parallelStream().filter(other -> areButtonsEquivalent(other.getButton(), binding.getButton()) && other.filter.equals(binding.filter)).count() > 1;
|
synchronized (BINDINGS) {
|
||||||
|
return BINDINGS.parallelStream().filter(other -> areButtonsEquivalent(other.getButton(), binding.getButton()) && other.filter.equals(binding.filter)).count() > 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -347,7 +361,8 @@ 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>();
|
||||||
for (var binding : BINDINGS) {
|
synchronized (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))) {
|
||||||
if (binding.isPressed())
|
if (binding.isPressed())
|
||||||
@@ -368,6 +383,7 @@ public class InputManager {
|
|||||||
float value = getBindingValue(binding, state);
|
float value = getBindingValue(binding, state);
|
||||||
|
|
||||||
states.put(binding, new ButtonStateValue(state, value));
|
states.put(binding, new ButtonStateValue(state, value));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
states.forEach((binding, state) -> {
|
states.forEach((binding, state) -> {
|
||||||
@@ -387,7 +403,9 @@ public class InputManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static @NotNull Stream<ButtonBinding> streamBindings() {
|
public static @NotNull Stream<ButtonBinding> streamBindings() {
|
||||||
return BINDINGS.stream();
|
synchronized (BINDINGS) {
|
||||||
|
return BINDINGS.stream();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static @NotNull Stream<ButtonCategory> streamCategories() {
|
public static @NotNull Stream<ButtonCategory> streamCategories() {
|
||||||
|
|||||||
Reference in New Issue
Block a user