Scheduler in another class problem

Discussion in 'Plugin Development' started by AaronL98, Jan 5, 2014.

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

    AaronL98

    Hello. I've been having trouble with creating a scheduler in another class. I get a NPE error on the line where i create the scheduler "plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable{})" etc
    I have referenced the main class:
    Code:java
    1. public static Main plugin;


    Here's my full code in class "Event.java"

    Code:java
    1. package net.unionnetworks.Game;
    2.  
    3. import me.confuser.barapi.BarAPI;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerJoinEvent;
    11.  
    12.  
    13. public class Event implements Listener {
    14. public static Main plugin;
    15.  
    16.  
    17. @EventHandler
    18. public void onPlayerJoin(PlayerJoinEvent e){
    19. Main.playersNeeded = Bukkit.getServer().getMaxPlayers() - Bukkit.getOnlinePlayers().length;
    20. Player p = e.getPlayer();
    21. Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "["+ChatColor.DARK_AQUA+"ML"+ChatColor.DARK_GRAY+"] "+ChatColor.GRAY+"Player "+ ChatColor.DARK_AQUA + p + ChatColor.GRAY+" has joined. Waiting on "+ChatColor.DARK_GRAY+"["+ChatColor.DARK_AQUA+ Main.playersNeeded + ChatColor.DARK_GRAY+"]"+ChatColor.GRAY+" players.");
    22. if(Bukkit.getServer().getOnlinePlayers().length == Bukkit.getServer().getMaxPlayers()){
    23. Bukkit.broadcastMessage(ChatColor.DARK_GRAY+"["+ChatColor.DARK_AQUA+"ML"+ChatColor.DARK_GRAY+"] "+ChatColor.GRAY+"Game starting in "+ChatColor.DARK_GRAY+"["+ChatColor.DARK_AQUA+"60"+ChatColor.DARK_GRAY+"] "+ChatColor.GRAY+"seconds");
    24.  
    25. plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable(){ //START OF YELLOW WARNINGS (DEPRICATED?)
    26.  
    27. public void run(){
    28. Bukkit.broadcastMessage("Debug 1");
    29. if(Main.countdown != -1){
    30. Bukkit.broadcastMessage("Debug 2");
    31. if(Main.countdown != 0){
    32. Bukkit.broadcastMessage(""+MinotaursLabyrinth.countdown);
    33. Main.countdown--;
    34. for(Player all : Bukkit.getServer().getOnlinePlayers()){
    35. BarAPI.setMessage(all, ChatColor.LIGHT_PURPLE + "Game Starting in " + ChatColor.DARK_GRAY +"["+ChatColor.DARK_AQUA+ MinotaursLabyrinth.countdown + ChatColor.DARK_GRAY+"]"+ChatColor.LIGHT_PURPLE+"seconds");
    36. BarAPI.setHealth(all, 100);
    37. }
    38.  
    39. }else{
    40. Bukkit.broadcastMessage(ChatColor.DARK_GRAY+"["+ChatColor.DARK_AQUA+"ML"+ChatColor.DARK_GRAY+"]"+ChatColor.GRAY+"The has begun");
    41. startGame.gameStart(); //This method is empty in other class, will fill it later
    42. Bukkit.broadcastMessage("Debug 3");
    43. Main.countdown--;
    44. }
    45. }
    46. }
    47.  
    48. }, 0L, 20L); //END OF YELLOW WARNINGS
    49.  
    50. }else{
    51. }
    52.  
    53. }
    54. }
    55.  


    I've indicated on the code where the one large yellow warning is in eclipse, telling me to add suppress warnings("Depricated").

    I also get an error in the console, saying that it could not pass PlayerJoinEvent to (plugin name, version)

    CubieX - I tried using .runTaskTimerAsynchrounously, the errors in eclipse go away, but it still throws a NPE

    mydeblob - You requested i post my code in the thread I seeked help for, owning to another user.
     
  2. Offline

    kumpelblase2

    That's more like declared, not referenced. If you don't explicitly give it a value, it's just reasonable that you get an npe.
     
  3. Offline

    AaronL98


    Should this work?
    Code:java
    1. public Event(Main plugin){
    2. this.plugin = plugin;
    3. }

    The this.plugin has an error "create field 'plugin' in type Event"
     
  4. Offline

    kumpelblase2

    AaronL98 When you remove the static modifier from plugin, then yes.
     
  5. Offline

    AaronL98

    kumpelblase2 Hmm. Struggling to understand what you mean by "When you remove the static modifier from plugin, then yes." I don't have the static modifier in that code.
     
  6. Offline

    kumpelblase2

    AaronL98 Not in that, but in your original one. The code you gave later does only work when you remove the static modifier in the original code.
     
Thread Status:
Not open for further replies.

Share This Page