Illuminatiiiiii
1/27/2019 - 8:33 PM

Custom Configs

For Episode 22

package me.illuminatiproductions.customconfigplugin;

import me.illuminatiproductions.customconfigplugin.commands.Message;
import me.illuminatiproductions.customconfigplugin.commands.ReloadCommand;
import me.illuminatiproductions.customconfigplugin.files.CustomConfig;
import org.bukkit.plugin.java.JavaPlugin;

public final class CustomConfigPlugin extends JavaPlugin {

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

        //Setup config
        getConfig().options().copyDefaults();
        saveDefaultConfig();

        CustomConfig.setup();
        CustomConfig.get().addDefault("Message", "this is the default message");
        CustomConfig.get().options().copyDefaults(true);
        CustomConfig.save();

        getCommand("message").setExecutor(new Message());
        getCommand("preload").setExecutor(new ReloadCommand());
    }
}
package me.illuminatiproductions.customconfigplugin.files;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;

public class CustomConfig {

    private static File file;
    private static FileConfiguration customFile;

    //Finds or generates the custom config file
    public static void setup(){
        file = new File(Bukkit.getServer().getPluginManager().getPlugin("CustomConfigPlugin").getDataFolder(), "customconfig.yml");

        if (!file.exists()){
            try{
                file.createNewFile();
            }catch (IOException e){
                //owww
            }
        }
        customFile = YamlConfiguration.loadConfiguration(file);
    }

    public static FileConfiguration get(){
        return customFile;
    }

    public static void save(){
        try{
            customFile.save(file);
        }catch (IOException e){
            System.out.println("Couldn't save file");
        }
    }

    public static void reload(){
        customFile = YamlConfiguration.loadConfiguration(file);
    }

}
package me.illuminatiproductions.customconfigplugin.commands;

import me.illuminatiproductions.customconfigplugin.files.CustomConfig;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class Message implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

        if (sender instanceof Player){
            Player player = (Player) sender;
            player.sendMessage(CustomConfig.get().getString("Message"));
        }

        return true;
    }
}
package me.illuminatiproductions.customconfigplugin.commands;

import me.illuminatiproductions.customconfigplugin.files.CustomConfig;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class ReloadCommand implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

        Player player = (Player) sender;
        CustomConfig.reload();


        return true;
    }
}