Search in sources :

Example 1 with MessageReceiver

use of org.spongepowered.api.text.channel.MessageReceiver in project LanternServer by LanternPowered.

the class LanternPaginationList method sendTo.

@SuppressWarnings("rawtypes")
@Override
public void sendTo(final MessageReceiver receiver, int page) {
    checkNotNull(receiver, "The message receiver cannot be null!");
    this.service.registerCommandOnce();
    MessageReceiver realSource = receiver;
    while (realSource instanceof ProxySource) {
        realSource = ((ProxySource) realSource).getOriginalSource();
    }
    final PaginationCalculator calculator = new PaginationCalculator(this.linesPerPage);
    Iterable<Map.Entry<Text, Integer>> counts = StreamSupport.stream(this.contents.spliterator(), false).map(input -> {
        int lines = calculator.getLines(input);
        return Maps.immutableEntry(input, lines);
    }).collect(Collectors.toList());
    Text title = this.title.orElse(null);
    if (title != null) {
        title = calculator.center(title, this.paginationSpacer);
    }
    ActivePagination pagination;
    if (this.contents instanceof List) {
        // If it started out as a list, it's probably reasonable to copy it to another list
        pagination = new ListPagination(realSource, calculator, ImmutableList.copyOf(counts), title, this.header.orElse(null), this.footer.orElse(null), this.paginationSpacer);
    } else {
        pagination = new IterablePagination(realSource, calculator, counts, title, this.header.orElse(null), this.footer.orElse(null), this.paginationSpacer);
    }
    this.service.getPaginationState(receiver, true).put(pagination);
    try {
        pagination.specificPage(page);
    } catch (CommandException e) {
        receiver.sendMessage(error(e.getText()));
    }
}
Also used : MessageReceiver(org.spongepowered.api.text.channel.MessageReceiver) ProxySource(org.spongepowered.api.command.source.ProxySource) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) CommandException(org.spongepowered.api.command.CommandException) PaginationList(org.spongepowered.api.service.pagination.PaginationList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Text(org.spongepowered.api.text.Text) Map(java.util.Map) Optional(java.util.Optional) StreamSupport(java.util.stream.StreamSupport) Nullable(javax.annotation.Nullable) CommandMessageFormatting.error(org.spongepowered.api.command.CommandMessageFormatting.error) MessageReceiver(org.spongepowered.api.text.channel.MessageReceiver) ProxySource(org.spongepowered.api.command.source.ProxySource) Text(org.spongepowered.api.text.Text) PaginationList(org.spongepowered.api.service.pagination.PaginationList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) CommandException(org.spongepowered.api.command.CommandException)

Example 2 with MessageReceiver

use of org.spongepowered.api.text.channel.MessageReceiver in project Nucleus by NucleusPowered.

the class CloneWorldCommand method executeCommand.

@Override
protected CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    WorldProperties worldToCopy = args.<WorldProperties>getOne(this.worldKey).get();
    final String oldName = worldToCopy.getWorldName();
    final String newName = args.<String>getOne(this.newKey).get();
    if (Sponge.getServer().getWorldProperties(newName).isPresent()) {
        throw ReturnMessageException.fromKey("command.world.clone.alreadyexists", newName);
    }
    Text message = Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.world.clone.starting", oldName, newName);
    src.sendMessage(message);
    if (!(src instanceof ConsoleSource)) {
        Sponge.getServer().getConsole().sendMessage(message);
    }
    // Well, you never know, the player might die or disconnect - we have to be vigilant.
    final Supplier<MessageReceiver> mr;
    if (src instanceof Player) {
        UUID uuid = ((Player) src).getUniqueId();
        mr = () -> Sponge.getServer().getPlayer(uuid).map(x -> (MessageReceiver) x).orElseGet(() -> new MessageReceiver() {

            @Override
            public void sendMessage(Text message) {
            }

            @Override
            public MessageChannel getMessageChannel() {
                return MessageChannel.TO_NONE;
            }

            @Override
            public void setMessageChannel(MessageChannel channel) {
            }
        });
    } else {
        mr = () -> src;
    }
    Sponge.getServer().copyWorld(worldToCopy, newName).handle((result, ex) -> {
        MessageReceiver m = mr.get();
        Text msg;
        if (ex == null && result.isPresent()) {
            msg = Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.world.clone.success", oldName, newName);
        } else {
            msg = Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.world.clone.failed", oldName, newName);
        }
        m.sendMessage(msg);
        if (!(m instanceof ConsoleSource)) {
            Sponge.getServer().getConsole().sendMessage(msg);
        }
        return result;
    });
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) MessageReceiver(org.spongepowered.api.text.channel.MessageReceiver) Text(org.spongepowered.api.text.Text) ConsoleSource(org.spongepowered.api.command.source.ConsoleSource) UUID(java.util.UUID) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 3 with MessageReceiver

use of org.spongepowered.api.text.channel.MessageReceiver in project Nucleus by NucleusPowered.

the class NucleusPlugin method reloadMessages.

@Override
public boolean reloadMessages() {
    boolean r = true;
    if (getConfigValue("core", CoreConfigAdapter.class, CoreConfig::isCustommessages).orElse(false)) {
        try {
            this.messageProvider = new ConfigMessageProvider(configDir.resolve("messages.conf"), ResourceMessageProvider.messagesBundle);
            this.commandMessageProvider = new ConfigMessageProvider(configDir.resolve("command-help-messages.conf"), ResourceMessageProvider.commandMessagesBundle);
            return true;
        } catch (Throwable exception) {
            r = false;
            // Blegh, relocations
            if (exception instanceof IOException && exception.getCause().getClass().getName().contains(ConfigException.class.getSimpleName())) {
                MessageReceiver s;
                if (Sponge.getGame().isServerAvailable()) {
                    s = Sponge.getServer().getConsole();
                } else {
                    s = new ClientMessageReciever();
                }
                exception = exception.getCause();
                s.sendMessage(Text.of(TextColors.RED, "It appears that there is an error in your messages file! The error is: "));
                s.sendMessage(Text.of(TextColors.RED, exception.getMessage()));
                s.sendMessage(Text.of(TextColors.RED, "Please correct this - then run ", TextColors.YELLOW, "/nucleus reload"));
                s.sendMessage(Text.of(TextColors.RED, "Ignoring messages.conf for now."));
                if (this.isDebugMode) {
                    exception.printStackTrace();
                }
            } else {
                this.logger.warn("Could not load custom messages file. Falling back.");
                exception.printStackTrace();
            }
        }
    }
    this.messageProvider = new ResourceMessageProvider(ResourceMessageProvider.messagesBundle);
    this.commandMessageProvider = new ResourceMessageProvider(ResourceMessageProvider.commandMessagesBundle);
    return r;
}
Also used : MessageReceiver(org.spongepowered.api.text.channel.MessageReceiver) ConfigMessageProvider(io.github.nucleuspowered.nucleus.internal.messages.ConfigMessageProvider) ConfigException(com.typesafe.config.ConfigException) IOException(java.io.IOException) CoreConfigAdapter(io.github.nucleuspowered.nucleus.modules.core.config.CoreConfigAdapter) ClientMessageReciever(io.github.nucleuspowered.nucleus.internal.client.ClientMessageReciever) ResourceMessageProvider(io.github.nucleuspowered.nucleus.internal.messages.ResourceMessageProvider)

Example 4 with MessageReceiver

use of org.spongepowered.api.text.channel.MessageReceiver in project SpongeCommon by SpongePowered.

the class ActivePagination method specificPage.

public void specificPage(int page) throws CommandException {
    MessageReceiver src = this.src.get().orElseThrow(() -> new CommandException(t("Source for pagination %s is no longer active!", getId())));
    this.currentPage = page;
    List<Text> toSend = new ArrayList<>();
    Text title = this.title;
    if (title != null) {
        toSend.add(title);
    }
    if (this.header != null) {
        toSend.add(this.header);
    }
    for (Text line : getLines(page)) {
        toSend.add(line);
    }
    Text footer = calculateFooter(page);
    toSend.add(this.calc.center(footer, this.padding));
    if (this.footer != null) {
        toSend.add(this.footer);
    }
    src.sendMessages(toSend);
}
Also used : MessageReceiver(org.spongepowered.api.text.channel.MessageReceiver) ArrayList(java.util.ArrayList) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException)

Example 5 with MessageReceiver

use of org.spongepowered.api.text.channel.MessageReceiver in project SpongeCommon by SpongePowered.

the class SpongePaginationList method sendTo.

@Override
public void sendTo(final MessageReceiver receiver, int page) {
    checkNotNull(receiver, "The message receiver cannot be null!");
    this.service.registerCommandOnce();
    MessageReceiver realSource = receiver;
    while (realSource instanceof ProxySource) {
        realSource = ((ProxySource) realSource).getOriginalSource();
    }
    final PaginationCalculator calculator = new PaginationCalculator(this.linesPerPage);
    Iterable<Map.Entry<Text, Integer>> counts = StreamSupport.stream(this.contents.spliterator(), false).map(input -> {
        int lines = calculator.getLines(input);
        return Maps.immutableEntry(input, lines);
    }).collect(Collectors.toList());
    Text title = this.title.orElse(null);
    if (title != null) {
        title = calculator.center(title, this.paginationSpacer);
    }
    // If the MessageReceiver is a Player, then upon death, they will become a different MessageReceiver object.
    // Thus, we use a supplier to supply the player from the server, if required.
    Supplier<Optional<MessageReceiver>> messageReceiverSupplier;
    if (receiver instanceof Player) {
        final UUID playerUuid = ((Player) receiver).getUniqueId();
        messageReceiverSupplier = () -> Sponge.getServer().getPlayer(playerUuid).map(x -> (MessageReceiver) x);
    } else {
        WeakReference<MessageReceiver> srcReference = new WeakReference<>(receiver);
        messageReceiverSupplier = () -> Optional.ofNullable(srcReference.get());
    }
    ActivePagination pagination;
    if (this.contents instanceof List) {
        // If it started out as a list, it's probably reasonable to copy it to another list
        pagination = new ListPagination(messageReceiverSupplier, calculator, ImmutableList.copyOf(counts), title, this.header.orElse(null), this.footer.orElse(null), this.paginationSpacer);
    } else {
        pagination = new IterablePagination(messageReceiverSupplier, calculator, counts, title, this.header.orElse(null), this.footer.orElse(null), this.paginationSpacer);
    }
    this.service.getPaginationState(receiver, true).put(pagination);
    try {
        pagination.specificPage(page);
    } catch (CommandException e) {
        receiver.sendMessage(error(e.getText()));
    }
}
Also used : MessageReceiver(org.spongepowered.api.text.channel.MessageReceiver) ProxySource(org.spongepowered.api.command.source.ProxySource) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Sponge(org.spongepowered.api.Sponge) UUID(java.util.UUID) Maps(com.google.common.collect.Maps) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) CommandException(org.spongepowered.api.command.CommandException) PaginationList(org.spongepowered.api.service.pagination.PaginationList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Text(org.spongepowered.api.text.Text) Map(java.util.Map) Optional(java.util.Optional) StreamSupport(java.util.stream.StreamSupport) Player(org.spongepowered.api.entity.living.player.Player) WeakReference(java.lang.ref.WeakReference) Nullable(javax.annotation.Nullable) CommandMessageFormatting.error(org.spongepowered.api.command.CommandMessageFormatting.error) Player(org.spongepowered.api.entity.living.player.Player) Optional(java.util.Optional) ProxySource(org.spongepowered.api.command.source.ProxySource) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) MessageReceiver(org.spongepowered.api.text.channel.MessageReceiver) WeakReference(java.lang.ref.WeakReference) PaginationList(org.spongepowered.api.service.pagination.PaginationList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) UUID(java.util.UUID)

Aggregations

MessageReceiver (org.spongepowered.api.text.channel.MessageReceiver)9 Text (org.spongepowered.api.text.Text)6 CommandException (org.spongepowered.api.command.CommandException)4 Maps (com.google.common.collect.Maps)3 List (java.util.List)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Nullable (javax.annotation.Nullable)3 Player (org.spongepowered.api.entity.living.player.Player)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 ImmutableList (com.google.common.collect.ImmutableList)2 ConfigException (com.typesafe.config.ConfigException)2 ArrayList (java.util.ArrayList)2 Supplier (java.util.function.Supplier)2 Sponge (org.spongepowered.api.Sponge)2 Listener (org.spongepowered.api.event.Listener)2 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 Gson (com.google.gson.Gson)1 TypeToken (com.google.gson.reflect.TypeToken)1