MapSignal
During a team fight, kills are recorded per player name, and the scoreboard menu shows who is leading at the top.
Kill counts are naturally a "name â count" map. Holding it in a MutableSignal<Map<...>> means copying the whole map and calling set for every kill.
What MapSignal doesâ
A MapSignal is a Map you can subscribe to. put, remove, merge, compute, and the other modifying methods all notify whatever depends on it. MapSignal.of() returns a MutableMapSignal.
Mind the two gets: kills.get("Bob") is the Map method and reads one value; the no-argument kills.get() is the Signal method and returns the whole map.
A kill leaderboardâ
The leader depends on everyone's kills, so it depends on the whole map.
The match starts; nobody has a kill.
MutableMapSignal<String, Integer> kills = MapSignal.of();
Signal<String> leader = kills.map(map -> map.entrySet().stream()
.max(Map.Entry.comparingByValue())
.map(entry -> entry.getKey() + " (" + entry.getValue() + " kills)")
.orElse("Nobody yet")
);
kills.merge("Alice", 1, Integer::sum); // puts 1 when the key is missing
kills.merge("Bob", 1, Integer::sum);
kills.merge("Bob", 1, Integer::sum); // adds to it when present
System.out.println(kills.get("Bob")); // 2
System.out.println(leader.get()); // Bob (2 kills)



kills | {} | |
|---|---|---|
leader | Nobody yet |
When each display position only cares about one key, such as each player's head showing their own kills, Alice's change also refreshes Bob's display. Use a KeyedSignal for that, which notifies each key separately.
Wrapping data read from a configâ
The waypoint list is read from a config file, and the config loader returns a LinkedHashMap that keeps the config order. Wrap it with wrap, and waypoints added later go at the end.
record Waypoint(String world, int x, int y, int z) {}
Map<String, Waypoint> loaded = new LinkedHashMap<>();
loaded.put("Spawn", new Waypoint("world", 0, 64, 0));
loaded.put("Mines", new Waypoint("world", 320, 12, -150));
MutableMapSignal<String, Waypoint> waypoints = MapSignal.wrap(loaded);
Signal<List<String>> names = waypoints.map(map -> List.copyOf(map.keySet()));
waypoints.put("Nether Portal", new Waypoint("world", -88, 70, 42));
System.out.println(names.get()); // [Spawn, Mines, Nether Portal]
Waypoints store a world name and coordinates rather than a Location, because a Location holds a World object.
Backing map and hooksâ
MapSignal.of() is backed by a LinkedHashMap: insertion order, null keys and values allowed. For cross-thread access, wrap a ConcurrentHashMap, which has no iteration order and rejects null.
MutableMapSignal<String, Integer> kills = MapSignal.wrap(new ConcurrentHashMap<>());
MutableMapSignal provides batch with the same rules as ListSignal. Its hooks are named a little differently.
| Hook | When |
|---|---|
beforePut((key, value) -> ...) | Before a value is stored; returns the value actually stored |
afterRemove((key, value) -> ...) | After an entry is removed; receives the removed key and value |
For example, kills.beforePut((name, count) -> Math.max(0, count)) keeps kill counts from going negative. Overwriting a key runs afterRemove on the old value, then beforePut on the new one.
Caveatsâ
wrap does not copy the map. Calling loaded.put(...) directly notifies nobody. Changes through keySet(), values(), entrySet(), and Map.Entry.setValue go through the wrapper and do notify. Maps created with of() are not thread safe.
A put with hooks reads before writing, so it is not atomic on concurrent maps. The compute family runs hooks inside the remapping function, where they may run more than once and must not touch the same map. Keep the hook handles.