Fierioziy / ParticleNativeAPI

A fast cross-version particle API for Spigot and Bukkit server.
MIT License
43 stars 5 forks source link

Can't create any particles at all (NPE) #3

Closed Emafire003 closed 4 years ago

Emafire003 commented 4 years ago

Hi, i was trying out this API for the first time yesterday, and since yesterday whenever i try to create a new particle packet it throws me an NPE exception. Even with the same code as the example one. Object packet = particles.FLAME().packet(true, attaccker.getLocation());

I'm using the latest version for the plugin itself and i've imported the api with maven. Am i doing something wrong or? (I've tried both Particles_1_8 and particles_1_13, i'm running the server and my plugin in 1.15.2)

Fierioziy commented 4 years ago

Hello, can you provide a stacktrace of a NPE? Then I could verify if a problem is with my plugin or a certain part in your code.

Generally any returned particle type (ex. FLAME) should always be non null (so you can use isValid on it).

Is attaccker not null? How particles variable is initialized?

Emafire003 commented 4 years ago

I'm sure attaccker is not null beacase i'm doing other stuff with it at the same moment, i've initialized the particles variable just like the example, `private static ServerConnection serverConn; private static Particles_1_13 particles;

//stuff

Plugin plugin = this.getServer().getPluginManager().getPlugin("ParticleNativeAPI"); if (plugin != null) { // you can safely cast plugin to ParticleNativeAPI plugin ParticleNativeAPI api = (ParticleNativeAPI) plugin;

        // ... or just directly access static getter
        api = ParticleNativeAPI.getPlugin();

        serverConn = api.getServerConnection();
        particles = api.getParticles_1_13();

        // of course, check if API successfully loaded
        if (!api.isValid()) {
            // handle error
        }
    }
    else {
        // handle plugin absence (and avoid referencing it)...
    }

//stuff

public static Particles_1_13 getParticles() { return particles; }

public static ServerConnection getServerConn() {
    return serverConn;
}

And here is the stacktrace: [21:11:11] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to SwordSkilz v0.1.0 org.bukkit.event.EventException: null at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:319) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:541) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:528) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:442) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:409) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:405) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1696) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at net.minecraft.server.v1_15_R1.PacketPlayInArmAnimation.a(SourceFile:31) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at net.minecraft.server.v1_15_R1.PacketPlayInArmAnimation.a(SourceFile:9) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at net.minecraft.server.v1_15_R1.MinecraftServer.ba(MinecraftServer.java:918) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:911) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeAll(SourceFile:103) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:894) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:828) [spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] at java.lang.Thread.run(Unknown Source) [?:1.8.0_241] Caused by: java.lang.NullPointerException at tk.emafire003.dev.skillevents.Linear.(Linear.java:53) ~[?:?] at tk.emafire003.dev.skillevents.SkillStartup.checkForCombo(SkillStartup.java:106) ~[?:?] at tk.emafire003.dev.skillevents.SkillStartup.onPlayerUse(SkillStartup.java:47) ~[?:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_241] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_241] at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:315) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-3d61a85] ... 20 more`

Fierioziy commented 4 years ago

TL;DR Check if you registered your events after you initialized particles and serverConn fields.

If you registered events before initializing particles and serverConn fields, then you might have acquired uninitialized variables in your event class constructor (for ex. by using getParticles() from your plugin class).

It's also better to handle error before accessing rest of API methods:

        // of course, check if API successfully loaded
        if (!api.isValid()) {
            // handle error
                return;
        }

            // better use methods if you are sure API is fine
            serverConn = api.getServerConnection();
        particles = api.getParticles_1_13();

Longer:

Caused by: java.lang.NullPointerException at tk.emafire003.dev.skillevents.Linear.(Linear.java:53) ~[?:?] at tk.emafire003.dev.skillevents.SkillStartup.checkForCombo(SkillStartup.java:106) ~[?:?] at tk.emafire003.dev.skillevents.SkillStartup.onPlayerUse(SkillStartup.java:47) ~[?:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_241] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_241]

If attaccker is not null (and returned FLAME particle type pretty much shouldn't be null as well), then it might be particles variable that's causing a trouble.

You've also said that this line:

Object packet = particles.FLAME().packet(true, attaccker.getLocation());

... was the source of a NPE.

I assume, that below code has been executed in an onEnable() method in your JavaPlugin class:

private static ServerConnection serverConn;
private static Particles_1_13 particles;

//stuff

Plugin plugin = this.getServer().getPluginManager().getPlugin("ParticleNativeAPI");
if (plugin != null) {
// you can safely cast plugin to ParticleNativeAPI plugin
ParticleNativeAPI api = (ParticleNativeAPI) plugin;

        // ... or just directly access static getter
        api = ParticleNativeAPI.getPlugin();

        serverConn = api.getServerConnection();
        particles = api.getParticles_1_13();

        // of course, check if API successfully loaded
        if (!api.isValid()) {
            // handle error
        }
    }
    else {
        // handle plugin absence (and avoid referencing it)...
    }

Are sure, thattk.emafire003.dev.skillevents.Linear and tk.emafire003.dev.skillevents.SkillStartup properly initialized its particles field/variable?

If they accessed getParticles() method from your JavaPlugin before executing this code:

Plugin plugin = this.getServer().getPluginManager().getPlugin("ParticleNativeAPI");
if (plugin != null) {
// you can safely cast plugin to ParticleNativeAPI plugin
ParticleNativeAPI api = (ParticleNativeAPI) plugin;

        // ... or just directly access static getter
        api = ParticleNativeAPI.getPlugin();

        serverConn = api.getServerConnection();
        particles = api.getParticles_1_13();

... then you might have acquired null even, if later you initialized particles and serverConn with above code.

Make sure that above code has been executed before you register your events. I think that's the issue.

Emafire003 commented 4 years ago

Oh of course it's because I've initialized them after the event! Thanks, I'm not yet 100% sure I'm going to test it now.

Emafire003 commented 4 years ago

Ok, now I can confirm that it indeed works. But I also had to add this.particles in the main and not use them as attributes in the Linear class (I had to put the in the method). Thanks for the help!

Btw great resource!