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()));
}
}
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();
}
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;
}
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);
}
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()));
}
}
Aggregations