Allow adding regions

+ Added WrappedRegion#contains
This commit is contained in:
Eric 2018-11-16 14:25:59 +01:00
parent 2b70f54d05
commit e43eed1821
4 changed files with 103 additions and 3 deletions

View File

@ -3,6 +3,8 @@ package org.codemc.worldguardwrapper.region;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import org.bukkit.Location;
public interface WrappedRegion { public interface WrappedRegion {
String getId(); String getId();
@ -13,4 +15,6 @@ public interface WrappedRegion {
int getPriority(); int getPriority();
boolean contains(Location location);
} }

View File

@ -8,6 +8,8 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.codemc.worldguardwrapper.flags.AbstractWrappedFlag; import org.codemc.worldguardwrapper.flags.AbstractWrappedFlag;
import org.codemc.worldguardwrapper.region.WrappedRegion; import org.codemc.worldguardwrapper.region.WrappedRegion;
import java.util.Arrays;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
@ -85,7 +87,7 @@ public interface IWorldGuardImplementation {
* @param id ID of the region * @param id ID of the region
* @return The region * @return The region
*/ */
Optional<WrappedRegion> getRegion(World world, String id); Optional<WrappedRegion> getRegion(@NonNull World world, @NonNull String id);
/** /**
* Get an unmodifiable map of regions containing the state of the * Get an unmodifiable map of regions containing the state of the
@ -97,7 +99,7 @@ public interface IWorldGuardImplementation {
* @param world The world * @param world The world
* @return A map of regions * @return A map of regions
*/ */
Map<String, WrappedRegion> getRegions(World world); Map<String, WrappedRegion> getRegions(@NonNull World world);
/** /**
* Get a set of regions at the given location. * Get a set of regions at the given location.
@ -105,6 +107,29 @@ public interface IWorldGuardImplementation {
* @param location The location * @param location The location
* @return A set of regions * @return A set of regions
*/ */
Set<WrappedRegion> getRegions(Location location); Set<WrappedRegion> getRegions(@NonNull Location location);
/**
* Add a region. If only two points are given, a cuboid region will be created.
*
* @param id The region ID
* @param points A {@link List} of points that the region should contain
* @param minY The minimum y coordinate
* @param maxY The maximum y coordinate
* @return The added region
*/
Optional<WrappedRegion> addRegion(@NonNull String id, @NonNull List<Location> points, int minY, int maxY);
/**
* Add a cuboid region.
*
* @param id The region ID
* @param point1 The first point of the region
* @param point2 The second point of the region
* @return The added region
*/
default Optional<WrappedRegion> addCuboidRegion(@NonNull String id, @NonNull Location point1, @NonNull Location point2) {
return addRegion(id, Arrays.asList(point1, point2), 0, 0);
}
} }

View File

@ -1,5 +1,7 @@
package org.codemc.worldguardwrapper.implementation.v6; package org.codemc.worldguardwrapper.implementation.v6;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.BlockVector2D;
import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet; import com.sk89q.worldguard.protection.ApplicableRegionSet;
@ -10,6 +12,8 @@ import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.flags.registry.FlagConflictException; import com.sk89q.worldguard.protection.flags.registry.FlagConflictException;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry; import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.NonNull; import lombok.NonNull;
@ -22,6 +26,7 @@ import org.codemc.worldguardwrapper.implementation.IWorldGuardImplementation;
import org.codemc.worldguardwrapper.region.WrappedRegion; import org.codemc.worldguardwrapper.region.WrappedRegion;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
@NoArgsConstructor @NoArgsConstructor
public class WorldGuardImplementation implements IWorldGuardImplementation { public class WorldGuardImplementation implements IWorldGuardImplementation {
@ -49,6 +54,14 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
return getApplicableRegions(location).map(applicableRegions -> applicableRegions.queryState(wrapPlayer(player).orElse(null), stateFlags)); return getApplicableRegions(location).map(applicableRegions -> applicableRegions.queryState(wrapPlayer(player).orElse(null), stateFlags));
} }
private BlockVector toBlockVector(Location location) {
return new BlockVector(location.getX(), location.getY(), location.getZ());
}
private List<BlockVector2D> toBlockVector2DList(List<Location> locations) {
return locations.stream().map(location -> new BlockVector2D(location.getX(), location.getZ())).collect(Collectors.toList());
}
private WrappedRegion toRegion(ProtectedRegion region) { private WrappedRegion toRegion(ProtectedRegion region) {
return new WrappedRegion() { return new WrappedRegion() {
@ -75,6 +88,11 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
return region.getPriority(); return region.getPriority();
} }
@Override
public boolean contains(Location location) {
return region.contains(toBlockVector(location));
}
}; };
} }
@ -179,4 +197,22 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
return set; return set;
} }
@Override
public Optional<WrappedRegion> addRegion(String id, List<Location> points, int minY, int maxY) {
ProtectedRegion region;
if (points.size() == 2) {
region = new ProtectedCuboidRegion(id, toBlockVector(points.get(0)), toBlockVector(points.get(1)));
} else {
region = new ProtectedPolygonalRegion(id, toBlockVector2DList(points), minY, maxY);
}
Optional<RegionManager> manager = getWorldManager(points.get(0).getWorld());
if (manager.isPresent()) {
manager.get().addRegion(region);
return Optional.of(toRegion(region));
} else {
return Optional.empty();
}
}
} }

View File

@ -2,6 +2,8 @@ package org.codemc.worldguardwrapper.implementation.v7;
import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.BukkitWorld; import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard; import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
@ -13,6 +15,8 @@ import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.flags.registry.FlagConflictException; import com.sk89q.worldguard.protection.flags.registry.FlagConflictException;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry; import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import lombok.NonNull; import lombok.NonNull;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -63,6 +67,14 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
return getApplicableRegions(location).map(applicableRegions -> applicableRegions.queryState(wrapPlayer(player).orElse(null), stateFlags)); return getApplicableRegions(location).map(applicableRegions -> applicableRegions.queryState(wrapPlayer(player).orElse(null), stateFlags));
} }
private BlockVector3 toBlockVector3(Location location) {
return BlockVector3.at(location.getX(), location.getY(), location.getZ());
}
private List<BlockVector2> toBlockVector2List(List<Location> locations) {
return locations.stream().map(location -> BlockVector2.at(location.getX(), location.getZ())).collect(Collectors.toList());
}
private WrappedRegion toRegion(ProtectedRegion region) { private WrappedRegion toRegion(ProtectedRegion region) {
return new WrappedRegion() { return new WrappedRegion() {
@ -89,6 +101,11 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
return region.getPriority(); return region.getPriority();
} }
@Override
public boolean contains(Location location) {
return region.contains(toBlockVector3(location));
}
}; };
} }
@ -195,4 +212,22 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
.map(this::toRegion) .map(this::toRegion)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
@Override
public Optional<WrappedRegion> addRegion(String id, List<Location> points, int minY, int maxY) {
ProtectedRegion region;
if (points.size() == 2) {
region = new ProtectedCuboidRegion(id, toBlockVector3(points.get(0)), toBlockVector3(points.get(1)));
} else {
region = new ProtectedPolygonalRegion(id, toBlockVector2List(points), minY, maxY);
}
Optional<RegionManager> manager = getWorldManager(points.get(0).getWorld());
if (manager.isPresent()) {
manager.get().addRegion(region);
return Optional.of(toRegion(region));
} else {
return Optional.empty();
}
}
} }