refactor(command): simplify region lookup in GravityFlip commands

- Replaced manual region search loops with a new `find` method in RegionRegistry for improved readability and efficiency.
- Updated `GravityFlipToggleSubCommand` and `GravityFlipTpSubCommand` to utilize the new method for finding regions by name.
- Enhanced the `add` method in RegionRegistry to use the `find` method for checking existing region names.
This commit is contained in:
2026-04-24 18:07:34 +02:00
parent 24ee43b3a3
commit c20bf42c36
4 changed files with 17 additions and 23 deletions
@@ -78,13 +78,20 @@ public final class RegionRegistry {
return out;
}
/** Returns the region with the given name from the current snapshot, or null. */
public GravityFlipRegion find(String name) {
if (name == null) return null;
for (GravityFlipRegion x : regionsSnapshot.get()) {
if (x.getName().equals(name)) return x;
}
return null;
}
/** Adds a region; throws IllegalArgumentException if the name already exists. */
public void add(GravityFlipRegion r) {
synchronized (mutationLock) {
for (GravityFlipRegion x : regionsSnapshot.get()) {
if (x.getName().equals(r.getName())) {
throw new IllegalArgumentException("region name already exists: " + r.getName());
}
if (find(r.getName()) != null) {
throw new IllegalArgumentException("region name already exists: " + r.getName());
}
config.getRegions().add(r);
regionsSnapshot.set(List.copyOf(config.getRegions()));