From bd1e41cbb6d9d933d32127a81bbed1af7db39b32 Mon Sep 17 00:00:00 2001 From: Gabriele C Date: Fri, 26 Jun 2020 20:43:13 +0200 Subject: [PATCH] Implement getPlayerSelection() and SelectionUtilities, bump version --- api/pom.xml | 2 +- .../IWorldGuardImplementation.java | 11 ++- .../utility/SelectionUtilities.java | 72 ++++++++++++++++ implementation/legacy/pom.xml | 4 +- .../legacy/WorldGuardImplementation.java | 70 ++++++++++++++-- .../legacy/region/WrappedRegion.java | 29 ++++--- implementation/pom.xml | 2 +- implementation/v6/pom.xml | 4 +- .../v6/WorldGuardImplementation.java | 73 ++++++++++++++-- .../v6/region/WrappedRegion.java | 35 ++++---- implementation/v7/pom.xml | 4 +- .../v7/WorldGuardImplementation.java | 84 +++++++++++++++++-- .../v7/region/WrappedRegion.java | 35 ++++---- library/pom.xml | 10 +-- pom.xml | 2 +- 15 files changed, 351 insertions(+), 86 deletions(-) create mode 100644 api/src/main/java/org/codemc/worldguardwrapper/utility/SelectionUtilities.java diff --git a/api/pom.xml b/api/pom.xml index c4ccea3..64f2467 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -7,7 +7,7 @@ org.codemc.worldguardwrapper worldguardwrapper-parent - 1.1.8-SNAPSHOT + 1.1.9-SNAPSHOT worldguardwrapper-api diff --git a/api/src/main/java/org/codemc/worldguardwrapper/implementation/IWorldGuardImplementation.java b/api/src/main/java/org/codemc/worldguardwrapper/implementation/IWorldGuardImplementation.java index 80aa6fa..3d5cb58 100644 --- a/api/src/main/java/org/codemc/worldguardwrapper/implementation/IWorldGuardImplementation.java +++ b/api/src/main/java/org/codemc/worldguardwrapper/implementation/IWorldGuardImplementation.java @@ -154,8 +154,9 @@ public interface IWorldGuardImplementation { } else if (selection instanceof IPolygonalSelection) { IPolygonalSelection sel = (IPolygonalSelection) selection; return addRegion(id, new ArrayList<>(sel.getPoints()), sel.getMinimumY(), sel.getMaximumY()); + } else { + throw new UnsupportedOperationException("Unknown " + selection.getClass().getSimpleName() + " selection type!"); } - return Optional.empty(); } /** @@ -167,4 +168,12 @@ public interface IWorldGuardImplementation { */ Optional> removeRegion(@NonNull World world, @NonNull String id); + /** + * Returns the current selection of the given player. + * + * @param player The player + * @return The current player's selection + */ + Optional getPlayerSelection(@NonNull Player player); + } diff --git a/api/src/main/java/org/codemc/worldguardwrapper/utility/SelectionUtilities.java b/api/src/main/java/org/codemc/worldguardwrapper/utility/SelectionUtilities.java new file mode 100644 index 0000000..52d6ea9 --- /dev/null +++ b/api/src/main/java/org/codemc/worldguardwrapper/utility/SelectionUtilities.java @@ -0,0 +1,72 @@ +package org.codemc.worldguardwrapper.utility; + +import lombok.experimental.UtilityClass; +import org.bukkit.Location; +import org.codemc.worldguardwrapper.selection.ICuboidSelection; +import org.codemc.worldguardwrapper.selection.IPolygonalSelection; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +@UtilityClass +public class SelectionUtilities { + + /** + * Creates a static cuboid selection. + * + * @param first the first point of the cuboid + * @param second the second point of the cuboid + * @return the selection + */ + public ICuboidSelection createCuboidSelection(Location first, Location second) { + Location minimum; + Location maximum; + if (first.getBlockY() > second.getBlockY()) { + maximum = first; + minimum = second; + } else { + maximum = second; + minimum = first; + } + return new ICuboidSelection() { + @Override + public Location getMinimumPoint() { + return minimum; + } + + @Override + public Location getMaximumPoint() { + return maximum; + } + }; + } + + /** + * Creates a static polygonal selection. + * + * @param points the points of the selection + * @param minY the minimum Y coordinate of the selection + * @param maxY the maximum Y coordinate of the selection + * @return the selection + */ + public IPolygonalSelection createPolygonalSelection(Collection points, int minY, int maxY) { + return new IPolygonalSelection() { + @Override + public Set getPoints() { + return new HashSet<>(points); + } + + @Override + public int getMinimumY() { + return minY; + } + + @Override + public int getMaximumY() { + return maxY; + } + }; + } + +} diff --git a/implementation/legacy/pom.xml b/implementation/legacy/pom.xml index fad4c43..7936abb 100644 --- a/implementation/legacy/pom.xml +++ b/implementation/legacy/pom.xml @@ -7,7 +7,7 @@ org.codemc.worldguardwrapper worldguardwrapper-implementation - 1.1.8-SNAPSHOT + 1.1.9-SNAPSHOT worldguardwrapper-implementation-legacy @@ -28,7 +28,7 @@ ${project.groupId} worldguardwrapper-api - 1.1.8-SNAPSHOT + 1.1.9-SNAPSHOT provided diff --git a/implementation/legacy/src/main/java/org/codemc/worldguardwrapper/implementation/legacy/WorldGuardImplementation.java b/implementation/legacy/src/main/java/org/codemc/worldguardwrapper/implementation/legacy/WorldGuardImplementation.java index 45011b9..a00f048 100644 --- a/implementation/legacy/src/main/java/org/codemc/worldguardwrapper/implementation/legacy/WorldGuardImplementation.java +++ b/implementation/legacy/src/main/java/org/codemc/worldguardwrapper/implementation/legacy/WorldGuardImplementation.java @@ -1,6 +1,10 @@ package org.codemc.worldguardwrapper.implementation.legacy; -import com.google.common.collect.Maps; +import com.sk89q.minecraft.util.commands.CommandException; +import com.sk89q.worldedit.BlockVector; +import com.sk89q.worldedit.bukkit.WorldEditPlugin; +import com.sk89q.worldedit.bukkit.selections.CuboidSelection; +import com.sk89q.worldedit.bukkit.selections.Polygonal2DSelection; import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.protection.ApplicableRegionSet; @@ -10,7 +14,6 @@ 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 lombok.NoArgsConstructor; import lombok.NonNull; import org.bukkit.Location; import org.bukkit.World; @@ -23,21 +26,33 @@ import org.codemc.worldguardwrapper.implementation.legacy.region.WrappedRegion; import org.codemc.worldguardwrapper.implementation.legacy.utility.WorldGuardFlagUtilities; import org.codemc.worldguardwrapper.implementation.legacy.utility.WorldGuardVectorUtilities; import org.codemc.worldguardwrapper.region.IWrappedRegion; +import org.codemc.worldguardwrapper.selection.ICuboidSelection; +import org.codemc.worldguardwrapper.selection.IPolygonalSelection; +import org.codemc.worldguardwrapper.selection.ISelection; import java.util.*; import java.util.stream.Collectors; -@NoArgsConstructor public class WorldGuardImplementation implements IWorldGuardImplementation { - private final WorldGuardPlugin plugin = WorldGuardPlugin.inst(); + private final WorldGuardPlugin worldGuardPlugin; + private final WorldEditPlugin worldEditPlugin; + + public WorldGuardImplementation() { + worldGuardPlugin = WorldGuardPlugin.inst(); + try { + worldEditPlugin = worldGuardPlugin.getWorldEdit(); + } catch (CommandException e) { + throw new RuntimeException(e); + } + } private Optional wrapPlayer(Player player) { - return Optional.ofNullable(player).map(bukkitPlayer -> plugin.wrapPlayer(player)); + return Optional.ofNullable(player).map(bukkitPlayer -> worldGuardPlugin.wrapPlayer(player)); } private Optional getWorldManager(@NonNull World world) { - return Optional.ofNullable(plugin.getRegionManager(world)); + return Optional.ofNullable(worldGuardPlugin.getRegionManager(world)); } private Optional getApplicableRegions(@NonNull Location location) { @@ -126,7 +141,7 @@ public class WorldGuardImplementation implements IWorldGuardImplementation { @Override public Map getRegions(World world) { - RegionManager regionManager = plugin.getRegionManager(world); + RegionManager regionManager = worldGuardPlugin.getRegionManager(world); Map regions = regionManager.getRegions(); Map map = new HashMap<>(); @@ -187,4 +202,45 @@ public class WorldGuardImplementation implements IWorldGuardImplementation { .collect(Collectors.toSet())); } + @Override + public Optional getPlayerSelection(@NonNull Player player) { + return Optional.ofNullable(worldEditPlugin.getSelection(player)) + .map(selection -> { + if (selection instanceof CuboidSelection) { + return new ICuboidSelection() { + @Override + public Location getMinimumPoint() { + return selection.getMinimumPoint(); + } + + @Override + public Location getMaximumPoint() { + return selection.getMaximumPoint(); + } + }; + } else if (selection instanceof Polygonal2DSelection) { + return new IPolygonalSelection() { + @Override + public Set getPoints() { + return ((Polygonal2DSelection) selection).getNativePoints().stream() + .map(vector -> new BlockVector(vector.toVector())) + .map(vector -> WorldGuardVectorUtilities.fromBlockVector(selection.getWorld(), vector)) + .collect(Collectors.toSet()); + } + + @Override + public int getMinimumY() { + return selection.getMinimumPoint().getBlockY(); + } + + @Override + public int getMaximumY() { + return selection.getMaximumPoint().getBlockY(); + } + }; + } else { + throw new UnsupportedOperationException("Unsupported " + selection.getClass().getSimpleName() + " selection!"); + } + }); + } } diff --git a/implementation/legacy/src/main/java/org/codemc/worldguardwrapper/implementation/legacy/region/WrappedRegion.java b/implementation/legacy/src/main/java/org/codemc/worldguardwrapper/implementation/legacy/region/WrappedRegion.java index 12052e4..d593cfe 100644 --- a/implementation/legacy/src/main/java/org/codemc/worldguardwrapper/implementation/legacy/region/WrappedRegion.java +++ b/implementation/legacy/src/main/java/org/codemc/worldguardwrapper/implementation/legacy/region/WrappedRegion.java @@ -2,6 +2,7 @@ package org.codemc.worldguardwrapper.implementation.legacy.region; import com.sk89q.worldedit.BlockVector; import com.sk89q.worldguard.protection.flags.Flag; +import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion; import lombok.AllArgsConstructor; @@ -34,7 +35,19 @@ public class WrappedRegion implements IWrappedRegion { @Override public ISelection getSelection() { - if (handle instanceof ProtectedPolygonalRegion) { + if (handle instanceof ProtectedCuboidRegion) { + return new ICuboidSelection() { + @Override + public Location getMinimumPoint() { + return WorldGuardVectorUtilities.fromBlockVector(world, handle.getMinimumPoint()); + } + + @Override + public Location getMaximumPoint() { + return WorldGuardVectorUtilities.fromBlockVector(world, handle.getMaximumPoint()); + } + }; + } else if (handle instanceof ProtectedPolygonalRegion) { return new IPolygonalSelection() { @Override public Set getPoints() { @@ -54,19 +67,9 @@ public class WrappedRegion implements IWrappedRegion { return handle.getMaximumPoint().getBlockY(); } }; + } else { + throw new UnsupportedOperationException("Unsupported " + handle.getClass().getSimpleName() + " region!"); } - - return new ICuboidSelection() { - @Override - public Location getMinimumPoint() { - return WorldGuardVectorUtilities.fromBlockVector(world, handle.getMinimumPoint()); - } - - @Override - public Location getMaximumPoint() { - return WorldGuardVectorUtilities.fromBlockVector(world, handle.getMaximumPoint()); - } - }; } @Override diff --git a/implementation/pom.xml b/implementation/pom.xml index cd269a9..24c57b6 100644 --- a/implementation/pom.xml +++ b/implementation/pom.xml @@ -7,7 +7,7 @@ org.codemc.worldguardwrapper worldguardwrapper-parent - 1.1.8-SNAPSHOT + 1.1.9-SNAPSHOT worldguardwrapper-implementation diff --git a/implementation/v6/pom.xml b/implementation/v6/pom.xml index a1c378d..5446f6c 100644 --- a/implementation/v6/pom.xml +++ b/implementation/v6/pom.xml @@ -7,7 +7,7 @@ org.codemc.worldguardwrapper worldguardwrapper-implementation - 1.1.8-SNAPSHOT + 1.1.9-SNAPSHOT worldguardwrapper-implementation-v6 @@ -28,7 +28,7 @@ ${project.groupId} worldguardwrapper-api - 1.1.8-SNAPSHOT + 1.1.9-SNAPSHOT provided diff --git a/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/WorldGuardImplementation.java b/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/WorldGuardImplementation.java index 88f7265..19c1d6e 100644 --- a/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/WorldGuardImplementation.java +++ b/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/WorldGuardImplementation.java @@ -1,6 +1,10 @@ package org.codemc.worldguardwrapper.implementation.v6; -import com.google.common.collect.Maps; +import com.sk89q.minecraft.util.commands.CommandException; +import com.sk89q.worldedit.BlockVector; +import com.sk89q.worldedit.bukkit.WorldEditPlugin; +import com.sk89q.worldedit.bukkit.selections.CuboidSelection; +import com.sk89q.worldedit.bukkit.selections.Polygonal2DSelection; import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.protection.ApplicableRegionSet; @@ -11,7 +15,6 @@ 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 lombok.NoArgsConstructor; import lombok.NonNull; import org.bukkit.Location; import org.bukkit.World; @@ -26,22 +29,35 @@ import org.codemc.worldguardwrapper.implementation.v6.region.WrappedRegion; import org.codemc.worldguardwrapper.implementation.v6.utility.WorldGuardFlagUtilities; import org.codemc.worldguardwrapper.implementation.v6.utility.WorldGuardVectorUtilities; import org.codemc.worldguardwrapper.region.IWrappedRegion; +import org.codemc.worldguardwrapper.selection.ICuboidSelection; +import org.codemc.worldguardwrapper.selection.IPolygonalSelection; +import org.codemc.worldguardwrapper.selection.ISelection; import java.util.*; import java.util.stream.Collectors; -@NoArgsConstructor public class WorldGuardImplementation implements IWorldGuardImplementation { - private final WorldGuardPlugin plugin = WorldGuardPlugin.inst(); - private final FlagRegistry flagRegistry = plugin.getFlagRegistry(); + private final WorldGuardPlugin worldGuardPlugin; + private final WorldEditPlugin worldEditPlugin; + private final FlagRegistry flagRegistry; + + public WorldGuardImplementation() { + worldGuardPlugin = WorldGuardPlugin.inst(); + try { + worldEditPlugin = worldGuardPlugin.getWorldEdit(); + } catch (CommandException e) { + throw new RuntimeException(e); + } + flagRegistry = worldGuardPlugin.getFlagRegistry(); + } private Optional wrapPlayer(Player player) { - return Optional.ofNullable(player).map(bukkitPlayer -> plugin.wrapPlayer(player)); + return Optional.ofNullable(player).map(bukkitPlayer -> worldGuardPlugin.wrapPlayer(player)); } private Optional getWorldManager(@NonNull World world) { - return Optional.ofNullable(plugin.getRegionManager(world)); + return Optional.ofNullable(worldGuardPlugin.getRegionManager(world)); } private Optional getApplicableRegions(@NonNull Location location) { @@ -152,7 +168,7 @@ public class WorldGuardImplementation implements IWorldGuardImplementation { @Override public Map getRegions(World world) { - RegionManager regionManager = plugin.getRegionManager(world); + RegionManager regionManager = worldGuardPlugin.getRegionManager(world); Map regions = regionManager.getRegions(); Map map = new HashMap<>(); @@ -213,4 +229,45 @@ public class WorldGuardImplementation implements IWorldGuardImplementation { .collect(Collectors.toSet())); } + @Override + public Optional getPlayerSelection(@NonNull Player player) { + return Optional.ofNullable(worldEditPlugin.getSelection(player)) + .map(selection -> { + if (selection instanceof CuboidSelection) { + return new ICuboidSelection() { + @Override + public Location getMinimumPoint() { + return selection.getMinimumPoint(); + } + + @Override + public Location getMaximumPoint() { + return selection.getMaximumPoint(); + } + }; + } else if (selection instanceof Polygonal2DSelection) { + return new IPolygonalSelection() { + @Override + public Set getPoints() { + return ((Polygonal2DSelection) selection).getNativePoints().stream() + .map(vector -> new BlockVector(vector.toVector())) + .map(vector -> WorldGuardVectorUtilities.fromBlockVector(selection.getWorld(), vector)) + .collect(Collectors.toSet()); + } + + @Override + public int getMinimumY() { + return selection.getMinimumPoint().getBlockY(); + } + + @Override + public int getMaximumY() { + return selection.getMaximumPoint().getBlockY(); + } + }; + } else { + throw new UnsupportedOperationException("Unsupported " + selection.getClass().getSimpleName() + " selection!"); + } + }); + } } diff --git a/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/region/WrappedRegion.java b/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/region/WrappedRegion.java index b1cfb38..fa9c0f9 100644 --- a/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/region/WrappedRegion.java +++ b/implementation/v6/src/main/java/org/codemc/worldguardwrapper/implementation/v6/region/WrappedRegion.java @@ -2,6 +2,7 @@ package org.codemc.worldguardwrapper.implementation.v6.region; import com.sk89q.worldedit.BlockVector; import com.sk89q.worldguard.protection.flags.Flag; +import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion; import lombok.AllArgsConstructor; @@ -18,11 +19,7 @@ import org.codemc.worldguardwrapper.selection.ICuboidSelection; import org.codemc.worldguardwrapper.selection.IPolygonalSelection; import org.codemc.worldguardwrapper.selection.ISelection; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.UUID; +import java.util.*; import java.util.stream.Collectors; @AllArgsConstructor @@ -34,7 +31,19 @@ public class WrappedRegion implements IWrappedRegion { @Override public ISelection getSelection() { - if (handle instanceof ProtectedPolygonalRegion) { + if (handle instanceof ProtectedCuboidRegion) { + return new ICuboidSelection() { + @Override + public Location getMinimumPoint() { + return WorldGuardVectorUtilities.fromBlockVector(world, handle.getMinimumPoint()); + } + + @Override + public Location getMaximumPoint() { + return WorldGuardVectorUtilities.fromBlockVector(world, handle.getMaximumPoint()); + } + }; + } else if (handle instanceof ProtectedPolygonalRegion) { return new IPolygonalSelection() { @Override public Set getPoints() { @@ -54,19 +63,9 @@ public class WrappedRegion implements IWrappedRegion { return handle.getMaximumPoint().getBlockY(); } }; + } else { + throw new UnsupportedOperationException("Unsupported " + handle.getClass().getSimpleName() + " region!"); } - - return new ICuboidSelection() { - @Override - public Location getMinimumPoint() { - return WorldGuardVectorUtilities.fromBlockVector(world, handle.getMinimumPoint()); - } - - @Override - public Location getMaximumPoint() { - return WorldGuardVectorUtilities.fromBlockVector(world, handle.getMaximumPoint()); - } - }; } @Override diff --git a/implementation/v7/pom.xml b/implementation/v7/pom.xml index e251f0c..7b7db89 100644 --- a/implementation/v7/pom.xml +++ b/implementation/v7/pom.xml @@ -7,7 +7,7 @@ org.codemc.worldguardwrapper worldguardwrapper-implementation - 1.1.8-SNAPSHOT + 1.1.9-SNAPSHOT worldguardwrapper-implementation-v7 @@ -28,7 +28,7 @@ ${project.groupId} worldguardwrapper-api - 1.1.8-SNAPSHOT + 1.1.9-SNAPSHOT provided diff --git a/implementation/v7/src/main/java/org/codemc/worldguardwrapper/implementation/v7/WorldGuardImplementation.java b/implementation/v7/src/main/java/org/codemc/worldguardwrapper/implementation/v7/WorldGuardImplementation.java index 200832e..673fe06 100644 --- a/implementation/v7/src/main/java/org/codemc/worldguardwrapper/implementation/v7/WorldGuardImplementation.java +++ b/implementation/v7/src/main/java/org/codemc/worldguardwrapper/implementation/v7/WorldGuardImplementation.java @@ -1,9 +1,14 @@ package org.codemc.worldguardwrapper.implementation.v7; -import com.google.common.collect.Maps; +import com.sk89q.minecraft.util.commands.CommandException; +import com.sk89q.worldedit.IncompleteRegionException; import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.bukkit.BukkitWorld; +import com.sk89q.worldedit.bukkit.WorldEditPlugin; import com.sk89q.worldedit.math.BlockVector2; +import com.sk89q.worldedit.regions.CuboidRegion; +import com.sk89q.worldedit.regions.Polygonal2DRegion; +import com.sk89q.worldedit.regions.Region; import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.WorldGuard; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; @@ -15,7 +20,6 @@ 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 lombok.NoArgsConstructor; import lombok.NonNull; import org.bukkit.Location; import org.bukkit.World; @@ -29,19 +33,33 @@ import org.codemc.worldguardwrapper.implementation.v7.flag.AbstractWrappedFlag; import org.codemc.worldguardwrapper.implementation.v7.region.WrappedRegion; import org.codemc.worldguardwrapper.implementation.v7.utility.WorldGuardFlagUtilities; import org.codemc.worldguardwrapper.region.IWrappedRegion; +import org.codemc.worldguardwrapper.selection.ICuboidSelection; +import org.codemc.worldguardwrapper.selection.IPolygonalSelection; +import org.codemc.worldguardwrapper.selection.ISelection; import java.util.*; import java.util.stream.Collectors; -@NoArgsConstructor public class WorldGuardImplementation implements IWorldGuardImplementation { - private final WorldGuard core = WorldGuard.getInstance(); - private final FlagRegistry flagRegistry = core.getFlagRegistry(); - private final WorldGuardPlugin plugin = WorldGuardPlugin.inst(); + private final WorldGuard core; + private final FlagRegistry flagRegistry; + private final WorldGuardPlugin worldGuardPlugin; + private final WorldEditPlugin worldEditPlugin; + + public WorldGuardImplementation() { + core = WorldGuard.getInstance(); + flagRegistry = core.getFlagRegistry(); + worldGuardPlugin = WorldGuardPlugin.inst(); + try { + worldEditPlugin = worldGuardPlugin.getWorldEdit(); + } catch (CommandException e) { + throw new RuntimeException(e); + } + } private Optional wrapPlayer(Player player) { - return Optional.ofNullable(player).map(bukkitPlayer -> plugin.wrapPlayer(player)); + return Optional.ofNullable(player).map(bukkitPlayer -> worldGuardPlugin.wrapPlayer(player)); } private Optional getWorldManager(@NonNull World world) { @@ -221,4 +239,56 @@ public class WorldGuardImplementation implements IWorldGuardImplementation { return set.map(protectedRegions -> protectedRegions.stream() .map(region -> new WrappedRegion(world, region)).collect(Collectors.toSet())); } + + @Override + public Optional getPlayerSelection(@NonNull Player player) { + Region region; + try { + region = worldEditPlugin.getSession(player).getSelection(BukkitAdapter.adapt(player.getWorld())); + } catch (IncompleteRegionException e) { + region = null; + } + return Optional.ofNullable(region) + .map(selection -> { + World world = Optional.ofNullable(selection.getWorld()).map(BukkitAdapter::adapt).orElse(null); + if (world == null) { + return null; + } + if (selection instanceof CuboidRegion) { + return new ICuboidSelection() { + @Override + public Location getMinimumPoint() { + return BukkitAdapter.adapt(world, selection.getMinimumPoint()); + } + + @Override + public Location getMaximumPoint() { + return BukkitAdapter.adapt(world, selection.getMaximumPoint()); + } + }; + } else if (selection instanceof Polygonal2DRegion) { + return new IPolygonalSelection() { + @Override + public Set getPoints() { + return ((Polygonal2DRegion) selection).getPoints().stream() + .map(BlockVector2::toBlockVector3) + .map(vector -> BukkitAdapter.adapt(world, vector)) + .collect(Collectors.toSet()); + } + + @Override + public int getMinimumY() { + return selection.getMinimumPoint().getBlockY(); + } + + @Override + public int getMaximumY() { + return selection.getMaximumPoint().getBlockY(); + } + }; + } else { + throw new UnsupportedOperationException("Unsupported " + selection.getClass().getSimpleName() + " selection!"); + } + }); + } } diff --git a/implementation/v7/src/main/java/org/codemc/worldguardwrapper/implementation/v7/region/WrappedRegion.java b/implementation/v7/src/main/java/org/codemc/worldguardwrapper/implementation/v7/region/WrappedRegion.java index 5573bd6..eecc43b 100644 --- a/implementation/v7/src/main/java/org/codemc/worldguardwrapper/implementation/v7/region/WrappedRegion.java +++ b/implementation/v7/src/main/java/org/codemc/worldguardwrapper/implementation/v7/region/WrappedRegion.java @@ -3,6 +3,7 @@ package org.codemc.worldguardwrapper.implementation.v7.region; import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.math.BlockVector2; import com.sk89q.worldguard.protection.flags.Flag; +import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion; import lombok.AllArgsConstructor; @@ -18,11 +19,7 @@ import org.codemc.worldguardwrapper.selection.ICuboidSelection; import org.codemc.worldguardwrapper.selection.IPolygonalSelection; import org.codemc.worldguardwrapper.selection.ISelection; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.UUID; +import java.util.*; import java.util.stream.Collectors; @AllArgsConstructor @@ -34,7 +31,19 @@ public class WrappedRegion implements IWrappedRegion { @Override public ISelection getSelection() { - if (handle instanceof ProtectedPolygonalRegion) { + if (handle instanceof ProtectedCuboidRegion) { + return new ICuboidSelection() { + @Override + public Location getMinimumPoint() { + return BukkitAdapter.adapt(world, handle.getMinimumPoint()); + } + + @Override + public Location getMaximumPoint() { + return BukkitAdapter.adapt(world, handle.getMaximumPoint()); + } + }; + } else if (handle instanceof ProtectedPolygonalRegion) { return new IPolygonalSelection() { @Override public Set getPoints() { @@ -54,19 +63,9 @@ public class WrappedRegion implements IWrappedRegion { return handle.getMaximumPoint().getBlockY(); } }; + } else { + throw new UnsupportedOperationException("Unsupported " + handle.getClass().getSimpleName() + " region!"); } - - return new ICuboidSelection() { - @Override - public Location getMinimumPoint() { - return BukkitAdapter.adapt(world, handle.getMinimumPoint()); - } - - @Override - public Location getMaximumPoint() { - return BukkitAdapter.adapt(world, handle.getMaximumPoint()); - } - }; } @Override diff --git a/library/pom.xml b/library/pom.xml index 72eb512..9b914c5 100644 --- a/library/pom.xml +++ b/library/pom.xml @@ -7,7 +7,7 @@ org.codemc.worldguardwrapper worldguardwrapper-parent - 1.1.8-SNAPSHOT + 1.1.9-SNAPSHOT worldguardwrapper @@ -18,22 +18,22 @@ ${project.groupId} worldguardwrapper-api - 1.1.8-SNAPSHOT + 1.1.9-SNAPSHOT ${project.groupId} worldguardwrapper-implementation-legacy - 1.1.8-SNAPSHOT + 1.1.9-SNAPSHOT ${project.groupId} worldguardwrapper-implementation-v6 - 1.1.8-SNAPSHOT + 1.1.9-SNAPSHOT ${project.groupId} worldguardwrapper-implementation-v7 - 1.1.8-SNAPSHOT + 1.1.9-SNAPSHOT diff --git a/pom.xml b/pom.xml index 39eab79..419e485 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.codemc.worldguardwrapper worldguardwrapper-parent - 1.1.8-SNAPSHOT + 1.1.9-SNAPSHOT pom