Search in sources :

Example 1 with ConfigMessageProvider

use of io.github.nucleuspowered.nucleus.internal.messages.ConfigMessageProvider 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 2 with ConfigMessageProvider

use of io.github.nucleuspowered.nucleus.internal.messages.ConfigMessageProvider in project Nucleus by NucleusPowered.

the class MessagesUpdateCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    // First, reload the messages.
    boolean reload = this.plugin.reloadMessages();
    if (!reload) {
        // There was a failure loading a custom file
        throw ReturnMessageException.fromKey("command.nucleus.messageupdate.couldnotload");
    }
    MessageProvider messageProvider = plugin.getMessageProvider();
    if (!(messageProvider instanceof ConfigMessageProvider)) {
        throw new ReturnMessageException(messageProvider.getTextMessageWithFormat("command.nucleus.messageupdate.notfile"));
    }
    ConfigMessageProvider cmp = (ConfigMessageProvider) messageProvider;
    List<String> keys = cmp.checkForMigration();
    src.sendMessage(messageProvider.getTextMessageWithFormat("command.nucleus.messageupdate.reloaded"));
    if (keys.isEmpty()) {
        return CommandResult.success();
    }
    if (args.hasAny("y")) {
        cmp.reset(keys);
        src.sendMessage(messageProvider.getTextMessageWithFormat("command.nucleus.messageupdate.reset"));
    } else {
        src.sendMessage(messageProvider.getTextMessageWithFormat("command.nucleus.messageupdate.sometoupdate", String.valueOf(keys.size())));
        keys.forEach(x -> src.sendMessage(Text.of(TextColors.YELLOW, x)));
        src.sendMessage(messageProvider.getTextMessageWithFormat("command.nucleus.messageupdate.confirm", "/nucleus update-messages -y").toBuilder().onClick(TextActions.runCommand("/nucleus update-messages -y")).build());
    }
    return CommandResult.success();
}
Also used : MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) ConfigMessageProvider(io.github.nucleuspowered.nucleus.internal.messages.ConfigMessageProvider) ConfigMessageProvider(io.github.nucleuspowered.nucleus.internal.messages.ConfigMessageProvider) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)

Aggregations

ConfigMessageProvider (io.github.nucleuspowered.nucleus.internal.messages.ConfigMessageProvider)2 ConfigException (com.typesafe.config.ConfigException)1 ClientMessageReciever (io.github.nucleuspowered.nucleus.internal.client.ClientMessageReciever)1 ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)1 MessageProvider (io.github.nucleuspowered.nucleus.internal.messages.MessageProvider)1 ResourceMessageProvider (io.github.nucleuspowered.nucleus.internal.messages.ResourceMessageProvider)1 CoreConfigAdapter (io.github.nucleuspowered.nucleus.modules.core.config.CoreConfigAdapter)1 IOException (java.io.IOException)1 MessageReceiver (org.spongepowered.api.text.channel.MessageReceiver)1