Why does my /time command lag?

Discussion in 'Plugin Development' started by Appljuze, Nov 8, 2012.

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

    Appljuze

    I'm writing a simple weather and time plugin, and for some reason, when I do /time day, or /time night, the entire server freezes for a second or two, then the time changes. It's like it lags. Here's my code... perhaps I did something wrong?
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("time")){
                if(args.length == 1){
                    if(args[0].equalsIgnoreCase("day")){
                        Bukkit.broadcastMessage(ChatColor.GREEN + player.getName() + " has changed the time to " + ChatColor.YELLOW + "day");
                        player.getWorld().setTime(500);
                        return true;
                    }else if(args[0].equalsIgnoreCase("night")){
                        Bukkit.broadcastMessage(ChatColor.GREEN + player.getName() + " has changed the time to " + ChatColor.YELLOW + "night");
                        player.getWorld().setTime(14000);
                        return true;
                    }else if(args[0].equalsIgnoreCase("midday")){
                        Bukkit.broadcastMessage(ChatColor.GREEN + player.getName() + " has changed the time to " + ChatColor.YELLOW + "mid-day");
                        player.getWorld().setTime(6000);
                        return true;
                    }else
                        time = Integer.parseInt(args[0]);
                        Bukkit.broadcastMessage(ChatColor.GREEN + player.getName() + " has changed the time to " + ChatColor.YELLOW + args[0] + " ticks");
                        player.getWorld().setTime(time);
                        return true;
                    }
                else if(args.length == 0){
                    return false;
                }
            }
     
  2. Offline

    cman1885

    Appljuze you gave us time commands.
     
  3. Offline

    Appljuze

    Oh damn, my bad. I meant time, not weather. I'll change the thread title.
     
  4. Offline

    hutattedonmyarm

    I'm doing it (basically) the same way as you do (but without the broadcast) and it works just fine... What other plugins are on your testserver?
    Also, put this:
    Code:
    time = Integer.parseInt(args[0]);
    in a try-catch-block...

    Code:
    try {
        p.getWorld().setTime(Integer.parseInt(args[1]));
        p.sendMessage(ChatColor.BLUE + "Time in world " + p.getWorld().getName() + " is now " + p.getWorld().getTime() + " ticks");
    } catch (Exception e) {
          return false;
    }
    My code, and it works perfectly...
     
  5. Offline

    Appljuze

    I have zero other plugins on the server. I never put it in a try-catch block, let me try that.
     
  6. Offline

    thehutch

    A few things to improve your plugin:

    1. check that the CommandSender is a Player before casting, in case the console changes it and you get a ClassCastException.
    2. No need for the "else if (args.length == 0) { return false }" Completely unnecessary because the method has already finished.
    3. You will need a try/catch around the Integer.parseInt(args[0]) because otherwise if the type anything other than those other 3 statements for example "dawn" the it'll try to convert "dawn" to a number which clearly isn't possible :D
    Also this "lag" could be because your computer has a bad graphics card and a long takes time to render all the light again.
     
  7. Offline

    Appljuze

    It's not my graphics card because 1) I just recently bought a new one and 2) It doesn't lag with the essentials time/weather setting. But thank you for pointing those couple things out, I'll fix them.
     
  8. Offline

    zeeveener

    When you say yours lags, but Essentials doesn't what do you mean?
    What sort of differences do you notice?
     
  9. Offline

    hutattedonmyarm

  10. Offline

    md_5

    Is the server actually lagging? Or just the time it takes for the change to appear?
    Try using /time and then chatting really quickly.
     
  11. Offline

    hutattedonmyarm

    Isn't chatting asynchronously?
    Might be better to try the command and then place a block quicly..
     
  12. blocks are rendered directly on the client when its placed by the client
    he can try adding a test plugin, that says "no lagg" when he right clicks a block
     
  13. Offline

    Appljuze

    Figured out the problem. I was missing curly braces on one of the lines in the /weather command, so it was messing up something. I don't understand how, but when I fixed it the lag went away. Thanks anyway for all of your guys' help.
     
    hutattedonmyarm likes this.
Thread Status:
Not open for further replies.

Share This Page