下面列出了org.bukkit.plugin.Plugin#getResource ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
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();
}
}
}
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();
}
}
}
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;
}
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);
}
}