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
|
@Override
|
||||||
protected void executeSync(@Nonnull CommandContext ctx) {
|
protected void executeSync(@Nonnull CommandContext ctx) {
|
||||||
String name = nameArg.get(ctx);
|
String name = nameArg.get(ctx);
|
||||||
GravityFlipRegion found = null;
|
GravityFlipRegion found = plugin.regions().find(name);
|
||||||
for (GravityFlipRegion r : plugin.regions().all()) {
|
|
||||||
if (r.getName().equals(name)) {
|
|
||||||
found = r;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (found == null) {
|
if (found == null) {
|
||||||
ctx.sendMessage(Message.raw("[gravityflip] Region '" + name + "' not found."));
|
ctx.sendMessage(Message.raw("[gravityflip] Region '" + name + "' not found."));
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -40,13 +40,7 @@ public final class GravityFlipTpSubCommand extends AbstractPlayerCommand {
|
|||||||
@Nonnull PlayerRef playerRef,
|
@Nonnull PlayerRef playerRef,
|
||||||
@Nonnull World world) {
|
@Nonnull World world) {
|
||||||
String name = nameArg.get(ctx);
|
String name = nameArg.get(ctx);
|
||||||
GravityFlipRegion target = null;
|
GravityFlipRegion target = plugin.regions().find(name);
|
||||||
for (GravityFlipRegion r : plugin.regions().all()) {
|
|
||||||
if (r.getName().equals(name)) {
|
|
||||||
target = r;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (target == null) {
|
if (target == null) {
|
||||||
ctx.sendMessage(Message.raw("[gravityflip] Region '" + name + "' not found."));
|
ctx.sendMessage(Message.raw("[gravityflip] Region '" + name + "' not found."));
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -78,14 +78,21 @@ public final class RegionRegistry {
|
|||||||
return out;
|
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. */
|
/** Adds a region; throws IllegalArgumentException if the name already exists. */
|
||||||
public void add(GravityFlipRegion r) {
|
public void add(GravityFlipRegion r) {
|
||||||
synchronized (mutationLock) {
|
synchronized (mutationLock) {
|
||||||
for (GravityFlipRegion x : regionsSnapshot.get()) {
|
if (find(r.getName()) != null) {
|
||||||
if (x.getName().equals(r.getName())) {
|
|
||||||
throw new IllegalArgumentException("region name already exists: " + r.getName());
|
throw new IllegalArgumentException("region name already exists: " + r.getName());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
config.getRegions().add(r);
|
config.getRegions().add(r);
|
||||||
regionsSnapshot.set(List.copyOf(config.getRegions()));
|
regionsSnapshot.set(List.copyOf(config.getRegions()));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,11 +65,10 @@ public final class RegionTickLoop {
|
|||||||
World w = worldSupplier.get();
|
World w = worldSupplier.get();
|
||||||
if (w == null) return;
|
if (w == null) return;
|
||||||
registry.refreshFor(w);
|
registry.refreshFor(w);
|
||||||
if (gravityApplier != null) {
|
if (gravityApplier != null || regionVisualizer != null) {
|
||||||
gravityApplier.apply(w, registry.currentSnapshot(w));
|
var snapshot = registry.currentSnapshot(w);
|
||||||
}
|
if (gravityApplier != null) gravityApplier.apply(w, snapshot);
|
||||||
if (regionVisualizer != null) {
|
if (regionVisualizer != null) regionVisualizer.visualize(w, snapshot);
|
||||||
regionVisualizer.visualize(w, registry.currentSnapshot(w));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
scheduleGuarded(initialDelayMs, tick);
|
scheduleGuarded(initialDelayMs, tick);
|
||||||
|
|||||||
Reference in New Issue
Block a user