Run sound task every 10 minutes
This commit is contained in:
parent
e01af5edab
commit
e47df41225
@ -1,15 +1,21 @@
|
|||||||
package mineplex.core.sound;
|
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 com.mineplex.spigot.MissingSoundEvent;
|
||||||
|
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
import mineplex.core.MiniPlugin;
|
import mineplex.core.MiniPlugin;
|
||||||
import mineplex.core.ReflectivelyCreateMiniPlugin;
|
import mineplex.core.ReflectivelyCreateMiniPlugin;
|
||||||
|
import mineplex.core.common.util.UtilServer;
|
||||||
import mineplex.core.slack.SlackAPI;
|
import mineplex.core.slack.SlackAPI;
|
||||||
import mineplex.core.slack.SlackMessage;
|
import mineplex.core.slack.SlackMessage;
|
||||||
import mineplex.core.slack.SlackTeam;
|
import mineplex.core.slack.SlackTeam;
|
||||||
import mineplex.core.thread.ThreadPool;
|
|
||||||
import mineplex.serverdata.Region;
|
import mineplex.serverdata.Region;
|
||||||
import mineplex.serverdata.redis.atomic.RedisStringRepository;
|
import mineplex.serverdata.redis.atomic.RedisStringRepository;
|
||||||
import mineplex.serverdata.servers.ServerManager;
|
import mineplex.serverdata.servers.ServerManager;
|
||||||
@ -21,7 +27,8 @@ import mineplex.serverdata.servers.ServerManager;
|
|||||||
@ReflectivelyCreateMiniPlugin
|
@ReflectivelyCreateMiniPlugin
|
||||||
public class SoundNotifier extends MiniPlugin
|
public class SoundNotifier extends MiniPlugin
|
||||||
{
|
{
|
||||||
private RedisStringRepository _repo;
|
private final RedisStringRepository _repo;
|
||||||
|
private final Set<String> _messages = Sets.newConcurrentHashSet();
|
||||||
|
|
||||||
private SoundNotifier()
|
private SoundNotifier()
|
||||||
{
|
{
|
||||||
@ -33,6 +40,40 @@ public class SoundNotifier extends MiniPlugin
|
|||||||
Region.ALL,
|
Region.ALL,
|
||||||
"missingSounds"
|
"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
|
@EventHandler
|
||||||
@ -40,18 +81,15 @@ public class SoundNotifier extends MiniPlugin
|
|||||||
{
|
{
|
||||||
final String message = event.getMessage();
|
final String message = event.getMessage();
|
||||||
|
|
||||||
ThreadPool.ASYNC.execute(() ->
|
synchronized (_messages)
|
||||||
{
|
{
|
||||||
if (_repo.get(message) == null)
|
_messages.add(message);
|
||||||
{
|
}
|
||||||
sendSlackMessage(event.getMessage());
|
|
||||||
_repo.set(message, "sent");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendSlackMessage(String message)
|
private void sendSlackMessage(String message)
|
||||||
{
|
{
|
||||||
|
message = String.format("[%s] %s", UtilServer.getServerName(), message);
|
||||||
SlackMessage slackMsg = new SlackMessage("Sound Bot", "notes", message);
|
SlackMessage slackMsg = new SlackMessage("Sound Bot", "notes", message);
|
||||||
SlackAPI.getInstance().sendMessage(SlackTeam.DEVELOPER, "#pc-sound-logging", slackMsg, true);
|
SlackAPI.getInstance().sendMessage(SlackTeam.DEVELOPER, "#pc-sound-logging", slackMsg, true);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user