Timer

Discussion in 'Plugin Development' started by CrazyISADev, Jan 6, 2015.

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

    CrazyISADev

    Hello I don't know whats up with me today but I can't make timers today. I've tried all my previous timers and they don't work in a Boolean I need a 10 minute timer that counts down to 5 min then broadcasts a message and hits 1 min and broadcasts a message and so on down to 30 seconds then 10,9,8,7,6,5,4,3,2,1 then a starter message

    Any can help
     
    Last edited by a moderator: Jan 6, 2015
  2. Offline

    mythbusterma

    @CrazyISADev

    Run a repeating task every second, send a message at those particular seconds.
     
  3. Offline

    CrazyISADev

    @mythbusterma mind giving me an example of this?
     
  4. Offline

    BlazingBroGamer

  5. Offline

    teej107

  6. You first need a runnable :
    Code:
    HashMap<String, Integer> timer = new HashMap<String, Integer>;
    
    public void setTime(Integer time) {
    timer.set("timer", time; }
    
    //runnable at 20  {
    ArrayList<String> list = Arrays.asList("300", "5", "4", "3" ,"2" ,"1");
    int time = timer.get("timer");
    
    if(time >= 1 {
    setTime(time-1);
    for(String listtosinglestring : list) {
    int broadcasttime = Intenger.valueOf(timestring);
    if(time == broadcasttime) {
             Bukkit.broadcastMessage(String.valueOf(time)+" seconds remaining.");
    } } } else {
         //If the timer it's at zero what do you wanna do ? you can reset the timer with :
         setTime(600);
    }
    }
    
    How to start the timer ? a little example :
    Code:
    
    @Override
    public boolean onCommand(CommandSender sender, Command cmds, String cmd, String[] args) {
    if(cmd.equalsIgnoreCase(“start”) {
    setTimer(600);
    sender.sendMessage(“The timer has been started”);
    return true;
    }
    }
    
    Code not texted, i did write this at school if something is not working just reply this post and i'll help you when i get on home .
     
    Last edited: Jan 7, 2015
  7. Offline

    nverdier

    @MaTaMoR_ He was given a useful link just so he could learn on his own. Please don't spoonfeed, it helps nobody learn anything.
     
  8. Offline

    1Rogue

    If you're using bukkit's scheduler to create a timer, you're doing it wrong.

    I do have a helper class in my library you could use, but I haven't finished fully testing it.

    Code:java
    1. new Countdown()
    2. .announceAtRange(1, 10, TimeUnit.SECONDS)
    3. .announceAt(1, TimeUnit.MINUTES)
    4. .announceAt(5, TimeUnit.MINUTES)
    5. .announceAt(10, TimeUnit.MINUTES)
    6. .setAnnouncementFormat("There are %s remaining until shutdown!")
    7. .start(300, () -> Bukkit.getServer().shutdown()); //Example: runs for 5 minutes and shuts down server


    (Though the real beauty for this class is being able to dynamically add a timer to a scoreboard):

    Code:java
    1. //Prereq: s1, s2, s3 are all type org.bukkit.scoreboard.Scoreboard
    2. new Countdown()
    3. .assignScoreboard(s1) //Add a timer to one scoreboard
    4. .assignScoreboard(s2) //Another!
    5. .assignScoreboard(s3, "Example title - %d:%s") //Add to a third with a custom title
    6. .setDefaultFormat("Normal - %d:%d:%s") //Set the title for boards that didn't specify
    7. .start(300, () -> Bukkit.getServer().shutdown());


    However if you're making a countdown timer, I would actually recommend against bukkit's scheduler as stated earlier. It is tick-based rather than realtime, and if the server slows down so will your timer. You should create a ScheduledExecutorService yourself (or conveniently, my lib also has a Scheduler class which is realtime based: )

    Code:java
    1. Scheduler.runAsyncTask(/* your runnable */, /* delay to start */, /* interval */);


    If you make your own timer, I recommend running at a half-second interval and checking for changes in System.nanotime() for accuracy.
     
    teej107 likes this.
  9. Christmas gift :)

    PS: Happy meal
     
  10. Offline

    teej107

    Looks like a lot more than a Runnable to me. I see Maps, Lists, Strings, oh my! The only thing that I don't see are the explanations.

    @1Rogue That looks like a really useful class!
     
  11. Offline

    ColonelHedgehog

    That looks nice, but are those lambda expressions? Aren't those only supported in Java 1.8?
     
  12. Offline

    1Rogue

    Well correct, but it accepts a Runnable (which is what () -> specifies in Java 8). I wrote it that way for simplicity and because lambdas are awesome.
     
  13. I guess he already now what's a string, hashmap, etc...
     
  14. @MaTaMoR_ Maybe, but spoon-feeding is bad, especially when it's so full of wrong / bad practice such as yours. :)

    Where are the access modifiers?

    Why are you taking the wrapper as parameter rather than the primitive? Doing this provides no real benefit, other than the fact you can now cause NPEs if you feel like it by passing null.

    What's the point in having a Map that can only ever store one K-V pair?

    I have absolutely no idea what "runnable at 20" means and this is precisely what the OP was asking about.

    Any reason this is a List as opposed to, say, an array? Also, program to an interface, not an implementation: It should be declared as List, not ArrayList.

    Oh, I was mistaken. You wouldn't need to pass null to cause an NPE, you'd just need to forget to call the setTime method. Honestly, why not force the method to be run? (They're called constructors ;))

    Compilation error.

    Oh that's what you were using the List for. In that case, why is it not a list of Integers? (Or an array of ints)

    You know, Lists have a contains() check. If you're gonna use a List, might as well use the method available.

    There's really no need for the String.valueOf() - you're appending a String right after it, which will automatically convert it for you.

    Excellent! I've now completely lost what's been closed and what hasn't been.

    So as you can see your code has some problems. Please don't spoon-feed, it's not helpful, it's harmful, and nobody learns anything. :)
     
  15. Offline

    1Rogue

    You missed like 40 things 2/10 terrible code review. ( ;p )

    Some more particular things since we're picking apart bad things in code:

    Code:java
    1. ArrayList<String> list = Arrays.asList("300", "5", "4", "3" ,"2" ,"1");


    Arrays#asList does not actually return java.util.ArrayList but an AbstractList implementation named "ArrayList" as well which is fixed-size. If you modify size by adding/removing, you will throw an exception.

    It's generally accepted to wrap the return value in an ArrayList yourself:

    Code:java
    1. List<String> list = new ArrayList<>(Arrays.asList(/* ... */));


    ...which leads into the next point that you shouldn't store variables as concrete implementations but rather as their supertype, so that other plausible implementations can also be used in place of them. (List<String> at the start, instead of ArrayList)

    Code:java
    1. for(String listtosinglestring : list) {
    2. int broadcasttime = Intenger.valueOf(timestring);


    Where did timestring come from?
    What happened to using listtosinglestring?

    (Why not use listToSingleString and make reading things easy? Or name it appropriately e.g. "String timePoint")
    Code:java
    1. public void setTime(Integer time) {
    2. timer.set("timer", time; }


    This wouldn't even compile. Additionally, why do you end your entire method randomly at the end of a line of code? If I were to rewrite this cleanly:

    Code:java
    1. public void setTime(Integer time) {
    2. timer.set("timer", time);
    3. }


    Though I don't really see the point of the entire map now looking at this method. You could just have an int field.

    @MaTaMoR_

    I understand you wrote it at school, but that doesn't provide rhyme or reason for completely abandoning any concept of a coding standard.
     
    AdamQpzm likes this.
  16. 1) that's a mistake i can say anything else.
    2) first i named the two variables listtosinglestring but after i changed the name from one and i did forget to fix the another one .
    3) that's just a space...
    4) i wrote the whole code at school pc ... i don't know where you live but the school pc here really sucks... i just wanted to help y never try to write a perfect code, reply me when you use a pc what crashes every minute .
     
    Last edited: Jan 7, 2015
  17. What do you mean? I pointed out a compilation error, and so did @1Rogue. It's literally impossible for code with a compilation error to "work". And no, that's not an opinion, that's a fact. You're also missing a very fundamental thing in programming - just because something works, it doesn't mean that you should be doing it that way. For example, instead of writing a String normally I could make a method that would convert it from binary and set their values that way. Assuming I do it right, will it work? Yes it will. But should I be doing it like that? No, of course I shouldn't.

    Aside from that and thinking more about the bad practices, you should care about what I think. I'm offering you constructive feedback on some things that you're doing wrong, so that you're able to improve your skills and learn from your mistakes. Not everyone gets those opportunities, so you should make use of them when they come along. Everybody makes mistakes and it's not something you should be ashamed of. Wasting an opportunity to learn and improve from your mistakes is something to be ashamed of.

    Of course, you're entitled to completely ignore the things I've said to you, insist that you don't care what I think, and continue on as you are without taking the feedback on board. But be honest here: Does that harm me, or you?
     
    TheOatBaron and Burnett like this.
  18. Offline

    TheOatBaron

    I highly disagree. True, it doesn't help anyone that doesn't want to learn. To visual learners that want to learn, it means the world because

    a) You see exactly what you need to be implemented
    b) You can tweak it and see the results
    c) Learn different parts of the API and how they can be applied.
     
  19. @TheOatBaron There's a clear difference between spoon-feeding and something that helps visual learners, however. I can say that I am certain that his post should not be classified as helpful. It has compilation errors, it has bad practices, it doesn't attempt to address the question asked by the OP, and it offers no explanation whatsoever. It is not a useful post to help visual learners. It's a spoon-feed attempt.

    As for actual examples, there are plenty on the linked page. They are the source to learn from.
     
  20. Offline

    mythbusterma

    @TheOatBaron

    You claim that they need examples to learn from, that's all well and good. A quick look at the fourth post on this page, before the dreadful spoon-feeding:

    Just a quick excerpt from that page:

    Oh, would you look at that. That's absolutely amazing, there's examples on there that DON'T have awful programming practises in them. Who would've thought that they actually knew how to write good examples when they made the Bukkit tutorials.

    R.I.P. your argument.
     
    AdamQpzm likes this.
  21.  
  22. Offline

    nverdier

    1) People just copy and paste. They rarely read it, and when they do, they don't fully understand it.
    2) You can do that if you write your own code as well. The only difference is they have no idea what to write when spoon-fed. Also, you're "tweaking" others code, and claiming it as your own.
    3) You can simply read the JavaDocs.
     
Thread Status:
Not open for further replies.

Share This Page