Skip to content

Commit

Permalink
Add platform implementation for list of players in ping-response: res…
Browse files Browse the repository at this point in the history
…olve jpenilla#70 ;
  • Loading branch information
HarvelsX committed Jun 3, 2023
1 parent 226995c commit 1b46308
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

import com.destroystokyo.paper.event.server.PaperServerListPingEvent;
import io.papermc.lib.PaperLib;
import java.util.UUID;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.util.CachedServerIcon;
Expand Down Expand Up @@ -58,6 +60,10 @@ public void handlePing(final @NonNull PaperServerListPingEvent event) {
}
});
response.icon(event::setServerIcon);
response.hover(lines -> {
event.getPlayerSample().clear();
lines.forEach(name -> event.getPlayerSample().add(Bukkit.createProfile(UUID.randomUUID(), name)));
});

if (response.disablePlayerListHover()) {
event.getPlayerSample().clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package xyz.jpenilla.minimotd.bungee;

import java.util.UUID;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import net.md_5.bungee.api.Favicon;
import net.md_5.bungee.api.ServerPing;
Expand Down Expand Up @@ -61,6 +62,12 @@ public void onPing(final @NonNull ProxyPingEvent e) {
mini.playerCount().applyCount(players::setOnline, players::setMax);
if (mini.disablePlayerListHover()) {
players.setSample(new ServerPing.PlayerInfo[]{});
} else {
mini.hover(strings -> players.setSample(
strings.stream()
.map(name -> new ServerPing.PlayerInfo(name, UUID.randomUUID()))
.toArray(ServerPing.PlayerInfo[]::new)
));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
*/
package xyz.jpenilla.minimotd.fabric.mixin;

import com.mojang.authlib.GameProfile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.status.ServerStatus;
import net.minecraft.server.MinecraftServer;
Expand Down Expand Up @@ -76,15 +80,23 @@ public ServerStatus injectHandleStatusRequest(final ServerStatusPacketListenerIm
});
response.icon(favicon -> modifiedStatus.favicon(Optional.of(favicon)));

final List<GameProfile> samples = new ArrayList<>();
if (!response.disablePlayerListHover()) {
if (response.hover() == null) {
samples.addAll(vanillaStatus.players().map(ServerStatus.Players::sample).orElse(Collections.emptyList()));
}
response.hover(strings -> strings.stream()
.map(name -> new GameProfile(UUID.randomUUID(), name))
.forEach(samples::add));
}

if (response.hidePlayerCount()) {
modifiedStatus.players(Optional.empty());
} else {
final ServerStatus.Players newPlayers = new ServerStatus.Players(
response.playerCount().maxPlayers(),
response.playerCount().onlinePlayers(),
response.disablePlayerListHover()
? Collections.emptyList()
: vanillaStatus.players().map(ServerStatus.Players::sample).orElse(Collections.emptyList())
samples
);
modifiedStatus.players(Optional.of(newPlayers));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
package xyz.jpenilla.minimotd.sponge7;

import com.google.inject.Inject;
import java.util.UUID;
import net.kyori.adventure.text.serializer.spongeapi.SpongeComponentSerializer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.spongepowered.api.event.EventListener;
import org.spongepowered.api.event.server.ClientPingServerEvent;
import org.spongepowered.api.network.status.Favicon;
import org.spongepowered.api.profile.GameProfile;
import xyz.jpenilla.minimotd.common.MiniMOTD;
import xyz.jpenilla.minimotd.common.PingResponse;
import xyz.jpenilla.minimotd.common.config.MiniMOTDConfig;
Expand Down Expand Up @@ -64,6 +66,9 @@ public void handle(final @NonNull ClientPingServerEvent event) {
mini.playerCount().applyCount(players::setOnline, players::setMax);
mini.motd(motd -> response.setDescription(SpongeComponentSerializer.get().serialize(motd)));
mini.icon(response::setFavicon);
mini.hover(strings -> strings.forEach(name ->
players.getProfiles().add(GameProfile.of(UUID.randomUUID(), name))
));

if (mini.disablePlayerListHover()) {
players.getProfiles().clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@

import com.google.inject.Inject;
import java.lang.reflect.Method;
import java.util.UUID;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.spongepowered.api.MinecraftVersion;
import org.spongepowered.api.event.EventListener;
import org.spongepowered.api.event.server.ClientPingServerEvent;
import org.spongepowered.api.network.status.Favicon;
import org.spongepowered.api.profile.GameProfile;
import xyz.jpenilla.minimotd.common.Constants;
import xyz.jpenilla.minimotd.common.MiniMOTD;
import xyz.jpenilla.minimotd.common.PingResponse;
Expand Down Expand Up @@ -84,6 +86,9 @@ public void handle(final @NonNull ClientPingServerEvent event) {
}
});
mini.icon(response::setFavicon);
mini.hover(strings -> strings.forEach(name ->
players.profiles().add(GameProfile.of(UUID.randomUUID(), name))
));

if (mini.disablePlayerListHover()) {
players.profiles().clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyPingEvent;
import com.velocitypowered.api.proxy.server.ServerPing;
import com.velocitypowered.api.proxy.server.ServerPing.SamplePlayer;
import com.velocitypowered.api.util.Favicon;
import java.util.UUID;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import xyz.jpenilla.minimotd.common.MiniMOTD;
Expand Down Expand Up @@ -57,6 +59,12 @@ private void handle(final ProxyPingEvent event) {
response.icon(pong::favicon);
response.motd(pong::description);
response.playerCount().applyCount(pong::onlinePlayers, pong::maximumPlayers);
response.hover(strings -> {
pong.clearSamplePlayers();
pong.samplePlayers(strings.stream().map(name ->
new SamplePlayer(name, UUID.randomUUID())
).toArray(SamplePlayer[]::new));
});

if (response.disablePlayerListHover()) {
pong.clearSamplePlayers();
Expand Down

0 comments on commit 1b46308

Please sign in to comment.