use of com.sk89q.minecraft.util.commands.Command in project WorldGuard by EngineHub.
the class MemberCommands method removeOwner.
@Command(aliases = { "removeowner", "remowner", "ro" }, usage = "<id> <owners...>", flags = "naw:", desc = "Remove an owner to a region", min = 1)
public void removeOwner(CommandContext args, Actor sender) throws CommandException {
warnAboutSaveFailures(sender);
// Get the world
World world = checkWorld(args, sender, 'w');
String id = args.getString(0);
RegionManager manager = checkRegionManager(world);
ProtectedRegion region = checkExistingRegion(manager, id, true);
// Check permissions
if (!getPermissionModel(sender).mayRemoveOwners(region)) {
throw new CommandPermissionsException();
}
Callable<DefaultDomain> callable;
if (args.hasFlag('a')) {
callable = region::getOwners;
} else {
if (args.argsLength() < 2) {
throw new CommandException("List some names to remove, or use -a to remove all.");
}
// Resolve owners asynchronously
DomainInputResolver resolver = new DomainInputResolver(WorldGuard.getInstance().getProfileService(), args.getParsedPaddedSlice(1, 0));
resolver.setLocatorPolicy(args.hasFlag('n') ? UserLocatorPolicy.NAME_ONLY : UserLocatorPolicy.UUID_AND_NAME);
callable = resolver;
}
final String description = String.format("Removing owners from the region '%s' on '%s'", region.getId(), world.getName());
AsyncCommandBuilder.wrap(callable, sender).registerWithSupervisor(worldGuard.getSupervisor(), description).sendMessageAfterDelay("(Please wait... querying player names...)").onSuccess(String.format("Region '%s' updated with owners removed.", region.getId()), region.getOwners()::removeAll).onFailure("Failed to remove owners", worldGuard.getExceptionConverter()).buildAndExec(worldGuard.getExecutorService());
}
use of com.sk89q.minecraft.util.commands.Command in project WorldGuard by EngineHub.
the class RegionCommands method teleport.
/**
* Teleport to a region
*
* @param args the arguments
* @param sender the sender
* @throws CommandException any error
*/
@Command(aliases = { "teleport", "tp" }, usage = "[-w world] [-c|s] <id>", flags = "csw:", desc = "Teleports you to the location associated with the region.", min = 1, max = 1)
public void teleport(CommandContext args, Actor sender) throws CommandException {
LocalPlayer player = worldGuard.checkPlayer(sender);
Location teleportLocation;
// Lookup the existing region
World world = checkWorld(args, player, 'w');
RegionManager regionManager = checkRegionManager(world);
ProtectedRegion existing = checkExistingRegion(regionManager, args.getString(0), true);
// Check permissions
if (!getPermissionModel(player).mayTeleportTo(existing)) {
throw new CommandPermissionsException();
}
// -s for spawn location
if (args.hasFlag('s')) {
teleportLocation = FlagValueCalculator.getEffectiveFlagOf(existing, Flags.SPAWN_LOC, player);
if (teleportLocation == null) {
throw new CommandException("The region has no spawn point associated.");
}
} else if (args.hasFlag('c')) {
// Check permissions
if (!getPermissionModel(player).mayTeleportToCenter(existing)) {
throw new CommandPermissionsException();
}
Region region = WorldEditRegionConverter.convertToRegion(existing);
if (region == null || region.getCenter() == null) {
throw new CommandException("The region has no center point.");
}
if (player.getGameMode() == GameModes.SPECTATOR) {
teleportLocation = new Location(world, region.getCenter(), 0, 0);
} else {
// It doesn't return the found location and it can't be checked if the location is inside the region.
throw new CommandException("Center teleport is only availible in Spectator gamemode.");
}
} else {
teleportLocation = FlagValueCalculator.getEffectiveFlagOf(existing, Flags.TELE_LOC, player);
if (teleportLocation == null) {
throw new CommandException("The region has no teleport point associated.");
}
}
String message = FlagValueCalculator.getEffectiveFlagOf(existing, Flags.TELE_MESSAGE, player);
// If message.isEmpty(), no message is sent by LocalPlayer#teleport(...)
if (message == null) {
message = Flags.TELE_MESSAGE.getDefault();
}
player.teleport(teleportLocation, message.replace("%id%", existing.getId()), "Unable to teleport to region '" + existing.getId() + "'.");
}
use of com.sk89q.minecraft.util.commands.Command in project WorldGuard by EngineHub.
the class RegionCommands method claim.
/**
* Claiming command for users.
*
* <p>This command is a joke and it needs to be rewritten. It was contributed
* code :(</p>
*
* @param args the arguments
* @param sender the sender
* @throws CommandException any error
*/
@Command(aliases = { "claim" }, usage = "<id>", desc = "Claim a region", min = 1, max = 1)
public void claim(CommandContext args, Actor sender) throws CommandException {
warnAboutSaveFailures(sender);
LocalPlayer player = worldGuard.checkPlayer(sender);
RegionPermissionModel permModel = getPermissionModel(player);
// Check permissions
if (!permModel.mayClaim()) {
throw new CommandPermissionsException();
}
String id = checkRegionId(args.getString(0), false);
RegionManager manager = checkRegionManager(player.getWorld());
checkRegionDoesNotExist(manager, id, false);
ProtectedRegion region = checkRegionFromSelection(player, id);
WorldConfiguration wcfg = WorldGuard.getInstance().getPlatform().getGlobalStateManager().get(player.getWorld());
// Check whether the player has created too many regions
if (!permModel.mayClaimRegionsUnbounded()) {
int maxRegionCount = wcfg.getMaxRegionCount(player);
if (maxRegionCount >= 0 && manager.getRegionCountOfPlayer(player) >= maxRegionCount) {
throw new CommandException("You own too many regions, delete one first to claim a new one.");
}
}
ProtectedRegion existing = manager.getRegion(id);
// Check for an existing region
if (existing != null) {
if (!existing.getOwners().contains(player)) {
throw new CommandException("This region already exists and you don't own it.");
}
}
// We have to check whether this region violates the space of any other region
ApplicableRegionSet regions = manager.getApplicableRegions(region);
// Check if this region overlaps any other region
if (regions.size() > 0) {
if (!regions.isOwnerOfAll(player)) {
throw new CommandException("This region overlaps with someone else's region.");
}
} else {
if (wcfg.claimOnlyInsideExistingRegions) {
throw new CommandException("You may only claim regions inside " + "existing regions that you or your group own.");
}
}
if (wcfg.maxClaimVolume >= Integer.MAX_VALUE) {
throw new CommandException("The maximum claim volume get in the configuration is higher than is supported. " + "Currently, it must be " + Integer.MAX_VALUE + " or smaller. Please contact a server administrator.");
}
// Check claim volume
if (!permModel.mayClaimRegionsUnbounded()) {
if (region instanceof ProtectedPolygonalRegion) {
throw new CommandException("Polygons are currently not supported for /rg claim.");
}
if (region.volume() > wcfg.maxClaimVolume) {
player.printError("This region is too large to claim.");
player.printError("Max. volume: " + wcfg.maxClaimVolume + ", your volume: " + region.volume());
return;
}
}
// Inherit from a template region
if (!Strings.isNullOrEmpty(wcfg.setParentOnClaim)) {
ProtectedRegion templateRegion = manager.getRegion(wcfg.setParentOnClaim);
if (templateRegion != null) {
try {
region.setParent(templateRegion);
} catch (CircularInheritanceException e) {
throw new CommandException(e.getMessage());
}
}
}
RegionAdder task = new RegionAdder(manager, region);
task.setLocatorPolicy(UserLocatorPolicy.UUID_ONLY);
task.setOwnersInput(new String[] { player.getName() });
final String description = String.format("Claiming region '%s'", id);
AsyncCommandBuilder.wrap(task, sender).registerWithSupervisor(WorldGuard.getInstance().getSupervisor(), description).sendMessageAfterDelay("(Please wait... " + description + ")").onSuccess(TextComponent.of(String.format("A new region has been claimed named '%s'.", id)), null).onFailure("Failed to claim region", WorldGuard.getInstance().getExceptionConverter()).buildAndExec(WorldGuard.getInstance().getExecutorService());
}
use of com.sk89q.minecraft.util.commands.Command in project WorldGuard by EngineHub.
the class RegionCommands method migrateUuid.
/**
* Migrate the region databases to use UUIDs rather than name.
*
* @param args the arguments
* @param sender the sender
* @throws CommandException any error
*/
@Command(aliases = { "migrateuuid" }, desc = "Migrate loaded databases to use UUIDs", max = 0)
public void migrateUuid(CommandContext args, Actor sender) throws CommandException {
// Check permissions
if (!getPermissionModel(sender).mayMigrateRegionNames()) {
throw new CommandPermissionsException();
}
LoggerToChatHandler handler = null;
Logger minecraftLogger = null;
if (sender instanceof LocalPlayer) {
handler = new LoggerToChatHandler(sender);
handler.setLevel(Level.ALL);
minecraftLogger = Logger.getLogger("com.sk89q.worldguard");
minecraftLogger.addHandler(handler);
}
try {
ConfigurationManager config = WorldGuard.getInstance().getPlatform().getGlobalStateManager();
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
RegionDriver driver = container.getDriver();
UUIDMigration migration = new UUIDMigration(driver, WorldGuard.getInstance().getProfileService(), WorldGuard.getInstance().getFlagRegistry());
migration.setKeepUnresolvedNames(config.keepUnresolvedNames);
sender.print("Now performing migration... this may take a while.");
container.migrate(migration);
sender.print("Migration complete!");
} catch (MigrationException e) {
log.log(Level.WARNING, "Failed to migrate", e);
throw new CommandException("Error encountered while migrating: " + e.getMessage());
} finally {
if (minecraftLogger != null) {
minecraftLogger.removeHandler(handler);
}
}
}
use of com.sk89q.minecraft.util.commands.Command in project WorldGuard by EngineHub.
the class RegionCommands method flag.
/**
* Set a flag.
*
* @param args the arguments
* @param sender the sender
* @throws CommandException any error
*/
@Command(aliases = { "flag", "f" }, usage = "<id> <flag> [-w world] [-g group] [value]", flags = "g:w:eh:", desc = "Set flags", min = 2)
public void flag(CommandContext args, Actor sender) throws CommandException {
warnAboutSaveFailures(sender);
// Get the world
World world = checkWorld(args, sender, 'w');
String flagName = args.getString(1);
String value = args.argsLength() >= 3 ? args.getJoinedStrings(2) : null;
RegionGroup groupValue = null;
FlagRegistry flagRegistry = WorldGuard.getInstance().getFlagRegistry();
RegionPermissionModel permModel = getPermissionModel(sender);
if (args.hasFlag('e')) {
if (value != null) {
throw new CommandException("You cannot use -e(mpty) with a flag value.");
}
value = "";
}
// Lookup the existing region
RegionManager manager = checkRegionManager(world);
ProtectedRegion existing = checkExistingRegion(manager, args.getString(0), true);
// Check permissions
if (!permModel.maySetFlag(existing)) {
throw new CommandPermissionsException();
}
String regionId = existing.getId();
Flag<?> foundFlag = Flags.fuzzyMatchFlag(flagRegistry, flagName);
// can use, and do nothing afterwards
if (foundFlag == null) {
AsyncCommandBuilder.wrap(new FlagListBuilder(flagRegistry, permModel, existing, world, regionId, sender, flagName), sender).registerWithSupervisor(WorldGuard.getInstance().getSupervisor(), "Flag list for invalid flag command.").onSuccess((Component) null, sender::print).onFailure((Component) null, WorldGuard.getInstance().getExceptionConverter()).buildAndExec(WorldGuard.getInstance().getExecutorService());
return;
} else if (value != null) {
if (foundFlag == Flags.BUILD || foundFlag == Flags.BLOCK_BREAK || foundFlag == Flags.BLOCK_PLACE) {
sender.print(buildFlagWarning);
if (!sender.isPlayer()) {
sender.printRaw("https://worldguard.enginehub.org/en/latest/regions/flags/#protection-related");
}
} else if (foundFlag == Flags.PASSTHROUGH) {
sender.print(passthroughFlagWarning);
if (!sender.isPlayer()) {
sender.printRaw("https://worldguard.enginehub.org/en/latest/regions/flags/#overrides");
}
}
}
// but not here -- in the model
if (!permModel.maySetFlag(existing, foundFlag, value)) {
throw new CommandPermissionsException();
}
// -g for group flag
if (args.hasFlag('g')) {
String group = args.getFlag('g');
RegionGroupFlag groupFlag = foundFlag.getRegionGroupFlag();
if (groupFlag == null) {
throw new CommandException("Region flag '" + foundFlag.getName() + "' does not have a group flag!");
}
// the [value] part throws an error.
try {
groupValue = groupFlag.parseInput(FlagContext.create().setSender(sender).setInput(group).setObject("region", existing).build());
} catch (InvalidFlagFormat e) {
throw new CommandException(e.getMessage());
}
}
// Set the flag value if a value was set
if (value != null) {
// Set the flag if [value] was given even if [-g group] was given as well
try {
value = setFlag(existing, foundFlag, sender, value).toString();
} catch (InvalidFlagFormat e) {
throw new CommandException(e.getMessage());
}
if (!args.hasFlag('h')) {
sender.print("Region flag " + foundFlag.getName() + " set on '" + regionId + "' to '" + value + "'.");
}
// No value? Clear the flag, if -g isn't specified
} else if (!args.hasFlag('g')) {
// Clear the flag only if neither [value] nor [-g group] was given
existing.setFlag(foundFlag, null);
// Also clear the associated group flag if one exists
RegionGroupFlag groupFlag = foundFlag.getRegionGroupFlag();
if (groupFlag != null) {
existing.setFlag(groupFlag, null);
}
if (!args.hasFlag('h')) {
sender.print("Region flag " + foundFlag.getName() + " removed from '" + regionId + "'. (Any -g(roups) were also removed.)");
}
}
// Now set the group
if (groupValue != null) {
RegionGroupFlag groupFlag = foundFlag.getRegionGroupFlag();
// If group set to the default, then clear the group flag
if (groupValue == groupFlag.getDefault()) {
existing.setFlag(groupFlag, null);
sender.print("Region group flag for '" + foundFlag.getName() + "' reset to default.");
} else {
existing.setFlag(groupFlag, groupValue);
sender.print("Region group flag for '" + foundFlag.getName() + "' set.");
}
}
// Print region information
if (args.hasFlag('h')) {
int page = args.getFlagInteger('h');
sendFlagHelper(sender, world, existing, permModel, page);
} else {
RegionPrintoutBuilder printout = new RegionPrintoutBuilder(world.getName(), existing, null, sender);
printout.append(SubtleFormat.wrap("(Current flags: "));
printout.appendFlagsList(false);
printout.append(SubtleFormat.wrap(")"));
printout.send(sender);
checkSpawnOverlap(sender, world, existing);
}
}
Aggregations