Solved Get config file in another plugin

Discussion in 'Plugin Development' started by Xp10d3, Jul 10, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    Xp10d3

    Hello. I don't have any code at the moment but have no clue how to approach this subject. How would I get the config file of another plugin? My goal is to get the data of another config file and then import it into a SQL database. I know this is possible as LuckPerms did this when importing a PermissionsEx config file, but instead of:
    Code:
    .yml -> library
    
    I want to do:
    Code:
    .yml -> MySQL
    
    Thanks.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Xp10d3 LuckPerms used the fixed structure of PEX.
    Do you have a fixed structure that you are able to translate into rows?
     
  3. Offline

    Xp10d3

    Yes. If you see here: https://bukkit.org/threads/somewhat-simple-rpg-leveling-system.487351/, it's fixed.
    Code:
    
    <uuid_of_user>:   uuid: <uuid>   race: Elf   exp: 6373   level: 48 <uuid_of_user2>:   uuid: <uuid>   race: Orc   exp: 1834   level: 13
    
    I actually did a bit of research and found this thread:
    https://bukkit.org/threads/how-to-get-info-from-a-yaml-in-another-plugins-directory.113555/#:~:text=Simply get the plugin by Bukkit.getPluginManager ().getPlugin ("NAME_OF_THE_PLUGIN").,have to see this: Mirroring the JavaPlugin implementation
    But am kinda confused as how I would get the content of the .yml file. I forgot most of my Java ngl cause it's been quite a bit since I did plugin development. Here is my current code:
    Code:
    package eltik.connection.sql;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    import eltik.connection.sql.MysqlSetterGetter;
    public class Core extends JavaPlugin {
      
        private Connection connection;
        public String host, database, username, password, table;
        public int port;
      
        FileConfiguration config = getConfig();
      
        Date now = new Date();
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
      
        public void onEnable() {
            otherLog("[" + format.format(now) + "]" + "Attempting to load config...");
            loadConfig();
            otherLog("[" + format.format(now) + "]" + "Attempting to connect to MySQL database...");
            mysqlSetup();
          
            getLogger().info("Successfully loaded commands.");
          
            config = getConfig();
          
            this.getServer().getPluginManager().registerEvents(new MysqlSetterGetter(), this);
            getLogger().info("Loaded MySQL class.");
            otherLog("Server has started at " + "[" + format.format(now) + "]");
        }
      
        public void loadConfig(){
            getConfig().options().copyDefaults(true);
            saveConfig();
            otherLog("[" + format.format(now) + "]" + "Successfully loaded config.");
        }
      
        public void mysqlSetup() {
            host = this.getConfig().getString("host");
            port = this.getConfig().getInt("port");
            database = this.getConfig().getString("database");
            username = this.getConfig().getString("username");
            password = this.getConfig().getString("password");
            table = this.getConfig().getString("table");
            try {
                synchronized (this) {
                    if (getConnection() != null && !getConnection().isClosed()) {
                        return;
                    }
                    Class.forName("com.mysql.jdbc.Driver");
                    setConnection(
                            DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database,
                                    this.username, this.password));
                    Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "MYSQL CONNECTED");
                    this.otherLog("MySQL successfully conected at " + "[" + format.format(now) + "]");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
      
        // Gets the connection.
        public Connection getConnection() {
            return connection;
        }
        // Sets the connection.
        public void setConnection(Connection connection) {
            this.connection = connection;
        }
      
        public void otherLog(String message) {
            try {
                File dataFolder = getDataFolder();
                if (!dataFolder.exists()) {
                    dataFolder.mkdir();
                }
              
                File saveTo = new File(getDataFolder(), "debug_log.txt");
                if (!saveTo.exists()) {
                    saveTo.createNewFile();
                    otherLog("[" + format.format(now) + "]" + "Successfully created debug_log.txt.");
                }
                FileWriter fw = new FileWriter(saveTo, true);
                PrintWriter pw = new PrintWriter(fw);
                pw.println(message);
                pw.flush();
                pw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
      
        File plugins = this.getDataFolder().getParentFile();
        File folder = new File(plugins.getPath()+File.separator+"test"+File.separator+"config.yml");
    }
    
    
    I'd assume it's something like folder.some_notation.
    EDIT: Oh, and I'm using Spigot 1.8.8.
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Xp10d3 plugin.getConfig()?
    Assuming that it is the normal config.yml
    Please use the normal logger as well.

    Why not just ask for sql support?
     
  5. Offline

    Xp10d3

    Never mind, I think I got it :p
    Code:
    package eltik.connection.sql;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Core extends JavaPlugin {
       
        private Connection connection;
        public String host, database, username, password, table;
        public int port;
       
        FileConfiguration config = getConfig();
       
        Date now = new Date();
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
       
        public void onEnable() {
            otherLog("[" + format.format(now) + "]" + "Attempting to load config...");
            loadConfig();
            otherLog("[" + format.format(now) + "]" + "Attempting to connect to MySQL database...");
            mysqlSetup();
           
            getLogger().info("Successfully loaded commands.");
           
            config = getConfig();
           
            this.getServer().getPluginManager().registerEvents(new MysqlSetterGetter(), this);
            getLogger().info("Loaded MySQL class.");
            otherLog("Server has started at " + "[" + format.format(now) + "]");
        }
       
        public void loadConfig(){
            getConfig().options().copyDefaults(true);
            saveConfig();
            otherLog("[" + format.format(now) + "]" + "Successfully loaded config.");
        }
       
        public void mysqlSetup() {
            host = this.getConfig().getString("host");
            port = this.getConfig().getInt("port");
            database = this.getConfig().getString("database");
            username = this.getConfig().getString("username");
            password = this.getConfig().getString("password");
            table = this.getConfig().getString("table");
            try {
                synchronized (this) {
                    if (getConnection() != null && !getConnection().isClosed()) {
                        return;
                    }
                    Class.forName("com.mysql.jdbc.Driver");
                    setConnection(
                            DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database,
                                    this.username, this.password));
                    Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "MYSQL CONNECTED");
                    this.otherLog("MySQL successfully conected at " + "[" + format.format(now) + "]");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
       
        // Gets the connection.
        public Connection getConnection() {
            return connection;
        }
        // Sets the connection.
        public void setConnection(Connection connection) {
            this.connection = connection;
        }
       
        public void otherLog(String message) {
            try {
                File dataFolder = getDataFolder();
                if (!dataFolder.exists()) {
                    dataFolder.mkdir();
                }
               
                File saveTo = new File(getDataFolder(), "debug_log.txt");
                if (!saveTo.exists()) {
                    saveTo.createNewFile();
                    otherLog("[" + format.format(now) + "]" + "Successfully created debug_log.txt.");
                }
                FileWriter fw = new FileWriter(saveTo, true);
                PrintWriter pw = new PrintWriter(fw);
                pw.println(message);
                pw.flush();
                pw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        /*
        File plugins = this.getDataFolder().getParentFile();
        File folder = new File(plugins.getPath()+File.separator+"test"+File.separator+"config.yml");
        */
        public void getOtherConfig() {
            File file = new File(Bukkit.getPluginManager().getPlugin("test").getDataFolder(), "config.yml");
            FileConfiguration otherFile = YamlConfiguration.loadConfiguration(file);
           
            ConfigurationSection section = otherFile.getConfigurationSection("test");
            for (String key : section.getKeys(false)) {
                String string = key + ':' + section.getDouble(key);
                Bukkit.getLogger().info(string);
            }
        }
    }
    
    
    And what do you mean by SQL support?
     
  6. Offline

    timtower Administrator Administrator Moderator

    @Xp10d3 That the original plugin supports a database
     
  7. Offline

    Xp10d3

    Ah. Yeah, I tried to ask for it here:
    https://bukkit.org/threads/somewhat-simple-rpg-leveling-system.487351/
    gochi9 was doing it and I accidentally forgot to ask him for SQL support at first, and he/she was already too far into the plugin to add it. I figured it might be easier to create a plugin like this or have another person make a plugin for SQL support, so I'm doing the former :p
     
Thread Status:
Not open for further replies.

Share This Page