Run sound task every 10 minutes

This commit is contained in:
Dan Mulloy 2018-05-28 12:02:53 -04:00 committed by Alexander Meech
parent e01af5edab
commit e47df41225

View File

@ -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<String> _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<String> messages;
synchronized (_messages)
{
messages = new HashSet<>(_messages);
_messages.clear();
}
Iterator<String> 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);
}