Solved Scheduler Error while inside onPlayerJoin

Discussion in 'Plugin Development' started by AaronL98, Jul 23, 2013.

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

    AaronL98

    Code:java
    1.  
    2. public int joinTimer = 30;
    3. @EventHandler
    4. public void onPlayerJoin(PlayerJoinEvent event){
    5. Bukkit.broadcastMessage(ChatColor.DARK_AQUA + "" + event.getPlayer().getName() + ChatColor.AQUA + " has joined the game");
    6. if(Bukkit.getOnlinePlayers().length == Bukkit.getMaxPlayers()){
    7.  
    8. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    9.  
    10. public void run() {
    11. if(joinTimer != -1){
    12. if(joinTimer != 0){
    13. joinTimer--;
    14. for(Player allPlayers : Bukkit.getServer().getOnlinePlayers())
    15. {
    16. Location allLoc = allPlayers.getLocation();
    17. allPlayers.setTotalExperience(joinTimer);
    18. allPlayers.playSound(allLoc, Sound.ORB_PICKUP, 1, 10);
    19. }
    20.  
    21. }else{
    22. joinTimer--;
    23. }
    24. }
    25. }
    26. }, 0L, 20L);
    27. }


    I was wondering if i could get some help here. What i want done is once there is 5 players in the server (5 is the player limit on server.properties) your XP level would be set to 30 and decrease 1 every second (countdown feature using XP levels) and when it reached 0 the game would start, but i'm getting an error as soon as the 5th person joins the game saying 'Could not pass event PlayerJoinEvent to PluginName v 1.0'

    EDIT: I forgot to mention that all this code is in a different class from my main.
    Class name: EventListener

    I would really love some help on this.

    -Aaron

    This is pretty urgent :l

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  2. Offline

    soulofw0lf

    1. int joinTimer = 30;
      needs to be within the scope of your runnable

    and something like this might be a bit cleaner
    Code:
    @EventHandler
    public void playerLogin(PlayerJoinEvent event){
    //messages and stuff to send
    playerChecker();
    }
    public void playerChecker(){
    int i = 0;
    for (Player p : Bukkit.getOnlinePlayers()){
    i++;
    if (i >= 5){
    playerCountdown():
    return;
    }
    }
    public void playerCountdown(){
    new BukkitRunnable(){
    int timer = 30;
    @Override
    public void run(){
    //code
    timer--;
    }
    }.runTaskTimer(plugin, 0, 20);
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  3. Offline

    AaronL98

    Im still getting the same exact error and i moved my int:

    Code:java
    1. public void onPlayerJoin(PlayerJoinEvent event){
    2. Bukkit.broadcastMessage(ChatColor.DARK_AQUA + "" + event.getPlayer().getName() + ChatColor.AQUA + " has joined the game");
    3. if(Bukkit.getOnlinePlayers().length == Bukkit.getMaxPlayers()){
    4.  
    5. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    6.  
    7. public void run() {
    8. int joinTimer = 30;
    9. if(joinTimer != -1){
    10. if(joinTimer != 0){
    11. joinTimer--;
    12. for(Player allPlayers : Bukkit.getServer().getOnlinePlayers())
    13. {
    14. Location allLoc = allPlayers.getLocation();
    15. allPlayers.setTotalExperience(joinTimer);
    16. allPlayers.playSound(allLoc, Sound.ORB_PICKUP, 1, 10);
    17. }
    18.  
    19. }else{
    20. joinTimer--;
    21. }
    22. }
    23. }
    24. }, 0L, 20L);
    25. }
     
  4. AaronL98
    What is the cause of this error?
     
  5. Offline

    CubieX

    The whole stack trace of the error would be handy.
     
  6. Offline

    AaronL98

    Assist I don't really know why its happening but in my log I'm getting errors about my scheduler and 'Could not pass event playerJoinEvent to PluginName v1.0' etc

    http://pastie.org/private/1xnipodjgq5u2ih5yezicg
    Thats the error i get once the server is full and the countdown is meant to start.

    any luck?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  7. Offline

    Paxination

    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {

    That line, plugin is null. Setup your instance to your main class correctly....one sec I will edit this post!

    EDIT

    Code:java
    1. private PLUGINNAME plugin;
    2.  
    3. public EventListener(PLUGINNAME plugin){
    4. this.plugin = plugin;
    5. }


    You need that right after your class definition. First one is a reference to your plugins main class so you can access methods from it, the second is a Constructor that passes the instance of your main class to your listener!

    You will also have to import your plugins main class in the listener, and import bukkits plugin class as well.
     
  8. Offline

    AaronL98

    Code:java
    1. Bukkit.getPluginManager().registerEvents(new EventListener(), this);


    That code has an error when implementing the code you gave me, its in my on enable.

    Thanks, the scheduler seems to work now, but the players xp level isnt being set to the int joinTimer, but all players can hear the Sound.ORBPICKUP ticking.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  9. Offline

    Paxination

    Its int joinTimer = 30;

    Its in the wrong spot. Put it at the begining of your class definition, like after the code I posted. and dont make it public either. That is your problem, each iteration of your runnable your resetting joinTimer to 30 again.

    Yeah i think he wants the levels to change for a VISUAL count down timer.

    You could always just broadcastmessage a countdown timer.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  10. Offline

    AaronL98

    Thanks guys ;) Worked perfectly. I dedicated to go for the visual XP level countdown cause its something different ;)
     
Thread Status:
Not open for further replies.

Share This Page