Java Code Examples for org.bukkit.plugin.Plugin#getResource()

The following examples show how to use org.bukkit.plugin.Plugin#getResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From NametagEdit with GNU General Public License v3.0 6 votes vote down vote up
public static YamlConfiguration getConfig(File file, String resource, Plugin plugin) {
    try {
        if (!file.exists()) {
            file.createNewFile();
            InputStream inputStream = plugin.getResource(resource);
            OutputStream outputStream = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            inputStream.close();
            outputStream.flush();
            outputStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return YamlConfiguration.loadConfiguration(file);
}
 
Example 2
Source File: BukkitTranslateContainer.java    From BedWars with GNU Lesser General Public License v3.0 5 votes vote down vote up
public BukkitTranslateContainer(String key, Plugin plugin, ITranslateContainer fallback) {
	this.key = key;
	this.fallback = fallback;

       InputStream in = plugin.getResource("languages/language_" + key + ".yml");
       if (in != null) {
           try {
               config.load(new InputStreamReader(in, StandardCharsets.UTF_8));
           } catch (IOException | InvalidConfigurationException e) {
               e.printStackTrace();
           }
       }
}
 
Example 3
Source File: BukkitTranslateContainer.java    From BedWars with GNU Lesser General Public License v3.0 5 votes vote down vote up
public BukkitTranslateContainer(String key, Plugin plugin, ITranslateContainer fallback) {
	this.key = key;
	this.fallback = fallback;

       InputStream in = plugin.getResource("languages/language_" + key + ".yml");
       if (in != null) {
           try {
               config.load(new InputStreamReader(in, StandardCharsets.UTF_8));
           } catch (IOException | InvalidConfigurationException e) {
               e.printStackTrace();
           }
       }
}
 
Example 4
Source File: ConfigurationFileUtil.java    From AntiVPN with MIT License 5 votes vote down vote up
public static Configuration getConfig(Plugin plugin, String resourcePath, File fileOnDisk) throws IOException {
    File parentDir = fileOnDisk.getParentFile();
    if (parentDir.exists() && !parentDir.isDirectory()) {
        Files.delete(parentDir.toPath());
    }
    if (!parentDir.exists()) {
        if (!parentDir.mkdirs()) {
            throw new IOException("Could not create parent directory structure.");
        }
    }
    if (fileOnDisk.exists() && fileOnDisk.isDirectory()) {
        Files.delete(fileOnDisk.toPath());
    }

    if (!fileOnDisk.exists()) {
        try (InputStreamReader reader = new InputStreamReader(plugin.getResource(resourcePath));
             BufferedReader in = new BufferedReader(reader);
             FileWriter writer = new FileWriter(fileOnDisk);
             BufferedWriter out = new BufferedWriter(writer)) {
            String line;
            while ((line = in.readLine()) != null) {
                out.write(line + System.lineSeparator());
            }
        }
    }

    ConfigurationLoader<ConfigurationNode> loader = YAMLConfigurationLoader.builder().setFlowStyle(DumperOptions.FlowStyle.BLOCK).setIndent(2).setFile(fileOnDisk).build();
    ConfigurationNode root = loader.load(ConfigurationOptions.defaults().setHeader("Comments are gone because update :(. Click here for new config + comments: https://www.spigotmc.org/resources/anti-vpn.58291/"));
    Configuration config = new Configuration(root);
    ConfigurationVersionUtil.conformVersion(loader, config, fileOnDisk);

    return config;
}
 
Example 5
Source File: LanguageFileUtil.java    From AntiVPN with MIT License 4 votes vote down vote up
public static Optional<File> getLanguage(Plugin plugin, Locale locale, boolean ignoreCountry) throws IOException {
    // Build resource path & file path for language
    // Use country is specified (and lang provides country)
    String resourcePath = ignoreCountry || locale.getCountry() == null || locale.getCountry().isEmpty() ? "lang_" + locale.getLanguage() + ".yml" : "lang_" + locale.getLanguage() + "_" + locale.getCountry() + ".yml";
    File langDir = new File(plugin.getDataFolder(), "lang");
    File fileOnDisk = new File(langDir, resourcePath);

    // Clean up/build language path on disk
    if (langDir.exists() && !langDir.isDirectory()) {
        Files.delete(langDir.toPath());
    }
    if (!langDir.exists()) {
        if (!langDir.mkdirs()) {
            throw new IOException("Could not create parent directory structure.");
        }
    }
    if (fileOnDisk.exists() && fileOnDisk.isDirectory()) {
        Files.delete(fileOnDisk.toPath());
    }

    // Check language version
    if (fileOnDisk.exists()) {
        try (InputStream inStream = plugin.getResource(resourcePath)) {
            if (inStream != null) {
                ConfigurationLoader<ConfigurationNode> fileLoader = YAMLConfigurationLoader.builder().setFlowStyle(DumperOptions.FlowStyle.BLOCK).setIndent(2).setFile(fileOnDisk).build();
                ConfigurationNode fileRoot = fileLoader.load();
                double fileVersion = fileRoot.getNode("acf-minecraft", "version").getDouble(1.0d);

                try (InputStreamReader reader = new InputStreamReader(inStream);
                     BufferedReader in = new BufferedReader(reader)) {
                    ConfigurationLoader<ConfigurationNode> streamLoader = YAMLConfigurationLoader.builder().setFlowStyle(DumperOptions.FlowStyle.BLOCK).setIndent(2).setSource(() -> in).build();
                    ConfigurationNode streamRoot = streamLoader.load();
                    double streamVersion = streamRoot.getNode("acf-minecraft", "version").getDouble(1.0d);

                    if (streamVersion > fileVersion) {
                        // Version update, backup & delete file on disk
                        File backupFile = new File(fileOnDisk.getParent(), fileOnDisk.getName() + ".bak");
                        if (backupFile.exists()) {
                            Files.delete(backupFile.toPath());
                        }

                        com.google.common.io.Files.copy(fileOnDisk, backupFile);
                        Files.delete(fileOnDisk.toPath());
                    }
                }
            }
        }
    }

    // Write language file to disk if not exists
    if (!fileOnDisk.exists()) {
        try (InputStream inStream = plugin.getResource(resourcePath)) {
            if (inStream != null) {
                try (InputStreamReader reader = new InputStreamReader(inStream);
                     BufferedReader in = new BufferedReader(reader);
                     FileWriter writer = new FileWriter(fileOnDisk);
                     BufferedWriter out = new BufferedWriter(writer)) {
                    String line;
                    while ((line = in.readLine()) != null) {
                        out.write(line + System.lineSeparator());
                    }
                }
            }
        }
    }

    if (fileOnDisk.exists()) {
        // Return file on disk
        return Optional.of(fileOnDisk);
    } else {
        // If we need a more generic language (eg. if we have "en_US" and we don't have "en_US.yml" but we do have "en.yml") then return the more generic language file
        // Otherwise, no language found
        return ignoreCountry ? Optional.empty() : getLanguage(plugin, locale, true);
    }
}