Search in sources :

Example 36 with CommandContext

use of org.spongepowered.api.command.args.CommandContext in project Nucleus by NucleusPowered.

the class KitViewCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    final Kit kitInfo = args.<Kit>getOne(KIT_PARAMETER).get();
    Inventory inventory = Util.getKitInventoryBuilder().property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.view.title", kitInfo.getName()))).build(this.plugin);
    List<ItemStack> lis = kitInfo.getStacks().stream().filter(x -> !x.getType().equals(ItemTypes.NONE)).map(ItemStackSnapshot::createStack).collect(Collectors.toList());
    if (this.processTokens) {
        KIT_HANDLER.processTokensInItemStacks(src, lis);
    }
    lis.forEach(inventory::offer);
    return src.openInventory(inventory).map(x -> {
        KIT_HANDLER.addViewer(x);
        return CommandResult.success();
    }).orElseThrow(() -> ReturnMessageException.fromKey("command.kit.view.cantopen", kitInfo.getName()));
}
Also used : Inventory(org.spongepowered.api.item.inventory.Inventory) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) ItemTypes(org.spongepowered.api.item.ItemTypes) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) Since(io.github.nucleuspowered.nucleus.internal.annotations.Since) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) KitFallbackBase(io.github.nucleuspowered.nucleus.modules.kit.commands.KitFallbackBase) GenericArguments(org.spongepowered.api.command.args.GenericArguments) ItemStack(org.spongepowered.api.item.inventory.ItemStack) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) Util(io.github.nucleuspowered.nucleus.Util) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) KitHandler(io.github.nucleuspowered.nucleus.modules.kit.handlers.KitHandler) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) InventoryTitle(org.spongepowered.api.item.inventory.property.InventoryTitle) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) Player(org.spongepowered.api.entity.living.player.Player) KitArgument(io.github.nucleuspowered.nucleus.argumentparsers.KitArgument) KitConfigAdapter(io.github.nucleuspowered.nucleus.modules.kit.config.KitConfigAdapter) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory)

Example 37 with CommandContext

use of org.spongepowered.api.command.args.CommandContext in project Nucleus by NucleusPowered.

the class StaffChatCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    Optional<String> toSend = args.getOne(message);
    if (toSend.isPresent()) {
        try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            frame.addContext(EventContexts.SHOULD_FORMAT_CHANNEL, StaffChatMessageChannel.getInstance().formatMessages());
            if (src instanceof Player) {
                Player pl = (Player) src;
                frame.pushCause(pl);
                frame.addContext(EventContextKeys.PLAYER_SIMULATED, pl.getProfile());
                MessageChannel mc = pl.getMessageChannel();
                pl.setMessageChannel(StaffChatMessageChannel.getInstance());
                pl.simulateChat(TextParsingUtils.addUrls(toSend.get()), Sponge.getCauseStackManager().getCurrentCause());
                pl.setMessageChannel(mc);
            } else {
                StaffChatMessageChannel.getInstance().send(src, TextParsingUtils.addUrls(toSend.get()), ChatTypes.CHAT);
            }
            return CommandResult.success();
        }
    }
    if (!(src instanceof Player)) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.staffchat.consoletoggle"));
    }
    Player player = (Player) src;
    StaffChatTransientModule s = plugin.getUserDataManager().get(player).map(y -> y.getTransient(StaffChatTransientModule.class)).orElseGet(StaffChatTransientModule::new);
    boolean result = !(src.getMessageChannel() instanceof StaffChatMessageChannel);
    if (result) {
        s.setPreviousMessageChannel(player.getMessageChannel());
        src.setMessageChannel(StaffChatMessageChannel.getInstance());
    } else {
        src.setMessageChannel(s.getPreviousMessageChannel().orElse(MessageChannel.TO_ALL));
    }
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.staffchat." + (result ? "on" : "off")));
    return CommandResult.success();
}
Also used : ChatTypes(org.spongepowered.api.text.chat.ChatTypes) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) RemainingStringsArgument(io.github.nucleuspowered.nucleus.argumentparsers.RemainingStringsArgument) TextParsingUtils(io.github.nucleuspowered.nucleus.internal.text.TextParsingUtils) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) EventContexts(io.github.nucleuspowered.nucleus.api.EventContexts) CommandSource(org.spongepowered.api.command.CommandSource) StaffChatMessageChannel(io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel) Sponge(org.spongepowered.api.Sponge) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CommandElement(org.spongepowered.api.command.args.CommandElement) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) StaffChatTransientModule(io.github.nucleuspowered.nucleus.modules.staffchat.datamodules.StaffChatTransientModule) Player(org.spongepowered.api.entity.living.player.Player) StaffChatMessageChannel(io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) StaffChatTransientModule(io.github.nucleuspowered.nucleus.modules.staffchat.datamodules.StaffChatTransientModule) StaffChatMessageChannel(io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel)

Example 38 with CommandContext

use of org.spongepowered.api.command.args.CommandContext in project Nucleus by NucleusPowered.

the class TemporaryMessageCommand method executeCommand.

@Override
protected CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    // Get the temporary message item.
    ServerListGeneralDataModule mod = plugin.getGeneralService().get(ServerListGeneralDataModule.class);
    if (args.hasAny("r")) {
        if (mod.getMessage().isPresent()) {
            // Remove
            mod.remove();
            // Send message.
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.serverlist.message.removed"));
            return CommandResult.success();
        }
        throw ReturnMessageException.fromKey("command.serverlist.message.noremoved");
    }
    // Which line?
    boolean linetwo = args.<Integer>getOne(line).map(x -> x == 2).orElse(false);
    Optional<String> onMessage = args.getOne(this.message);
    if (!onMessage.isPresent()) {
        boolean isValid = mod.getExpiry().map(x -> x.isAfter(Instant.now())).orElse(false);
        if (!isValid) {
            throw ReturnMessageException.fromKey("command.serverlist.message.isempty");
        }
        if (linetwo) {
            mod.setLineTwo(null);
        } else {
            mod.setLineOne(null);
        }
        Optional<Text> newMessage = mod.getMessage();
        if (newMessage.isPresent()) {
            // Send message
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.serverlist.message.set"));
            src.sendMessage(newMessage.get());
        } else {
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.serverlist.message.empty"));
        }
        return CommandResult.success();
    }
    String nMessage = onMessage.get();
    // If the expiry is null or before now, and there is no timespan, then it's an hour.
    Instant endTime = args.<Long>getOne(timespan).map(x -> Instant.now().plus(x, ChronoUnit.SECONDS)).orElseGet(() -> mod.getExpiry().map(x -> x.isBefore(Instant.now()) ? x.plusSeconds(3600) : x).orElseGet(() -> Instant.now().plusSeconds(3600)));
    // Set the expiry.
    mod.setExpiry(endTime);
    if (linetwo) {
        mod.setLineTwo(nMessage);
    } else {
        mod.setLineOne(nMessage);
    }
    Optional<Text> newMessage = mod.getMessage();
    if (newMessage.isPresent()) {
        // Send message
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.serverlist.message.set"));
        src.sendMessage(newMessage.get());
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.serverlist.message.expiry", Util.getTimeToNow(endTime)));
        return CommandResult.success();
    }
    throw ReturnMessageException.fromKey("command.serverlist.message.notset");
}
Also used : BoundedIntegerArgument(io.github.nucleuspowered.nucleus.argumentparsers.BoundedIntegerArgument) CommandResult(org.spongepowered.api.command.CommandResult) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) CommandSource(org.spongepowered.api.command.CommandSource) ServerListGeneralDataModule(io.github.nucleuspowered.nucleus.modules.serverlist.datamodules.ServerListGeneralDataModule) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) Instant(java.time.Instant) CommandElement(org.spongepowered.api.command.args.CommandElement) GenericArguments(org.spongepowered.api.command.args.GenericArguments) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) ChronoUnit(java.time.temporal.ChronoUnit) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) TimespanArgument(io.github.nucleuspowered.nucleus.argumentparsers.TimespanArgument) Optional(java.util.Optional) Util(io.github.nucleuspowered.nucleus.Util) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) Instant(java.time.Instant) ServerListGeneralDataModule(io.github.nucleuspowered.nucleus.modules.serverlist.datamodules.ServerListGeneralDataModule) Text(org.spongepowered.api.text.Text)

Example 39 with CommandContext

use of org.spongepowered.api.command.args.CommandContext in project Nucleus by NucleusPowered.

the class PowertoolCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    ItemStack itemStack = src.getItemInHand(HandTypes.MAIN_HAND).orElseThrow(() -> ReturnMessageException.fromKey("command.powertool.noitem"));
    Optional<String> command = args.getOne(commandKey);
    PowertoolUserDataModule inu = Nucleus.getNucleus().getUserDataManager().getUnchecked(src).get(PowertoolUserDataModule.class);
    return command.map(s -> setPowertool(src, inu, itemStack.getType(), s)).orElseGet(() -> viewPowertool(src, inu, itemStack));
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule) GenericArguments(org.spongepowered.api.command.args.GenericArguments) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Lists(com.google.common.collect.Lists) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Util(io.github.nucleuspowered.nucleus.Util) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) TextColors(org.spongepowered.api.text.format.TextColors) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) HandTypes(org.spongepowered.api.data.type.HandTypes) Optional(java.util.Optional) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) Player(org.spongepowered.api.entity.living.player.Player) ItemType(org.spongepowered.api.item.ItemType) ItemStack(org.spongepowered.api.item.inventory.ItemStack) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule)

Example 40 with CommandContext

use of org.spongepowered.api.command.args.CommandContext in project Nucleus by NucleusPowered.

the class WarpCommand method preProcessChecks.

@Override
protected ContinueMode preProcessChecks(final CommandSource source, CommandContext args) {
    if (args.<Player>getOne(playerKey).map(x -> !(source instanceof Player) || x.getUniqueId().equals(((Player) source).getUniqueId())).orElse(false)) {
        // Bypass cooldowns
        args.putArg(NoModifiersArgument.NO_COOLDOWN_ARGUMENT, true);
        return ContinueMode.CONTINUE;
    }
    if (!plugin.getEconHelper().economyServiceExists() || permissions.testCostExempt(source) || args.hasAny("y")) {
        return ContinueMode.CONTINUE;
    }
    Warp wd = args.<Warp>getOne(warpNameArg).get();
    Optional<Double> i = wd.getCost();
    double cost = i.orElse(this.defaultCost);
    if (cost <= 0) {
        return ContinueMode.CONTINUE;
    }
    String costWithUnit = plugin.getEconHelper().getCurrencySymbol(cost);
    if (plugin.getEconHelper().hasBalance((Player) source, cost)) {
        String command = String.format("/warp -y %s", wd.getName());
        source.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.cost.details", wd.getName(), costWithUnit));
        source.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.cost.clickaccept").toBuilder().onClick(TextActions.runCommand(command)).onHover(TextActions.showText(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.cost.clickhover", command))).append(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.cost.alt")).build());
    } else {
        source.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.warp.cost.nomoney", wd.getName(), costWithUnit));
    }
    return ContinueMode.STOP;
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) NoCost(io.github.nucleuspowered.nucleus.internal.annotations.command.NoCost) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) HashMap(java.util.HashMap) GenericArguments(org.spongepowered.api.command.args.GenericArguments) Warp(io.github.nucleuspowered.nucleus.api.nucleusdata.Warp) WarpArgument(io.github.nucleuspowered.nucleus.argumentparsers.WarpArgument) Lists(com.google.common.collect.Lists) PermissionRegistry(io.github.nucleuspowered.nucleus.internal.PermissionRegistry) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) AdditionalCompletionsArgument(io.github.nucleuspowered.nucleus.argumentparsers.AdditionalCompletionsArgument) RequiredArgumentsArgument(io.github.nucleuspowered.nucleus.argumentparsers.RequiredArgumentsArgument) SelectorWrapperArgument(io.github.nucleuspowered.nucleus.argumentparsers.SelectorWrapperArgument) WarpConfigAdapter(io.github.nucleuspowered.nucleus.modules.warp.config.WarpConfigAdapter) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) ContinueMode(io.github.nucleuspowered.nucleus.internal.command.ContinueMode) NucleusTeleportHandler(io.github.nucleuspowered.nucleus.internal.teleport.NucleusTeleportHandler) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) User(org.spongepowered.api.entity.living.player.User) CommandSource(org.spongepowered.api.command.CommandSource) WarpConfig(io.github.nucleuspowered.nucleus.modules.warp.config.WarpConfig) NicknameArgument(io.github.nucleuspowered.nucleus.argumentparsers.NicknameArgument) CauseStackHelper(io.github.nucleuspowered.nucleus.util.CauseStackHelper) Sponge(org.spongepowered.api.Sponge) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) NoModifiersArgument(io.github.nucleuspowered.nucleus.argumentparsers.NoModifiersArgument) UseWarpEvent(io.github.nucleuspowered.nucleus.modules.warp.event.UseWarpEvent) Optional(java.util.Optional) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) Player(org.spongepowered.api.entity.living.player.Player) Warp(io.github.nucleuspowered.nucleus.api.nucleusdata.Warp) Player(org.spongepowered.api.entity.living.player.Player)

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