Minecrell / ServerListPlus

A flexible Minecraft plugin to customize the appearance of your server in the server list
https://git.io/slp
GNU General Public License v3.0
240 stars 58 forks source link

How to access Configuration data through code #415

Closed Andre601 closed 4 months ago

Andre601 commented 5 months ago

To be completely clear here, my goal is to implement a migration tool into my own plugin to allow transfering over configuration stuff from SLP to it.

My initial idea was to setup a system where I would load the YAML file through SnakeYAML and parse all its values over to my own files... Tho, now looking at the currently available API of SLP, I can see a possible use-case of utilizing the API to access the file of it that was to migrate the data over, which could also be a bit safer to do in the end at the cost of requiring the plugin to be present.

I can understand if you don't like to help me in implementing a feature that would give people the option to migrate over from SLP, so if you don't want to provide info on this, just say so and I'll close this issue. Tho, I hope you are open for this and can provide some info here and maybe even recommended aproaches on how too do this in the end.

stephan-gh commented 5 months ago

If you load after SLP (with depend or soft depend) you should be able to get the SLP instance and then access the parsed configuration classes through the configuration manager. To avoid conflicts between SLP and your plugin, you could then call the function that implements /slp disable (this will also persist across reboots).

Note however that this isn't officially part of the public API (only Replacement API and Ban API is public, see API page in SLP wiki). You would be directly accessing internal classes of SLP that may change in the future. Given the maintenance-only state of SLP I doubt that any of this will change in the future, though.

I'm also just spontaneously looking at the classes and may have typos, but I think it should roughly work like this:

ServerListPlusCore slp = ServerListPlusCore.getInstance();
ServerStatusConf conf = slp.getConf(ServerStatusConf.class);
// ... parse config ...

// run /slp disable, returns true if enable state changed, or throws an exception
slp.getProfiles().setEnabled(false);

There is also PluginConf.class and (on Bukkit) BukkitConf.class for the !Plugin and !Bukkit part of the configuration, but I guess these aren't too interesting.

SLP transforms the ServerStatusConf to a PersonalizedStatusPatch, available via slp.getStatus().getPatch() and slp.getStatus().getHosts(). There the strings are already partially pre-processed (certain static placeholders applied, possibly color codes replaced, ...). I guess this is also not helpful for you, so looking at the ServerStatusConf is probably best.

Andre601 commented 5 months ago

Yeah, been looking at the patches one with getDefaults, getPersonalized and getBanned so far...

Andre601 commented 4 months ago

It is done