Search in sources :

Example 1 with ClientConnection

use of net.minecraft.network.ClientConnection in project ImmersivePortalsMod by qouteall.

the class ClientWorldLoader method createFakedClientWorld.

// fool minecraft using the faked world
private ClientWorld createFakedClientWorld(DimensionType dimension) {
    assert mc.world.dimension.getType() == mc.player.dimension;
    assert (mc.player.dimension != dimension);
    isLoadingFakedWorld = true;
    // TODO get load distance
    int chunkLoadDistance = 3;
    WorldRenderer worldRenderer = new WorldRenderer(mc);
    ClientWorld newWorld;
    try {
        ClientPlayNetworkHandler newNetworkHandler = new ClientPlayNetworkHandler(mc, new ChatScreen("You should not be seeing me. I'm just a faked screen."), new ClientConnection(NetworkSide.CLIENTBOUND), new GameProfile(null, "faked_profiler_id"));
        // multiple net handlers share the same playerListEntries object
        ((IEClientPlayNetworkHandler) newNetworkHandler).setPlayerListEntries(((IEClientPlayNetworkHandler) mc.player.networkHandler).getPlayerListEntries());
        newWorld = new ClientWorld(newNetworkHandler, new LevelInfo(0L, GameMode.CREATIVE, true, isHardCore, LevelGeneratorType.FLAT), dimension, chunkLoadDistance, mc.getProfiler(), worldRenderer);
    } catch (Exception e) {
        throw new IllegalStateException("Creating Faked World " + dimension + " " + clientWorldMap.keySet(), e);
    }
    worldRenderer.setWorld(newWorld);
    worldRenderer.apply(mc.getResourceManager());
    ((IEClientPlayNetworkHandler) ((IEClientWorld) newWorld).getNetHandler()).setWorld(newWorld);
    clientWorldMap.put(dimension, newWorld);
    worldRendererMap.put(dimension, worldRenderer);
    Helper.log("Faked World Created " + dimension);
    isLoadingFakedWorld = false;
    return newWorld;
}
Also used : ChatScreen(net.minecraft.client.gui.screen.ChatScreen) IEClientPlayNetworkHandler(com.qouteall.immersive_portals.ducks.IEClientPlayNetworkHandler) GameProfile(com.mojang.authlib.GameProfile) LevelInfo(net.minecraft.world.level.LevelInfo) ClientConnection(net.minecraft.network.ClientConnection) IEClientWorld(com.qouteall.immersive_portals.ducks.IEClientWorld) ClientWorld(net.minecraft.client.world.ClientWorld) WorldRenderer(net.minecraft.client.render.WorldRenderer) IEClientPlayNetworkHandler(com.qouteall.immersive_portals.ducks.IEClientPlayNetworkHandler) ClientPlayNetworkHandler(net.minecraft.client.network.ClientPlayNetworkHandler)

Example 2 with ClientConnection

use of net.minecraft.network.ClientConnection in project Client by MatHax.

the class ServerListPinger method add.

public void add(final MServerInfo entry, final Runnable runnable) throws UnknownHostException {
    Timer timeoutTimer = new Timer();
    ServerAddress serverAddress = ServerAddress.parse(entry.address);
    timeoutTimer.schedule(new TimerTask() {

        @Override
        public void run() {
            notifyDisconnectListeners();
        }
    }, 20000);
    final ClientConnection clientConnection = ClientConnection.connect(new InetSocketAddress(InetAddress.getByName(serverAddress.getAddress()), serverAddress.getPort()), false);
    failedToConnect = false;
    this.clientConnections.add(clientConnection);
    entry.label = "multiplayer.status.pinging";
    entry.ping = -1L;
    entry.playerListSummary = null;
    clientConnection.setPacketListener(new ClientQueryPacketListener() {

        private boolean sentQuery;

        private boolean received;

        private long startTime;

        public void onResponse(QueryResponseS2CPacket packet) {
            if (this.received)
                clientConnection.disconnect(new TranslatableText("multiplayer.status.unrequested"));
            else {
                this.received = true;
                ServerMetadata serverMetadata = packet.getServerMetadata();
                if (serverMetadata.getDescription() != null)
                    entry.label = serverMetadata.getDescription().asString();
                else
                    entry.label = "";
                if (serverMetadata.getVersion() != null) {
                    entry.version = serverMetadata.getVersion().getGameVersion();
                    entry.protocolVersion = serverMetadata.getVersion().getProtocolVersion();
                } else {
                    entry.version = "multiplayer.status.old";
                    entry.protocolVersion = 0;
                }
                if (serverMetadata.getPlayers() != null) {
                    entry.playerCountLabel = ServerListPinger.getPlayerCountLabel(serverMetadata.getPlayers().getOnlinePlayerCount(), serverMetadata.getPlayers().getPlayerLimit());
                    entry.playerCount = serverMetadata.getPlayers().getOnlinePlayerCount();
                    entry.playercountMax = serverMetadata.getPlayers().getPlayerLimit();
                    List<Text> list = Lists.newArrayList();
                    if (ArrayUtils.isNotEmpty(serverMetadata.getPlayers().getSample())) {
                        GameProfile[] gameProfiles = serverMetadata.getPlayers().getSample();
                        for (GameProfile gameProfile : gameProfiles) {
                            list.add(new LiteralText(gameProfile.getName()));
                        }
                        if (serverMetadata.getPlayers().getSample().length < serverMetadata.getPlayers().getOnlinePlayerCount())
                            list.add(new TranslatableText("multiplayer.status.and_more", serverMetadata.getPlayers().getOnlinePlayerCount() - serverMetadata.getPlayers().getSample().length));
                        entry.playerListSummary = list;
                    }
                } else
                    entry.playerCountLabel = "multiplayer.status.unknown";
                String string = null;
                if (serverMetadata.getFavicon() != null) {
                    String string2 = serverMetadata.getFavicon();
                    if (string2.startsWith("data:image/png;base64,"))
                        string = string2.substring("data:image/png;base64,".length());
                    else
                        MatHax.LOG.error("Invalid server icon (unknown format)");
                }
                if (!Objects.equals(string, entry.getIcon())) {
                    entry.setIcon(string);
                    runnable.run();
                }
                this.startTime = Util.getMeasuringTimeMs();
                clientConnection.send(new QueryPingC2SPacket(this.startTime));
                this.sentQuery = true;
                notifyDisconnectListeners();
            }
        }

        public void onPong(QueryPongS2CPacket packet) {
            long l = this.startTime;
            long m = Util.getMeasuringTimeMs();
            entry.ping = m - l;
            clientConnection.disconnect(new TranslatableText("multiplayer.status.finished"));
        }

        public void onDisconnected(Text reason) {
            if (!this.sentQuery) {
                MatHax.LOG.error("Can't ping {}: {}", entry.address, reason.getString());
                entry.label = "multiplayer.status.cannot_connect";
                entry.playerCountLabel = "";
                entry.playerCount = 0;
                entry.playercountMax = 0;
                ServerListPinger.this.ping(entry);
            }
            notifyDisconnectListeners();
        }

        public ClientConnection getConnection() {
            return clientConnection;
        }
    });
    try {
        clientConnection.send(new HandshakeC2SPacket(serverAddress.getAddress(), serverAddress.getPort(), NetworkState.STATUS));
        clientConnection.send(new QueryRequestC2SPacket());
    } catch (Throwable throwable) {
        MatHax.LOG.error("[MatHax] Couldn't send handshake", throwable);
    }
}
Also used : TranslatableText(net.minecraft.text.TranslatableText) QueryPingC2SPacket(net.minecraft.network.packet.c2s.query.QueryPingC2SPacket) HandshakeC2SPacket(net.minecraft.network.packet.c2s.handshake.HandshakeC2SPacket) InetSocketAddress(java.net.InetSocketAddress) QueryResponseS2CPacket(net.minecraft.network.packet.s2c.query.QueryResponseS2CPacket) ServerAddress(net.minecraft.client.network.ServerAddress) ServerMetadata(net.minecraft.server.ServerMetadata) LiteralText(net.minecraft.text.LiteralText) TranslatableText(net.minecraft.text.TranslatableText) Text(net.minecraft.text.Text) QueryPongS2CPacket(net.minecraft.network.packet.s2c.query.QueryPongS2CPacket) ClientQueryPacketListener(net.minecraft.network.listener.ClientQueryPacketListener) GameProfile(com.mojang.authlib.GameProfile) QueryRequestC2SPacket(net.minecraft.network.packet.c2s.query.QueryRequestC2SPacket) ClientConnection(net.minecraft.network.ClientConnection) LiteralText(net.minecraft.text.LiteralText)

Example 3 with ClientConnection

use of net.minecraft.network.ClientConnection in project Client by MatHax.

the class ServerListPinger method cancel.

public void cancel() {
    synchronized (this.clientConnections) {
        Iterator<ClientConnection> iterator = this.clientConnections.iterator();
        while (iterator.hasNext()) {
            ClientConnection clientConnection = iterator.next();
            if (clientConnection.isOpen()) {
                iterator.remove();
                clientConnection.disconnect(new TranslatableText("multiplayer.status.cancelled"));
            }
        }
    }
}
Also used : TranslatableText(net.minecraft.text.TranslatableText) ClientConnection(net.minecraft.network.ClientConnection)

Example 4 with ClientConnection

use of net.minecraft.network.ClientConnection in project Client by MatHax.

the class ServerListPinger method tick.

public void tick() {
    synchronized (this.clientConnections) {
        Iterator<ClientConnection> iterator = this.clientConnections.iterator();
        while (iterator.hasNext()) {
            ClientConnection clientConnection = iterator.next();
            if (clientConnection.isOpen())
                clientConnection.tick();
            else {
                iterator.remove();
                clientConnection.handleDisconnection();
            }
        }
    }
}
Also used : ClientConnection(net.minecraft.network.ClientConnection)

Aggregations

ClientConnection (net.minecraft.network.ClientConnection)4 GameProfile (com.mojang.authlib.GameProfile)2 TranslatableText (net.minecraft.text.TranslatableText)2 IEClientPlayNetworkHandler (com.qouteall.immersive_portals.ducks.IEClientPlayNetworkHandler)1 IEClientWorld (com.qouteall.immersive_portals.ducks.IEClientWorld)1 InetSocketAddress (java.net.InetSocketAddress)1 ChatScreen (net.minecraft.client.gui.screen.ChatScreen)1 ClientPlayNetworkHandler (net.minecraft.client.network.ClientPlayNetworkHandler)1 ServerAddress (net.minecraft.client.network.ServerAddress)1 WorldRenderer (net.minecraft.client.render.WorldRenderer)1 ClientWorld (net.minecraft.client.world.ClientWorld)1 ClientQueryPacketListener (net.minecraft.network.listener.ClientQueryPacketListener)1 HandshakeC2SPacket (net.minecraft.network.packet.c2s.handshake.HandshakeC2SPacket)1 QueryPingC2SPacket (net.minecraft.network.packet.c2s.query.QueryPingC2SPacket)1 QueryRequestC2SPacket (net.minecraft.network.packet.c2s.query.QueryRequestC2SPacket)1 QueryPongS2CPacket (net.minecraft.network.packet.s2c.query.QueryPongS2CPacket)1 QueryResponseS2CPacket (net.minecraft.network.packet.s2c.query.QueryResponseS2CPacket)1 ServerMetadata (net.minecraft.server.ServerMetadata)1 LiteralText (net.minecraft.text.LiteralText)1 Text (net.minecraft.text.Text)1