Skip to content

配置文件

Korrent 支持插件创建自己的配置文件

定义插件配置

java
import moe.shizuki.korrent.plugin.annotation.KorrentConfig;
import moe.shizuki.korrent.plugin.config.PluginConfig;

@KorrentConfig
public class ExampleConfig {
    private Example example;

    public ExampleConfig() {
        this.example = new Example();
    }

    public Example getExample() {
        return example;
    }

    public class Example {
        private String name;
        private String text;

        public Example() {
            this.name = "Example";
            this.text = "Example plugin";
        }

        public String getName() {
            return name;
        }

        public String getText() {
            return text;
        }
    }
}
kotlin
import moe.shizuki.korrent.plugin.annotation.KorrentConfig
import moe.shizuki.korrent.plugin.config.PluginConfig

@KorrentConfig
class ExampleConfig(
    val example: Example = Example()
) : PluginConfig() {
    class Example(
        val name: String = "Example",
        val text: String = "Example plugin"
    )
}

获取配置管理器

java
import moe.shizuki.korrent.plugin.KorrentPlugin;
import moe.shizuki.korrent.plugin.config.PluginConfigManager;
import org.pf4j.PluginWrapper;

public class DemoPlugin extends KorrentPlugin {
    public static PluginConfigManager manager = new PluginConfigManager("null");
    
    public DemoPlugin(PluginWrapper wrapper) {
        super(wrapper);
        
        manager = this.getPluginConfigManager;
    }
}
kotlin
import moe.shizuki.korrent.plugin.KorrentPlugin
import moe.shizuki.korrent.plugin.config.PluginConfigManager
import org.pf4j.PluginWrapper

class DemoPlugin(wrapper: PluginWrapper): KorrentPlugin(wrapper) {
    companion object {
        var manager: PluginConfigManager = PluginConfigManager("null")
    }
    
    init {
        manager = this.getPluginConfigManager
    }
}

读取配置文件

java
import com.google.common.eventbus.Subscribe;
import moe.shizuki.korrent.KorrentConstantKt;
import moe.shizuki.korrent.bittorrent.event.QBittorrentTagCreatedEvent;
import moe.shizuki.korrent.plugin.annotation.KorrentEvent;

@KorrentEvent
public class OnEvent {
    @Subscribe
    public void onEvent(QBittorrentTagCreatedEvent event) {
        String text = DemoPlugin.manager.load(ExampleConfig.class).getDemo().getText();
        System.out.println(text + event.getTag());
    }
}
kotlin
import com.google.common.eventbus.Subscribe
import moe.shizuki.korrent.bittorrent.event.QBittorrentTagCreatedEvent
import moe.shizuki.korrent.plugin.annotation.KorrentEvent
import moe.shizuki.korrent.pluginConfigManager

@KorrentEvent
class OnEvent {
    @Subscribe
    fun onEvent(event: QBittorrentTagCreatedEvent) {
        val demo: ExampleConfig = Demo.manager.load()
        val text = demo.text
        
        println(text + event.tag)
    }
}