Search in sources :

Example 1 with ServerStatusResponse

use of net.minecraft.network.ServerStatusResponse in project SpongeCommon by SpongePowered.

the class MixinLegacyPingHandler method readLegacy.

private boolean readLegacy(ChannelHandlerContext ctx, ByteBuf buf) {
    if (buf.readUnsignedByte() != 0xFE) {
        return false;
    }
    MinecraftServer server = this.networkSystem.getServer();
    InetSocketAddress client = (InetSocketAddress) ctx.channel().remoteAddress();
    ServerStatusResponse response;
    int i = buf.readableBytes();
    switch(i) {
        case 0:
            LOGGER.debug("Ping: (<=1.3) from {}:{}", client.getAddress(), client.getPort());
            response = SpongeStatusResponse.postLegacy(server, client, SpongeLegacyMinecraftVersion.V1_3, null);
            if (response != null) {
                this.writeResponse(ctx, String.format("%s§%d§%d", SpongeStatusResponse.getUnformattedMotd(response), response.getPlayers().getOnlinePlayerCount(), response.getPlayers().getMaxPlayers()));
            } else {
                ctx.close();
            }
            break;
        case 1:
            if (buf.readUnsignedByte() != 0x01) {
                return false;
            }
            LOGGER.debug("Ping: (1.4-1.5) from {}:{}", client.getAddress(), client.getPort());
            response = SpongeStatusResponse.postLegacy(server, client, SpongeLegacyMinecraftVersion.V1_5, null);
            if (response != null) {
                this.writeResponse(ctx, String.format("§1\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d", response.getVersion().getProtocol(), response.getVersion().getName(), SpongeStatusResponse.getMotd(response), response.getPlayers().getOnlinePlayerCount(), response.getPlayers().getMaxPlayers()));
            } else {
                ctx.close();
            }
            break;
        default:
            if (buf.readUnsignedByte() != 0x01 || buf.readUnsignedByte() != 0xFA) {
                return false;
            }
            if (!buf.isReadable(2)) {
                break;
            }
            short length = buf.readShort();
            if (!buf.isReadable(length * 2)) {
                break;
            }
            if (!buf.readBytes(length * 2).toString(Charsets.UTF_16BE).equals("MC|PingHost")) {
                return false;
            }
            if (!buf.isReadable(2)) {
                break;
            }
            length = buf.readShort();
            if (!buf.isReadable(length)) {
                break;
            }
            int protocol = buf.readUnsignedByte();
            length = buf.readShort();
            String host = buf.readBytes(length * 2).toString(Charsets.UTF_16BE);
            int port = buf.readInt();
            LOGGER.debug("Ping: (1.6) from {}:{}", client.getAddress(), client.getPort());
            response = SpongeStatusResponse.postLegacy(server, client, new SpongeLegacyMinecraftVersion(SpongeLegacyMinecraftVersion.V1_6, protocol), InetSocketAddress.createUnresolved(NetworkUtil.cleanVirtualHost(host), port));
            if (response != null) {
                this.writeResponse(ctx, String.format("§1\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d", response.getVersion().getProtocol(), response.getVersion().getName(), SpongeStatusResponse.getMotd(response), response.getPlayers().getOnlinePlayerCount(), response.getPlayers().getMaxPlayers()));
            } else {
                ctx.close();
            }
            break;
    }
    return true;
}
Also used : SpongeLegacyMinecraftVersion(org.spongepowered.common.network.status.SpongeLegacyMinecraftVersion) InetSocketAddress(java.net.InetSocketAddress) ServerStatusResponse(net.minecraft.network.ServerStatusResponse) MinecraftServer(net.minecraft.server.MinecraftServer)

Example 2 with ServerStatusResponse

use of net.minecraft.network.ServerStatusResponse in project SpongeCommon by SpongePowered.

the class SpongeStatusResponse method clone.

private static ServerStatusResponse clone(ServerStatusResponse original) {
    ServerStatusResponse clone = new ServerStatusResponse();
    clone.setServerDescription(original.getServerDescription());
    if (original.getFavicon() != null) {
        ((ClientPingServerEvent.Response) clone).setFavicon(((StatusResponse) original).getFavicon().get());
    }
    clone.setPlayers(clone(original.getPlayers()));
    clone.setVersion(clone(original.getVersion()));
    return clone;
}
Also used : ServerStatusResponse(net.minecraft.network.ServerStatusResponse) StatusResponse(org.spongepowered.api.network.status.StatusResponse) ServerStatusResponse(net.minecraft.network.ServerStatusResponse) ServerStatusResponse(net.minecraft.network.ServerStatusResponse) StatusResponse(org.spongepowered.api.network.status.StatusResponse)

Example 3 with ServerStatusResponse

use of net.minecraft.network.ServerStatusResponse in project SpongeVanilla by SpongePowered.

the class DedicatedServerMixin_Vanilla method vanilla$onServerInitialize.

@Inject(method = "init()Z", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/dedicated/DedicatedPlayerList;<init>(Lnet/minecraft/server/dedicated/DedicatedServer;)V", shift = At.Shift.BEFORE))
private void vanilla$onServerInitialize(CallbackInfoReturnable<Boolean> ci) {
    if (this.getFolderName() == null) {
        this.setFolderName(this.settings.getStringProperty("level-name", "world"));
    }
    vanilla$spongeVanilla.initialize();
    ServerStatusResponse statusResponse = getServerStatusResponse();
    statusResponse.setServerDescription(new TextComponentString(this.getMotd()));
    statusResponse.setVersion(new ServerStatusResponse.Version(MINECRAFT_VERSION.getName(), MINECRAFT_VERSION.getProtocol()));
    this.applyServerIconToResponse(statusResponse);
}
Also used : ServerStatusResponse(net.minecraft.network.ServerStatusResponse) TextComponentString(net.minecraft.util.text.TextComponentString) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 4 with ServerStatusResponse

use of net.minecraft.network.ServerStatusResponse 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 5 with ServerStatusResponse

use of net.minecraft.network.ServerStatusResponse in project Magma by magmafoundation.

the class StandardPaperServerListPingEventImpl method processRequest.

@SuppressWarnings("deprecation")
public static void processRequest(MinecraftServer server, NetworkManager networkManager) {
    StandardPaperServerListPingEventImpl event = new StandardPaperServerListPingEventImpl(server, networkManager, server.getServerStatusResponse());
    server.server.getPluginManager().callEvent(event);
    // Close connection immediately if event is cancelled
    if (event.isCancelled()) {
        networkManager.closeChannel(null);
        return;
    }
    // Setup response
    ServerStatusResponse ping = new ServerStatusResponse();
    // Description
    ping.setServerDescription(new TextComponentString(event.getMotd()));
    // Players
    if (!event.shouldHidePlayers()) {
        ping.setPlayers(new ServerStatusResponse.Players(event.getMaxPlayers(), event.getNumPlayers()));
        ping.getPlayers().setPlayers(event.getPlayerSampleHandle());
    }
    // Version
    ping.setVersion(new ServerStatusResponse.Version(event.getVersion(), event.getProtocolVersion()));
    // Favicon
    if (event.getServerIcon() != null) {
        ping.setFavicon(event.getServerIcon().getData());
    }
    // Send response
    networkManager.sendPacket(new SPacketServerInfo(ping));
}
Also used : ServerStatusResponse(net.minecraft.network.ServerStatusResponse) TextComponentString(net.minecraft.util.text.TextComponentString) SPacketServerInfo(net.minecraft.network.status.server.SPacketServerInfo)

Aggregations

ServerStatusResponse (net.minecraft.network.ServerStatusResponse)12 GameProfile (com.mojang.authlib.GameProfile)4 NetworkManager (net.minecraft.network.NetworkManager)4 StringTextComponent (net.minecraft.util.text.StringTextComponent)3 TextComponentString (net.minecraft.util.text.TextComponentString)3 MoreObjects (com.google.common.base.MoreObjects)2 Strings (com.google.common.base.Strings)2 HashBasedTable (com.google.common.collect.HashBasedTable)2 HashMultimap (com.google.common.collect.HashMultimap)2 SetMultimap (com.google.common.collect.SetMultimap)2 Sets (com.google.common.collect.Sets)2 Table (com.google.common.collect.Table)2 MatrixStack (com.mojang.blaze3d.matrix.MatrixStack)2 File (java.io.File)2 java.util (java.util)2 ArrayList (java.util.ArrayList)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Collectors (java.util.stream.Collectors)2 Nullable (javax.annotation.Nullable)2 Minecraft (net.minecraft.client.Minecraft)2