Plugin already being initialized?

Discussion in 'Plugin Development' started by JavaGod, Aug 21, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    JavaGod

    So in my plugin it's seems as if whenever I run a command it gives me an internal error in game and in the console it states plugin is already initialized along with 3 other errors.

    Errors in console:
    Code:
    Caused by: java.lang.ExceptionInInitializerError
            at me.Bryan.School.Commands.school.onCommand(school.java:53) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
    Code:
    Caused by: java.lang.IllegalArgumentException: Plugin already initialized!
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:122) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:67) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at me.Bryan.School.Main.<init>(Main.java:17) ~[?:?]
            at me.Bryan.School.CountDowns.StudyCountdown.<clinit>(StudyCountdown.java:13) ~[?:?]
            at me.Bryan.School.Commands.school.onCommand(school.java:53) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
    Code:
    Caused by: java.lang.IllegalStateException: Initial initialization
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:125) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:67) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at me.Bryan.School.Main.<init>(Main.java:17) ~[?:?]

    Classes (schools class/ Errors will have comment lines.):
    Code:
    public class school implements CommandExecutor {
        public static Location classroom;
        public Location spawn;
        Main main;
    
        public school(Main main) {
            this.main = main;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                Location location = new Location(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ());
               
                if (args.length == 0) {
                    player.sendMessage(ChatUtils.prefix + ChatColor.GOLD + "Command usage:");
                    player.sendMessage(ChatColor.GREEN + "/class join");
                    player.sendMessage(ChatColor.GREEN + "/class start");
                    player.sendMessage(ChatColor.GREEN + "/class kick <player>");
                    player.sendMessage(ChatColor.GREEN + "/class setclassroom");
                    player.sendMessage(ChatColor.GREEN + "/class end");
                    player.sendMessage(ChatColor.GREEN + "/class setspawn");
                    return true;
                }
                if (args[0].equalsIgnoreCase("join")) {
                    if(!main.getStudents().contains(player)) {
                    main.getStudents().add(player);
                    player.sendMessage(ChatUtils.prefix + ChatColor.GREEN + "You've saved a spot in class!");
                    return true;
                    }else if(main.getStudents().contains(player)){
                        main.getStudents().remove(player);
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "You've removed your spot in class!");
                    }
                } else if (args[0].equalsIgnoreCase("start")) {
                    if (player.hasPermission("school.teacher")) {
                        main.setStatus(ClassManager.STUDYPERIOD);
                        StudyCountdown.startStudyCountDown(); //ERROR LINE 53
                         Bukkit.broadcastMessage(ChatUtils.prefix +"Class is initiated /class join to save a spot!");
                        player.sendMessage(ChatUtils.prefix + ChatColor.GREEN + "You've just started a class.");
                        return true;
                    } else {
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "You're no teacher!");
                        return true;
                    }
                } else if (args[0].equalsIgnoreCase("kick")) {
                    if (args.length == 1) {
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "Please specify a player");
                        return true;
                    }
                    String message = "";
                    for (int i = 1; i < args.length; i++) {
                        message += args[i] + " ";
                    }
                    message.trim();
                    if (Bukkit.getServer().getPlayer(message) != null && main.getStudents().contains(message)) {
                        if (player.hasPermission("school.kick")) {
                            main.getStudents().remove(message);
                            Bukkit.dispatchCommand(sender, "spawn " + message);
                            player.sendMessage(ChatUtils.prefix + ChatColor.GREEN + "That player was kicked from your class!");
                            return true;
                        } else {
                            player.sendMessage(ChatUtils.prefix + ChatColor.RED + "You're no teacher");
                            return true;
                        }
                    } else {
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "Player null");
                        return true;
                    }
                } else if (args[0].equalsIgnoreCase("setclassroom")) {
                    if (player.hasPermission("school.teacher")) {
                        classroom.add(player.getLocation());
                        player.sendMessage(ChatUtils.prefix + ChatColor.GREEN + "Classroom set location!");
                        Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.RED + ChatColor.BOLD
                                + "Staff, please don't set another class room location.");
                    } else {
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "You're no teacher");
                    }
                } else if (args[0].equalsIgnoreCase("end")) {
                    for (Player all : main.getStudents()) {
                        all.teleport(spawn);
                        all.sendMessage(ChatUtils.prefix + ChatColor.GREEN + "Class has ended!");
                    }
                    main.getStudents().clear();
                } else if (args[0].equalsIgnoreCase("setspawn")) {
                    if (player.hasPermission("school.teacher")) {
                        spawn.add(location);
                        player.sendMessage(ChatUtils.prefix + ChatColor.GREEN
                                + "You've set the spawn location, where all students get teleported back.");
                    } else {
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "You're no teacher");
                    }
                } else {
                    player.sendMessage(ChatUtils.prefix + ChatColor.RED + "Unknown Argument! Please use one of the correct arguments.");
                }
            } else {
                sender.sendMessage(ChatUtils.prefix + ChatColor.RED + "Only players may use class commands.");
                return true;
            }
            return true;
        }
    
    }
    
    Main class (error will have comment):
    Code:
    public class Main extends JavaPlugin{ //ERROR LINE 17
        public List<Player> students = new ArrayList<Player>();
        public Main plugin;
        public ClassManager status;
        public static int classCD;
        public static int studyCD;
        public int classCounter = 90;
        public int studyCounter = 30;
        public boolean school = false;
        public boolean studyPeriod = false;
        public int minStudents = 6;
        public List<Player> getStudents() {
            return students;
        }
        public void onEnable() {
            System.out.println("works");
            registerEvents();
            registerCommands();
        }
       
        public void onDisable() {
            students = null;
        }
       
        public void registerEvents() {
            Bukkit.getServer().getPluginManager().registerEvents(new PlayerLeave(plugin), this);
            Bukkit.getServer().getPluginManager().registerEvents(new PlayerHit(plugin), this);
        }
       
        public void registerCommands() {
            this.getCommand("class").setExecutor(new school(this));
        }
       
        public static void schoolStart() {
                 StudyCountdown.startStudyCountDown();
                 Bukkit.broadcastMessage(ChatUtils.prefix +"Class is initiated /class join to save a spot!");
        }
    
        public void setStudents(List<Player> students) {
            this.students = students;
        }
        public ClassManager getStatus() {
            return status;
        }
        public void setStatus(ClassManager status) {
            this.status = status;
        }
    
        public Main getInstance() {
            return plugin;
        }
    
        public void setInstance(Main plugin) {
            this.plugin = plugin;
        }
    
        public int getClassCD() {
            return classCD;
        }
    
        public int getStudyCD() {
            return studyCD;
        }
    
        public int getClassCounter() {
            return classCounter;
        }
    
        public void setClassCounter(int classCounter) {
            this.classCounter = classCounter;
        }
    
        public int getStudyCounter() {
            return studyCounter;
        }
    
        public void setStudyCounter(int studyCounter) {
            this.studyCounter = studyCounter;
        }
    
        public boolean isSchool() {
            return school;
        }
    
        public void setSchool(boolean school) {
            this.school = school;
        }
    
        public boolean isStudyPeriod() {
            return studyPeriod;
        }
    
        public void setStudyPeriod(boolean studyPeriod) {
            this.studyPeriod = studyPeriod;
        }
    
        public int getMinStudents() {
            return minStudents;
        }
    
        public void setMinStudents(int minStudents) {
            this.minStudents = minStudents;
        }
    }
    
    Study counter class (Error has comment):
    Code:
    public class StudyCountdown {
        private static final Main main = new Main(); //ERROR LINE 13
        public static void startStudyCountDown() {
                Main.studyCD = Bukkit.getScheduler().scheduleSyncRepeatingTask(main.getInstance(), new Runnable() {
                    @Override
                    public void run() {
                        if(Bukkit.getOnlinePlayers().size() < main.getMinStudents()) {
                            return;
                        }
                       
                        if((Bukkit.getOnlinePlayers().size() < main.getMinStudents()) && (main.getStudyCounter() == 20)) {
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.GOLD +"Restarting studyCounter.. Not enough players online");
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.GOLD +"Amount of students needed: 6");
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.GOLD +"Amount of players online " +Bukkit.getServer().getOnlinePlayers().size());
                   
                }
                       
                        for(Player all : main.getStudents()) {
                            all.setLevel(main.getStudyCounter());
                            all.setExp((float)main.getStudyCounter()/(float)90); // Need a float variable to set level exp bar full
                        }
                       
                        if(main.getStudyCounter() == 20 || main.getStudyCounter() == 10 || main.getStudyCounter() == 5 || main.getStudyCounter() == 4 || main.getStudyCounter() == 3 || main.getStudyCounter() == 2) {
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.GOLD +"Class starting in " + main.getStudyCounter() + ChatColor.GOLD +" seconds");
                        }
                       
                        if(main.getStudyCounter() == 1) {
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.GOLD + "Class starts in 1 second");
                            for(Player all : main.getStudents()) {
                                all.playSound(all.getLocation(), Sound.BLOCK_ANVIL_PLACE, 10f, 0f);
                            }
                        }
                       
                        if(main.getStudyCounter() == 0) {
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.GOLD +"teleported all players to class!!");
                            for(Player all : main.getStudents()) {
                                all.playSound(all.getLocation(), Sound.ENTITY_CHICKEN_EGG, 10f, 0f);
                                all.teleport(school.classroom);
                            }
                            ClassCountdown.startClassCountDown();
                            //main.setStatus() = ClassManager.STUDYPERIOD;
                            main.setSchool(false);
                            main.setStudyPeriod(true);
                        }
                       
                        main.setClassCounter(-1);
                    }
                }, 0L, 20L);
           
        }
    
    }
    
    Help is appreciated, I understand if I may not have a grand understanding of Java and I'm still learning on a website. But I'm trying to put in an update for my server and I don't have the money to hire a developer. Help is very much appreciated.
     
  2. Offline

    Zombie_Striker

    Your here is that an ititlialization of a vatiable has failed. Please post line 53.
    This is caused by a duplicate main class. Make sue only one class in your entire plugin extends JavaPLugin, and make sure you never call create a new Main instance.

    This is also caused by an itialization error. Please post line 17.

    BTW: This can be reduced to "= player.getLocation()"
     
  3. Offline

    JavaGod

    Figured out the problem, I did call a new Main() instance in my class. But now I'm getting some different errors. I usually test my plugin out as I finish each command and part by part, but my test server broke down at the time so I just kind of coded it fully and now it's having problems.

    Code:
    Caused by: java.lang.NullPointerException
            at me.Bryan.School.CountDowns.StudyCountdown.startStudyCountDown(StudyCountdown.java:15) ~[?:?]
            at me.Bryan.School.Commands.school.onCommand(school.java:53) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
    Line 15: Main.studyCD = Bukkit.getScheduler().scheduleSyncRepeatingTask(main.getInstance(), new Runnable() {

    Line 53: StudyCountdown.startStudyCountDown();


    Code:
    Caused by: java.lang.NullPointerException
            at me.Bryan.School.Commands.school.onCommand(school.java:87) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
    Line 87: classroom.add(location);
    The location method: Location location = player.getLocation();
    and classroom method: public static Location classroom;

    Also whenever I type /class kick <player> even though the player is in the list it still says the player is null, here is the code:

    I understand this may not be the most efficient way to do it, but I thought about a way to do it for a long time and this was the only way i could kind of think of doing it. If there is a better way please let me know!

    Code:
     else if (args[0].equalsIgnoreCase("kick")) {
                    if (args.length == 1) {
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "Please specify a player");
                        return true;
                    }
                    String message = "";
                    for (int i = 1; i < args.length; i++) {
                        message += args[i] + " ";
                    }
                    message.trim();
                    if (Bukkit.getServer().getPlayer(message) != null && main.getStudents().contains(message)) {
                        if (player.hasPermission("school.kick")) {
                            main.getStudents().remove(message);
                            Bukkit.dispatchCommand(sender, "spawn " + message);
                            player.sendMessage(ChatUtils.prefix + ChatColor.GREEN + "That player was kicked from your class!");
                            return true;
                        } else {
                            player.sendMessage(ChatUtils.prefix + ChatColor.RED + "You're no teacher");
                            return true;
                        }
                    } else {
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "Player null");
                        return true;
                    }
    Thanks for your help @Zombie_Striker This is all probably simple stuff that you can figure out in 2 seconds. But I'm a bit of a newb :/
     
    Last edited: Aug 21, 2016
  4. Offline

    Zombie_Striker

    @JavaGod
    1. For the first error, the issue is most likely the instance (main.getInstance()) is null, or that "Main" is null. Null check those variables to see what is null.
    2. For the second error, classroom is most likely null. I do not see the line where you initialize it. Make sure it is not null before you use that instance.
    3. This is a simple mistake. Instead of look for the player with the name <player>, you are looking for a player whose naems is the reason they are banned. You will never find the player "was hacking" on your server. Replace "getPlayer(message)" with "getPlayer(args[1])". Also, change the for loop to start at 2, since the arg[1] is the player.
     
  5. Offline

    JavaGod

    1. When you say checking for null you mean like if(main.getInstance() != null) {Then do my stuff?} Because when I do that, the error still pops up.
    2. Fixed my class room thing by just setting classroom = location;
    3. I did what you've asked and here is the code, but it's still not working?
    Code:
    } else if (args[0].equalsIgnoreCase("kick")) {
                    if (args.length == 1) {
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "Please specify a player");
                        return true;
                    }
                    String message = "";
                    for (int i = 2; i < args.length; i++) {
                        message += args[i] + " ";
                    }
                    message.trim();
                    if (Bukkit.getServer().getPlayer(args[1]) != null && main.getStudents().contains(args[1])) {
                        if (player.hasPermission("school.kick")) {
                            main.getStudents().remove(message);
                            Bukkit.dispatchCommand(sender, "spawn " + message);
                            player.sendMessage(ChatUtils.prefix + ChatColor.GREEN + "That player was kicked from your class!");
                            return true;
                        } else {
                            player.sendMessage(ChatUtils.prefix + ChatColor.RED + "You're no teacher");
                            return true;
                        }
    @Zombie_Striker
     
    Last edited: Aug 21, 2016
  6. Offline

    Zombie_Striker

    Well, then "main" is most likely null.

    Just noticed that "main" is not a variable, but a class. Please follow Java naming conventions (packagenames should be all lowercase, classes should start with an Uppercase letter, and variables should start with a lowercase letter.)

    You are calling "getInstance", which I assume means you are getting the main instance on potentially a null variable. You most likely mean Main.getInstance(), as "main.getInstance()" should throw an NPE, since main would most likely be null. Change "main" to "Main", and add "static" to the getInstance and the 'plugin' field (like this)
    Code:
    public static Main plugin;
    
    public static Main getInstance() {
    return  plugin;
    }
    
    What does args[1] return? What does "getPlayer(args[1])" return? Are you sure that player is online?
     
  7. Offline

    JavaGod

    1. My Main class is called "Main" but I just made a line called public static Main main; just to make things easier for me. I've been using that line a lot and I never had any problems with it. Also added a static to getInstance and I'm still getting the issue for some reason.

    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'class' in plugin McCitiesSchool v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.PlayerConnection.handleCommand(PlayerConnection.java:1351) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:1186) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_92]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_92]
            at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:733) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_92]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at org.bukkit.craftbukkit.v1_10_R1.scheduler.CraftScheduler.validate(CraftScheduler.java:397) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at org.bukkit.craftbukkit.v1_10_R1.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:123) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at org.bukkit.craftbukkit.v1_10_R1.scheduler.CraftScheduler.scheduleSyncRepeatingTask(CraftScheduler.java:119) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at me.Bryan.School.CountDowns.StudyCountdown.startStudyCountDown(StudyCountdown.java:15) ~[?:?]
            at me.Bryan.School.Commands.school.onCommand(school.java:54) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
            ... 15 more
    My main thing: private static Main main;
    Line 15: Main.studyCD = Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getInstance(), new Runnable() {
    Line 54: StudyCountdown.startStudyCountDown();


    2. I'm sure the player is online cause it's me. And the args[1] should return the player they are trying to kick. Here is the whole class:

    Code:
        public static Location classroom;
        public Location spawn;
        Main main;
    
        public school(Main main) {
            this.main = main;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                Location location = player.getLocation();
               
                if (args.length == 0) {
                    player.sendMessage(ChatUtils.prefix + ChatColor.GOLD + "Command usage:");
                    player.sendMessage(ChatColor.GREEN + "/class join");
                    player.sendMessage(ChatColor.GREEN + "/class start");
                    player.sendMessage(ChatColor.GREEN + "/class kick <player>");
                    player.sendMessage(ChatColor.GREEN + "/class setclassroom");
                    player.sendMessage(ChatColor.GREEN + "/class end");
                    player.sendMessage(ChatColor.GREEN + "/class setspawn");
                    player.sendMessage(ChatColor.GREEN + "/class classroom");
                    return true;
                }
                if (args[0].equalsIgnoreCase("join")) {
                    if(!main.getStudents().contains(player)) {
                    main.getStudents().add(player);
                    player.sendMessage(ChatUtils.prefix + ChatColor.GREEN + "You've saved a spot in class!");
                    return true;
                    }else if(main.getStudents().contains(player)){
                        main.getStudents().remove(player);
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "You've removed your spot in class!");
                    }
                } else if (args[0].equalsIgnoreCase("start")) {
                    if (player.hasPermission("school.teacher")) {
                        main.setStatus(ClassManager.STUDYPERIOD);
                        StudyCountdown.startStudyCountDown(); //ERROR LINE 53
                         Bukkit.broadcastMessage(ChatUtils.prefix +"Class is initiated /class join to save a spot!");
                        player.sendMessage(ChatUtils.prefix + ChatColor.GREEN + "You've just started a class.");
                        return true;
                    } else {
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "You're no teacher!");
                        return true;
                    }
                } else if (args[0].equalsIgnoreCase("kick")) {
                    if (args.length == 1) {
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "Please specify a player");
                        return true;
                    }
                    String message = "";
                    for (int i = 2; i < args.length; i++) {
                        message += args[i] + " ";
                    }
                    message.trim();
                    if (Bukkit.getServer().getPlayer(args[1]) != null && main.getStudents().contains(args[1])) {
                        if (player.hasPermission("school.kick")) {
                            main.getStudents().remove(message);
                            Bukkit.dispatchCommand(sender, "spawn " + message);
                            player.sendMessage(ChatUtils.prefix + ChatColor.GREEN + "That player was kicked from your class!");
                            return true;
                        } else {
                            player.sendMessage(ChatUtils.prefix + ChatColor.RED + "You're no teacher");
                            return true;
                        }
                    } else {
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "Player null");
                        return true;
                    }
                } else if (args[0].equalsIgnoreCase("setclassroom")) {
                    if (player.hasPermission("school.teacher")) {
                        if(classroom == null) {
                        classroom = location;
                        player.sendMessage(ChatUtils.prefix + ChatColor.GREEN + "Classroom set location!");
                        Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.RED + ChatColor.BOLD
                                + "Staff, please don't set another class room location.");
                        } else {
                            classroom = null;
                            player.sendMessage(ChatUtils.prefix + ChatColor.GREEN + "Classroom location removed!");
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.RED + ChatColor.BOLD
                                    + "Classroom location has been removed please set a new one ASAP");
                        }
                    } else {
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "You're no teacher");
                    }
                } else if (args[0].equalsIgnoreCase("end")) {
                    for (Player all : main.getStudents()) {
                        all.teleport(spawn);
                        all.sendMessage(ChatUtils.prefix + ChatColor.GREEN + "Class has ended!");
                    }
                    main.getStudents().clear();
                } else if (args[0].equalsIgnoreCase("setspawn")) {
                    if (player.hasPermission("school.teacher")) {
                        spawn.add(location);
                        player.sendMessage(ChatUtils.prefix + ChatColor.GREEN
                                + "You've set the spawn location, where all students get teleported back.");
                    } else {
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "You're no teacher");
                    }
                }else if(args[0].equalsIgnoreCase("classroom")) {
                    player.teleport(classroom);
                    player.sendMessage(ChatUtils.prefix + ChatColor.GREEN + "You're being teleported to the classroom!");
                } else {
                    player.sendMessage(ChatUtils.prefix + ChatColor.RED + "Unknown Argument! Please use one of the correct arguments.");
                }
            } else {
                sender.sendMessage(ChatUtils.prefix + ChatColor.RED + "Only players may use class commands.");
                return true;
            }
            return true;
        }
     
  8. Offline

    Zombie_Striker

    What this means is that the instance 'plugin' is null, meaning it was never set to the main class. You should put "this.plugin=this" in the onEnable for the main class (or use setInstance(this) if you want to use that method.).

    Are you getting the "player null" message? If so, either the server is not recognizing your player (which means you will have to loop through all the players online, and test if the for looped player's name is equal to args[1]) or the player is not in the students array (which means you will have to for loop through that array and check if your player is in it.)
     
  9. Offline

    JavaGod

    This is what I did to check if player name exists in the list, yet nothing still? Is there something I'm doing wrong o.o
    Code:
    } else if (args[0].equalsIgnoreCase("kick")) {
                    if (args.length == 1) {
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "Please specify a player");
                        return true;
                    }
                    String message = "";
                    for (int i = 2; i < args.length; i++) {
                        message += args[i] + " ";
                    }
                    message.trim();
                    for(Player students : main.getStudents()){
                         String p = students.getName();
                    if (p == args[1]) {
                        if (player.hasPermission("school.kick")) {
                            main.getStudents().remove(message);
                            Bukkit.dispatchCommand(sender, "spawn " + message);
                            player.sendMessage(ChatUtils.prefix + ChatColor.GREEN + "That player was kicked from your class!");
                            return true;
                        } else {
                            player.sendMessage(ChatUtils.prefix + ChatColor.RED + "You're no teacher");
                            return true;
                        }
                  
                    } else {
                        player.sendMessage(ChatUtils.prefix + ChatColor.RED + "Player null");
                        return true;
                    }
                    }
    And I'm not too sure about what you meant about my main class? Can you please give me an example that would work with it @Zombie_Striker ?
     
  10. Offline

    sniddunc

    I would recommend that you use the .getPlugin(Main.class) method to create a new instance of the plugin/main class.
    For example:

    Code:
    Plugin plugin = Main.getPlugin(Main.class);
    The main class is the class that extends JavaPlugin and contains the onEnable and onDisable methods.
     
  11. Offline

    Zombie_Striker

    @sniddunc
    The problem can be solved simply bad adding one line to the onEnable, instead of just creating an new method that has to manage to find the instance of the main class.

    @JavaGod
    This is what I meant:
    Code:
    public void onEnable(){
    this.plugin = this; // ===Here
    registerEvents();
    ...
    }
    [edit] re-read your post:
    First, fix your formatting. If you're using an IDE on windows, use ctrl+shift+f to format.

    Now, the if statement will never be true. Strings cannot be compared using ==. Use .equals() instead. If this does not fix your problem, then you will need to debug and print out what 'p' is equal to, and what args[1] is equal to, and then you will need to figure out why it is not finding your player.
     
  12. Offline

    sniddunc

    @Zombie_Striker

    I wasn't suggesting he creates a new method. When a class extends JavaPlugin you can use getPlugin() on it to get the instance of the Plugin/main class.
     
  13. Offline

    JavaGod

    Thanks guys I got it!!!! @Zombie_Striker and @sniddunc. Appreciate all the help!!!

    But when making a countdown is this correct? I want to make sure I'm doing this the best way.
    Code:
    public static void startStudyCountDown() {
                Main.studyCD = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                    @Override
                    public void run() {
                        if(Bukkit.getOnlinePlayers().size() < main.getMinStudents()) {
                            Bukkit.getScheduler().cancelTask(Main.studyCD);
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.RED + ChatColor.BOLD + "The class has been cancelled. Not enough players online!");
                        }
                   
                        if((Bukkit.getOnlinePlayers().size() < main.getMinStudents()) && (main.getStudyCounter() == 20)) {
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.GOLD +"Restarting studyCounter.. Not enough players online");
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.GOLD +"Amount of students needed: 6");
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.GOLD +"Amount of players online " +Bukkit.getServer().getOnlinePlayers().size());
               
                }
                   
                        for(Player all : main.getStudents()) {
                            all.setLevel(main.getStudyCounter());
                            all.setExp((float)main.getStudyCounter()/(float)90); // Need a float variable to set level exp bar full
                        }
                   
                        if(main.getStudyCounter() == 20 || main.getStudyCounter() == 10 || main.getStudyCounter() == 5 || main.getStudyCounter() == 4 || main.getStudyCounter() == 3 || main.getStudyCounter() == 2) {
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.GOLD +"Class starting in " + main.getStudyCounter() + ChatColor.GOLD +" seconds");
                        }
                   
                        if(main.getStudyCounter() == 1) {
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.GOLD + "Class starts in 1 second");
                            for(Player all : main.getStudents()) {
                                all.playSound(all.getLocation(), Sound.BLOCK_ANVIL_PLACE, 10f, 0f);
                            }
                        }
                   
                        if(main.getStudyCounter() == 0) {
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.GOLD +"teleported all players to class!!");
                            for(Player all : main.getStudents()) {
                                all.playSound(all.getLocation(), Sound.ENTITY_CHICKEN_EGG, 10f, 0f);
                                all.teleport(school.classroom);
                            }
                            ClassCountdown.startClassCountDown();
                            //main.setStatus() = ClassManager.STUDYPERIOD;
                            main.setSchool(false);
                            main.setStudyPeriod(true);
                        }
                   
                        main.setClassCounter(-1);
                    }
           
                }, 0L, 20L);
        }
    I'm using a repeating task, and I'm getting an error at line 20:
    Code:
    if(Bukkit.getOnlinePlayers().size() < main.getMinStudents()) {
                            Bukkit.getScheduler().cancelTask(Main.studyCD);
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.RED + ChatColor.BOLD + "The class has been cancelled. Not enough players online!");
                        }
    And it happens every 1 second in console because obviously it's a repeating task. Even though I ask it to stop the task. I also want to know if there is another way I could make this countdown timer?

    ERROR:
    Code:
    ava.lang.NullPointerException
            at me.Bryan.School.CountDowns.StudyCountdown$1.run(StudyCountdown.java:20) ~[?:?]
            at org.bukkit.craftbukkit.v1_10_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot-1.10.2.jar:git-Spigot-1e4dd71-5e5cf84]
            at org.bukkit.craftbukkit.v1_10_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [spigot-1.10.2.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:723) [spigot-1.10.2.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot-1.10.2.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:668) [spigot-1.10.2.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:567) [spigot-1.10.2.jar:git-Spigot-1e4dd71-5e5cf84]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_72]
    [01:46:36 WARN]: [McCitiesSchool] Task #134 for McCitiesSchool v1.0 generated an exception
    java.lang.NullPointerException
            at me.Bryan.School.CountDowns.StudyCountdown$1.run(StudyCountdown.java:20) ~[?:?]
            at org.bukkit.craftbukkit.v1_10_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot-1.10.2.jar:git-Spigot-1e4dd71-5e5cf84]
            at org.bukkit.craftbukkit.v1_10_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [spigot-1.10.2.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:723) [spigot-1.10.2.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot-1.10.2.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:668) [spigot-1.10.2.jar:git-Spigot-1e4dd71-5e5cf84]
            at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:567) [spigot-1.10.2.jar:git-Spigot-1e4dd71-5e5cf84]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_72]
    [01:46:37 WARN]: [McCitiesSchool] Task #134 for McCitiesSchool v1.0 generated an exception
    
    @sniddunc @Zombie_Striker @bwfcwalshy
     
    Last edited: Aug 22, 2016
  14. Offline

    SantaClawz69

    The error is happening because
    1. It's a repeating task so it happens every second
    2. You probably don't have enough players online. Clearly you have an integer that has the minimum players online needed and it's not meeting the requirements so it's not going through.
     
  15. Offline

    Zombie_Striker

    @SantaClawz69
    That is most likey not why the error is occuring. Even if there are no players online, that would not throw an NPE.
    The issue is caused by something being null. The variables that can be null are "main" and "Main.studyCD" (Not this one. Since it's an int, this would throw an AOOB instead). Null check for these variables, and figure out which one of these are null.
     
  16. Offline

    SantaClawz69

    I realize that, but I'm just saying that the error is being thrown because it's running into that situation. Obviously if he had enough players I doubt he'll be running into that.
     
  17. Offline

    sniddunc

    What is getMinStudents() returning? Does it have a default value or is it null?

    If this happens when there are no players or not enough players on the server, make sure getMinStudents() is not null. If it's not that, it's another variable you used around line 20.
     
  18. Offline

    Zombie_Striker

    MinStudents can't return a null value. That method returns an int. ints can never be null.
     
  19. Offline

    JavaGod

    Yeah what @Zombie_Striker mentioned. My minStudents has a value of 2. @sniddunc or @Zombie_Striker can you explain to me what you mean by check if main is null? Do you mean

    if(main == null) {do stuff}??

    EDIT: @Zombie_Striker and @sniddunc or anyone else, that the exact line that is causing this freaking error is if(Bukkit.getOnlinePlayers().size() < main.getMinStudents()) {
     
    Last edited: Aug 22, 2016
  20. Offline

    SantaClawz69

    That's not what he meant by null checking the main variable I don't think.
     
  21. Offline

    Zombie_Striker

    @JavaGod
    Yes and no. That is, in a sense, null checking. The problem is, that addresses the problem without fixing it. Main can still be null. What you need to do is use something like the following:
    Code:
    System.out.println("is it null? "+main==null)
    If this returns true, then that means the main is null. After that, you need to make sure main cannot be null.
     
  22. Offline

    JavaGod

    Main is not null, it prints out false.

    What's giving me the error is this line right here:
    if (main.getStudents() != null) {


    Here is the full code to that method:
    Code:
    if (Main.studyCounter == 0) {
                        if (main.getStudents() != null) {
                            Bukkit.broadcastMessage(
                                    ChatUtils.prefix + ChatColor.GOLD + "teleported all players to class!!");
                            for (Player all : main.getStudents()) {
                                all.playSound(all.getLocation(), Sound.ENTITY_CHICKEN_EGG, 10f, 0f);
                                all.teleport(school.classroom);
                            }
                            ClassCountdown.startClassCountDown();
                            // main.setStatus() = ClassManager.STUDYPERIOD;
                            main.setSchool(true);
                            main.setStudyPeriod(false);
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.GOLD + "Class has started!");
                            Bukkit.getScheduler().cancelAllTasks();
                        } else {
                            Bukkit.broadcastMessage(
                                    ChatUtils.prefix + ChatColor.GOLD + "No players joined the class");
                            Bukkit.getScheduler().cancelAllTasks();
                        }
                    }
     
  23. Offline

    Zombie_Striker

    @JavaGod
    There is a difference between these two variables. One of them is the Class object, another is an instance of that class. Since "Main.studycounter" does not throw an NPE, we know that that Class object is not null (which it should be, since it does not make sense for it to be null.) "main", however, can be null. Add this line instead before the Students check:
    Code:
    if(main==null)
     main = Main.getInstance();
     
  24. Offline

    JavaGod

    @Zombie_Striker when you asked me to print out if main ==null in sysout, it said main was not null. Despite that I tried your code, and it was still giving me errors on that line.

    Is there another possibility?
    This is the code:
    Code:
                    if (Main.studyCounter == 0) {
                        if(main == null)
                            main = Main.getInstance();
                        if (main.getStudents() != null) {
                            Bukkit.broadcastMessage(
                                    ChatUtils.prefix + ChatColor.GOLD + "teleported all players to class!!");
                            for (Player all : main.getStudents()) {
                                all.playSound(all.getLocation(), Sound.ENTITY_CHICKEN_EGG, 10f, 0f);
                                all.teleport(school.classroom);
                            }
                            ClassCountdown.startClassCountDown();
                            // main.setStatus() = ClassManager.STUDYPERIOD;
                            main.setSchool(true);
                            main.setStudyPeriod(false);
                            Bukkit.broadcastMessage(ChatUtils.prefix + ChatColor.GOLD + "Class has started!");
                            Bukkit.getScheduler().cancelAllTasks();
                        } else {
                            Bukkit.broadcastMessage(
                                    ChatUtils.prefix + ChatColor.GOLD + "No players joined the class");
                            Bukkit.getScheduler().cancelAllTasks();
                        }
                    }
    The line that's giving me error:
    if (main.getStudents() != null) {

    The stack trace:
    Code:
    [20:22:16 WARN]: [McCitiesSchool] Task #2 for McCitiesSchool v1.0 generated an exception
    java.lang.NullPointerException
            at me.Bryan.School.CountDowns.StudyCountdown$1.run(StudyCountdown.java:33) ~[?:?]
            at org.bukkit.craftbukkit.v1_10_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at org.bukkit.craftbukkit.v1_10_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:741) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571) [spigot.jar:git-Spigot-5391d73-0ebb9c7]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_92]
     
  25. Offline

    mythbusterma

    @JavaGod

    Main is null, that is the only possibility if that is indeed the line in question.
     
  26. Offline

    JavaGod

    So what should I do? I really need this for an update for my server.
     
  27. Offline

    mythbusterma

Thread Status:
Not open for further replies.

Share This Page