Skip to content

Data Storage

AlphaHelix edited this page Mar 18, 2018 · 3 revisions
private SimpleDataStorage<UUID, Economy> balance;

    public void onEnable() {
        //Init the database connection to MySQL
        DataStorage.init(JSONDatabase.DatabaseType.MYSQL, "" /*only important for SQLite*/);

        //Init the database connection to SQLite
        DataStorage.init(JSONDatabase.DatabaseType.SQLITE, "plugins/Your_Plugin/database.db");


        //A SimpleDataStorage can save his data into MySQL, SQLite, YAML and JSON

        //First how to use with MySQL
        balance = new SimpleDataStorage<>(new JSONDatabase(JSONDatabase.UniqueIdentifier.UUID, "someTable", "someDatabase", JSONDatabase.DatabaseType.MYSQL), Economy.class /*the class of the value*/);

        //Now how to use with SQLite
        balance = new SimpleDataStorage<>(new JSONDatabase(JSONDatabase.UniqueIdentifier.UUID, "someTable", "plugins/Your_Plugin/database.db", JSONDatabase.DatabaseType.SQLITE), Economy.class);

        //How to use with YAML
        balance = new SimpleDataStorage<>(new SimpleFile("plugins/Your_Plugin", "data.yml"), Economy.class);

        //How to use with JSON
        balance = new SimpleDataStorage<>(new SimpleJSONFile("plugins/Your_Plugin", "data.json"), Economy.class);

        //With the SimpleDataStorage you can do:

        balance.setValue("somePath", new Economy(UUID.randomUUID())); //saves the Economy of some UUID to "somePath" and overrides it if already in it

        balance.setDefaultValue("somePath", new Economy(UUID.randomUUID()));//saves the Economy of some UUID to "somePath" doesn't override

        balance.getValue("somePath", Economy.class, result -> { //gets the Economy which is saved at "somePath"
            //do something with the economy result!
        });

        balance.getValues(Economy.class, result -> { //gets all Economy which is saved inside the storage
            //do something with all economies in the storage 
        });

        balance.getKeys(result -> {
            //do something with all the keys inside the storage
        });

        balance.hasValue("somePath", result -> { //check if the storage has a specific key, result is true if so
        });
        
        balance.removeValue("somePath"); //removes the value at the path "somePath"
    }

To establish a connection with sql2 you simply need to call this method

SQLConnectionHandler.initialize(/*new MySQLInformationFile(JavaPlugin) or new SQLiteInformationFile(JavaPlugin)*/);

Clone this wiki locally