Git Product home page Git Product logo

filemanagerbukkit1's Introduction

FileManagerBukkit1

FileManagerBukkit1

My website, My website

Discord, Support My Server Discord

Kaory Studios, Support Kaoty Studios

Kaory Studios Discord, Support Kaoty Studios

Using:

package com.github.happyuky7.filemanagerbukkit1;

import com.github.happyuky7.filemanagerbukkit1.FileManager;
import org.bukkit.plugin.java.JavaPlugin;

public class YourPluginMain extends JavaPlugin {

    /*Created 
    private FileManager yourconfignameorvalue;
    */
    
    private FileManager config;
    

    private FileManager playerdata;

    @Override
    public void onEnable() {
        // Plugin startup logic

        registerConfigs();

    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }

    public void registerConfigs() {
        /*Register your config
        this.yourconfignameorvalue = new FileManager(this, "yourconfignameorvalue");
        */
        
        this.config = new FileManager(this, "config");
        this.playerdata = new FileManager(this, "data/playerdata");
    }

    /*Created Method to call in another class
    public FileManager getYpurConfigNameORValue() {
        return this.yourconfignameorvalue;
    }
    */
    public FileManager getConfig() {
        return this.config;
    }


    public FileManager getPlayerData() {
        return this.playerdata;
    }
}
package com.github.happyuky7.filemanagerbukkit1;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationOptions;
import org.bukkit.configuration.MemoryConfigurationOptions;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

public class FileManager extends YamlConfiguration {

    /*
    FileManager Link: https://github.com/Happyuky7/FileManagerBukkit1
    */

    private final String fileName;

    private final JavaPlugin plugin;

    private final File folder;

    public FileManager(JavaPlugin plugin, String fileName, File folder) {
        this(plugin, fileName, ".yml", folder);
    }

    public FileManager(JavaPlugin plugin, String filename, String fileextension, File folder) {
        this.folder = folder;
        this.plugin = plugin;
        this.fileName = filename + (filename.endsWith(fileextension) ? "" : fileextension);
        createFile();
    }

    public FileManager(JavaPlugin plugin, String fileName) {
        this(plugin, fileName, ".yml");
    }

    public FileManager(JavaPlugin plugin, String fileName, String fileExtension) {
        this(plugin, fileName, fileExtension, plugin.getDataFolder());
    }

    public JavaPlugin getPlugin() {
        return this.plugin;
    }

    public String getColouredString(String path) {
        String getted;
        try {
            getted = getString(path);
        } catch (NullPointerException e) {
            getted = path;
        }
        return ChatColor.translateAlternateColorCodes('&', getted);
    }

    public List<String> getColouredStringList(String path) {
        List<String> f = new ArrayList<>();
        for (String l : getStringList(path))
            f.add(l.replace('&', '&'));
        return f;
    }

    public <T> T get(Class<T> clazz, String path) {
        Object obj = get(path);
        return clazz.cast(obj);
    }

    private void createFile() {
        try {
            File file = new File(this.folder, this.fileName);
            if (!file.exists()) {
                if (this.plugin.getResource(this.fileName) != null) {
                    this.plugin.saveResource(this.fileName, false);
                } else {
                    save(file);
                }
                load(file);
            } else {
                load(file);
                save(file);
            }
        } catch (Exception ex) {
            this.plugin.getLogger().log(Level.SEVERE, "error occurred creating the " + this.fileName + " file");
            this.plugin.getLogger().log(Level.SEVERE, "");
            this.plugin.getLogger().log(Level.SEVERE, "Error:" + ex.getMessage());
        }
    }

    public void save() {
        File folder = this.plugin.getDataFolder();
        try {
            save(new File(folder, this.fileName));
        } catch (Exception ex) {
            this.plugin.getLogger().log(Level.SEVERE, "error occurred while saving the " + this.fileName + " file");
            this.plugin.getLogger().log(Level.SEVERE, "");
            this.plugin.getLogger().log(Level.SEVERE, "Error:" + ex.getMessage());
        }
    }

    public void reload() {
        File folder = this.plugin.getDataFolder();
        File file = new File(folder, this.fileName);
        try {
            load(file);
        } catch (IOException|org.bukkit.configuration.InvalidConfigurationException e) {
            this.plugin.getLogger().log(Level.SEVERE, "error occurred while reloading the " + this.fileName + " file");
            this.plugin.getLogger().log(Level.SEVERE, "");
            this.plugin.getLogger().log(Level.SEVERE, "Error:" + e.getMessage());
        }
    }
}



Join us

  • Feel free to open a PR! We accept contributions.
  • Discord

Special Thanks To

IntelliJ IDEA logo IntelliJ IDEA logo Azul Java logo

IntelliJ IDEA, Code editor for Java and other programming languages and programs.

Azul Java The world’s largest commercial provider of OpenJDK

filemanagerbukkit1's People

Contributors

happyuky7 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.