Skip to content

Using the ResouceAPI

ancap-kun edited this page Feb 17, 2023 · 1 revision

ResouceAPI - a way to work with resources of different versions. ResourceAPI allows you to load and transfer values in config. With ResourceAPI your plugin users will never have to recreate configs and manually re-fill everything again!

Basic

The ResourceAPI is already integrated into the AncapPluginAPI, and when you get a config via this.getConfiguration from the AncapPlugin descendant, you are given a ResourceAPI-processed config. All you have to do is fill in the value-transfer-map.yml:

main-domain:
  test-config.yml:
    2: # version to which the transfer is made
      - "test-relocated:test.relocated" # old path : new path
      - "test-empty-relocated:test.empty-relocated"
      - "test-incorrect.relocation:test-incorrect-relocation"
custom:
  LanguageAPI: # Locales are populated here, one transfer mapping for all locales at once
      2:
        - "ru.ancap.framework.messages.not-enough-args:ru.ancap.framework.messages.not-enough-arguments"

And add the version field to your config. For normal config this field is config-version, for locales from LanguageAPI this field is version:

config-version: 1

Extended

This is how you can create your own domain/resource preparer yourself:

private final Cache<FileConfiguration> configCache = new Cache<>(1_000_000_000);
    
protected ConfigurationSection getConfiguration(String fileName) {
    return this.configCache.get(() -> this.newResourceSource(FileConfigurationPreparator.resolveConflicts(
    (version) -> this.valueTransferMap() /* from AncapPlugin heir */ != null ? 
        BuiltTransferMap.makeFor(this.valueTransferMap().getConfigurationSection("main-domain. "+fileName), version) :
        BuiltTransferMap.EMPTY,
            "config-version"
    ))).getResource(fileName));
}
Clone this wiki locally