Solved Timing out because of loop

Discussion in 'Plugin Help/Development/Requests' started by tytwining, Jan 2, 2015.

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

    tytwining

    So, what I'm trying to do is have a countdown clock. That's all. I then look at something online and found this simple thing:
    Code:
    private void startTimer(int StartNumber,boolean Countdown,String lobbyN) {
            AtomicInteger i = new AtomicInteger(StartNumber);
            sendChannelMsg(lobbyN,ChatColor.GOLD + "Starting in " + ChatColor.YELLOW + i.get());
            for(;;) {
                if(i.decrementAndGet() <= 0) {
                    sendChannelMsg(lobbyN,ChatColor.GOLD + "Starting!");
                    break;
                }
                if(i.get() <= 5 && Countdown) {
                    sendChannelMsg(lobbyN,ChatColor.GOLD + "Starting in " + ChatColor.YELLOW + i.get());
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        
    And it works. Sortof, not at all. Right when it initializes, it instantly times out my server. How can I fix this so it can create 0 lag because it is possible for 20 or more of these to run at once?
     
  2. Offline

    teej107

  3. Offline

    tytwining

    Thanks, but another problem that has come up is that creating a new class means that I cannot get variables and the config. How can I fix this?
     
  4. Offline

    teej107

    Why can't you get variables from the config when you create a class?
     
  5. Offline

    tytwining

    Because it doesn't extend JavaPlugin...? getConfig() is not found.
     
  6. Offline

    teej107

  7. Offline

    tytwining

    I'm not exactly understanding that. What I need to do is get a method from the Main class to the LobbyTimer class which I cannot seem to do.
     
  8. Offline

    teej107

  9. Offline

    tytwining

    @teej107
    Okay... So I seem to have done everything so far, and it's all working mostly correctly. It still times out though! I'm using the scheduler thing, but it doesn't seem to fix my problem. Here's the code:

    Code:
    package com.google.twiningtyler.cah;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    public class TimeKeeper extends BukkitRunnable {
        private final JavaPlugin plugin;
        private final String lobbyName;
        private int counter = 300;
      
        private void sendChannelMsg(String channel, String message) {
            Player[] players = Bukkit.getOnlinePlayers();
            for(Player i : players) {
                if(plugin.getConfig().getString("channels." + i.getUniqueId().toString()).equalsIgnoreCase(channel)) {
                    i.sendMessage(message);
                }
            }
        }
      
        public TimeKeeper(JavaPlugin plugin,String lobbyName) {
            this.plugin = plugin;
            this.lobbyName = lobbyName;
        }
      
      
        @Override
        public void run() {
            sendChannelMsg(lobbyName,ChatColor.YELLOW + "Starting the game in " + counter + " seconds.");
            for(;;) {
                if(counter >= 0) {
                    counter--;
                    plugin.getLogger().info(String.valueOf(300-counter));
                }else{
                    //break
                    this.cancel();
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    And I'm calling this with this:

    Code:
    BukkitTask task = (BukkitTask) new TimeKeeper(JavaPlugin.getPlugin(this.getClass()),lName).runTask(JavaPlugin.getPlugin(this.getClass()));
     
  10. Offline

    teej107

    @tytwining Don't ever use Thread.sleep(). Ever! Bukkit Schedulers already use delays if you set it up right.
     
  11. Offline

    tytwining

    @teej107 How am I supposed to add a delay then?

    EDIT: I've gotten everything to work, but there is for some reason a pretty big delay between starting the run the timer for when it actually starts and I can't find why it's doing it.

    The code:
    <the code>

    That is being run by this:
    <the code>

    EDIT: I got it all working! Thanks so much @teej107
     
    Last edited: Jan 3, 2015
Thread Status:
Not open for further replies.

Share This Page