Search in sources :

Example 11 with ReturnMessageException

use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.

the class BasicCraftingCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    Inventory i = Inventory.builder().of(getArchetype()).build(plugin);
    src.openInventory(i).orElseThrow(() -> new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.crafting.error")));
    return CommandResult.success();
}
Also used : ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) Inventory(org.spongepowered.api.item.inventory.Inventory)

Example 12 with ReturnMessageException

use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.

the class TempBanCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    User u = args.<User>getOne(user).get();
    Long time = args.<Long>getOne(duration).get();
    String reason = args.<String>getOne(reasonKey).orElse(plugin.getMessageProvider().getMessageWithFormat("ban.defaultreason"));
    if (permissions.testSuffix(u, "exempt.target", src, false)) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.tempban.exempt", u.getName()));
    }
    if (!u.isOnline() && !permissions.testSuffix(src, "offline")) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.tempban.offline.noperms"));
    }
    if (time > banConfig.getMaximumTempBanLength() && banConfig.getMaximumTempBanLength() != -1 && !permissions.testSuffix(src, "exempt.length")) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.tempban.length.toolong", Util.getTimeStringFromSeconds(this.banConfig.getMaximumTempBanLength())));
        return CommandResult.success();
    }
    BanService service = Sponge.getServiceManager().provideUnchecked(BanService.class);
    if (service.isBanned(u.getProfile())) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.ban.alreadyset", u.getName()));
        return CommandResult.empty();
    }
    // Expiration date
    Instant date = Instant.now().plus(time, ChronoUnit.SECONDS);
    // Create the ban.
    Ban bp = Ban.builder().type(BanTypes.PROFILE).profile(u.getProfile()).source(src).expirationDate(date).reason(TextSerializers.FORMATTING_CODE.deserialize(reason)).build();
    service.addBan(bp);
    MutableMessageChannel send = new PermissionMessageChannel(BanCommand.notifyPermission).asMutable();
    send.addMember(src);
    send.send(plugin.getMessageProvider().getTextMessageWithFormat("command.tempban.applied", u.getName(), Util.getTimeStringFromSeconds(time), src.getName()));
    send.send(plugin.getMessageProvider().getTextMessageWithFormat("standard.reasoncoloured", reason));
    if (Sponge.getServer().getPlayer(u.getUniqueId()).isPresent()) {
        Sponge.getServer().getPlayer(u.getUniqueId()).get().kick(TextSerializers.FORMATTING_CODE.deserialize(reason));
    }
    return CommandResult.success();
}
Also used : PermissionMessageChannel(io.github.nucleuspowered.nucleus.util.PermissionMessageChannel) User(org.spongepowered.api.entity.living.player.User) Instant(java.time.Instant) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) BanService(org.spongepowered.api.service.ban.BanService) Ban(org.spongepowered.api.util.ban.Ban) MutableMessageChannel(org.spongepowered.api.text.channel.MutableMessageChannel)

Example 13 with ReturnMessageException

use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.

the class GamemodeBase method baseCommand.

CommandResult baseCommand(CommandSource src, Player user, GameMode gm) throws Exception {
    if (!this.permissions.testSuffix(src, MODE_MAP.computeIfAbsent(gm.getId(), key -> {
        String[] keySplit = key.split(":", 2);
        String r = keySplit[keySplit.length - 1].toLowerCase();
        MODE_MAP.put(key, "modes." + r);
        return "modes." + r;
    }))) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.gamemode.permission", gm.getTranslation().get()));
    }
    DataTransactionResult dtr = user.offer(Keys.GAME_MODE, gm);
    if (dtr.isSuccessful()) {
        if (!src.equals(user)) {
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.gamemode.set.other", user.getName(), gm.getName()));
        }
        user.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.gamemode.set.base", gm.getName()));
        return CommandResult.success();
    }
    throw ReturnMessageException.fromKey("command.gamemode.error", user.getName());
}
Also used : DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)

Example 14 with ReturnMessageException

use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.

the class MeCommand method executeCommand.

@Override
public CommandResult executeCommand(@Nonnull CommandSource src, CommandContext args) throws Exception {
    String message = ChatListener.stripPermissionless(src, args.<String>getOne(messageKey).get());
    Text header = config.getMePrefix().getForCommandSource(src);
    TextParsingUtils.StyleTuple t = plugin.getTextParsingUtils().getLastColourAndStyle(header, null);
    Text originalMessage = TextSerializers.FORMATTING_CODE.deserialize(message);
    MessageEvent.MessageFormatter formatter = new MessageEvent.MessageFormatter(Text.builder().color(t.colour).style(t.style).append(TextSerializers.FORMATTING_CODE.deserialize(message)).toText());
    // Doing this here rather than in the constructor removes the < > notation.
    formatter.setHeader(header);
    // We create an event so that other plugins can provide transforms, such as Boop, and that we
    // can catch it in ignore and mutes, and so can other plugins.
    MessageChannelEvent.Chat event = CauseStackHelper.createFrameWithCausesAndContextWithReturn(c -> SpongeEventFactory.createMessageChannelEventChat(c, channel, Optional.of(channel), formatter, originalMessage, false), EventContext.builder().add(EventContexts.SHOULD_FORMAT_CHANNEL, false).build(), src);
    if (Sponge.getEventManager().post(event)) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.me.cancel"));
    }
    event.getChannel().orElse(channel).send(src, Util.applyChatTemplate(event.getFormatter()), ChatTypes.CHAT);
    return CommandResult.success();
}
Also used : MessageChannelEvent(org.spongepowered.api.event.message.MessageChannelEvent) MessageEvent(org.spongepowered.api.event.message.MessageEvent) Text(org.spongepowered.api.text.Text) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) TextParsingUtils(io.github.nucleuspowered.nucleus.internal.text.TextParsingUtils)

Example 15 with ReturnMessageException

use of io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException in project Nucleus by NucleusPowered.

the class SkullCommand method executeCommand.

@Override
public CommandResult executeCommand(Player pl, CommandContext args) throws Exception {
    User user = this.getUserFromArgs(User.class, pl, player, args);
    int amount = args.<Integer>getOne(amountKey).orElse(1);
    if (this.isUseMinecraftCommand) {
        CommandResult result = Sponge.getCommandManager().process(Sponge.getServer().getConsole(), String.format("minecraft:give %s skull %d 3 {SkullOwner:%s}", pl.getName(), amount, user.getName()));
        if (result.getSuccessCount().orElse(0) > 0) {
            pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.success.plural", String.valueOf(amount), user.getName()));
            return result;
        }
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.error", user.getName()));
    }
    int fullStacks = amount / 64;
    int partialStack = amount % 64;
    // Create the Skull
    ItemStack skullStack = ItemStack.builder().itemType(ItemTypes.SKULL).quantity(64).build();
    // Set it to subject skull type and set the owner to the specified subject
    if (skullStack.offer(Keys.SKULL_TYPE, SkullTypes.PLAYER).isSuccessful() && skullStack.offer(Keys.REPRESENTED_PLAYER, user.getProfile()).isSuccessful()) {
        skullStack.toContainer().set(DataQuery.of("SkullOwner"), user.getName());
        List<ItemStack> itemStackList = Lists.newArrayList();
        // If there were stacks, create as many as needed.
        if (fullStacks > 0) {
            itemStackList.add(skullStack);
            for (int i = 2; i <= fullStacks; i++) {
                itemStackList.add(skullStack.copy());
            }
        }
        // Same with the partial stacks.
        if (partialStack > 0) {
            ItemStack is = skullStack.copy();
            is.setQuantity(partialStack);
            itemStackList.add(is);
        }
        int accepted = 0;
        int failed = 0;
        Inventory inventoryToOfferTo = pl.getInventory().query(Hotbar.class, GridInventory.class);
        for (ItemStack itemStack : itemStackList) {
            int stackSize = itemStack.getQuantity();
            InventoryTransactionResult itr = inventoryToOfferTo.offer(itemStack);
            int currentFail = itr.getRejectedItems().stream().mapToInt(ItemStackSnapshot::getQuantity).sum();
            failed += currentFail;
            accepted += stackSize - currentFail;
        }
        // What was accepted?
        if (accepted > 0) {
            if (failed > 0) {
                pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.semifull", String.valueOf(failed)));
            }
            if (accepted == 1) {
                pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.success.single", user.getName()));
            } else {
                pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.success.plural", String.valueOf(accepted), user.getName()));
            }
            return CommandResult.success();
        }
        pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.full", user.getName()));
        return CommandResult.empty();
    } else {
        pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.error", user.getName()));
        return CommandResult.empty();
    }
}
Also used : User(org.spongepowered.api.entity.living.player.User) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) GridInventory(org.spongepowered.api.item.inventory.type.GridInventory) InventoryTransactionResult(org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult) CommandResult(org.spongepowered.api.command.CommandResult)

Aggregations

ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)45 Player (org.spongepowered.api.entity.living.player.Player)14 World (org.spongepowered.api.world.World)14 Text (org.spongepowered.api.text.Text)11 User (org.spongepowered.api.entity.living.player.User)9 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)9 CommandResult (org.spongepowered.api.command.CommandResult)7 Inventory (org.spongepowered.api.item.inventory.Inventory)6 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)5 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)5 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)5 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)5 Optional (java.util.Optional)5 CommandContext (org.spongepowered.api.command.args.CommandContext)5 CommandElement (org.spongepowered.api.command.args.CommandElement)5 GenericArguments (org.spongepowered.api.command.args.GenericArguments)5 Home (io.github.nucleuspowered.nucleus.api.nucleusdata.Home)4 Warp (io.github.nucleuspowered.nucleus.api.nucleusdata.Warp)4 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)4 NucleusTeleportHandler (io.github.nucleuspowered.nucleus.internal.teleport.NucleusTeleportHandler)4