Vaerys_Dawn

Nice Code

Nov 11th, 2017
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. private static void doEventDaily(ZonedDateTime nowUTC) {
  2.         ZonedDateTime midnightUTC = ZonedDateTime.now(ZoneOffset.UTC);
  3.         midnightUTC = midnightUTC.withHour(0).withSecond(0).withMinute(0).withNano(0).plusDays(1);
  4.         long initialDelay = midnightUTC.toEpochSecond() - nowUTC.toEpochSecond() + 4;
  5.         logger.trace("Now UTC = " + Utility.formatTimeSeconds(nowUTC.toEpochSecond()));
  6.         logger.trace("Midnight UTC = " + Utility.formatTimeSeconds(midnightUTC.toEpochSecond()));
  7.         logger.trace("Delay = " + Utility.formatTimeSeconds(initialDelay));
  8.         if (initialDelay < 120) {
  9.             initialDelay += 24 * 60 * 60;
  10.         }
  11.         Timer timer = new Timer();
  12.         timer.scheduleAtFixedRate(new TimerTask() {
  13.             @Override
  14.             public void run() {
  15.                 //init vars
  16.                 ZonedDateTime timeNow = ZonedDateTime.now(ZoneOffset.UTC);
  17.                 DayOfWeek day = timeNow.getDayOfWeek();
  18.                 Random random = new Random();
  19.  
  20.                 //do checks
  21.                 Client.checkPatrons();
  22.                 checkKeepAlive();
  23.  
  24.                 //update Active Event
  25.                 Globals.updateEvent();
  26.                 TimedEvent event = Globals.getCurrentEvent();
  27.  
  28.                 //handle avatars
  29.                 Client.handleAvatars();
  30.  
  31.                 //backups
  32.                 Globals.backupAll();
  33.  
  34.                 logger.info("Running Daily tasks for " + day);
  35.  
  36.                 for (GuildObject task : Globals.getGuilds()) {
  37.                     GuildConfig guildconfig = task.config;
  38.                     //do decay
  39.                     XpHandler.doDecay(task, nowUTC);
  40.  
  41.                     //get general channel
  42.                     IChannel generalChannel = task.getChannelByType(Command.CHANNEL_GENERAL);
  43.  
  44.                     //do daily messages
  45.                     if (generalChannel != null && guildconfig.dailyMessage) {
  46.                         DailyMessage finalMessage = Globals.getDailyMessage(day);
  47.                         List<DailyMessage> messages;
  48.                         if (event != null && event.doSpecialMessages() && event.getMessagesDay(day).size() != 0) {
  49.                             messages = event.getMessagesDay(day);
  50.                         } else {
  51.                             messages = new ArrayList<>(Globals.getDailyMessages().getMessages());
  52.                             messages.add(Globals.getDailyMessage(day));
  53.                         }
  54.                         int randomMessage = random.nextInt(messages.size());
  55.                         finalMessage = messages.get(randomMessage);
  56.                         task.config.setLastDailyMessage(finalMessage);
  57.                         CommandObject command = new CommandObject(task,generalChannel);
  58.                         Utility.sendMessage(finalMessage.getContents(command),generalChannel);
  59.                     }
  60.                 }
  61.             }
  62.         }, initialDelay * 1000, 24 * 60 * 60 * 1000);
  63.     }
Advertisement
Add Comment
Please, Sign In to add comment