Merge pull request #1615 from JOO200/toRegionSelector

Add Helper to convert WorldGuard's ProtectedRegion to WorldEdit's Region and RegionSelector
This commit is contained in:
wizjany 2020-07-29 23:42:00 -04:00 committed by GitHub
commit efe1e48bd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 26 deletions

View File

@ -31,6 +31,7 @@
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Polygonal2DRegion;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionSelector;
import com.sk89q.worldedit.regions.selector.CuboidRegionSelector;
import com.sk89q.worldedit.regions.selector.Polygonal2DRegionSelector;
import com.sk89q.worldedit.util.formatting.component.ErrorFormat;
@ -54,6 +55,7 @@
import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import com.sk89q.worldguard.protection.util.WorldEditRegionConverter;
import java.util.Set;
import java.util.stream.Collectors;
@ -394,35 +396,15 @@ protected static boolean checkSpawnOverlap(Actor sender, World world, ProtectedR
protected static void setPlayerSelection(Actor actor, ProtectedRegion region, World world) throws CommandException {
LocalSession session = WorldEdit.getInstance().getSessionManager().get(actor);
// Set selection
if (region instanceof ProtectedCuboidRegion) {
ProtectedCuboidRegion cuboid = (ProtectedCuboidRegion) region;
BlockVector3 pt1 = cuboid.getMinimumPoint();
BlockVector3 pt2 = cuboid.getMaximumPoint();
CuboidRegionSelector selector = new CuboidRegionSelector(world, pt1, pt2);
RegionSelector selector = WorldEditRegionConverter.convertToSelector(region);
if (selector != null) {
selector.setWorld(world);
session.setRegionSelector(world, selector);
selector.explainRegionAdjust(actor, session);
actor.print("Region selected as a cuboid.");
} else if (region instanceof ProtectedPolygonalRegion) {
ProtectedPolygonalRegion poly2d = (ProtectedPolygonalRegion) region;
Polygonal2DRegionSelector selector = new Polygonal2DRegionSelector(
world, poly2d.getPoints(),
poly2d.getMinimumPoint().getBlockY(),
poly2d.getMaximumPoint().getBlockY());
session.setRegionSelector(world, selector);
selector.explainRegionAdjust(actor, session);
actor.print("Region selected as a polygon.");
} else if (region instanceof GlobalProtectedRegion) {
throw new CommandException(
"Can't select global regions! " +
"That would cover the entire world.");
actor.print("Region selected as " + region.getType().getName());
} else {
throw new CommandException("Unknown region type: " +
region.getClass().getCanonicalName());
throw new CommandException("Can't select that region! " +
"The region type '" + region.getType().getName() + "' can't be selected.");
}
}

View File

@ -0,0 +1,75 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldGuard team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldguard.protection.util;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Polygonal2DRegion;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionSelector;
import com.sk89q.worldedit.regions.selector.CuboidRegionSelector;
import com.sk89q.worldedit.regions.selector.Polygonal2DRegionSelector;
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
/**
* A helper class to convert regions from WorldGuard to WorldEdit
*/
public final class WorldEditRegionConverter {
private WorldEditRegionConverter() {
}
/**
* Converts a ProtectedRegion to a WorldEdit Region, otherwise null if
* the ProtectedRegion can't be converted to a RegionSelector.
*
* @param region the WorldGuard region
* @return the WorldEdit Region
*/
public static Region convertToRegion(ProtectedRegion region) {
if (region instanceof ProtectedCuboidRegion) {
return new CuboidRegion(null, region.getMinimumPoint(), region.getMaximumPoint());
}
if (region instanceof ProtectedPolygonalRegion) {
return new Polygonal2DRegion(null, region.getPoints(),
region.getMinimumPoint().getY(), region.getMaximumPoint().getY());
}
return null;
}
/**
* Converts a ProtectedRegion to a WorldEdit RegionSelector, otherwise null if
* the ProtectedRegion can't be converted to a RegionSelector.
*
* @param region the WorldGuard region
* @return the WorldEdit Region
*/
public static RegionSelector convertToSelector(ProtectedRegion region) {
if (region instanceof ProtectedCuboidRegion) {
return new CuboidRegionSelector(null, region.getMinimumPoint(), region.getMaximumPoint());
}
if (region instanceof ProtectedPolygonalRegion) {
return new Polygonal2DRegionSelector(null, region.getPoints(),
region.getMinimumPoint().getY(), region.getMaximumPoint().getY());
}
return null;
}
}