Run the plugin message reader async to prevent the timeout event

warning from BungeeCord
This commit is contained in:
games647
2016-07-26 14:30:11 +02:00
parent c73bb70256
commit fb357424e6

View File

@ -6,6 +6,7 @@ import com.github.games647.fastlogin.bungee.tasks.AsyncToggleMessage;
import com.github.games647.fastlogin.core.PlayerProfile; import com.github.games647.fastlogin.core.PlayerProfile;
import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import java.util.Arrays;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
@ -41,43 +42,50 @@ public class PluginMessageListener implements Listener {
} }
private void readMessage(PluginMessageEvent pluginMessageEvent) { private void readMessage(PluginMessageEvent pluginMessageEvent) {
byte[] data = pluginMessageEvent.getData(); //so that we can safely process this in the background
ByteArrayDataInput dataInput = ByteStreams.newDataInput(data); final byte[] data = Arrays.copyOf(pluginMessageEvent.getData(), pluginMessageEvent.getData().length);
String subchannel = dataInput.readUTF(); final ProxiedPlayer forPlayer = (ProxiedPlayer) pluginMessageEvent.getReceiver();
ProxiedPlayer forPlayer = (ProxiedPlayer) pluginMessageEvent.getReceiver(); ProxyServer.getInstance().getScheduler().runAsync(plugin, new Runnable() {
if ("ON".equals(subchannel)) { @Override
String playerName = dataInput.readUTF(); public void run() {
ByteArrayDataInput dataInput = ByteStreams.newDataInput(data);
String subchannel = dataInput.readUTF();
if ("ON".equals(subchannel)) {
String playerName = dataInput.readUTF();
if (playerName.equals(forPlayer.getName()) && plugin.getConfig().getBoolean("premium-warning") if (playerName.equals(forPlayer.getName()) && plugin.getConfig().getBoolean("premium-warning")
&& !plugin.getPendingConfirms().contains(forPlayer.getUniqueId())) { && !plugin.getPendingConfirms().contains(forPlayer.getUniqueId())) {
forPlayer.sendMessage(TextComponent.fromLegacyText(plugin.getCore().getMessage("premium-warning"))); String message = plugin.getCore().getMessage("premium-warning");
plugin.getPendingConfirms().add(forPlayer.getUniqueId()); forPlayer.sendMessage(TextComponent.fromLegacyText(message));
return; plugin.getPendingConfirms().add(forPlayer.getUniqueId());
} return;
}
plugin.getPendingConfirms().remove(forPlayer.getUniqueId()); plugin.getPendingConfirms().remove(forPlayer.getUniqueId());
AsyncToggleMessage task = new AsyncToggleMessage(plugin, forPlayer, playerName, true); AsyncToggleMessage task = new AsyncToggleMessage(plugin, forPlayer, playerName, true);
ProxyServer.getInstance().getScheduler().runAsync(plugin, task); ProxyServer.getInstance().getScheduler().runAsync(plugin, task);
} else if ("OFF".equals(subchannel)) { } else if ("OFF".equals(subchannel)) {
String playerName = dataInput.readUTF(); String playerName = dataInput.readUTF();
AsyncToggleMessage task = new AsyncToggleMessage(plugin, forPlayer, playerName, false); AsyncToggleMessage task = new AsyncToggleMessage(plugin, forPlayer, playerName, false);
ProxyServer.getInstance().getScheduler().runAsync(plugin, task); ProxyServer.getInstance().getScheduler().runAsync(plugin, task);
} else if ("SUCCESS".equals(subchannel)) { } else if ("SUCCESS".equals(subchannel)) {
if (forPlayer.getPendingConnection().isOnlineMode()) { if (forPlayer.getPendingConnection().isOnlineMode()) {
//bukkit module successfully received and force logged in the user //bukkit module successfully received and force logged in the user
//update only on success to prevent corrupt data //update only on success to prevent corrupt data
BungeeLoginSession loginSession = plugin.getSession().get(forPlayer.getPendingConnection()); BungeeLoginSession loginSession = plugin.getSession().get(forPlayer.getPendingConnection());
PlayerProfile playerProfile = loginSession.getProfile(); PlayerProfile playerProfile = loginSession.getProfile();
loginSession.setRegistered(true); loginSession.setRegistered(true);
if (!loginSession.isAlreadySaved()) { if (!loginSession.isAlreadySaved()) {
playerProfile.setPremium(true); playerProfile.setPremium(true);
plugin.getCore().getStorage().save(playerProfile); plugin.getCore().getStorage().save(playerProfile);
loginSession.setAlreadySaved(true); loginSession.setAlreadySaved(true);
}
}
} }
} }
} });
} }
} }