Scheduling an event specific time per day

Discussion in 'Plugin Development' started by flash1110, Feb 16, 2015.

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

    flash1110

    Basically, I have a koth plugin, and I want a koth event to happen at a couple specific hours per day. I looked this up, and found a thread about this on this very website, however when I tried to use it I got an error. So, basically I would really love someone to explain this to me. The code that was on the thread is listed down below.

    Error:
    Code:
    [11:05:52 ERROR]: Error occurred while enabling TXKoth v1.0 (Is it up to date?)
    java.lang.IllegalArgumentException: Plugin cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:203) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.validate(CraftScheduler.java:391) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:120) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.scheduleSyncRepeatingTask(CraftScheduler.java:116) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
        at me.flash1110.txplugin.TimeScheduler.scheduleRepeatAtTime(TimeScheduler.java:28) ~[?:?]
        at me.flash1110.txplugin.StartKoth.startAtSevenA(StartKoth.java:18) ~[?:?]
        at me.flash1110.txplugin.Main.onEnable(Main.java:16) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:324) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugin(CraftServer.java:455) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.enablePlugins(CraftServer.java:389) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.n(MinecraftServer.java:352) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.g(MinecraftServer.java:326) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.a(MinecraftServer.java:282) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.init(DedicatedServer.java:182) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:436) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    TimeScheduler class
    Code:
    public class TimeScheduler {
       
        static long ticks;
       
        public static int scheduleRepeatAtTime(Plugin plugin, Runnable task, int hour) {
            Calendar cal = Calendar.getInstance();
           
            long now = cal.getTimeInMillis();
           
            if (cal.get(Calendar.HOUR_OF_DAY ) >= hour) {
                cal.add(Calendar.DATE, 1);
                cal.set(Calendar.HOUR_OF_DAY, hour);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MILLISECOND, 0);
               
                long offset = now - System.currentTimeMillis();
                ticks = offset / 50L;
            
            }
              return Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, task, ticks, 1728000L);
        }
    StartKoth class
    Code:
    public static void startAtMidnight() {
            TimeScheduler.scheduleRepeatAtTime(Main.getPlugin(), new Runnable() {
                public void run() {
                    Bukkit.broadcastMessage(CaptureKoth.prefix + ChatColor.GREEN + "The Koth is now open to be captured");
                    CaptureKoth.isStopped = false;
                }
            }, 0);
        }
       
        public static void startAtSevenA() {
            TimeScheduler.scheduleRepeatAtTime(Main.getPlugin(), new Runnable() {
                public void run() {
                    Bukkit.broadcastMessage(CaptureKoth.prefix + ChatColor.GREEN + "The Koth is now open to be captured");
                    CaptureKoth.isStopped = false;
                }
            }, 7);
        }
       
        public static void startAtTenA() {
            TimeScheduler.scheduleRepeatAtTime(Main.getPlugin(), new Runnable() {
                public void run() {
                    Bukkit.broadcastMessage(CaptureKoth.prefix + ChatColor.GREEN + "The Koth is now open to be captured");
                    CaptureKoth.isStopped = false;
                }
            }, 10);
        }
       
        public static void startAtNoon() {
            TimeScheduler.scheduleRepeatAtTime(Main.getPlugin(), new Runnable() {
                public void run() {
                    Bukkit.broadcastMessage(CaptureKoth.prefix + ChatColor.GREEN + "The Koth is now open to be captured");
                    CaptureKoth.isStopped = false;
                }
            }, 12);
        }
       
        public static void startAtSevenP() {
            TimeScheduler.scheduleRepeatAtTime(Main.getPlugin(), new Runnable() {
                public void run() {
                    CaptureKoth.isStopped = false;
                }
            }, 19);
        }
    onEnable
    Code:
    public void onEnable() {
           
            StartKoth.startAtSevenA();
            StartKoth.startAtMidnight();
            StartKoth.startAtNoon();
            StartKoth.startAtSevenP();
            StartKoth.startAtTenA();
            CaptureKoth.isStopped = true;
        }
     
  2. Offline

    WinX64

    You're passing null as the plugin when scheduling your task. Are you sure Main.getPlugin() isn't returning null?
     
  3. Offline

    flash1110

    @WinX64
    Due to your statement, I have figured it out. In the onEnable, I'm using "plugin = this," however I'm calling the methods before that. Thanks so much.
     
  4. Offline

    AoH_Ruthless

    Why do you have 5 methods doing the same exact thing? Why not have one method and pass a parameter for the time you want it to start? The only difference is the the integer hour you are using.

    Additionally, your issue stems from a null plugin value. Frankly, I would avoid using static modifiers for this kind of thing and start making use of a constructor if I were you.
     
  5. Offline

    flash1110

    @AoH_Ruthless
    I changed it to one method. Now, the method is activating onEnable, instead of waiting for the correct time.
     
Thread Status:
Not open for further replies.

Share This Page