Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from Andre601/feature/favicon-support
Browse files Browse the repository at this point in the history
Add Favicon support for Server list profiles.
  • Loading branch information
Andre601 authored Aug 21, 2022
2 parents 15f7b5d + 50f878e commit 0d84335
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 19 deletions.
Binary file added .github/assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/assets/version_1.5.0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@
import ch.andre601.advancedserverlist.core.parsing.ComponentParser;
import ch.andre601.advancedserverlist.core.profiles.ProfileManager;
import ch.andre601.advancedserverlist.core.profiles.ServerListProfile;
import ch.andre601.advancedserverlist.core.profiles.favicon.FaviconHandler;
import ch.andre601.advancedserverlist.core.profiles.replacer.StringReplacer;
import ch.andre601.advancedserverlist.core.profiles.replacer.placeholders.PlayerPlaceholders;
import ch.andre601.advancedserverlist.core.profiles.replacer.placeholders.ServerPlaceholders;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import net.md_5.bungee.api.Favicon;
import net.md_5.bungee.api.ServerPing;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.event.ProxyPingEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
import net.md_5.bungee.event.EventPriority;

import java.awt.image.BufferedImage;
import java.net.InetSocketAddress;

public class PingEvent implements Listener{
Expand All @@ -59,15 +63,15 @@ public void onProxyPing(ProxyPingEvent event){
if(protocol == null)
return;

InetSocketAddress address = (InetSocketAddress)event.getConnection().getSocketAddress();
String playerName = plugin.getCore().getPlayerHandler().getPlayerByIp(
((InetSocketAddress)event.getConnection().getSocketAddress()).getHostString()
);
InetSocketAddress host = event.getConnection().getVirtualHost();

int online = ping.getPlayers().getOnline();
int max = ping.getPlayers().getMax();

PlayerPlaceholders playerPlaceholders = new PlayerPlaceholders(
new BungeePlayer(plugin.getCore().getPlayerHandler().getPlayerByIp(address.getHostString()), protocol.getProtocol())
);
PlayerPlaceholders playerPlaceholders = new PlayerPlaceholders(new BungeePlayer(playerName, protocol.getProtocol()));
ServerPlaceholders serverPlaceholders = new ServerPlaceholders(online, max, host == null ? null : host.getHostString());

ServerListProfile profile = ProfileManager.get(plugin.getCore())
Expand Down Expand Up @@ -128,7 +132,24 @@ public void onProxyPing(ProxyPingEvent event){
ping.getPlayers().setSample(playerInfos);
}

ping.setFavicon(ping.getFaviconObject());
if(!profile.getFavicon().isEmpty()){
String favName = StringReplacer.replace(profile.getFavicon(), playerPlaceholders.getReplacements());

BufferedImage img = new FaviconHandler(plugin.getCore(), favName).get().getAsBufferedImage();
if(img == null){
plugin.getPluginLogger().warn("Could not obtain valid Favicon to use.");
ping.setFavicon(ping.getFaviconObject());
}else{
try{
Favicon favicon = Favicon.create(img);
ping.setFavicon(favicon);
}catch(Exception ex){
plugin.getPluginLogger().warn("Unable to override Favicon. Reason: %s", ex.getMessage());
ping.setFavicon(ping.getFaviconObject());
}
}
}

ping.setVersion(protocol);

event.setResponse(ping);
Expand Down
12 changes: 12 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
<artifactId>adventure-text-serializer-legacy</artifactId>
<version>4.11.0</version>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>1.3.3</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -86,6 +92,12 @@
<version>3.3.0</version>
<configuration>
<minimizeJar>true</minimizeJar>
<relocations>
<relocation>
<pattern>com.github.benmanes</pattern>
<shadedPattern>ch.andre601.advancedserverlist.core.depends.caffeine</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
<artifact>*:*</artifact>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
import ch.andre601.advancedserverlist.core.file.FileHandler;
import ch.andre601.advancedserverlist.core.interfaces.PluginCore;
import ch.andre601.advancedserverlist.core.interfaces.PluginLogger;
import ch.andre601.advancedserverlist.core.profiles.players.GenericPlayer;
import ch.andre601.advancedserverlist.core.profiles.players.PlayerHandler;
import ch.andre601.advancedserverlist.core.profiles.replacer.placeholders.PlayerPlaceholders;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -113,7 +111,7 @@ private void load(){

getPluginLogger().info("Starting AdvancedServerList v%s...", version);

getPluginLogger().info("Proxy: " + plugin.getPlatformName() + " " + plugin.getPlatformVersion());
getPluginLogger().info("Platform: " + plugin.getPlatformName() + " " + plugin.getPlatformVersion());

if(getFileHandler().loadConfig()){
getPluginLogger().info("Successfully loaded config.yml!");
Expand All @@ -129,6 +127,9 @@ private void load(){
return;
}

if(!getPath().resolve("favicons").toFile().exists() && getPath().resolve("favicons").toFile().mkdirs())
getPluginLogger().info("Successfully created favicons folder.");

getPluginLogger().info("Loading Commands...");
plugin.loadCommands();
getPluginLogger().info("Commands loaded!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class ServerListProfile{
private final List<String> motd;
private final List<String> players;
private final String playerCount;
private final String favicon;
private final boolean hidePlayers;
private final int xMore;

Expand All @@ -53,6 +54,7 @@ public ServerListProfile(ConfigurationNode node, PluginLogger logger){
this.motd = getList(node, "motd", true);
this.players = getList(node, "players", false);
this.playerCount = node.node("playerCount").getString("");
this.favicon = node.node("favicon").getString("");
this.hidePlayers = node.node("hidePlayers").getBoolean();
this.xMore = node.node("xMore").getInt(-1);
}
Expand All @@ -73,6 +75,10 @@ public String getPlayerCount(){
return playerCount;
}

public String getFavicon(){
return favicon;
}

public boolean shouldHidePlayers(){
return hidePlayers;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* MIT License
*
* Copyright (c) 2022 Andre_601
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package ch.andre601.advancedserverlist.core.profiles.favicon;

import ch.andre601.advancedserverlist.core.AdvancedServerList;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

import java.util.concurrent.TimeUnit;

public class FaviconHandler{

private final Cache<String, GenericFavicon> favicons = Caffeine.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
.build();

private final AdvancedServerList core;
private final String input;

public FaviconHandler(AdvancedServerList core, String input){
this.core = core;
this.input = input;
}

public GenericFavicon get(){
return favicons.get(input, k -> new GenericFavicon(core, input));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* MIT License
*
* Copyright (c) 2022 Andre_601
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package ch.andre601.advancedserverlist.core.profiles.favicon;

import ch.andre601.advancedserverlist.core.AdvancedServerList;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Locale;

public class GenericFavicon{

private final AdvancedServerList core;
private final String input;
private final BufferedImage image;

private byte[] bytes = null;

public GenericFavicon(AdvancedServerList core, String input){
this.core = core;
this.input = input;
this.image = resolve();
}

public byte[] getAsByteArray(){
if(bytes != null && bytes.length > 0)
return bytes;

if(image == null){
this.core.getPluginLogger().warn("Cannot convert Favicon BufferedImage to Byte array. BufferedImage was null.");
return null;
}

try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.setUseCache(false);
ImageIO.write(image, "PNG", baos);

return (bytes = baos.toByteArray());
}catch(IOException ex){
this.core.getPluginLogger().warn("Cannot convert Favicon BufferedImage to Byte array. %s", ex.getMessage());
return null;
}
}

public BufferedImage getAsBufferedImage(){
return image;
}

private BufferedImage resolve(){
InputStream stream;

if(input.toLowerCase(Locale.ROOT).startsWith("https://")){
stream = getFromUrl(input);
}else
if(input.toLowerCase(Locale.ROOT).endsWith(".png")){
File folder = core.getPath().resolve("favicons").toFile();
if(!folder.exists()){
this.core.getPluginLogger().warn("Cannot get Favicon for profile. Favicons folder is not present.");
return null;
}

File file = new File(folder, input);

try{
stream = new FileInputStream(file);
}catch(FileNotFoundException ex){
this.core.getPluginLogger().warn("Unable to create Favicon data. %s", ex.getMessage());
return null;
}
}else{
stream = getFromUrl("https://mc-heads.net/avatar/" + input + "/64");
}

if(stream == null){
this.core.getPluginLogger().warn("Unable to get Data for Favicon. InputStream was null.");
return null;
}

try{
BufferedImage original = ImageIO.read(stream);
if(original == null)
return null;

BufferedImage favicon = new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB);
Graphics2D img = favicon.createGraphics();

// We use this to resize the image to 64x64 pixels.
img.drawImage(original, 0, 0, 64, 64, null);
img.dispose();

return favicon;
}catch(IOException ex){
this.core.getPluginLogger().warn("Cannot resize favicon into 64x64. %s", ex.getMessage());
return null;
}
}

private InputStream getFromUrl(String url){
try{
URL avatarUrl = new URL(url);
URLConnection connection = avatarUrl.openConnection();
connection.setRequestProperty("User-Agent", "AdvancedServerList/" + core.getVersion());
connection.connect();

return connection.getInputStream();
}catch(IOException ex){
this.core.getPluginLogger().warn("Unable to connect to URL %s! %s", url, ex.getMessage());
return null;
}
}


}
16 changes: 16 additions & 0 deletions core/src/main/resources/profiles/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ players:
#
playerCount: '<grey>Text'

#
# Allows you to override the default Favicon used by the Server/Proxy.
# You can provide three specific inputs:
# - A valid URL pointing to a PNG file.
# - ${player name} for a per-player avatar being displayed (Uses mc-heads.net)
# - Image file name (i.e. example.png) for a file in the "favicons" folder.
#
# NOTE:
# - The favicon will be cached for 5 minutes to avoid spam and rate limits.
# - BungeeCord/Waterfall may report issues that the plugin takes a long time to process.
# This is something I cannot really get improved without help.
#
# Read more: https://github.com/Andre601/AdvancedServerList/wiki/Profiles#favicon
#
favicon: ''

#
# When set to 0 or higher will alter the max player count to the online count + xMore.
# Example: With 20 online players would 'xMore: 1' show 20/21 and 'xMore: 0' show 20/20
Expand Down
Loading

0 comments on commit 0d84335

Please sign in to comment.