Search in sources :

Example 26 with CommandContext

use of org.spongepowered.api.command.args.CommandContext in project Skree by Skelril.

the class RegionAddMemberCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    if (!(src instanceof Player)) {
        src.sendMessage(Text.of("You must be a player to use this command (for now ;) )!"));
        return CommandResult.empty();
    }
    Optional<RegionService> optService = Sponge.getServiceManager().provide(RegionService.class);
    if (!optService.isPresent()) {
        src.sendMessage(Text.of(TextColors.DARK_RED, "The region service is not currently running."));
        return CommandResult.empty();
    }
    RegionService service = optService.get();
    Player player = (Player) src;
    Optional<Region> optRef = service.getSelectedRegion(player);
    if (!optRef.isPresent()) {
        player.sendMessage(Text.of(TextColors.RED, "You do not currently have a region selected."));
        return CommandResult.empty();
    }
    Region ref = optRef.get();
    if (!ref.getMembers().contains(player.getUniqueId())) {
        player.sendMessage(Text.of(TextColors.RED, "You must be a member of the region to modify it!"));
        return CommandResult.empty();
    }
    List<UUID> newMembers = args.<User>getAll("player").stream().map(Identifiable::getUniqueId).filter(a -> !ref.getMembers().contains(a)).collect(Collectors.toList());
    ref.addMember(newMembers);
    player.sendMessage(Text.of(TextColors.YELLOW, "Added ", newMembers.size(), " players to the region."));
    return CommandResult.success();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) GenericArguments.allOf(org.spongepowered.api.command.args.GenericArguments.allOf) User(org.spongepowered.api.entity.living.player.User) CommandSource(org.spongepowered.api.command.CommandSource) Sponge(org.spongepowered.api.Sponge) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) RegionService(com.skelril.skree.service.RegionService) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Region(com.skelril.skree.service.internal.region.Region) Optional(java.util.Optional) GenericArguments.user(org.spongepowered.api.command.args.GenericArguments.user) Player(org.spongepowered.api.entity.living.player.Player) TextColors(org.spongepowered.api.text.format.TextColors) Identifiable(org.spongepowered.api.util.Identifiable) Player(org.spongepowered.api.entity.living.player.Player) User(org.spongepowered.api.entity.living.player.User) Region(com.skelril.skree.service.internal.region.Region) RegionService(com.skelril.skree.service.RegionService) UUID(java.util.UUID) Identifiable(org.spongepowered.api.util.Identifiable)

Example 27 with CommandContext

use of org.spongepowered.api.command.args.CommandContext in project Skree by Skelril.

the class RegionListMarkersCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    if (!(src instanceof Player)) {
        src.sendMessage(Text.of("You must be a player to use this command (for now ;) )!"));
        return CommandResult.empty();
    }
    Optional<RegionService> optService = Sponge.getServiceManager().provide(RegionService.class);
    if (!optService.isPresent()) {
        src.sendMessage(Text.of(TextColors.DARK_RED, "The region service is not currently running."));
        return CommandResult.empty();
    }
    RegionService service = optService.get();
    Player player = (Player) src;
    Optional<Region> optRef = service.getSelectedRegion(player);
    if (!optRef.isPresent()) {
        player.sendMessage(Text.of(TextColors.RED, "You do not currently have a region selected."));
        return CommandResult.empty();
    }
    Region ref = optRef.get();
    PaginationService pagination = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
    List<RegionPoint> points = ref.getPoints();
    List<Text> result = ref.getFullPoints().stream().map(a -> Text.of((points.contains(a) ? TextColors.GREEN : TextColors.RED), a.getFloorX(), ", ", a.getFloorY(), ", ", a.getFloorZ())).collect(Collectors.toList());
    pagination.builder().contents(result).title(Text.of(TextColors.GOLD, "Region Markers")).padding(Text.of(" ")).sendTo(src);
    return CommandResult.success();
}
Also used : RegionPoint(com.skelril.skree.service.internal.region.RegionPoint) CommandResult(org.spongepowered.api.command.CommandResult) CommandSource(org.spongepowered.api.command.CommandSource) Sponge(org.spongepowered.api.Sponge) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Collectors(java.util.stream.Collectors) RegionService(com.skelril.skree.service.RegionService) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Region(com.skelril.skree.service.internal.region.Region) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) TextColors(org.spongepowered.api.text.format.TextColors) Player(org.spongepowered.api.entity.living.player.Player) Region(com.skelril.skree.service.internal.region.Region) RegionService(com.skelril.skree.service.RegionService) Text(org.spongepowered.api.text.Text) PaginationService(org.spongepowered.api.service.pagination.PaginationService) RegionPoint(com.skelril.skree.service.internal.region.RegionPoint)

Example 28 with CommandContext

use of org.spongepowered.api.command.args.CommandContext in project Skree by Skelril.

the class RegionListMembersCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    if (!(src instanceof Player)) {
        src.sendMessage(Text.of("You must be a player to use this command (for now ;) )!"));
        return CommandResult.empty();
    }
    Optional<RegionService> optService = Sponge.getServiceManager().provide(RegionService.class);
    if (!optService.isPresent()) {
        src.sendMessage(Text.of(TextColors.DARK_RED, "The region service is not currently running."));
        return CommandResult.empty();
    }
    RegionService service = optService.get();
    Player player = (Player) src;
    Optional<Region> optRef = service.getSelectedRegion(player);
    if (!optRef.isPresent()) {
        player.sendMessage(Text.of(TextColors.RED, "You do not currently have a region selected."));
        return CommandResult.empty();
    }
    Region ref = optRef.get();
    UserStorageService userService = Sponge.getServiceManager().provideUnchecked(UserStorageService.class);
    PaginationService pagination = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
    List<Text> result = ref.getMembers().stream().map(userService::get).filter(Optional::isPresent).map(Optional::get).sorted((a, b) -> a.getName().compareToIgnoreCase(b.getName())).map(a -> Text.of((a.isOnline() ? TextColors.GREEN : TextColors.RED), a.getName())).collect(Collectors.toList());
    pagination.builder().contents(result).title(Text.of(TextColors.GOLD, "Region Members")).padding(Text.of(" ")).sendTo(src);
    return CommandResult.success();
}
Also used : UserStorageService(org.spongepowered.api.service.user.UserStorageService) CommandResult(org.spongepowered.api.command.CommandResult) CommandSource(org.spongepowered.api.command.CommandSource) Sponge(org.spongepowered.api.Sponge) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Collectors(java.util.stream.Collectors) RegionService(com.skelril.skree.service.RegionService) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) UserStorageService(org.spongepowered.api.service.user.UserStorageService) List(java.util.List) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Region(com.skelril.skree.service.internal.region.Region) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) TextColors(org.spongepowered.api.text.format.TextColors) Player(org.spongepowered.api.entity.living.player.Player) Optional(java.util.Optional) Region(com.skelril.skree.service.internal.region.Region) RegionService(com.skelril.skree.service.RegionService) Text(org.spongepowered.api.text.Text) PaginationService(org.spongepowered.api.service.pagination.PaginationService)

Example 29 with CommandContext

use of org.spongepowered.api.command.args.CommandContext in project SpongeCommon by SpongePowered.

the class SpongeCommandFactory method sendContainerMeta.

public static void sendContainerMeta(CommandSource src, CommandContext args, String argumentName) {
    for (PluginContainer container : args.<PluginContainer>getAll(argumentName)) {
        Text.Builder builder = Text.builder().append(title(container.getName()));
        container.getVersion().ifPresent(version -> builder.append(Text.of((" v" + version))));
        appendPluginMeta(builder, "ID", container.getId());
        appendPluginMeta(builder, "Description", container.getDescription());
        appendPluginMeta(builder, "URL", container.getUrl().map(url -> {
            ClickAction.OpenUrl action = null;
            try {
                // make the url clickable
                action = TextActions.openUrl(new URL(url));
            } catch (MalformedURLException e) {
            // or not
            }
            return Text.builder(url).onClick(action);
        }));
        if (!container.getAuthors().isEmpty()) {
            appendPluginMeta(builder, "Authors", String.join(", ", container.getAuthors()));
        }
        appendPluginMeta(builder, "Main class", container.getInstance().map(instance -> instance.getClass().getCanonicalName()));
        src.sendMessage(builder.build());
    }
}
Also used : IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) ClickAction(org.spongepowered.api.text.action.ClickAction) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) DimensionType(org.spongepowered.api.world.DimensionType) URL(java.net.URL) CommandCallable(org.spongepowered.api.command.CommandCallable) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) GenericArguments(org.spongepowered.api.command.args.GenericArguments) GenericArguments.choices(org.spongepowered.api.command.args.GenericArguments.choices) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) IMixinDimensionType(org.spongepowered.common.interfaces.world.IMixinDimensionType) GenericArguments.plugin(org.spongepowered.api.command.args.GenericArguments.plugin) WorldConfig(org.spongepowered.common.config.type.WorldConfig) SpongeTimingsFactory(co.aikar.timings.SpongeTimingsFactory) TextActions(org.spongepowered.api.text.action.TextActions) User(org.spongepowered.api.entity.living.player.User) GenericArguments.dimension(org.spongepowered.api.command.args.GenericArguments.dimension) CommandSource(org.spongepowered.api.command.CommandSource) TextStyles(org.spongepowered.api.text.format.TextStyles) Collection(java.util.Collection) GenericArguments.optional(org.spongepowered.api.command.args.GenericArguments.optional) Sponge(org.spongepowered.api.Sponge) Instant(java.time.Instant) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) MixinEnvironment(org.spongepowered.asm.mixin.MixinEnvironment) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) EntityUtil(org.spongepowered.common.entity.EntityUtil) BlockState(org.spongepowered.api.block.BlockState) List(java.util.List) SpongeApiTranslationHelper(org.spongepowered.api.util.SpongeApiTranslationHelper) World(org.spongepowered.api.world.World) DimensionConfig(org.spongepowered.common.config.type.DimensionConfig) CommandManager(org.spongepowered.api.command.CommandManager) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) SpongeConfig(org.spongepowered.common.config.SpongeConfig) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) SpongeImpl(org.spongepowered.common.SpongeImpl) Timings(co.aikar.timings.Timings) GenericArguments.firstParsing(org.spongepowered.api.command.args.GenericArguments.firstParsing) SpongeImplHooks(org.spongepowered.common.SpongeImplHooks) LocalDateTime(java.time.LocalDateTime) CommandMapping(org.spongepowered.api.command.CommandMapping) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) Function(java.util.function.Function) TreeSet(java.util.TreeSet) IMPLEMENTATION(org.spongepowered.api.Platform.Component.IMPLEMENTATION) ArrayList(java.util.ArrayList) PaginationList(org.spongepowered.api.service.pagination.PaginationList) RayTraceResult(net.minecraft.util.math.RayTraceResult) GenericArguments.seq(org.spongepowered.api.command.args.GenericArguments.seq) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Chunk(net.minecraft.world.chunk.Chunk) WorldServer(net.minecraft.world.WorldServer) PluginContainer(org.spongepowered.api.plugin.PluginContainer) TextColors(org.spongepowered.api.text.format.TextColors) Nullable(javax.annotation.Nullable) GenericArguments.literal(org.spongepowered.api.command.args.GenericArguments.literal) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) GenericArguments.world(org.spongepowered.api.command.args.GenericArguments.world) Entity(net.minecraft.entity.Entity) CommandResult(org.spongepowered.api.command.CommandResult) MalformedURLException(java.net.MalformedURLException) SpongeHooks(org.spongepowered.common.util.SpongeHooks) WorldManager(org.spongepowered.common.world.WorldManager) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) GenericArguments.string(org.spongepowered.api.command.args.GenericArguments.string) DecimalFormat(java.text.DecimalFormat) BlockUtil(org.spongepowered.common.block.BlockUtil) GeneralConfigBase(org.spongepowered.common.config.type.GeneralConfigBase) File(java.io.File) CommandException(org.spongepowered.api.command.CommandException) IBlockState(net.minecraft.block.state.IBlockState) ChildCommandElementExecutor(org.spongepowered.api.command.args.ChildCommandElementExecutor) DateTimeFormatter(java.time.format.DateTimeFormatter) GenericArguments.flags(org.spongepowered.api.command.args.GenericArguments.flags) GenericArguments.optionalWeak(org.spongepowered.api.command.args.GenericArguments.optionalWeak) GlobalConfig(org.spongepowered.common.config.type.GlobalConfig) Comparator(java.util.Comparator) MalformedURLException(java.net.MalformedURLException) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Text(org.spongepowered.api.text.Text) URL(java.net.URL)

Example 30 with CommandContext

use of org.spongepowered.api.command.args.CommandContext in project SpongeAPI by SpongePowered.

the class CommandSpec method process.

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
    checkPermission(source);
    final CommandArgs args = new CommandArgs(arguments, getInputTokenizer().tokenize(arguments, false));
    final CommandContext context = new CommandContext();
    this.populateContext(source, args, context);
    return getExecutor().execute(source, context);
}
Also used : CommandArgs(org.spongepowered.api.command.args.CommandArgs) CommandContext(org.spongepowered.api.command.args.CommandContext)

Aggregations

CommandContext (org.spongepowered.api.command.args.CommandContext)55 CommandResult (org.spongepowered.api.command.CommandResult)46 Text (org.spongepowered.api.text.Text)45 CommandSource (org.spongepowered.api.command.CommandSource)40 List (java.util.List)36 Collectors (java.util.stream.Collectors)30 CommandElement (org.spongepowered.api.command.args.CommandElement)30 Player (org.spongepowered.api.entity.living.player.Player)29 Optional (java.util.Optional)28 Sponge (org.spongepowered.api.Sponge)28 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)27 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)27 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)27 NonnullByDefault (org.spongepowered.api.util.annotation.NonnullByDefault)27 GenericArguments (org.spongepowered.api.command.args.GenericArguments)26 TextColors (org.spongepowered.api.text.format.TextColors)24 Util (io.github.nucleuspowered.nucleus.Util)19 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)19 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)18 CommandException (org.spongepowered.api.command.CommandException)18