Search in sources :

Example 1 with Protocol

use of com.viaversion.viaversion.api.protocol.Protocol in project ViaVersion by ViaVersion.

the class BungeeServerHandler method checkServerChange.

public void checkServerChange(ServerConnectedEvent e, UserConnection user) throws Exception {
    if (user == null)
        return;
    // Handle server/version change
    if (user.has(BungeeStorage.class)) {
        BungeeStorage storage = user.get(BungeeStorage.class);
        ProxiedPlayer player = storage.getPlayer();
        if (e.getServer() != null) {
            if (!e.getServer().getInfo().getName().equals(storage.getCurrentServer())) {
                // Clear auto-team
                EntityTracker1_9 oldEntityTracker = user.getEntityTracker(Protocol1_9To1_8.class);
                if (oldEntityTracker != null) {
                    if (oldEntityTracker.isAutoTeam() && oldEntityTracker.isTeamExists()) {
                        oldEntityTracker.sendTeamPacket(false, true);
                    }
                }
                String serverName = e.getServer().getInfo().getName();
                storage.setCurrentServer(serverName);
                int protocolId = ProtocolDetectorService.getProtocolId(serverName);
                if (protocolId <= ProtocolVersion.v1_8.getVersion()) {
                    // 1.8 doesn't have BossBar packet
                    if (storage.getBossbar() != null) {
                        // This ensures we can encode it properly as only the 1.9 protocol is currently implemented.
                        if (user.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) {
                            for (UUID uuid : storage.getBossbar()) {
                                PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.BOSSBAR, null, user);
                                wrapper.write(Type.UUID, uuid);
                                // remove
                                wrapper.write(Type.VAR_INT, 1);
                                wrapper.send(Protocol1_9To1_8.class);
                            }
                        }
                        storage.getBossbar().clear();
                    }
                }
                ProtocolInfo info = user.getProtocolInfo();
                int previousServerProtocol = info.getServerProtocolVersion();
                // Refresh the pipes
                List<ProtocolPathEntry> protocolPath = Via.getManager().getProtocolManager().getProtocolPath(info.getProtocolVersion(), protocolId);
                ProtocolPipeline pipeline = user.getProtocolInfo().getPipeline();
                user.clearStoredObjects();
                pipeline.cleanPipes();
                if (protocolPath == null) {
                    // TODO Check Bungee Supported Protocols? *shrugs*
                    protocolId = info.getProtocolVersion();
                } else {
                    List<Protocol> protocols = new ArrayList<>(protocolPath.size());
                    for (ProtocolPathEntry entry : protocolPath) {
                        protocols.add(entry.protocol());
                    }
                    pipeline.add(protocols);
                }
                info.setServerProtocolVersion(protocolId);
                // Add version-specific base Protocol
                pipeline.add(Via.getManager().getProtocolManager().getBaseProtocol(protocolId));
                // Workaround 1.13 server change
                int id1_13 = ProtocolVersion.v1_13.getVersion();
                boolean toNewId = previousServerProtocol < id1_13 && protocolId >= id1_13;
                boolean toOldId = previousServerProtocol >= id1_13 && protocolId < id1_13;
                if (previousServerProtocol != -1 && (toNewId || toOldId)) {
                    Collection<String> registeredChannels = (Collection<String>) getRegisteredChannels.invoke(e.getPlayer().getPendingConnection());
                    if (!registeredChannels.isEmpty()) {
                        Collection<String> newChannels = new HashSet<>();
                        for (Iterator<String> iterator = registeredChannels.iterator(); iterator.hasNext(); ) {
                            String channel = iterator.next();
                            String oldChannel = channel;
                            if (toNewId) {
                                channel = InventoryPackets.getNewPluginChannelId(channel);
                            } else {
                                channel = InventoryPackets.getOldPluginChannelId(channel);
                            }
                            if (channel == null) {
                                iterator.remove();
                                continue;
                            }
                            if (!oldChannel.equals(channel)) {
                                iterator.remove();
                                newChannels.add(channel);
                            }
                        }
                        registeredChannels.addAll(newChannels);
                    }
                    PluginMessage brandMessage = (PluginMessage) getBrandMessage.invoke(e.getPlayer().getPendingConnection());
                    if (brandMessage != null) {
                        String channel = brandMessage.getTag();
                        if (toNewId) {
                            channel = InventoryPackets.getNewPluginChannelId(channel);
                        } else {
                            channel = InventoryPackets.getOldPluginChannelId(channel);
                        }
                        if (channel != null) {
                            brandMessage.setTag(channel);
                        }
                    }
                }
                user.put(storage);
                user.setActive(protocolPath != null);
                // Init all protocols TODO check if this can get moved up to the previous for loop, and doesn't require the pipeline to already exist.
                for (Protocol protocol : pipeline.pipes()) {
                    protocol.init(user);
                }
                EntityTracker1_9 newTracker = user.getEntityTracker(Protocol1_9To1_8.class);
                if (newTracker != null) {
                    if (Via.getConfig().isAutoTeam()) {
                        String currentTeam = null;
                        for (Team team : player.getScoreboard().getTeams()) {
                            if (team.getPlayers().contains(info.getUsername())) {
                                currentTeam = team.getName();
                            }
                        }
                        // Reinitialize auto-team
                        newTracker.setAutoTeam(true);
                        if (currentTeam == null) {
                            // Send auto-team as it was cleared above
                            newTracker.sendTeamPacket(true, true);
                            newTracker.setCurrentTeam("viaversion");
                        } else {
                            // Auto-team will be sent when bungee send remove packet
                            newTracker.setAutoTeam(Via.getConfig().isAutoTeam());
                            newTracker.setCurrentTeam(currentTeam);
                        }
                    }
                }
                Object wrapper = channelWrapper.get(player);
                setVersion.invoke(wrapper, protocolId);
                Object entityMap = getEntityMap.invoke(null, protocolId);
                entityRewrite.set(player, entityMap);
            }
        }
    }
}
Also used : ProxiedPlayer(net.md_5.bungee.api.connection.ProxiedPlayer) ProtocolPathEntry(com.viaversion.viaversion.api.protocol.ProtocolPathEntry) EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) BungeeStorage(com.viaversion.viaversion.bungee.storage.BungeeStorage) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ProtocolPipeline(com.viaversion.viaversion.api.protocol.ProtocolPipeline) PluginMessage(net.md_5.bungee.protocol.packet.PluginMessage) ProtocolInfo(com.viaversion.viaversion.api.connection.ProtocolInfo) Team(net.md_5.bungee.api.score.Team) StorableObject(com.viaversion.viaversion.api.connection.StorableObject) Protocol(com.viaversion.viaversion.api.protocol.Protocol)

Example 2 with Protocol

use of com.viaversion.viaversion.api.protocol.Protocol in project ViaVersion by ViaVersion.

the class ProtocolPipelineImpl method registerPackets.

@Override
protected void registerPackets() {
    protocolList = new CopyOnWriteArrayList<>();
    // Create a backing set for faster contains calls with larger pipes
    protocolSet = Sets.newSetFromMap(new ConcurrentHashMap<>());
    // This is a pipeline so we register basic pipes
    Protocol baseProtocol = Via.getManager().getProtocolManager().getBaseProtocol();
    protocolList.add(baseProtocol);
    protocolSet.add(baseProtocol.getClass());
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Protocol(com.viaversion.viaversion.api.protocol.Protocol) AbstractSimpleProtocol(com.viaversion.viaversion.api.protocol.AbstractSimpleProtocol)

Example 3 with Protocol

use of com.viaversion.viaversion.api.protocol.Protocol in project ViaVersion by ViaVersion.

the class ProtocolPipelineImpl method add.

@Override
public void add(Collection<Protocol> protocols) {
    protocolList.addAll(protocols);
    for (Protocol protocol : protocols) {
        protocol.init(userConnection);
        this.protocolSet.add(protocol.getClass());
    }
    moveBaseProtocolsToTail();
}
Also used : Protocol(com.viaversion.viaversion.api.protocol.Protocol) AbstractSimpleProtocol(com.viaversion.viaversion.api.protocol.AbstractSimpleProtocol)

Example 4 with Protocol

use of com.viaversion.viaversion.api.protocol.Protocol in project ViaVersion by ViaVersion.

the class ProtocolPipelineImpl method moveBaseProtocolsToTail.

private void moveBaseProtocolsToTail() {
    // Move base Protocols to the end, so the login packets can be modified by other protocols
    List<Protocol> baseProtocols = null;
    for (Protocol protocol : protocolList) {
        if (protocol.isBaseProtocol()) {
            if (baseProtocols == null) {
                baseProtocols = new ArrayList<>();
            }
            baseProtocols.add(protocol);
        }
    }
    if (baseProtocols != null) {
        protocolList.removeAll(baseProtocols);
        protocolList.addAll(baseProtocols);
    }
}
Also used : Protocol(com.viaversion.viaversion.api.protocol.Protocol) AbstractSimpleProtocol(com.viaversion.viaversion.api.protocol.AbstractSimpleProtocol)

Example 5 with Protocol

use of com.viaversion.viaversion.api.protocol.Protocol in project ViaVersion by ViaVersion.

the class PacketWrapperImpl method constructPacket.

/**
 * Let the packet go through the protocol pipes and write it to ByteBuf
 *
 * @param packetProtocol      The protocol version of the packet.
 * @param skipCurrentPipeline Skip the current pipeline
 * @return Packet buffer
 * @throws Exception if it fails to write
 */
private ByteBuf constructPacket(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, Direction direction) throws Exception {
    // Apply current pipeline - for outgoing protocol, the collection will be reversed in the apply method
    Protocol[] protocols = user().getProtocolInfo().getPipeline().pipes().toArray(PROTOCOL_ARRAY);
    boolean reverse = direction == Direction.CLIENTBOUND;
    int index = -1;
    for (int i = 0; i < protocols.length; i++) {
        if (protocols[i].getClass() == packetProtocol) {
            index = i;
            break;
        }
    }
    if (index == -1) {
        // The given protocol is not in the pipeline
        throw new NoSuchElementException(packetProtocol.getCanonicalName());
    }
    if (skipCurrentPipeline) {
        index = reverse ? index - 1 : index + 1;
    }
    // Reset reader before we start
    resetReader();
    // Apply other protocols
    apply(direction, user().getProtocolInfo().getState(), index, protocols, reverse);
    ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer();
    try {
        writeToBuffer(output);
        return output.retain();
    } finally {
        output.release();
    }
}
Also used : Protocol(com.viaversion.viaversion.api.protocol.Protocol) ByteBuf(io.netty.buffer.ByteBuf) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

Protocol (com.viaversion.viaversion.api.protocol.Protocol)13 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)5 Type (com.viaversion.viaversion.api.type.Type)5 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)4 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)3 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)3 AbstractSimpleProtocol (com.viaversion.viaversion.api.protocol.AbstractSimpleProtocol)3 ProtocolPathEntry (com.viaversion.viaversion.api.protocol.ProtocolPathEntry)3 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)3 ArrayList (java.util.ArrayList)3 Nullable (org.checkerframework.checker.nullness.qual.Nullable)3 Via (com.viaversion.viaversion.api.Via)2 BaseProtocol (com.viaversion.viaversion.protocols.base.BaseProtocol)2 ClientboundPackets1_13 (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13)2 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)2 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)2 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)1 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)1 ProtocolInfo (com.viaversion.viaversion.api.connection.ProtocolInfo)1 StorableObject (com.viaversion.viaversion.api.connection.StorableObject)1