Skip to content

Commit

Permalink
fix: Fix thread safety in ManagedKeyMappingRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Jun 14, 2024
1 parent 963c718 commit 9128568
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
import net.blay09.mods.kuma.api.ManagedKeyMapping;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ManagedKeyMappingRegistry {
public static final Comparator<ManagedKeyMapping> COMPARATOR = Comparator.comparingInt((ManagedKeyMapping it) -> it.getBinding().modifiers().size())
.reversed();

private static final List<ManagedKeyMapping> keyMappings = new ArrayList<>();
private static final List<ManagedKeyMapping> keyMappings = Collections.synchronizedList(new ArrayList<>());

public static void register(ManagedKeyMapping keyMapping) {
keyMappings.add(keyMapping);
keyMappings.sort(COMPARATOR);
}

public static List<ManagedKeyMapping> getKeyMappings() {
return keyMappings.stream().sorted(COMPARATOR).toList();
synchronized (keyMappings) {
return keyMappings.stream().sorted(COMPARATOR).toList();
}
}
}

0 comments on commit 9128568

Please sign in to comment.