Skip to content
Permalink
Browse files

Allow WorldEdit's //world override to affect region commands.

Priority is `-w` flag > //world override > player world > error.
Also makes regions selectable from console.
  • Loading branch information
wizjany committed Mar 19, 2020
1 parent 1172ebd commit aca0d843f6553bc2687803d8447b777c41be81de
@@ -129,22 +129,21 @@ public RegionCommands(WorldGuard worldGuard) {
* @throws CommandException any error
*/
@Command(aliases = {"define", "def", "d", "create"},
usage = "<id> [<owner1> [<owner2> [<owners...>]]]",
flags = "ng",
usage = "[-w <world>] <id> [<owner1> [<owner2> [<owners...>]]]",
flags = "ngw:",
desc = "Defines a region",
min = 1)
public void define(CommandContext args, Actor sender) throws CommandException {
warnAboutSaveFailures(sender);
LocalPlayer player = worldGuard.checkPlayer(sender);

// Check permissions
if (!getPermissionModel(player).mayDefine()) {
if (!getPermissionModel(sender).mayDefine()) {
throw new CommandPermissionsException();
}

String id = checkRegionId(args.getString(0), false);

World world = player.getWorld();
World world = checkWorld(args, sender, 'w');
RegionManager manager = checkRegionManager(world);

checkRegionDoesNotExist(manager, id, true);
@@ -154,7 +153,7 @@ public void define(CommandContext args, Actor sender) throws CommandException {
if (args.hasFlag('g')) {
region = new GlobalProtectedRegion(id);
} else {
region = checkRegionFromSelection(player, id);
region = checkRegionFromSelection(sender, id);
}

RegionAdder task = new RegionAdder(manager, region);
@@ -182,24 +181,22 @@ public void define(CommandContext args, Actor sender) throws CommandException {
* @throws CommandException any error
*/
@Command(aliases = {"redefine", "update", "move"},
usage = "<id>",
usage = "[-w <world>] <id>",
desc = "Re-defines the shape of a region",
flags = "g",
flags = "gw:",
min = 1, max = 1)
public void redefine(CommandContext args, Actor sender) throws CommandException {
warnAboutSaveFailures(sender);

LocalPlayer player = worldGuard.checkPlayer(sender);

String id = checkRegionId(args.getString(0), false);

World world = player.getWorld();
World world = checkWorld(args, sender, 'w');
RegionManager manager = checkRegionManager(world);

ProtectedRegion existing = checkExistingRegion(manager, id, false);

// Check permissions
if (!getPermissionModel(player).mayRedefine(existing)) {
if (!getPermissionModel(sender).mayRedefine(existing)) {
throw new CommandPermissionsException();
}

@@ -208,7 +205,7 @@ public void redefine(CommandContext args, Actor sender) throws CommandException
if (args.hasFlag('g')) {
region = new GlobalProtectedRegion(id);
} else {
region = checkRegionFromSelection(player, id);
region = checkRegionFromSelection(sender, id);
}

region.copyFrom(existing);
@@ -221,9 +218,9 @@ public void redefine(CommandContext args, Actor sender) throws CommandException
.sendMessageAfterDelay("(Please wait... " + description + ")")
.onSuccess((Component) null,
t -> {
player.print(String.format("Region '%s' has been updated with a new area.", region.getId()));
warnAboutDimensions(player, region);
informNewUser(player, manager, region);
sender.print(String.format("Region '%s' has been updated with a new area.", region.getId()));
warnAboutDimensions(sender, region);
informNewUser(sender, manager, region);
checkSpawnOverlap(sender, world, region);
})
.onFailure(String.format("Failed to update the region '%s'", region.getId()), worldGuard.getExceptionConverter())
@@ -338,28 +335,34 @@ public void claim(CommandContext args, Actor sender) throws CommandException {
* @throws CommandException any error
*/
@Command(aliases = {"select", "sel", "s"},
usage = "[id]",
usage = "[-w <world>] [id]",
desc = "Load a region as a WorldEdit selection",
min = 0, max = 1)
min = 0, max = 1,
flags = "w:")
public void select(CommandContext args, Actor sender) throws CommandException {
LocalPlayer player = worldGuard.checkPlayer(sender);
RegionManager manager = checkRegionManager(player.getWorld());
World world = checkWorld(args, sender, 'w');
RegionManager manager = checkRegionManager(world);
ProtectedRegion existing;

// If no arguments were given, get the region that the player is inside
if (args.argsLength() == 0) {
existing = checkRegionStandingIn(manager, player, "/rg select %id%");
LocalPlayer player = worldGuard.checkPlayer(sender);
if (!player.getWorld().equals(world)) { // confusing to get current location regions in another world
throw new CommandException("Please specify a region name."); // just don't allow that
}
world = player.getWorld();
existing = checkRegionStandingIn(manager, player, "/rg select -w \"" + world.getName() + "\" %id%");
} else {
existing = checkExistingRegion(manager, args.getString(0), false);
}

// Check permissions
if (!getPermissionModel(player).maySelect(existing)) {
if (!getPermissionModel(sender).maySelect(existing)) {
throw new CommandPermissionsException();
}

// Select
setPlayerSelection(player, existing);
setPlayerSelection(sender, existing, world);
}

/**
@@ -407,7 +410,7 @@ public void info(CommandContext args, Actor sender) throws CommandException {
throw new CommandPermissionsException();
}

setPlayerSelection(worldGuard.checkPlayer(sender), existing);
setPlayerSelection(worldGuard.checkPlayer(sender), existing, world);
}

// Print region information
@@ -84,9 +84,25 @@ protected static RegionPermissionModel getPermissionModel(Actor sender) {
* @throws CommandException on error
*/
protected static World checkWorld(CommandContext args, Actor sender, char flag) throws CommandException {
return checkWorld(args, sender, flag, true);
}

protected static World checkWorld(CommandContext args, Actor sender, char flag, boolean allowWorldEditOverride) throws CommandException {
if (args.hasFlag(flag)) {
return WorldGuard.getInstance().getPlatform().getMatcher().matchWorld(sender, args.getFlag(flag));
} else {
if (allowWorldEditOverride) {
try {
World override = WorldEdit.getInstance().getSessionManager().get(sender).getWorldOverride();
if (override != null) {
if (sender instanceof LocalPlayer && !override.equals(((LocalPlayer) sender).getWorld())) {
sender.printDebug(TextComponent.of("Using //world override for region command: " + override.getName()));
}
return override;
}
} catch (NoSuchMethodError ignored) {
}
}
if (sender instanceof LocalPlayer) {
return ((LocalPlayer) sender).getWorld();
} else {
@@ -219,16 +235,17 @@ protected static ProtectedRegion checkRegionStandingIn(RegionManager regionManag
}

/**
* Get a WorldEdit selection for a player, or emit an exception if there is none
* Get a WorldEdit selection for an actor, or emit an exception if there is none
* available.
*
* @param player the player
* @param actor the actor
* @return the selection
* @throws CommandException thrown on an error
*/
protected static Region checkSelection(LocalPlayer player) throws CommandException {
protected static Region checkSelection(Actor actor) throws CommandException {
try {
return WorldEdit.getInstance().getSessionManager().get(player).getRegionSelector(player.getWorld()).getRegion();
LocalSession localSession = WorldEdit.getInstance().getSessionManager().get(actor);
return localSession.getRegionSelector(localSession.getSelectionWorld()).getRegion();
} catch (IncompleteRegionException e) {
throw new CommandException(
"Please select an area first. " +
@@ -273,15 +290,15 @@ protected static RegionManager checkRegionManager(World world) throws CommandExc
}

/**
* Create a {@link ProtectedRegion} from the player's selection.
* Create a {@link ProtectedRegion} from the actor's selection.
*
* @param player the player
* @param actor the actor
* @param id the ID of the new region
* @return a new region
* @throws CommandException thrown on an error
*/
protected static ProtectedRegion checkRegionFromSelection(LocalPlayer player, String id) throws CommandException {
Region selection = checkSelection(player);
protected static ProtectedRegion checkRegionFromSelection(Actor actor, String id) throws CommandException {
Region selection = checkSelection(actor);

// Detect the type of region from WorldEdit
if (selection instanceof Polygonal2DRegion) {
@@ -365,35 +382,35 @@ protected static boolean checkSpawnOverlap(Actor sender, World world, ProtectedR
}

/**
* Set a player's selection to a given region.
* Set an actor's selection to a given region.
*
* @param player the player
* @param actor the actor
* @param region the region
* @throws CommandException thrown on a command error
*/
protected static void setPlayerSelection(LocalPlayer player, ProtectedRegion region) throws CommandException {
LocalSession session = WorldEdit.getInstance().getSessionManager().get(player);
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(player.getWorld(), pt1, pt2);
session.setRegionSelector(player.getWorld(), selector);
selector.explainRegionAdjust(player, session);
player.print("Region selected as a cuboid.");
CuboidRegionSelector selector = new CuboidRegionSelector(world, pt1, pt2);
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(
player.getWorld(), poly2d.getPoints(),
world, poly2d.getPoints(),
poly2d.getMinimumPoint().getBlockY(),
poly2d.getMaximumPoint().getBlockY());
session.setRegionSelector(player.getWorld(), selector);
selector.explainRegionAdjust(player, session);
player.print("Region selected as a polygon.");
session.setRegionSelector(world, selector);
selector.explainRegionAdjust(actor, session);
actor.print("Region selected as a polygon.");

} else if (region instanceof GlobalProtectedRegion) {
throw new CommandException(

0 comments on commit aca0d84

Please sign in to comment.
You can’t perform that action at this time.