Search code examples
javastaticbukkitnon-static

Java static and non-static and world interface not instanceable error


I have an issue with

Cannot make a static reference to the non-static method spawnParticle(blabla)

This is what I call my code..

    public class Particle implements CommandExecutor
    {

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] arg) {

        if (sender instanceof Player)
        {
            Particle particle = new Particle();
            Player player = (Player) sender;
            double x = ((Player) sender).getLocation().getX();
            double y = ((Player) sender).getLocation().getY();
            double z = ((Player) sender).getLocation().getZ();

            World.spawnParticle(org.bukkit.Particle.TOTEM, x, y, z, 1, 0, 0, 0);

        }
        return false;
    }


}

I already read a lot about non static static issue solving and know what that issue mean, but I really dont know how to solve it here. The Problem extended to that world is an interface and cant be inhanced. A fix is in the comments Thanks for help


Solution

  • The method is a instance method, wich means you have to instance the object with new and World is an interface, so you can't instance it, you have to instance a class that implements this interface, the player has the world

    (don't forget to import the class WorldEvent)

    if (sender instanceof Player)
            {
                Particle particle = new Particle();
                Player player = (Player) sender;
                double x = ((Player) sender).getLocation().getX();
                double y = ((Player) sender).getLocation().getY();
                double z = ((Player) sender).getLocation().getZ();
                World w = sender.getWorld();
                w.spawnParticle(org.bukkit.Particle.TOTEM, x, y, z, 1, 0, 0, 0);
    
            }