[SOLVED] How to check if it is day or night?

Discussion in 'Plugin Development' started by Tim Visee, Apr 9, 2011.

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

    Tim Visee

    Hello,

    I'm writing a plugin and I want to know with a command if it is day or night, can anyone help me please?
    If an event that runs if the day or night changes, I would also like this to have.

    Thanks!

    Tim Visée

    --------------------------------------------------

    Solution:
    Underneeth you can find the solution for my problem;

    Note: Don't forget to change the world name 'world' to the world where you want to get the time from.

    Code:
    public boolean day() {
        Server server = getServer();
        long time = server.getWorld("world").getTime();
    
        return time < 12300 || time > 23850;
    }
    If you want to get the world name from a player that is in that world use this;
    Code:
    String WorldName = player.getServer().getName();
     
    Cryptite and krazyswaggaO like this.
  2. Offline

    Sammy

    Code:
    .getServer().getWorld("name").getTime();
    ;)
     
  3. Offline

    Tim Visee

    Thanks :)

    I've made a function that will return if it's day or night. The function return night when it gets dark.
    (It is already night when time is 12000 but then it isn't dark)

    Don't forget to change the world name to the name of your world!
    Code:
    public boolean day() {
        Server server = getServer();
        long time = server.getWorld("world").getTime();
    
        if(time > 0 && time < 12300) {
            return true;
        } else {
            return false;
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
  4. Offline

    xupwup

    FTFY
     
  5. Offline

    Tim Visee

    Yes, that's the same thing... (Only a little bit shorter)

    Updated code,
    (I forgot to add 'if(time > 23850)', and i've made the code shorter with the trick of xupwup

    Code:
    public boolean day() {
        Server server = getServer();
        long time = server.getWorld("world").getTime();
    
        return time < 12300 || time > 23850;
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
  6. Offline

    xupwup

    If I am not mistaken, the time keeps on counting, so after day two, the time is 48000. If so, you need to change your code a little bit, see above.
     
  7. Offline

    Tim Visee

    No it isn't i've made a plugin that show's the time for me, every day the time starts again at zero.
     
  8. Offline

    Sammy

    you shouldn't use "world" as the world name, make it variable or go get it from a player, not everyone calls it "world"
     
  9. Offline

    Tim Visee

    So this is the solution;

    Don't forget to change the world name 'world' to the world where you want to get the time from.

    Code:
    public boolean day() {
        Server server = getServer();
        long time = server.getWorld("world").getTime();
    
        return time < 12300 || time > 23850;
    }
     
  10. Offline

    Sammy

    Still not good enough man ^^use this:
    Code:
    String WorldName = ply.getServer().getName();
    this way you will know the WorldName where the player is located
     
  11. Offline

    Tim Visee

    But it's not for users, it's for a timer that check if it's day or night in the plugin.
    But i added this metoth in the first message of this threat
     
  12. Offline

    Sammy

    But that way you are making a plugin that will only work if the server as that world name... you can't distribute a plugin with that handicap imo
     
  13. Offline

    Tim Visee

    Yes, but i didn't get if by a player but by a methoth that show me all te worlds in that server.
     
  14. Offline

    Sammy

    That works too :)
     
Thread Status:
Not open for further replies.

Share This Page