Search in sources :

Example 1 with SServerInfoPacket

use of net.minecraft.network.status.server.SServerInfoPacket in project Arclight by IzzelAliz.

the class ServerStatusNetHandlerMixin method arclight$handleServerPing.

@Redirect(method = "processServerQuery", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/NetworkManager;sendPacket(Lnet/minecraft/network/IPacket;)V"))
private void arclight$handleServerPing(NetworkManager networkManager, IPacket<?> packetIn) {
    Object[] players = this.server.getPlayerList().players.toArray();
    ArclightPingEvent event = new ArclightPingEvent(networkManager, server);
    Bukkit.getPluginManager().callEvent(event);
    List<GameProfile> profiles = new ArrayList<>(players.length);
    Object[] array;
    for (int length = (array = players).length, i = 0; i < length; ++i) {
        Object player = array[i];
        if (player != null) {
            profiles.add(((ServerPlayerEntity) player).getGameProfile());
        }
    }
    ServerStatusResponse.Players playerSample = new ServerStatusResponse.Players(event.getMaxPlayers(), profiles.size());
    if (!profiles.isEmpty()) {
        Collections.shuffle(profiles);
        profiles = profiles.subList(0, Math.min(profiles.size(), SpigotConfig.playerSample));
    }
    playerSample.setPlayers(profiles.toArray(new GameProfile[0]));
    ServerStatusResponse ping = new ServerStatusResponse();
    ping.setFavicon(event.icon.value);
    ping.setServerDescription(new StringTextComponent(event.getMotd()));
    ping.setPlayers(playerSample);
    int version = SharedConstants.getVersion().getProtocolVersion();
    ping.setVersion(new ServerStatusResponse.Version(this.server.getServerModName() + " " + this.server.getMinecraftVersion(), version));
    ping.setForgeData(this.server.getServerStatusResponse().getForgeData());
    networkManager.sendPacket(new SServerInfoPacket(ping));
}
Also used : ArrayList(java.util.ArrayList) GameProfile(com.mojang.authlib.GameProfile) StringTextComponent(net.minecraft.util.text.StringTextComponent) ServerStatusResponse(net.minecraft.network.ServerStatusResponse) ArclightPingEvent(io.izzel.arclight.common.mod.util.ArclightPingEvent) SServerInfoPacket(net.minecraft.network.status.server.SServerInfoPacket) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 2 with SServerInfoPacket

use of net.minecraft.network.status.server.SServerInfoPacket in project TheAPI by TheDevTec.

the class v1_16_R3_Mohist method processServerListPing.

@Override
public boolean processServerListPing(String player, Object channel, Object packet) {
    SServerInfoPacket status = (SServerInfoPacket) packet;
    ServerStatusResponse ping;
    try {
        ping = (ServerStatusResponse) field.get(status);
    } catch (Exception e) {
        return false;
    }
    List<PlayerProfile> players = new ArrayList<>();
    for (Player p : Bukkit.getOnlinePlayers()) players.add(new PlayerProfile(p.getName(), p.getUniqueId()));
    ServerListPingEvent event = new ServerListPingEvent(Bukkit.getOnlinePlayers().size(), Bukkit.getMaxPlayers(), players, Bukkit.getMotd(), ping.func_151316_d(), ((InetSocketAddress) ((Channel) channel).remoteAddress()).getAddress(), ping.func_151322_c().func_151303_a(), ping.func_151322_c().func_151304_b());
    HandlerList.callEvent(event);
    if (event.isCancelled())
        return true;
    Players playerSample = new Players(event.getMaxPlayers(), event.getOnlinePlayers());
    if (event.getPlayersText() != null) {
        GameProfile[] profiles = new GameProfile[event.getPlayersText().size()];
        int i = -1;
        for (PlayerProfile s : event.getPlayersText()) profiles[++i] = new GameProfile(s.getUUID(), s.getName());
        playerSample.func_151330_a(profiles);
    } else
        playerSample.func_151330_a(new GameProfile[0]);
    ping.func_151319_a(playerSample);
    if (event.getMotd() != null)
        ping.func_151315_a((ITextComponent) toIChatBaseComponent(ComponentAPI.toComponent(event.getMotd(), true)));
    else
        ping.func_151315_a((ITextComponent) BukkitLoader.getNmsProvider().chatBase("{\"text\":\"\"}"));
    if (event.getVersion() != null)
        ping.func_151321_a(new Version(event.getVersion(), event.getProtocol()));
    if (event.getFalvicon() != null)
        ping.func_151320_a(event.getFalvicon());
    return false;
}
Also used : CraftPlayer(org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer) Player(org.bukkit.entity.Player) PlayerProfile(me.devtec.theapi.bukkit.events.ServerListPingEvent.PlayerProfile) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) ITextComponent(net.minecraft.util.text.ITextComponent) GameProfile(com.mojang.authlib.GameProfile) Version(net.minecraft.network.ServerStatusResponse.Version) ServerStatusResponse(net.minecraft.network.ServerStatusResponse) ServerListPingEvent(me.devtec.theapi.bukkit.events.ServerListPingEvent) SServerInfoPacket(net.minecraft.network.status.server.SServerInfoPacket) Players(net.minecraft.network.ServerStatusResponse.Players)

Aggregations

GameProfile (com.mojang.authlib.GameProfile)2 ArrayList (java.util.ArrayList)2 ServerStatusResponse (net.minecraft.network.ServerStatusResponse)2 SServerInfoPacket (net.minecraft.network.status.server.SServerInfoPacket)2 ArclightPingEvent (io.izzel.arclight.common.mod.util.ArclightPingEvent)1 Channel (io.netty.channel.Channel)1 ServerListPingEvent (me.devtec.theapi.bukkit.events.ServerListPingEvent)1 PlayerProfile (me.devtec.theapi.bukkit.events.ServerListPingEvent.PlayerProfile)1 Players (net.minecraft.network.ServerStatusResponse.Players)1 Version (net.minecraft.network.ServerStatusResponse.Version)1 ITextComponent (net.minecraft.util.text.ITextComponent)1 StringTextComponent (net.minecraft.util.text.StringTextComponent)1 CraftPlayer (org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer)1 Player (org.bukkit.entity.Player)1 Redirect (org.spongepowered.asm.mixin.injection.Redirect)1