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:
@@ -27,13 +27,7 @@ public final class GravityFlipToggleSubCommand extends CommandBase {
|
||||
@Override
|
||||
protected void executeSync(@Nonnull CommandContext ctx) {
|
||||
String name = nameArg.get(ctx);
|
||||
GravityFlipRegion found = null;
|
||||
for (GravityFlipRegion r : plugin.regions().all()) {
|
||||
if (r.getName().equals(name)) {
|
||||
found = r;
|
||||
break;
|
||||
}
|
||||
}
|
||||
GravityFlipRegion found = plugin.regions().find(name);
|
||||
if (found == null) {
|
||||
ctx.sendMessage(Message.raw("[gravityflip] Region '" + name + "' not found."));
|
||||
return;
|
||||
|
||||
@@ -40,13 +40,7 @@ public final class GravityFlipTpSubCommand extends AbstractPlayerCommand {
|
||||
@Nonnull PlayerRef playerRef,
|
||||
@Nonnull World world) {
|
||||
String name = nameArg.get(ctx);
|
||||
GravityFlipRegion target = null;
|
||||
for (GravityFlipRegion r : plugin.regions().all()) {
|
||||
if (r.getName().equals(name)) {
|
||||
target = r;
|
||||
break;
|
||||
}
|
||||
}
|
||||
GravityFlipRegion target = plugin.regions().find(name);
|
||||
if (target == null) {
|
||||
ctx.sendMessage(Message.raw("[gravityflip] Region '" + name + "' not found."));
|
||||
return;
|
||||
|
||||
@@ -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()));
|
||||
|
||||
@@ -65,11 +65,10 @@ public final class RegionTickLoop {
|
||||
World w = worldSupplier.get();
|
||||
if (w == null) return;
|
||||
registry.refreshFor(w);
|
||||
if (gravityApplier != null) {
|
||||
gravityApplier.apply(w, registry.currentSnapshot(w));
|
||||
}
|
||||
if (regionVisualizer != null) {
|
||||
regionVisualizer.visualize(w, registry.currentSnapshot(w));
|
||||
if (gravityApplier != null || regionVisualizer != null) {
|
||||
var snapshot = registry.currentSnapshot(w);
|
||||
if (gravityApplier != null) gravityApplier.apply(w, snapshot);
|
||||
if (regionVisualizer != null) regionVisualizer.visualize(w, snapshot);
|
||||
}
|
||||
};
|
||||
scheduleGuarded(initialDelayMs, tick);
|
||||
|
||||
Reference in New Issue
Block a user