Skip to content

Commit 10ff4f6

Browse files
Improve utility organization, region sorting
1 parent 4d63450 commit 10ff4f6

4 files changed

Lines changed: 54 additions & 38 deletions

File tree

src/main/java/org/skriptlang/skriptworldguard/SkriptWorldGuard.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ protected WorldGuardRegion deserialize(Fields fields) throws StreamCorruptedExce
129129
}
130130
WorldGuardRegion region = RegionUtils.getRegion(world, id);
131131
if (region == null) {
132-
throw new StreamCorruptedException("The " + RegionUtils.toString(world, id) + " from WorldGuard could not be found. Does it still exist?");
132+
throw new StreamCorruptedException("The " + WorldGuardRegion.toString(world, id) + " from WorldGuard could not be found. Does it still exist?");
133133
}
134134
return region;
135135
}

src/main/java/org/skriptlang/skriptworldguard/elements/expressions/ExprRegionsAt.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ protected WorldGuardRegion[] get(Event event) {
6363
for (Location location : locations) {
6464
regions.addAll(RegionUtils.getRegionsAt(location));
6565
}
66+
regions.sort(WorldGuardRegion::compareTo);
6667
return regions.toArray(new WorldGuardRegion[0]);
6768
}
6869

src/main/java/org/skriptlang/skriptworldguard/worldguard/RegionUtils.java

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,34 @@
3030
import java.util.List;
3131
import java.util.stream.Collectors;
3232

33-
public class RegionUtils {
33+
/**
34+
* Utility methods for working with {@link WorldGuardRegion}s.
35+
*/
36+
public final class RegionUtils {
37+
38+
private RegionUtils() { }
39+
40+
/**
41+
* A helper method to simplify getting the RegionContainer from WorldGuard.
42+
* @return The RegionContainer from the {@link WorldGuardPlatform}.
43+
*/
44+
public static RegionContainer getRegionContainer() {
45+
return WorldGuard.getInstance().getPlatform().getRegionContainer();
46+
}
47+
48+
/**
49+
* A helper method to simplify getting the RegionManager for a world.
50+
* @param world The world to get the RegionManager for.
51+
* @return The RegionManager for the given world, or null if:
52+
* <ul>
53+
* <li>Region data for the given world has not been loaded</li>
54+
* <li>Region data for the given world has failed to load</li>
55+
* <li>Support for regions has been disabled</li>
56+
* </ul>
57+
*/
58+
public static @Nullable RegionManager getRegionManager(World world) {
59+
return getRegionContainer().get(BukkitAdapter.adapt(world));
60+
}
3461

3562
/**
3663
* A helper method to simplify getting a region in a world.
@@ -157,38 +184,4 @@ public Block next() {
157184
};
158185
}
159186

160-
/**
161-
* A helper method to provide a standardized way to stringify a {@link ProtectedRegion}.
162-
* @param world The world of a region.
163-
* @param id The ID of a region.
164-
* @return A stringified version of a region.
165-
* @see WorldGuardRegion#toString()
166-
*/
167-
public static String toString(World world, String id) {
168-
return "region \"" + id + "\" in the world \"" + world.getName() + "\"";
169-
}
170-
171-
/**
172-
* A helper method to simplify getting the RegionContainer from WorldGuard.
173-
* @return The RegionContainer from the {@link WorldGuardPlatform}.
174-
*/
175-
public static RegionContainer getRegionContainer() {
176-
return WorldGuard.getInstance().getPlatform().getRegionContainer();
177-
}
178-
179-
/**
180-
* A helper method to simplify getting the RegionManager for a world.
181-
* @param world The world to get the RegionManager for.
182-
* @return The RegionManager for the given world, or null if:
183-
* <ul>
184-
* <li>Region data for the given world has not been loaded</li>
185-
* <li>Region data for the given world has failed to load</li>
186-
* <li>Support for regions has been disabled</li>
187-
* </ul>
188-
*/
189-
@Nullable
190-
public static RegionManager getRegionManager(World world) {
191-
return WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(world));
192-
}
193-
194187
}

src/main/java/org/skriptlang/skriptworldguard/worldguard/WorldGuardRegion.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,27 @@
66
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
77
import org.bukkit.Location;
88
import org.bukkit.World;
9+
import org.jetbrains.annotations.NotNull;
910
import org.skriptlang.skript.lang.converter.Converters;
1011

1112
/**
1213
* A utility record for associating a {@link ProtectedRegion} with a {@link World}.
1314
* @param world The world {@code region} is within.
1415
* @param region WorldGuard region.
1516
*/
16-
public record WorldGuardRegion(World world, ProtectedRegion region) implements AnyNamed, AnyContains<Object> {
17+
public record WorldGuardRegion(World world, ProtectedRegion region)
18+
implements Comparable<WorldGuardRegion>, AnyNamed, AnyContains<Object> {
19+
20+
/**
21+
* A helper method to provide a standardized way to stringify a {@link WorldGuardRegion} from its components.
22+
* @param world The world of a region.
23+
* @param id The ID of a region.
24+
* @return A stringified version of a region.
25+
* @see WorldGuardRegion#toString()
26+
*/
27+
public static String toString(World world, String id) {
28+
return "region \"" + id + "\" in the world \"" + world.getName() + "\"";
29+
}
1730

1831
@Override
1932
public boolean equals(Object other) {
@@ -25,7 +38,16 @@ public boolean equals(Object other) {
2538

2639
@Override
2740
public String toString() {
28-
return RegionUtils.toString(world, region.getId());
41+
return toString(world, region.getId());
42+
}
43+
44+
/*
45+
* Comparable
46+
*/
47+
48+
@Override
49+
public int compareTo(@NotNull WorldGuardRegion other) {
50+
return Integer.compare(this.region.getPriority(), other.region.getPriority());
2951
}
3052

3153
/*

0 commit comments

Comments
 (0)