From d1bfb9e1628d208a40b8de3f9b42c73d593c90aa Mon Sep 17 00:00:00 2001 From: Tux Date: Tue, 25 Aug 2015 20:12:55 -0400 Subject: [PATCH] If we find an async ping event-hostile plugin (currently only ServerListPlus), run the ping handling handler synchronously. This is meant as a move to attempt to introduce a better mechanism for handling async events in a more sane way. --- .../redisbungee/RedisBungeeListener.java | 49 ++++++++++++++----- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java index e22175f..001c577 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java @@ -28,6 +28,7 @@ package com.imaginarycode.minecraft.redisbungee; import com.google.common.base.Joiner; import com.google.common.collect.HashMultimap; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Multimap; import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataOutput; @@ -37,6 +38,7 @@ import com.imaginarycode.minecraft.redisbungee.util.RedisCallable; import lombok.AllArgsConstructor; import net.md_5.bungee.api.AbstractReconnectHandler; import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ServerPing; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.ComponentBuilder; @@ -69,6 +71,8 @@ public class RedisBungeeListener implements Listener { private final RedisBungee plugin; private final List exemptAddresses; + private static final List ASYNC_PING_EVENT_HOSTILE = ImmutableList.of("ServerListPlus"); + @EventHandler(priority = EventPriority.LOWEST) public void onLogin(final LoginEvent event) { event.registerIntent(plugin); @@ -179,20 +183,39 @@ public class RedisBungeeListener implements Listener { return; } - event.registerIntent(plugin); - plugin.getProxy().getScheduler().runAsync(plugin, new Runnable() { - @Override - public void run() { - ServerPing old = event.getResponse(); - ServerPing reply = new ServerPing(); - reply.setPlayers(new ServerPing.Players(old.getPlayers().getMax(), plugin.getCount(), old.getPlayers().getSample())); - reply.setDescription(old.getDescription()); - reply.setFavicon(old.getFaviconObject()); - reply.setVersion(old.getVersion()); - event.setResponse(reply); - event.completeIntent(plugin); + boolean runAsync = true; + for (String s : ASYNC_PING_EVENT_HOSTILE) { + if (ProxyServer.getInstance().getPluginManager().getPlugin(s) != null) { + runAsync = false; + break; } - }); + } + + if (runAsync) { + event.registerIntent(plugin); + plugin.getProxy().getScheduler().runAsync(plugin, new Runnable() { + @Override + public void run() { + ServerPing old = event.getResponse(); + ServerPing reply = new ServerPing(); + reply.setPlayers(new ServerPing.Players(old.getPlayers().getMax(), plugin.getCount(), old.getPlayers().getSample())); + reply.setDescription(old.getDescription()); + reply.setFavicon(old.getFaviconObject()); + reply.setVersion(old.getVersion()); + event.setResponse(reply); + event.completeIntent(plugin); + } + }); + } else { + // Async ping event will not work as an async-hostile plugin was found, so perform the ping modification synchronously. + ServerPing old = event.getResponse(); + ServerPing reply = new ServerPing(); + reply.setPlayers(new ServerPing.Players(old.getPlayers().getMax(), plugin.getCount(), old.getPlayers().getSample())); + reply.setDescription(old.getDescription()); + reply.setFavicon(old.getFaviconObject()); + reply.setVersion(old.getVersion()); + event.setResponse(reply); + } } @EventHandler