Help with countdown

Discussion in 'Plugin Development' started by moomann, Aug 21, 2014.

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

    moomann

    Im making a groundclear plugin

    I want to have a ten minutes timer which will stop using /stopcleartimer or after ten minutes. Halftime there must be a message and a call to a function.
    I searched for solutions, but i have no idea how to start this,
    I hope some one can help me,
    Thanks
     
  2. Offline

    Wingzzz

    Create a runnable that will repeat every X ticks and then compare System.currentTimeMillis() to see if the specified amount of time has actually passed on the machine. If so, do whatever you wish- otherwise do nothing.
     
  3. Offline

    moomann

    thanks but I didn't see your reply before.
    so, after some more searching I found a code.
    the timer code I put in my Clearcountdown.java
    and the command /startcleartimer I put in my Clearcommands.java

    Clearcountdown.java:
    Code:java
    1. public void clearTimer()
    2. {
    3. Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable()
    4. {
    5. @Override
    6. public void run()
    7. {
    8. Bukkit.broadcastMessage("test");
    9. }
    10. }
    11. , 20, 20);
    12. }

    Clearcommand.java
    Code:java
    1. if(cmd.getLabel().equalsIgnoreCase("startcleartimer")){
    2. Bukkit.broadcastMessage("Clearing started");
    3. Clearcountdown.clearTimer();
    4. }


    but then I get the error:
    Code:
    non-static method clearTimer() cannot be referenced from a static context
    anyone knows how to fix it
     
  4. the method "Timer()" is not static. The method that is calling "Timer()" is static. This can't work. Both must be static or both must be not static to be able to call another. So set the other one non-static or set "Timer()" static. Also, where is your method "Timer()" and where are you calling it? can you show me the code pls?
     
  5. Offline

    moomann

    I didn't copy the error, I typed it in. I made a mistake with typing so Timer(); = clearTimer();

    making clearTimer(); static won't work because getServer(); isn't static, how do I do that then :confused:
     
  6. Offline

    Konkz

    Wingzzz Wow. What are you doing on these grounds once again? ;3
     
  7. moomann the code you showed to us, is it the whole? just whithout the class bodys? because i would like to see all of it. i cant even see somewhere a "static"
     
  8. Offline

    moomann

    Code:java
    1.  
    2. import org.bukkit.Bukkit;
    3.  
    4. public class Clearcountdown {
    5. public void clearTimer()
    6. {
    7. Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable()
    8. {
    9. @Override
    10. public void run()
    11. {
    12. Bukkit.broadcastMessage("test");
    13. }
    14. }
    15. , 20, 20);
    16. }
    17. }
    18.  

    and commands,

    Code:java
    1. import org.bukkit.command.Command;
    2. import org.bukkit.command.CommandExecutor;
    3. import org.bukkit.command.CommandSender;
    4.  
    5. public class Commands implements CommandExecutor{
    6. @Override
    7. public boolean onCommand(CommandSender sender, Command cmd, String Label, String [] args) {
    8. if(cmd.getLabel().equalsIgnoreCase("startcleartimer")){
    9. Bukkit.broadcastMessage("Clearing started");
    10. Clearcountdown.clearTimer();
    11. }
    12. }
     
  9. okay here is your error: you try to access clearTimer(); from the class without having an instance. you must make clearTimer(); static or you make an instance of the class Clearcountdown. I would rather do the second. looks like this:
    Code:java
    1. import org.bukkit.command.Command;
    2. import org.bukkit.command.CommandExecutor;
    3. import org.bukkit.command.CommandSender;
    4.  
    5. public class Commands implements CommandExecutor{
    6.  
    7. private Clearcountdown clearCountdown = new Clearcountdown();
    8.  
    9. @Override
    10. public boolean onCommand(CommandSender sender, Command cmd, String Label, String [] args) {
    11. if(cmd.getLabel().equalsIgnoreCase("startcleartimer")){
    12. Bukkit.broadcastMessage("Clearing started");
    13. clearCountdown.clearTimer();
    14. }
    15. }
     
  10. Offline

    moomann

    that gives me an error on "this"
    Code:
    incompatible types: clearTimer cannot be converted to Plugin
    I mean this "this"
    Code:java
    1. Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable()


    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  11. moomann with this you point at the object you are working with at the moment. in this case you are pointing at the instance of the class you are calling "this" in. this class does not extend Plugin and so it can't be used as the first Parameter, because the method want's scheduleAsyncRepeatingTask(Plugin, Runnable). you must use the instance of a class which is a Plugin!!
     
  12. Offline

    coasterman10

    Here's my first piece of advice: do NOT create any other classes unless you actually are making a new object! In Java, code is broken into files by objects they represent, NOT by functionality.

    A good tutorial on scheduling is here: wiki.bukkit.org/Scheduler_Programming. I would recommend using BukkitRunnable, by either defining a new class to extend it, or using an anonymous declaration inline with the calling code.
     
Thread Status:
Not open for further replies.

Share This Page