Skip to content

Make can build actually query the given regions rather than their locations. #21

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.util.Direction;
import ch.njol.util.Kleenean;
import com.sk89q.worldedit.math.BlockVector3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skriptworldguard.worldguard.WorldGuardRegion;
import org.skriptlang.skriptworldguard.worldguard.RegionUtils;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skriptworldguard.worldguard.RegionUtils;
import org.skriptlang.skriptworldguard.worldguard.WorldGuardRegion;

@Name("Can Build In Regions")
@Description("A condition that tests whether the given players can build in the given regions or the regions of the given locations.")
Expand Down Expand Up @@ -66,7 +65,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, @NotNull Kleenean
@Override
public boolean check(@NotNull Event event) {
if (locations != null) {
Location[] locations = this.locations.getAll(event);
Location[] locations = this.locations.getAll(event); // get all to avoid double-eval + permit or lists.
return players.check(event, player -> SimpleExpression.check(
locations,
location -> RegionUtils.canBuild(player, location),
Expand All @@ -75,14 +74,10 @@ public boolean check(@NotNull Event event) {
), isNegated());
} else {
assert regions != null;
WorldGuardRegion[] regions = this.regions.getAll(event);
WorldGuardRegion[] regions = this.regions.getAll(event); // get all to avoid double-eval + permit or lists.
return players.check(event, player -> SimpleExpression.check(
regions,
region -> { // Convert region to location essentially
BlockVector3 minPoint = region.getRegion().getMinimumPoint();
Location location = new Location(region.getWorld(), minPoint.getX(), minPoint.getY(), minPoint.getZ());
return RegionUtils.canBuild(player, location);
},
region -> RegionUtils.canBuild(player, region),
false,
this.regions.getAnd()
), isNegated());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.internal.platform.WorldGuardPlatform;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.RegionResultSet;
import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion;
Expand All @@ -17,11 +20,14 @@
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class RegionUtils {

Expand Down Expand Up @@ -70,7 +76,13 @@ public static Collection<WorldGuardRegion> getRegionsAt(Location location) {
return regions;
}

public static boolean canBuild(Player player, Location location) {
/**
* Tests if a player can build at a given location.
* @param player The player to test with
* @param location The location to test at
* @return Whether the given player can build at the location.
*/
public static boolean canBuild(Player player, @NotNull Location location) {
World world = location.getWorld();
if (world == null) {
return false;
Expand All @@ -82,6 +94,22 @@ public static boolean canBuild(Player player, Location location) {
return getRegionContainer().createQuery().testBuild(BukkitAdapter.adapt(location), WorldGuardPlugin.inst().wrapPlayer(player));
}

/**
* Tests if a player can build in all the given regions.
* @param player The player to test with
* @param regions The regions to test against
* @return Whether the given player can build in all the regions.
*/
public static boolean canBuild(Player player, WorldGuardRegion... regions) {
// create queryable set of regions
ApplicableRegionSet regionSet = new RegionResultSet(
(List<ProtectedRegion>) Arrays.stream(regions)
.map(WorldGuardRegion::getRegion)
.collect(Collectors.toCollection(ArrayList::new)), null);
return regionSet.testState(WorldGuardPlugin.inst().wrapPlayer(player), Flags.BUILD);
}


public static List<Block> getBlocksInRegion(WorldGuardRegion region) {
ProtectedRegion protectedRegion = region.getRegion();
List<Block> blocks = new ArrayList<>();
Expand Down