Skip to content

Commit 75ca427

Browse files
committed
Load favicons asynchronously on BungeeCord (if supported)
Closes #40
1 parent a4d5566 commit 75ca427

1 file changed

Lines changed: 43 additions & 7 deletions

File tree

Bungee/src/main/java/net/minecrell/serverlistplus/bungee/BungeePlugin.java

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import net.md_5.bungee.api.config.ServerInfo;
4242
import net.md_5.bungee.api.connection.PendingConnection;
4343
import net.md_5.bungee.api.connection.ProxiedPlayer;
44+
import net.md_5.bungee.api.event.AsyncEvent;
4445
import net.md_5.bungee.api.event.LoginEvent;
4546
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
4647
import net.md_5.bungee.api.event.ProxyPingEvent;
@@ -211,13 +212,6 @@ public int getProtocolVersion() {
211212
if (protocol != null) version.setProtocol(protocol);
212213
}
213214

214-
// Favicon
215-
FaviconSource favicon = response.getFavicon();
216-
if (favicon != null) {
217-
Optional<Favicon> icon = faviconCache.getUnchecked(favicon);
218-
if (icon.isPresent()) ping.setFavicon(icon.get());
219-
}
220-
221215
if (players != null) {
222216
if (response.hidePlayers()) {
223217
ping.setPlayers(null);
@@ -248,7 +242,49 @@ public int getProtocolVersion() {
248242
}
249243
}
250244
}
245+
246+
// Favicon
247+
FaviconSource favicon = response.getFavicon();
248+
if (favicon != null) {
249+
Optional<Favicon> icon;
250+
// Check if instanceof AsyncEvent for compatibility with 1.7.10
251+
if (event instanceof AsyncEvent) {
252+
icon = faviconCache.getIfPresent(favicon);
253+
} else {
254+
icon = faviconCache.getUnchecked(favicon);
255+
}
256+
257+
if (icon == null) {
258+
// Load favicon asynchronously
259+
event.registerIntent(BungeePlugin.this);
260+
getProxy().getScheduler().runAsync(BungeePlugin.this, new AsyncFaviconLoader(event, favicon));
261+
} else if (icon.isPresent()) {
262+
ping.setFavicon(icon.get());
263+
}
264+
}
265+
}
266+
}
267+
268+
private final class AsyncFaviconLoader implements Runnable {
269+
270+
private final ProxyPingEvent event;
271+
private final FaviconSource source;
272+
273+
private AsyncFaviconLoader(ProxyPingEvent event, FaviconSource source) {
274+
this.event = event;
275+
this.source = source;
251276
}
277+
278+
@Override
279+
public void run() {
280+
Optional<Favicon> favicon = faviconCache.getUnchecked(this.source);
281+
if (favicon.isPresent()) {
282+
event.getResponse().setFavicon(favicon.get());
283+
}
284+
285+
event.completeIntent(BungeePlugin.this);
286+
}
287+
252288
}
253289

254290
@Override

0 commit comments

Comments
 (0)