-
-
Notifications
You must be signed in to change notification settings - Fork 6
ExprRegions #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
ExprRegions #18
Changes from all commits
c419c7f
d773752
084bd6c
6cf31bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,78 @@ | ||||||
package org.skriptlang.skriptworldguard.elements.expressions; | ||||||
|
||||||
import ch.njol.skript.Skript; | ||||||
import ch.njol.skript.doc.*; | ||||||
import ch.njol.skript.lang.Expression; | ||||||
import ch.njol.skript.lang.ExpressionType; | ||||||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||||||
import ch.njol.skript.lang.util.SimpleExpression; | ||||||
import ch.njol.util.Kleenean; | ||||||
import org.bukkit.World; | ||||||
import org.bukkit.event.Event; | ||||||
import org.jetbrains.annotations.Nullable; | ||||||
import org.skriptlang.skriptworldguard.worldguard.RegionUtils; | ||||||
import org.skriptlang.skriptworldguard.worldguard.WorldGuardRegion; | ||||||
|
||||||
import java.util.ArrayList; | ||||||
import java.util.Arrays; | ||||||
import java.util.List; | ||||||
import java.util.stream.Collectors; | ||||||
|
||||||
@Name("All Regions") | ||||||
@Description("Get all regions from all worlds or from a specific world.") | ||||||
@Examples({ | ||||||
"send all regions", | ||||||
"set {_regions::*} to the regions in world \"world\"" | ||||||
}) | ||||||
@Since("INSERT VERSION") | ||||||
@RequiredPlugins("WorldGuard 7") | ||||||
public class ExprRegions extends SimpleExpression<WorldGuardRegion> { | ||||||
|
||||||
static { | ||||||
Skript.registerExpression(ExprRegions.class, WorldGuardRegion.class, ExpressionType.SIMPLE, | ||||||
"(all [[of] the]|the) regions [(in|of|from) %-worlds%]"); | ||||||
} | ||||||
|
||||||
private @Nullable Expression<World> worlds; | ||||||
|
||||||
@Override | ||||||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||||||
if (exprs[0] != null) | ||||||
//noinspection unchecked | ||||||
worlds = (Expression<World>) exprs[0]; | ||||||
return true; | ||||||
} | ||||||
|
||||||
@Override | ||||||
protected WorldGuardRegion @Nullable [] get(Event event) { | ||||||
if (worlds == null) | ||||||
return RegionUtils.getRegions(); | ||||||
World[] worlds = this.worlds.getArray(event); | ||||||
if (worlds.length == 0) | ||||||
return null; | ||||||
List<WorldGuardRegion> regions = new ArrayList<>(); | ||||||
for (World world : worlds) { | ||||||
WorldGuardRegion[] worldRegions = RegionUtils.getRegions(world); | ||||||
if (worldRegions == null || worldRegions.length == 0) | ||||||
continue; | ||||||
regions.addAll(Arrays.stream(worldRegions).collect(Collectors.toList())); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Any reason for using a stream? |
||||||
} | ||||||
return regions.toArray(new WorldGuardRegion[0]); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public boolean isSingle() { | ||||||
return false; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public Class<WorldGuardRegion> getReturnType() { | ||||||
return WorldGuardRegion.class; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public String toString(@Nullable Event event, boolean debug) { | ||||||
return "all of the regions" + (worlds == null ? "" : " " + worlds.toString(event, debug)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Might be worth specifying this |
||||||
} | ||||||
|
||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import com.sk89q.worldedit.bukkit.BukkitAdapter; | ||
import com.sk89q.worldedit.math.BlockVector3; | ||
import com.sk89q.worldedit.regions.Polygonal2DRegion; | ||
import com.sk89q.worldedit.regions.Region; | ||
import com.sk89q.worldguard.WorldGuard; | ||
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; | ||
import com.sk89q.worldguard.internal.platform.WorldGuardPlatform; | ||
|
@@ -12,16 +13,16 @@ | |
import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion; | ||
import com.sk89q.worldguard.protection.regions.ProtectedRegion; | ||
import com.sk89q.worldguard.protection.regions.RegionContainer; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.Location; | ||
import org.bukkit.World; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.util.Vector; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.*; | ||
import java.util.Map.Entry; | ||
|
||
public class RegionUtils { | ||
|
||
|
@@ -136,9 +137,38 @@ public static RegionContainer getRegionContainer() { | |
* - region data for the given world has failed to load | ||
* - support for regions has been disabled | ||
*/ | ||
@Nullable | ||
public static RegionManager getRegionManager(World world) { | ||
public static @Nullable RegionManager getRegionManager(World world) { | ||
return WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(world)); | ||
} | ||
|
||
/** | ||
* @return All regions of all worlds | ||
*/ | ||
public static WorldGuardRegion @Nullable [] getRegions() { | ||
return getRegions(null); | ||
} | ||
|
||
/** | ||
* Get all regions of all worlds or provide {@code world} to get all regions of that {@link World} | ||
* @param world The {@link World} to get regions from, or {@code null} to get regions from all worlds | ||
* @return All regions from the world, or all worlds if {@code world} is null. | ||
*/ | ||
public static WorldGuardRegion @Nullable [] getRegions(@Nullable World world) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are these methods nullable? They don't appear to return null (nor should they - just return an empty array) |
||
WorldGuardPlatform platform = WorldGuard.getInstance().getPlatform(); | ||
RegionContainer container = platform.getRegionContainer(); | ||
Map<World, RegionManager> managers = new HashMap<>(); | ||
if (world == null) { | ||
for (World world1 : Bukkit.getWorlds()) | ||
managers.put(world1, container.get(BukkitAdapter.adapt(world1))); | ||
} else { | ||
managers.put(world, container.get(BukkitAdapter.adapt(world))); | ||
Comment on lines
+162
to
+164
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why aren't these using getRegionManager()? I'd think the loss of benefits of caching the container would be outweighed by the improvements to readability. |
||
} | ||
List<WorldGuardRegion> regions = new ArrayList<>(); | ||
for (Entry<World, RegionManager> managerEntry : managers.entrySet()) { | ||
for (Entry<String, ProtectedRegion> regionEntry : managerEntry.getValue().getRegions().entrySet()) | ||
regions.add(new WorldGuardRegion(managerEntry.getKey(), regionEntry.getValue())); | ||
} | ||
return regions.toArray(new WorldGuardRegion[0]); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
due to presence of expression (even if optional)