Plasmo Voice

Plasmo Voice

2M Downloads

Try to create addon and get error

isArkaDarkTime opened this issue ยท 3 comments

commented

Hello! I try to create addon for plasmovoice and get error voiceServer is null

Caused by: java.lang.NullPointerException: Cannot invoke "su.plo.voice.api.server.PlasmoVoiceServer.getActivationManager()" because "this.voiceServer" is null

Full log:

[15:04:40 ERROR]: [ModernPluginLoadingStrategy] Could not load plugin 'PlasmoVoiceRadio-1.0.jar' in folder 'plugins\.paper-remapped'
org.bukkit.plugin.InvalidPluginException: Exception initializing main class `arkadarktime.PlasmoVoiceRadioAddon'
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:106) ~[paper-mojangapi-1.21.1-R0.1-SNAPSHOT.jar:?]
        at io.papermc.paper.plugin.provider.type.spigot.SpigotPluginProvider.createInstance(SpigotPluginProvider.java:125) ~[paper-1.21.1.jar:1.21.1-132-b48403b]
        at io.papermc.paper.plugin.provider.type.spigot.SpigotPluginProvider.createInstance(SpigotPluginProvider.java:35) ~[paper-1.21.1.jar:1.21.1-132-b48403b]
        at io.papermc.paper.plugin.entrypoint.strategy.modern.ModernPluginLoadingStrategy.loadProviders(ModernPluginLoadingStrategy.java:116) ~[paper-1.21.1.jar:1.21.1-132-b48403b]
        at io.papermc.paper.plugin.storage.SimpleProviderStorage.enter(SimpleProviderStorage.java:38) ~[paper-1.21.1.jar:1.21.1-132-b48403b]
        at io.papermc.paper.plugin.entrypoint.LaunchEntryPointHandler.enter(LaunchEntryPointHandler.java:40) ~[paper-1.21.1.jar:1.21.1-132-b48403b]
        at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:547) ~[paper-1.21.1.jar:1.21.1-132-b48403b]
        at net.minecraft.server.dedicated.DedicatedServer.initServer(DedicatedServer.java:292) ~[paper-1.21.1.jar:1.21.1-132-b48403b]
        at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1214) ~[paper-1.21.1.jar:1.21.1-132-b48403b]
        at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:329) ~[paper-1.21.1.jar:1.21.1-132-b48403b]
        at java.base/java.lang.Thread.run(Thread.java:1570) ~[?:?]
Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:72) ~[?:?]
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502) ~[?:?]
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486) ~[?:?]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:98) ~[paper-mojangapi-1.21.1-R0.1-SNAPSHOT.jar:?]
        ... 10 more
Caused by: java.lang.NullPointerException: Cannot invoke "su.plo.voice.api.server.PlasmoVoiceServer.getActivationManager()" because "this.voiceServer" is null
        at PlasmoVoiceRadio-1.0.jar/arkadarktime.RadioAddon.<init>(RadioAddon.java:19) ~[PlasmoVoiceRadio-1.0.jar:?]
        at PlasmoVoiceRadio-1.0.jar/arkadarktime.PlasmoVoiceRadioAddon.<init>(PlasmoVoiceRadioAddon.java:7) ~[PlasmoVoiceRadio-1.0.jar:?]
        at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62) ~[?:?]
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502) ~[?:?]
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486) ~[?:?]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:98) ~[paper-mojangapi-1.21.1-R0.1-SNAPSHOT.jar:?]
        ... 10 more

My code:
Main class:

package arkadarktime;

import org.bukkit.plugin.java.JavaPlugin;
import su.plo.voice.api.addon.InjectPlasmoVoice;
import su.plo.voice.api.server.PlasmoVoiceServer;

public final class PlasmoVoiceRadioAddon extends JavaPlugin {
   private final RadioAddon addon = new RadioAddon(this);

   @InjectPlasmoVoice
   private PlasmoVoiceServer voiceServer;

   @Override
   public void onLoad() {
       PlasmoVoiceServer.getAddonsLoader().load(addon);
   }
}

Addon class:

package arkadarktime;

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.event.EventSubscribe;
import su.plo.voice.api.server.PlasmoVoiceServer;
import su.plo.voice.api.server.audio.capture.ServerActivation;
import su.plo.voice.api.server.event.connection.UdpClientConnectEvent;

@Addon(id = "pv-addon-radio", name = "Radio Addon", version = "1.0", authors = {"ArkaDarkTime"})
public class RadioAddon implements AddonInitializer {
    private final PlasmoVoiceRadioAddon plugin;

    @InjectPlasmoVoice
    private PlasmoVoiceServer voiceServer;

    ServerActivation activation = voiceServer.getActivationManager().getActivationByName("proximity").orElseThrow(() -> new IllegalStateException("Proximity activation not found"));

    public RadioAddon(PlasmoVoiceRadioAddon plugin) {
        this.plugin = plugin;
    }

    @Override
    public void onAddonInitialize() {
        plugin.getLogger().info("RadioAddon initialized");
        voiceServer.getEventBus().register(this, UdpClientConnectEvent.class, EventPriority.NORMAL, this::onClientConnected);
    }

    @Override
    public void onAddonShutdown() {
        plugin.getLogger().info("RadioAddon shut down");
    }

    @EventSubscribe
    public void onClientConnected(UdpClientConnectEvent event) {
        plugin.getLogger().info("CLIENT CONNECTED!");
        activation.onPlayerActivationStart(player -> System.out.println(player + " start speaking to activation"));

        activation.onPlayerActivation((player, packet) -> {
            System.out.println(player + " is speaking to activation");
            return ServerActivation.Result.HANDLED;
        });

        activation.onPlayerActivationEnd((player, packet) -> {
            System.out.println(player + " has finished speaking to activation");
            return ServerActivation.Result.HANDLED;
        });

        plugin.getLogger().info(event.getConnection().getPlayer() + " connected to UDP");
    }
}
commented

I know that pv-addon-discs has radio support, I am creating this for myself to learn how to work with your api

commented

image
You can't access voiceServer during class initialization, it's only available after the addon initialization (in onAddonInitialize).

So in your case you should access activation manager in the onAddonInitialize:

@Override
public void onAddonInitialize() {
    ServerActivation activation = voiceServer.getActivationManager().getActivationByName("proximity").orElseThrow(() -> new IllegalStateException("Proximity activation not found"));
}
commented

Yes, that makes sense... thank you!