diff --git a/Plugins/Mineplex.Core/src/mineplex/core/sound/SoundNotifier.java b/Plugins/Mineplex.Core/src/mineplex/core/sound/SoundNotifier.java index d77491002..3719521b9 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/sound/SoundNotifier.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/sound/SoundNotifier.java @@ -1,15 +1,21 @@ package mineplex.core.sound; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import com.google.common.collect.Sets; import com.mineplex.spigot.MissingSoundEvent; import org.bukkit.event.EventHandler; +import org.bukkit.scheduler.BukkitRunnable; import mineplex.core.MiniPlugin; import mineplex.core.ReflectivelyCreateMiniPlugin; +import mineplex.core.common.util.UtilServer; import mineplex.core.slack.SlackAPI; import mineplex.core.slack.SlackMessage; import mineplex.core.slack.SlackTeam; -import mineplex.core.thread.ThreadPool; import mineplex.serverdata.Region; import mineplex.serverdata.redis.atomic.RedisStringRepository; import mineplex.serverdata.servers.ServerManager; @@ -21,7 +27,8 @@ import mineplex.serverdata.servers.ServerManager; @ReflectivelyCreateMiniPlugin public class SoundNotifier extends MiniPlugin { - private RedisStringRepository _repo; + private final RedisStringRepository _repo; + private final Set _messages = Sets.newConcurrentHashSet(); private SoundNotifier() { @@ -33,6 +40,40 @@ public class SoundNotifier extends MiniPlugin Region.ALL, "missingSounds" ); + + // run task every 10 minutes + long delay = 20 * 60 * 10; + + new BukkitRunnable() + { + @Override + public void run() + { + if (!_messages.isEmpty()) + { + Set messages; + + synchronized (_messages) + { + messages = new HashSet<>(_messages); + _messages.clear(); + } + + Iterator iter = messages.iterator(); + while (iter.hasNext()) + { + String message = iter.next(); + if (_repo.get(message) == null) + { + sendSlackMessage(message); + _repo.set(message, "sent"); + } + + iter.remove(); + } + } + } + }.runTaskTimerAsynchronously(_plugin, delay, delay); } @EventHandler @@ -40,18 +81,15 @@ public class SoundNotifier extends MiniPlugin { final String message = event.getMessage(); - ThreadPool.ASYNC.execute(() -> + synchronized (_messages) { - if (_repo.get(message) == null) - { - sendSlackMessage(event.getMessage()); - _repo.set(message, "sent"); - } - }); + _messages.add(message); + } } private void sendSlackMessage(String message) { + message = String.format("[%s] %s", UtilServer.getServerName(), message); SlackMessage slackMsg = new SlackMessage("Sound Bot", "notes", message); SlackAPI.getInstance().sendMessage(SlackTeam.DEVELOPER, "#pc-sound-logging", slackMsg, true); }