• Trong thời gian này, bạn không thể tạo bài mới, đăng comment, hay tạo tài nguyên mới để diễn đàn dọn dẹp nội dung.
    Diễn đàn sẽ sớm mở trở lại, chi tiết hãy theo dõi tại Discord
Kazoku

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

  • Người tạo chủ đề Người tạo chủ đề Kazoku
  • Ngày gửi Ngày gửi

Kazoku

DEVELOPER
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]
 
Nội dung tương tự Xem nhiều nhất Xem thêm

Thống kê nội dung

Tạo
Kazoku,
Trả lời
0
Lượt xem
339
Quay lại
Trên cùng