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:
@@ -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()));
|
||||
|
||||
Reference in New Issue
Block a user