Search in sources :

Example 1 with TextFileController

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

the class InfoHandler method onReload.

@Override
public void onReload() throws Exception {
    // Get the config directory, check to see if "info/" exists.
    Path infoDir = plugin.getConfigDirPath().resolve("info");
    if (!Files.exists(infoDir)) {
        Files.createDirectories(infoDir);
        AssetManager am = Sponge.getAssetManager();
        // They exist.
        am.getAsset(plugin, "info.txt").get().copyToFile(infoDir.resolve("info.txt"));
        am.getAsset(plugin, "colors.txt").get().copyToFile(infoDir.resolve("colors.txt"));
        am.getAsset(plugin, "links.txt").get().copyToFile(infoDir.resolve("links.txt"));
    } else if (!Files.isDirectory(infoDir)) {
        throw new IllegalStateException("The file " + infoDir.toAbsolutePath().toString() + " should be a directory.");
    }
    // Get all txt files.
    List<Path> files;
    try (Stream<Path> sp = Files.list(infoDir)) {
        files = sp.filter(Files::isRegularFile).filter(x -> validFile.matcher(x.getFileName().toString()).matches()).collect(Collectors.toList());
    }
    // Collect them and put the resultant controllers into a temporary map.
    Map<String, TextFileController> mst = Maps.newHashMap();
    files.forEach(x -> {
        try {
            String name = x.getFileName().toString();
            name = name.substring(0, name.length() - 4);
            if (mst.keySet().stream().anyMatch(name::equalsIgnoreCase)) {
                plugin.getLogger().warn(NucleusPlugin.getNucleus().getMessageProvider().getMessageWithFormat("info.load.duplicate", x.getFileName().toString()));
                // This is a function, so return is appropriate, not break.
                return;
            }
            mst.put(name, new TextFileController(x, true));
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
    // All good - replace it all!
    infoFiles.clear();
    infoFiles.putAll(mst);
}
Also used : Path(java.nio.file.Path) AssetManager(org.spongepowered.api.asset.AssetManager) TextFileController(io.github.nucleuspowered.nucleus.internal.TextFileController) IOException(java.io.IOException)

Example 2 with TextFileController

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

the class InfoCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    Optional<InfoArgument.Result> oir = args.getOne(key);
    if (infoConfig.isUseDefaultFile() && !oir.isPresent() && !args.hasAny("l")) {
        // Do we have a default?
        String def = infoConfig.getDefaultInfoSection();
        Optional<TextFileController> list = infoHandler.getSection(def);
        if (list.isPresent()) {
            oir = Optional.of(new InfoArgument.Result(infoHandler.getInfoSections().stream().filter(def::equalsIgnoreCase).findFirst().get(), list.get()));
        }
    }
    if (oir.isPresent()) {
        TextFileController controller = oir.get().text;
        Text def = TextSerializers.FORMATTING_CODE.deserialize(oir.get().name);
        Text title = plugin.getMessageProvider().getTextMessageWithTextFormat("command.info.title.section", controller.getTitle(src).orElseGet(() -> Text.of(def)));
        controller.sendToPlayer(src, title);
        return CommandResult.success();
    }
    // Create a list of pages to load.
    Set<String> sections = infoHandler.getInfoSections();
    if (sections.isEmpty()) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.info.none"));
    }
    // Create the text.
    List<Text> s = Lists.newArrayList();
    sections.forEach(x -> {
        Text.Builder tb = Text.builder().append(Text.builder(x).color(TextColors.GREEN).style(TextStyles.ITALIC).onHover(TextActions.showText(plugin.getMessageProvider().getTextMessageWithFormat("command.info.hover", x))).onClick(TextActions.runCommand("/info " + x)).build());
        // If there is a title, then add it.
        infoHandler.getSection(x).get().getTitle(src).ifPresent(sub -> tb.append(Text.of(TextColors.GOLD, " - ")).append(sub));
        s.add(tb.build());
    });
    Util.getPaginationBuilder(src).contents().header(plugin.getMessageProvider().getTextMessageWithFormat("command.info.header.default")).title(plugin.getMessageProvider().getTextMessageWithFormat("command.info.title.default")).contents(s.stream().sorted(Comparator.comparing(Text::toPlain)).collect(Collectors.toList())).padding(Text.of(TextColors.GOLD, "-")).sendTo(src);
    return CommandResult.success();
}
Also used : Text(org.spongepowered.api.text.Text) TextFileController(io.github.nucleuspowered.nucleus.internal.TextFileController) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CommandResult(org.spongepowered.api.command.CommandResult)

Example 3 with TextFileController

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

the class NucleusPlugin method reload.

@Override
public synchronized boolean reload() {
    try {
        this.moduleContainer.reloadSystemConfig();
        reloadMessages();
        this.commandsConfig.load();
        this.itemDataService.load();
        this.warmupConfig = null;
        CoreConfig coreConfig = this.getInternalServiceManager().getService(CoreConfigAdapter.class).get().getNodeOrDefault();
        this.isDebugMode = coreConfig.isDebugmode();
        this.isTraceUserCreations = coreConfig.traceUserCreations();
        this.savesandloads = coreConfig.isPrintSaveLoad();
        for (TextFileController tfc : textFileControllers.values()) {
            tfc.load();
        }
        fireReloadables();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
Also used : CoreConfig(io.github.nucleuspowered.nucleus.modules.core.config.CoreConfig) TextFileController(io.github.nucleuspowered.nucleus.internal.TextFileController) ConfigException(com.typesafe.config.ConfigException) QuickStartModuleLoaderException(uk.co.drnaylor.quickstart.exceptions.QuickStartModuleLoaderException) IOException(java.io.IOException) QuickStartModuleDiscoveryException(uk.co.drnaylor.quickstart.exceptions.QuickStartModuleDiscoveryException) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) NoModuleException(uk.co.drnaylor.quickstart.exceptions.NoModuleException) IncorrectAdapterTypeException(uk.co.drnaylor.quickstart.exceptions.IncorrectAdapterTypeException)

Aggregations

TextFileController (io.github.nucleuspowered.nucleus.internal.TextFileController)3 IOException (java.io.IOException)2 ConfigException (com.typesafe.config.ConfigException)1 ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)1 CoreConfig (io.github.nucleuspowered.nucleus.modules.core.config.CoreConfig)1 Path (java.nio.file.Path)1 ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)1 AssetManager (org.spongepowered.api.asset.AssetManager)1 CommandResult (org.spongepowered.api.command.CommandResult)1 Text (org.spongepowered.api.text.Text)1 IncorrectAdapterTypeException (uk.co.drnaylor.quickstart.exceptions.IncorrectAdapterTypeException)1 NoModuleException (uk.co.drnaylor.quickstart.exceptions.NoModuleException)1 QuickStartModuleDiscoveryException (uk.co.drnaylor.quickstart.exceptions.QuickStartModuleDiscoveryException)1 QuickStartModuleLoaderException (uk.co.drnaylor.quickstart.exceptions.QuickStartModuleLoaderException)1