Skip to content
On this page

Events

Plasmo Voice has its own event system, allowing it to function on various platforms.

Registering event handlers

To register event handlers, you need to create addon first, how to do it see Creating an addon.

You can register or unregister event handlers at any time you want.

There are two ways to register event handlers:

@EventSubscribe

java
package com.plasmovoice.testaddon;

import su.plo.voice.api.addon.AddonInitializer;
import su.plo.voice.api.addon.InjectPlasmoVoice;
import su.plo.voice.api.addon.annotation.Addon;
import su.plo.voice.api.event.EventSubscribe;
import su.plo.voice.api.server.PlasmoVoiceServer;
import su.plo.voice.api.server.event.connection.UdpClientConnectEvent;

@Addon(
        id = "pv-addon-test",
        name = "Test Addon",
        version = "1.0.0",
        authors = {"Plasmo"}
)
public final class TestAddon implements AddonInitializer {

    @InjectPlasmoVoice
    private PlasmoVoiceServer voiceServer;

    @Override
    public void onAddonInitialize() {
        voiceServer.getEventBus().register(this, new EventListener());
    }

    public static final class EventListener {

        @EventSubscribe
        public void onClientConnected(UdpClientConnectEvent event) {
            System.out.println(
                    event.getConnection().getPlayer() + " connected to UDP"
            );
        }
    }
}
kotlin
package com.plasmovoice.testaddon

import su.plo.voice.api.addon.AddonInitializer
import su.plo.voice.api.addon.annotation.Addon
import su.plo.voice.api.addon.injectPlasmoVoice
import su.plo.voice.api.event.EventSubscribe
import su.plo.voice.api.server.PlasmoVoiceServer
import su.plo.voice.api.server.event.connection.UdpClientConnectEvent

@Addon(
    id = "pv-addon-test",
    name = "Test Addon",
    version = "1.0.0",
    authors = ["Plasmo"]
)
class TestAddon : AddonInitializer {

    private val voiceServer: PlasmoVoiceServer by injectPlasmoVoice()

    override fun onAddonInitialize() {
        voiceServer.eventBus.register(this, EventListener())
    }

    class EventListener {

        @EventSubscribe
        fun onClientConnected(event: UdpClientConnectEvent) {
            println("${event.connection.player} connected to UDP")
        }
    }
}

You can also use @EventSubscribe in your addon class. Using it this way doesn't require registering an event handler in the event bus, as the addon class is registered by default.

java
package com.plasmovoice.testaddon;

import su.plo.voice.api.addon.AddonInitializer;
import su.plo.voice.api.addon.InjectPlasmoVoice;
import su.plo.voice.api.addon.annotation.Addon;
import su.plo.voice.api.event.EventSubscribe;
import su.plo.voice.api.server.PlasmoVoiceServer;
import su.plo.voice.api.server.event.connection.UdpClientConnectEvent;

@Addon(
        id = "pv-addon-test",
        name = "Test Addon",
        version = "1.0.0",
        authors = {"Plasmo"}
)
public final class TestAddon implements AddonInitializer {

    @InjectPlasmoVoice
    private PlasmoVoiceServer voiceServer;

    @Override
    public void onAddonInitialize() {
    }

    @EventSubscribe
    public void onClientConnected(UdpClientConnectEvent event) {
        System.out.println(
                event.getConnection().getPlayer() + " connected to UDP"
        );
    }
}
kotlin
package com.plasmovoice.testaddon

import su.plo.voice.api.addon.AddonInitializer
import su.plo.voice.api.addon.annotation.Addon
import su.plo.voice.api.addon.injectPlasmoVoice
import su.plo.voice.api.event.EventSubscribe
import su.plo.voice.api.server.PlasmoVoiceServer
import su.plo.voice.api.server.event.connection.UdpClientConnectEvent

@Addon(
    id = "pv-addon-test",
    name = "Test Addon",
    version = "1.0.0",
    authors = ["Plasmo"]
)
class TestAddon : AddonInitializer {

    private val voiceServer: PlasmoVoiceServer by injectPlasmoVoice()

    override fun onAddonInitialize() {}

    @EventSubscribe
    fun onClientConnected(event: UdpClientConnectEvent) {
        println("${event.connection.player} connected to UDP")
    }
}

Method as event handler

java
package com.plasmovoice.testaddon;

import su.plo.voice.api.addon.AddonInitializer;
import su.plo.voice.api.addon.InjectPlasmoVoice;
import su.plo.voice.api.addon.annotation.Addon;
import su.plo.voice.api.event.EventPriority;
import su.plo.voice.api.server.PlasmoVoiceServer;
import su.plo.voice.api.server.event.connection.UdpClientConnectEvent;

@Addon(
        id = "pv-addon-test",
        name = "Test Addon",
        version = "1.0.0",
        authors = {"Plasmo"}
)
public final class TestAddon implements AddonInitializer {

    @InjectPlasmoVoice
    private PlasmoVoiceServer voiceServer;

    @Override
    public void onAddonInitialize() {
        voiceServer.getEventBus().register(
                this,
                UdpClientConnectEvent.class,
                EventPriority.NORMAL,
                this::onClientConnected
        );
    }

    private void onClientConnected(UdpClientConnectEvent event) {
        System.out.println(
                event.getConnection().getPlayer() + " connected to UDP"
        );
    }
}
kotlin
package com.plasmovoice.testaddon

import su.plo.voice.api.addon.AddonInitializer
import su.plo.voice.api.addon.annotation.Addon
import su.plo.voice.api.addon.injectPlasmoVoice
import su.plo.voice.api.event.EventPriority
import su.plo.voice.api.server.PlasmoVoiceServer
import su.plo.voice.api.server.event.connection.UdpClientConnectEvent

@Addon(
    id = "pv-addon-test",
    name = "Test Addon",
    version = "1.0.0",
    authors = ["Plasmo"]
)
class TestAddon : AddonInitializer {

    private val voiceServer: PlasmoVoiceServer by injectPlasmoVoice()

    override fun onAddonInitialize() {
        voiceServer.eventBus.register(
            this,
            UdpClientConnectEvent::class.java,
            EventPriority.NORMAL,
            ::onClientConnected
        )
    }

    private fun onClientConnected(event: UdpClientConnectEvent) {
        println("${event.connection.player} connected to UDP")
    }
}

List of events

You can see all available events in dokka, which can be found in the su.plo.voice.api.{module}.event.* packages.