Skip to content

Event Listening

Event listening is the core of the Korrent plugin, and we use Guava EventBus to implement it.

Listen for event

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

@KorrentEvent
public class OnEvent {
    @Subscribe
    public void onEvent(QBittorrentTagCreatedEvent event) {
        System.out.println(event.getTag());
    }
}
kotlin
import com.google.common.eventbus.Subscribe
import moe.shizuki.korrent.bittorrent.event.QBittorrentTagCreatedEvent
import moe.shizuki.korrent.plugin.annotation.KorrentEvent

@KorrentEvent
class OnEvent {
    @Subscribe
    fun onEvent(event: QBittorrentTagCreatedEvent) {
        println(event.tag)
    }
}

Listen to multiple events in a single class

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

@KorrentEvent
public class OnEvent {
    @Subscribe
    public void onTagCreated(QBittorrentTagCreatedEvent event) {
        System.out.println(event.getTag());
    }

    @Subscribe
    public void onTagRemoved(QBittorrentTagCreatedEvent event) {
        System.out.println(event.getTag());
    }
}
kotlin
import com.google.common.eventbus.Subscribe
import moe.shizuki.korrent.bittorrent.event.QBittorrentTagCreatedEvent
import moe.shizuki.korrent.plugin.annotation.KorrentEvent

@KorrentEvent
class OnEvent {
    @Subscribe
    fun onTagCreated(event: QBittorrentTagCreatedEvent) {
        println(event.tag)
    }

    @Subscribe
    fun onTagRemoved(event: QBittorrentTagCreatedEvent) {
        println(event.tag)
    }
}

Automatically register listeners

The purpose of @KorrentEvent is to automatically register a class as a listener, you do not need to manually manage the life cycle of the listener.

Manual management of listeners in any way is not recommended.