37 lines
1.4 KiB
Java
37 lines
1.4 KiB
Java
package games.dmg.fantasystick.ability;
|
|
|
|
import java.util.Comparator;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
import java.util.Optional;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
/** Process-local validated definitions; saved unknown IDs remain character data, not executable definitions. */
|
|
public final class AbilityRegistry {
|
|
private final ConcurrentHashMap<String, AbilityDefinition> definitions = new ConcurrentHashMap<>();
|
|
|
|
public void register(AbilityDefinition definition) {
|
|
Objects.requireNonNull(definition);
|
|
if (definitions.putIfAbsent(definition.id(), definition) != null) {
|
|
throw new IllegalArgumentException("Duplicate ability ID: " + definition.id());
|
|
}
|
|
}
|
|
|
|
/** Replace one registered definition after an acknowledged runtime configuration save. */
|
|
public void replace(AbilityDefinition definition) {
|
|
Objects.requireNonNull(definition);
|
|
definitions.compute(definition.id(), (id, existing) -> {
|
|
if (existing == null) throw new IllegalArgumentException("Unknown ability ID: " + id);
|
|
return definition;
|
|
});
|
|
}
|
|
|
|
public Optional<AbilityDefinition> find(String id) {
|
|
return id == null ? Optional.empty() : Optional.ofNullable(definitions.get(id));
|
|
}
|
|
|
|
public List<AbilityDefinition> definitions() {
|
|
return definitions.values().stream().sorted(Comparator.comparing(AbilityDefinition::id)).toList();
|
|
}
|
|
}
|