• Chào bạn, hãy đăng ký hoặc đăng nhập để tham gia cùng bọn mình và sử dụng được đầy đủ chức năng của diễn đàn :).
Kazoku

Chia sẻ Code get, saveDefault or reload Custom.yml

Kazoku

DEVELOPER
THÀNH VIÊN
Tham gia
30/06/2017
Bài viết
888
Dành cho Developer
Code mẫu custom.yml
[HIDE]
PHP:
package me.takahatashun;

import org.bukkit.plugin.java.JavaPlugin;
import me.takahatashun.Configuration.CustomYAML;

public final class Main extends JavaPlugin {
    private static Main instance;
    private static CustomYAML example;
    @Override
    public void onEnable() {
        instance = this;
        example = new CustomYAML(getDataFolder().toString(), "example.yml");
    }
    public static Main getInstance() {
        return instance;
    }
    public static CustomYAML getExample() {
        return example;
    }
}
PHP:
package me.takahatashun.Configuration;

import com.google.common.base.Charsets;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.*;
import java.nio.charset.Charset;
import java.util.logging.Level;

import static com.google.common.io.ByteStreams.toByteArray;
import static me.takahatashun.Main.getInstance;
import static org.bukkit.plugin.PluginAwareness.Flags.UTF8;

public class CustomYAML {
    private File file;
    private FileConfiguration config;
    private String resourcepath;

    public CustomYAML(String filepath, String filename, String resourcepath){
        this.file = new File(filepath,filename);
        if(resourcepath.isEmpty()){
            this.resourcepath = filename;
        } else {
            this.resourcepath = resourcepath + File.separator + filename;
        }
        this.config = null;
    }

    public CustomYAML(String filepath, String filename){
        this.file = new File(filepath,filename);
        this.resourcepath = filename;
        this.config = null;
    }

    public FileConfiguration getConfig(){
        if (this.config == null) {
            reloadConfig();
        }
        return this.config;
    }
    public void reloadConfig(){
         this.config = YamlConfiguration.loadConfiguration(file);

        final InputStream ConfigStream = getInstance().getResource(this.resourcepath);
        if (ConfigStream == null) {
            return;
        }

        final YamlConfiguration Config;
        if (isStrictlyUTF8()) {
            Config = YamlConfiguration.loadConfiguration(new InputStreamReader(ConfigStream, Charsets.UTF_8));
        } else {
            final byte[] contents;
            Config = new YamlConfiguration();
            try {
                contents = toByteArray(ConfigStream);
            } catch (final IOException e) {
                getInstance().getLogger().log(Level.SEVERE, "Unexpected failure reading "+this.file.getName());
                return;
            }

            final String text = new String(contents, Charset.defaultCharset());
            if (!text.equals(new String(contents, Charsets.UTF_8))) {
                getInstance().getLogger().warning("Default system encoding may have misread "+this.file.getName()+" from plugin jar");
            }

            try {
                Config.loadFromString(text);
            } catch (final InvalidConfigurationException e) {
                getInstance().getLogger().log(Level.SEVERE, "Cannot load "+this.file.getName()+" from jar");
            }
        }
        this.config.setDefaults(Config);
        try {
            this.config.load(this.file);
        } catch (FileNotFoundException e){
            this.saveDefaultConfig();
        } catch (InvalidConfigurationException | IOException e) {
            e.printStackTrace();
        }
    }
    public void saveDefaultConfig(){
        if(this.file == null || !(this.file.exists())){
            try {
                getInstance().saveResource(this.resourcepath, false);
            } catch (NullPointerException ex){
                getInstance().getLogger().log(Level.SEVERE,this.file.getName()+" in jar not found");
            }
        }
    }
    private static boolean isStrictlyUTF8() {
        return getInstance().getDescription().getAwareness().contains(UTF8);
    }
}
[/HIDE]
 
Similar content Most view Xem thêm
Back
Top Bottom