Run fn every 60 minutes, also, how can I loop this?

Discussion in 'Plugin Development' started by Plazmotech, May 24, 2012.

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

    Plazmotech

    I need a simple way to call a function every 60 minutes. How can I do this? And this is what I have:

    package com.webs.playsoulcraft.plazmotech.java.MineRegen;

    import java.util.logging.Logger;

    import org.bukkit.Location;
    import org.bukkit.block.Block;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;


    public class Main extends JavaPlugin{

    public final Logger log = Logger.getLogger("Minecraft");


    @Override
    public void onEnable() {
    this.log.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    this.log.info("Plaz's Mine Regen is now enabled!");
    this.log.info("Copyright 2012 Plazmotech Co. All rights reserved.");
    this.log.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    }

    @Override
    public void onDisable() {
    this.log.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    this.log.info("Plaz's Mine Regen is now disabled!");
    this.log.info("Copyright 2012 Plazmotech Co. All rights reserved.");
    this.log.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    }

    public void onPlayerInteract(PlayerInteractEvent event) {
    final Action action = event.getAction();
    if (action == Action.LEFT_CLICK_BLOCK) {
    Location l1 = event.getClickedBlock().getLocation();
    } else if (action == Action.RIGHT_CLICK_BLOCK) {
    Location l2 = event.getClickedBlock().getLocation();
    }
    }
    }

    I need to run a function I will implement every hour, how? Remember: The function will use l1, and l2. Also, how can I loop this to get every block inbetween?
     
  2. From http://wiki.bukkit.org/Plugin_Tutorial you get to http://wiki.bukkit.org/Scheduler_Programming and there you have enough examples how to make any task.

    You should make your locations as global if you want to use them in another method.... define them at the begining of the code.... and if you want to get blocks between them, you'll have to first determine which is smaller and then make 3 loops for X, Y and Z axis with starting values from the smaller to the values of the biggest.
     
  3. Offline

    Wundark

    Under onEnable add this.

    The time is worked out like this:
    1 second = 20 ticks
    60 seconds = 1 min
    60 mins = 1 hour

    20 * 60 * 60 = 72000
     
  4. Offline

    desht

    Since the regeneration code will presumably be replacing blocks in the world, you can't use an async task. scheduleSyncRepeatingTask() is the method needed here.
     
    Father Of Time likes this.
  5. Offline

    Father Of Time

    Also, I wouldn't use a timer that is on a big giant 60 minute timer, but rather make a Date variable and have a timer that ticks once a minute, and every minute check that date field to see if it's been 60 mins since the last action was taken.

    Doing it this way makes the system not care about server down time, if the server shuts down with 40 minutes on the clock it will resume at the 40 minutes on reload (assuming you incorperate some level of persistance); but if you have one big looping timer and do a /reload or restart the server the timer will be lost and you will have to start counting down from 60 minutes again.

    Personally once a timer loop starts to exceed 30 minutes I start looking into whether a date field would be more benificial than a giant loop (or a long variable works too).

    Just food for thought, take care!
     
    desht likes this.
Thread Status:
Not open for further replies.

Share This Page