Storage Examples

Storage Examples: [Simple]

Step 1:

Create a class example this:

1
2
3
4
5
6
import me.vthreento.api.storage.entity.Config;
 
public class VthreentoFrameworkConfig implements Config
{
        public boolean EnableMetrics = true;
}

Step 2a:

Import in JavaPlugin the Storage System like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import me.vthreento.api.storage.Storage;
import me.vthreento.plugin.config.VthreentoFrameworkConfig;
import org.bukkit.plugin.java.JavaPlugin;

public class VthreentoFramework extends JavaPlugin
{
    private VthreentoFrameworkConfig config;

    public void onLoad()
    {
        Storage<VthreentoFrameworkConfig> configStorage = new Storage<>( getDataFolder( ).getPath( ) + "/config.yml", VthreentoFrameworkConfig.class, 0 );
        config = configStorage.load();
        configStorage.save( );
    }
}

Step 2b:

If you want to reload the config with command use this code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import me.vthreento.api.storage.Storage;
import me.vthreento.plugin.config.VthreentoFrameworkConfig;
import org.bukkit.plugin.java.JavaPlugin;

public class VthreentoFramework extends JavaPlugin
{
    private VthreentoFrameworkConfig config;
    private Storage<VthreentoFrameworkConfig> configStorage;

    //    To reload use "config = configStorage.load();"
    
    public void onLoad()
    {
        configStorage = new Storage<>( getDataFolder( ).getPath( ) + "/config.yml", VthreentoFrameworkConfig.class, 0 );
        config = configStorage.load();
        configStorage.save( );
    }
}

Step 3:

Use config System:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import me.vthreento.api.storage.Storage;
import me.vthreento.plugin.config.VthreentoFrameworkConfig;
import org.bukkit.plugin.java.JavaPlugin;

public class VthreentoFramework extends JavaPlugin
{
    private VthreentoFrameworkConfig config;
    private Storage<VthreentoFrameworkConfig> configStorage;

    //    To reload use "config = configStorage.load();"
    
    public void onLoad()
    {
        configStorage = new Storage<>( getDataFolder( ).getPath( ) + "/config.yml", VthreentoFrameworkConfig.class, 0 );
        config = configStorage.load();
        configStorage.save( );
        
        if ( config.EnableMetrics )
        {
            getLogger().log( Level.INFO, "EnableMetrics" );
        }
    }
}

Step 4:

Enable the autosave System:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import me.vthreento.api.storage.Storage;
import me.vthreento.plugin.config.VthreentoFrameworkConfig;
import org.bukkit.plugin.java.JavaPlugin;

public class VthreentoFramework extends JavaPlugin
{
    private VthreentoFrameworkConfig config;
    private Storage<VthreentoFrameworkConfig> configStorage;

    //    To reload use "config = configStorage.load();"
    
    public void onLoad()
    {
        //    Change the autosavetimer from 0 to 120 (sec)
        configStorage = new Storage<>( getDataFolder( ).getPath( ) + "/config.yml", VthreentoFrameworkConfig.class, 120 );

        //    Add to allow AutoSave!
        configStorage.setAllowAutoSave( true );

        config = configStorage.load();
        configStorage.save( );
        
        if ( config.EnableMetrics )
        {
            getLogger().log( Level.INFO, "EnableMetrics" );
        }
    }
}

Log example:

1
2
3
4
5
6
7
8
9
# File location: <server location>/logs/storage/<date>.log

00:19:32 [ Storage | SEVERE ] ----------- Storage Log 28-07-2014 - 00:19:32 -----------
00:19:32 [ Storage | INFO ] Trying to load Config config.yml
00:19:32 [ Storage | INFO ] [ plugins\VthreentoFramework\config.yml -> EnableMetrics: true ] *Reading from file!
00:19:32 [ Storage | INFO ] Config config.yml Loaded!
00:19:32 [ Storage | INFO ] Trying to save Config config.yml
00:19:32 [ Storage | INFO ] [ plugins\VthreentoFramework\config.yml <- EnableMetrics:true ] *Printed to file!
00:19:32 [ Storage | INFO ] Config config.yml Saved!