Search in sources :

Example 1 with AssetManager

use of org.spongepowered.api.asset.AssetManager 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)

Aggregations

TextFileController (io.github.nucleuspowered.nucleus.internal.TextFileController)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 AssetManager (org.spongepowered.api.asset.AssetManager)1