Java Code Examples for com.destroystokyo.paper.event.server.PaperServerListPingEvent#getPlayerSample()

The following examples show how to use com.destroystokyo.paper.event.server.PaperServerListPingEvent#getPlayerSample() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PaperPingResponseHandler.java    From ProtocolSupport with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public ServerPingResponseEvent createResponse(Connection connection) {
	PaperServerListPingEvent bevent = new PaperServerListPingEvent(
		new StatusClientImpl(connection),
		Bukkit.getMotd(),
		Bukkit.getOnlinePlayers().size(), Bukkit.getMaxPlayers(),
		createServerVersionString(), connection.getVersion().getId(),
		Bukkit.getServerIcon()
	);
	List<PlayerProfile> playerSample = bevent.getPlayerSample();
	Bukkit.getOnlinePlayers().stream()
	.limit(SpigotConfig.playerSample)
	.map(player -> new NameUUIDPlayerProfile(player.getUniqueId(), player.getName()))
	.forEach(playerSample::add);
	Bukkit.getPluginManager().callEvent(bevent);

	ServerPingResponseEvent revent = new ServerPingResponseEvent(
		connection,
		new ProtocolInfo(bevent.getProtocolVersion(), bevent.getVersion()),
		bevent.getServerIcon() != null ? ServerPlatform.get().getMiscUtils().convertBukkitIconToBase64(bevent.getServerIcon()) : null,
		bevent.getMotd(),
		bevent.getNumPlayers(), bevent.getMaxPlayers(),
		bevent.getPlayerSample().stream().map(PlayerProfile::getName).collect(Collectors.toList())
	);
	Bukkit.getPluginManager().callEvent(revent);

	return revent;
}
 
Example 2
Source File: PaperEventHandler.java    From ServerListPlus with GNU General Public License v3.0 4 votes vote down vote up
private void handlePaperServerListPing(final PaperServerListPingEvent event) {
    if (bukkit.getCore() == null) return; // Too early, we haven't finished initializing yet

    StatusRequest request = bukkit.getCore().createRequest(event.getAddress());
    request.setProtocolVersion(event.getClient().getProtocolVersion());
    InetSocketAddress host = event.getClient().getVirtualHost();
    if (host != null) {
        request.setTarget(host);
    }

    StatusResponse response = request.createResponse(bukkit.getCore().getStatus(),
            // Return unknown player counts if it has been hidden
            new ResponseFetcher() {
                @Override
                public Integer getOnlinePlayers() {
                    return event.shouldHidePlayers() ? null : event.getNumPlayers();
                }

                @Override
                public Integer getMaxPlayers() {
                    return event.shouldHidePlayers() ? null : event.getMaxPlayers();
                }

                @Override
                public int getProtocolVersion() {
                    return event.getProtocolVersion();
                }
            }
    );

    // Description
    String message = response.getDescription();
    if (message != null) event.setMotd(message);

    // Version name
    message = response.getVersion();
    if (message != null) event.setVersion(message);
    // Protocol version
    Integer protocol = response.getProtocolVersion();
    if (protocol != null) event.setProtocolVersion(protocol);

    if (response.hidePlayers()) {
        event.setHidePlayers(true);
    } else {
        // Online players
        Integer count = response.getOnlinePlayers();
        if (count != null) event.setNumPlayers(count);
        // Max players
        count = response.getMaxPlayers();
        if (count != null) event.setMaxPlayers(count);

        // Player hover
        message = response.getPlayerHover();
        if (message != null) {
            List<PlayerProfile> profiles = event.getPlayerSample();
            profiles.clear();

            if (!message.isEmpty()) {
                if (response.useMultipleSamples()) {
                    count = response.getDynamicSamples();
                    List<String> lines = count != null ? Helper.splitLinesCached(message, count) :
                            Helper.splitLinesCached(message);

                    for (String line : lines) {
                        profiles.add(bukkit.getServer().createProfile(line));
                    }
                } else {
                    profiles.add(bukkit.getServer().createProfile(message));
                }
            }
        }
    }

    // Favicon
    FaviconSource favicon = response.getFavicon();
    if (favicon != null) {
        CachedServerIcon icon = bukkit.getFavicon(favicon);
        if (icon != null)
            try {
                event.setServerIcon(icon);
            } catch (UnsupportedOperationException ignored) {}
    }
}