Skip to content

Config Migration

TM (Holly) edited this page Jan 10, 2021 · 2 revisions

If you plan on moving from your old configuration to a new configuration, then CM has a dedicated section for it - you're in luck!

This page will explain how such works and how to use it effectively.

Contents:

When to move options over

CM has a dedicated method for moving options from old formats to their new ones, doing all of the hard work for you.

This method is called #moveToNew, and must be declared inside either your CMFile subclass or when you initialise the CMFile class:

public class MyConfigFile extends CMFile {

    public MyConfigFile() {
        super(MyPlugin.getInstance(), "config");
        load();
    }

    @Override
    public void loadDefaults() {
        // Add a section
        addSection("Important Stuff");
        // Using addComment
        addComment("Hello, world!");
        addDefault("foo", "bar");
        // Using addDefault
        addDefault("test", "test1", "Testing 1, testing 2!");
    }
    
    // This is where your attention is needed.
    @Override
    public void moveToNew() {
    
    }
}

(or)

CMFile myConfigFile = new CMFile(this, "config") {
    @Override
    public void loadDefaults() {
        // Add a section
        addSection("Important Stuff");
        // Using addComment
        addComment("Hello, world!");
        addDefault("foo", "bar");
        // Using addDefault
        addDefault("test", "test1", "Testing 1, testing 2!");
    }
    
    @Override
    public void moveToNew() {
   
    }
};
myConfigFile.load();

This method is called right after the #loadDefaults method is called.

Moving options

Inside this new method, you can then call the #moveTo(String, String) method, which will transfer data from the first path to the second one, deleting the old path.

As an example, if we were to move the data from a path called "foo" to a new path called "bar":

CMFile myConfigFile = new CMFile(this, "config") {
    @Override
    public void loadDefaults() {
        // Add a section
        addSection("Important Stuff");
        // Using addComment
        addComment("Hello, world!");
        addDefault("foo", "bar");
        // Using addDefault
        addDefault("test", "test1", "Testing 1, testing 2!");
    }
    
    @Override
    public void moveToNew() {
        moveTo("foo", "bar");
    }
};
myConfigFile.load();

The config that looked like this before:

#####################
#  Important Stuff  #
#####################

# Hello, world!
foo: bar

# Testing 1, testing 2!
test: test1

Would become this:

#####################
#  Important Stuff  #
#####################

# Hello, world!
bar: bar

# Testing 1, testing 2!
test: test1

However, the old path doesn't need to be declared in the #loadDefaults method. If your old configuration had an option called old-option but you want to move it to test, then just add it in:

moveTo("old-option", "test");

And the data held in old-option will be put into test.

That's it. lol

ConfigurationMaster


Basics


Advanced

  • Best Practices
  • Comments
  • Sections
  • Headers

Clone this wiki locally