package com.crossge.necessities;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.scheduler.BukkitRunnable;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Random;

public class Announcer {
    private BukkitRunnable announcerTask;
    private final ArrayList<String> messages = new ArrayList<>();
    private final Random r = new Random();

    void init() {
        Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "Loading Announcer messages...");
        try (BufferedReader read = new BufferedReader(new FileReader(new File("plugins/Necessities/announcements.txt")))) {
            String line;
            while ((line = read.readLine()) != null)
                this.messages.add(ChatColor.translateAlternateColorCodes('&', line));
        } catch (Exception ignored) {
        }
        if (this.messages.isEmpty())
            return;
        this.announcerTask = new BukkitRunnable() {
            @Override
            public void run() {
                Bukkit.broadcastMessage(messages.get(r.nextInt(messages.size())));
            }
        };
        YamlConfiguration config = Necessities.getInstance().getConfig();
        this.announcerTask.runTaskTimerAsynchronously(Necessities.getInstance(), 0, 20 * 60 * config.getInt("Announcements.frequency"));
        Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "Announcer messages loaded.");
    }

    void exit() {
        this.announcerTask.cancel();
    }

    public void reloadAnnouncer() {
        exit();
        this.messages.clear();
        init();
    }
}
@@ -1541,24 +1541,42 @@
 id exit() %7B%0A
+        try %7B%0A    
         this
@@ -1596,24 +1596,72 @@
 k.cancel();%0A
+        %7D catch (Exception ignored) %7B%0A        %7D%0A
     %7D%0A%0A    p

Fix a bug on close if there are no announcements