Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cron;
- import java.util.Calendar;
- import java.util.concurrent.ScheduledFuture;
- import java.util.logging.Logger;
- import com.l2jserver.gameserver.Announcements;
- import com.l2jserver.gameserver.ThreadPoolManager;
- import com.l2jserver.gameserver.util.Util;
- import events.Tournament.Tournament;
- import events.Tournament.TournamentManager;
- /**
- * @author denser
- */
- public class AutoEventTournament
- {
- public static final Logger _log = Logger.getLogger(AutoEventTournament.class.getName());
- public startTournament _task;
- public String[] EVENT_INTERVAL = { "23:59" };
- public int[] EVENT_INTERVAL_DAYS = { Calendar.WEDNESDAY };
- public AutoEventTournament()
- {
- if (Util.contains(EVENT_INTERVAL_DAYS, Calendar.DAY_OF_WEEK))
- {
- this.calculateEventStart();
- _log.info(getClass().getSimpleName() + ": Started.");
- }
- }
- private class startTournament implements Runnable
- {
- private long _startTime;
- public ScheduledFuture<?> nextRun;
- public startTournament(long startTime)
- {
- _startTime = startTime;
- }
- public void setStartTime(long startTime)
- {
- _startTime = startTime;
- }
- private long getTime()
- {
- long delay = Math.round((_startTime - System.currentTimeMillis()) / 1000L);
- return delay;
- }
- private void announce(long time)
- {
- Announcements announce = Announcements.getInstance();
- if (time >= 3600 && time % 3600 == 0)
- {
- if (!TournamentManager.getInstance().isRunning())
- announce.announceToAll(Tournament.EVENT_NAME + ": " + (time / 60 / 60) + " часов до начала! Спешите на регистрацию!");
- }
- else if (time >= 60)
- {
- if (!TournamentManager.getInstance().isRunning())
- announce.announceToAll(Tournament.EVENT_NAME + ": " + (time / 60) + " минут до начала! Спешите на регистрацию!");
- }
- else
- {
- if (!TournamentManager.getInstance().isRunning())
- announce.announceToAll(Tournament.EVENT_NAME + ": " + time + " секунд до начала! Спешите на регистрацию!");
- }
- }
- public void run()
- {
- int delay = Math.round((_startTime - System.currentTimeMillis()) / 1000F);
- if (delay > 0)
- this.announce(delay);
- int nextMsg = 0;
- if (delay > 3600)
- nextMsg = delay - 3600;
- else if (delay > 1800)
- nextMsg = delay - 1800;
- else if (delay > 900)
- nextMsg = delay - 900;
- else if (delay > 600)
- nextMsg = delay - 600;
- else if (delay > 300)
- nextMsg = delay - 300;
- else if (delay > 60)
- nextMsg = delay - 60;
- else if (delay > 5)
- nextMsg = delay - 5;
- else if (delay > 0)
- nextMsg = delay;
- else
- {
- //start
- TournamentManager.getInstance().start();
- }
- if (delay > 0)
- nextRun = ThreadPoolManager.getInstance().scheduleGeneral(this, nextMsg * 1000);
- }
- }
- public void calculateEventStart()
- {
- try
- {
- Calendar currentTime = Calendar.getInstance();
- Calendar nextStartTime = null;
- Calendar testStartTime = null;
- for (String timeOfDay : EVENT_INTERVAL)
- {
- // Creating a Calendar object from the specified interval value
- testStartTime = Calendar.getInstance();
- testStartTime.setLenient(true);
- String[] splitTimeOfDay = timeOfDay.split(":");
- testStartTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splitTimeOfDay[0]));
- testStartTime.set(Calendar.MINUTE, Integer.parseInt(splitTimeOfDay[1]));
- // If the date is in the past, make it the next day (Example: Checking for "1:00", when the time is 23:57.)
- if (testStartTime.getTimeInMillis() < currentTime.getTimeInMillis())
- testStartTime.add(Calendar.DAY_OF_MONTH, 1);
- // Check for the test date to be the minimum (smallest in the specified list)
- if (nextStartTime == null || testStartTime.getTimeInMillis() < nextStartTime.getTimeInMillis())
- nextStartTime = testStartTime;
- }
- _task = new startTournament(nextStartTime.getTimeInMillis());
- ThreadPoolManager.getInstance().executeTask(_task);
- }
- catch (Exception e)
- {
- _log.warning("Tornament: Error with interval check.");
- }
- }
- public void skipDelay()
- {
- if (_task.nextRun.cancel(false))
- {
- _task.setStartTime(System.currentTimeMillis());
- ThreadPoolManager.getInstance().executeTask(_task);
- }
- }
- public long getTimeToNextStep()
- {
- return _task.getTime();
- }
- public static void main(String[] args)
- {
- new AutoEventTournament();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement