Search in sources :

Example 1 with Protocol1_9To1_8

use of com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8 in project ViaBackwards by ViaVersion.

the class Protocol1_11_1To1_12 method registerPackets.

@Override
protected void registerPackets() {
    blockItemPackets.register();
    entityPackets.register();
    new SoundPackets1_12(this).register();
    new ChatPackets1_12(this).register();
    registerClientbound(ClientboundPackets1_12.TITLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int action = wrapper.passthrough(Type.VAR_INT);
                if (action >= 0 && action <= 2) {
                    JsonElement component = wrapper.read(Type.COMPONENT);
                    wrapper.write(Type.COMPONENT, Protocol1_9To1_8.fixJson(component.toString()));
                }
            });
        }
    });
    cancelClientbound(ClientboundPackets1_12.ADVANCEMENTS);
    cancelClientbound(ClientboundPackets1_12.UNLOCK_RECIPES);
    cancelClientbound(ClientboundPackets1_12.SELECT_ADVANCEMENTS_TAB);
}
Also used : Entity1_12Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types) ServerboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3) ServerboundPackets1_12(com.viaversion.viaversion.protocols.protocol1_12to1_11_1.ServerboundPackets1_12) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) ClientboundPackets1_12(com.viaversion.viaversion.protocols.protocol1_12to1_11_1.ClientboundPackets1_12) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) EntityPackets1_12(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.EntityPackets1_12) BackwardsProtocol(com.viaversion.viabackwards.api.BackwardsProtocol) ChatPackets1_12(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.ChatPackets1_12) BlockItemPackets1_12(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.BlockItemPackets1_12) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8) BackwardsMappings(com.viaversion.viabackwards.api.data.BackwardsMappings) SoundPackets1_12(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.SoundPackets1_12) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ClientboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3) ShoulderTracker(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.ShoulderTracker) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) SoundPackets1_12(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.SoundPackets1_12) ChatPackets1_12(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.ChatPackets1_12)

Example 2 with Protocol1_9To1_8

use of com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8 in project DirtMultiversion by DirtPowered.

the class ViaPlugin method tick.

@Override
public void tick() {
    for (UserData userData : api.getAllConnections()) {
        ProtocolStorage s = userData.getProtocolStorage();
        if (!s.hasObject(PlayerMovementTracker.class))
            return;
        PlayerMovementTracker movementTracker = s.get(PlayerMovementTracker.class);
        if ((System.currentTimeMillis() - movementTracker.getLastLocationUpdate()) >= 50) {
            if (userData.getUniqueId() == null)
                return;
            UserConnection userConnection = connectionManager.getConnectedClient(userData.getUniqueId());
            if (userConnection == null)
                return;
            ProtocolInfo protocolInfo = userConnection.getProtocolInfo();
            if (protocolInfo == null)
                return;
            if (protocolInfo.getPipeline().contains(Protocol1_9To1_8.class) && protocolInfo.getState() == State.PLAY) {
                PacketWrapper wrapper = new PacketWrapperImpl(0x03, null, userConnection);
                wrapper.write(Type.BOOLEAN, true);
                try {
                    wrapper.sendToServer(Protocol1_9To1_8.class, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Also used : ProtocolStorage(com.github.dirtpowered.dirtmv.data.user.ProtocolStorage) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) UserData(com.github.dirtpowered.dirtmv.data.user.UserData) PacketWrapperImpl(com.viaversion.viaversion.protocol.packet.PacketWrapperImpl) ProtocolInfo(com.viaversion.viaversion.api.connection.ProtocolInfo) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) PlayerMovementTracker(com.github.dirtpowered.dirtmv.network.versions.Release47To5.entity.PlayerMovementTracker) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8)

Example 3 with Protocol1_9To1_8

use of com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8 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 4 with Protocol1_9To1_8

use of com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8 in project ViaVersion by ViaVersion.

the class Protocol1_12To1_11_1 method registerPackets.

@Override
protected void registerPackets() {
    metadataRewriter.register();
    itemRewriter.register();
    registerClientbound(ClientboundPackets1_9_3.SPAWN_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // 1 - UUID
            map(Type.UUID);
            // 2 - Type
            map(Type.BYTE);
            // Track Entity
            handler(metadataRewriter.objectTrackerHandler());
        }
    });
    registerClientbound(ClientboundPackets1_9_3.SPAWN_MOB, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Entity UUID
            map(Type.UUID);
            // 2 - Entity Type
            map(Type.VAR_INT);
            // 3 - X
            map(Type.DOUBLE);
            // 4 - Y
            map(Type.DOUBLE);
            // 5 - Z
            map(Type.DOUBLE);
            // 6 - Yaw
            map(Type.BYTE);
            // 7 - Pitch
            map(Type.BYTE);
            // 8 - Head Pitch
            map(Type.BYTE);
            // 9 - Velocity X
            map(Type.SHORT);
            // 10 - Velocity Y
            map(Type.SHORT);
            // 11 - Velocity Z
            map(Type.SHORT);
            // 12 - Metadata
            map(Types1_12.METADATA_LIST);
            // Track mob and rewrite metadata
            handler(metadataRewriter.trackerAndRewriterHandler(Types1_12.METADATA_LIST));
        }
    });
    registerClientbound(ClientboundPackets1_9_3.CHAT_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (!Via.getConfig().is1_12NBTArrayFix())
                        return;
                    try {
                        JsonElement obj = Protocol1_9To1_8.FIX_JSON.transform(null, wrapper.passthrough(Type.COMPONENT).toString());
                        TranslateRewriter.toClient(obj, wrapper.user());
                        ChatItemRewriter.toClient(obj, wrapper.user());
                        wrapper.set(Type.COMPONENT, 0, obj);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld);
                    Chunk chunk = wrapper.passthrough(type);
                    for (int i = 0; i < chunk.getSections().length; i++) {
                        ChunkSection section = chunk.getSections()[i];
                        if (section == null)
                            continue;
                        for (int y = 0; y < 16; y++) {
                            for (int z = 0; z < 16; z++) {
                                for (int x = 0; x < 16; x++) {
                                    int block = section.getBlockWithoutData(x, y, z);
                                    // Is this a bed?
                                    if (block == 26) {
                                        // NBT -> { color:14, x:132, y:64, z:222, id:"minecraft:bed" } (Debug output)
                                        CompoundTag tag = new CompoundTag();
                                        // Set color to red (Default in previous versions)
                                        tag.put("color", new IntTag(14));
                                        tag.put("x", new IntTag(x + (chunk.getX() << 4)));
                                        tag.put("y", new IntTag(y + (i << 4)));
                                        tag.put("z", new IntTag(z + (chunk.getZ() << 4)));
                                        tag.put("id", new StringTag("minecraft:bed"));
                                        // Add a fake block entity
                                        chunk.getBlockEntities().add(tag);
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
    });
    metadataRewriter.registerRemoveEntities(ClientboundPackets1_9_3.DESTROY_ENTITIES);
    metadataRewriter.registerMetadataRewriter(ClientboundPackets1_9_3.ENTITY_METADATA, Types1_12.METADATA_LIST);
    registerClientbound(ClientboundPackets1_9_3.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.INT);
            map(Type.UNSIGNED_BYTE);
            map(Type.INT);
            handler(wrapper -> {
                ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
                int dimensionId = wrapper.get(Type.INT, 1);
                clientChunks.setEnvironment(dimensionId);
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.INT);
            handler(wrapper -> {
                ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                int dimensionId = wrapper.get(Type.INT, 0);
                clientWorld.setEnvironment(dimensionId);
            });
        }
    });
    new SoundRewriter(this, this::getNewSoundId).registerSound(ClientboundPackets1_9_3.SOUND);
    // New packet at 0x01
    cancelServerbound(ServerboundPackets1_12.PREPARE_CRAFTING_GRID);
    // Client Settings (max length changed)
    registerServerbound(ServerboundPackets1_12.CLIENT_SETTINGS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Locale
            map(Type.STRING);
            // 1 - view distance
            map(Type.BYTE);
            // 2 - chat mode
            map(Type.VAR_INT);
            // 3 - chat colors
            map(Type.BOOLEAN);
            // 4 - chat flags
            map(Type.UNSIGNED_BYTE);
            // 5 - main hand
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // As part of the fix for MC-111054, the max length of
                    // the locale was raised to 16 (from 7), and the client
                    // now makes sure that resource packs have names in that
                    // length.  However, for older servers, it is still 7,
                    // and thus the server will reject it (and the client
                    // won't know that the pack's invalid).
                    // The fix is to just silently lower the length.  The
                    // server doesn't actually use the locale anywhere, so
                    // this is fine.
                    String locale = wrapper.get(Type.STRING, 0);
                    if (locale.length() > 7) {
                        wrapper.set(Type.STRING, 0, locale.substring(0, 7));
                    }
                }
            });
        }
    });
    // New packet at 0x17
    cancelServerbound(ServerboundPackets1_12.RECIPE_BOOK_DATA);
    // New packet 0x19
    cancelServerbound(ServerboundPackets1_12.ADVANCEMENT_TAB);
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Types1_12(com.viaversion.viaversion.api.type.types.version.Types1_12) JsonElement(com.google.gson.JsonElement) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) MetadataRewriter1_12To1_11_1(com.viaversion.viaversion.protocols.protocol1_12to1_11_1.metadata.MetadataRewriter1_12To1_11_1) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ItemRewriter(com.viaversion.viaversion.api.rewriter.ItemRewriter) Entity1_12Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types) EntityRewriter(com.viaversion.viaversion.rewriter.EntityRewriter) ServerboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3) ViaProviders(com.viaversion.viaversion.api.platform.providers.ViaProviders) Chunk1_9_3_4Type(com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) InventoryPackets(com.viaversion.viaversion.protocols.protocol1_12to1_11_1.packets.InventoryPackets) Via(com.viaversion.viaversion.api.Via) AbstractProtocol(com.viaversion.viaversion.api.protocol.AbstractProtocol) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) InventoryQuickMoveProvider(com.viaversion.viaversion.protocols.protocol1_12to1_11_1.providers.InventoryQuickMoveProvider) ClientboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) Chunk1_9_3_4Type(com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) JsonElement(com.google.gson.JsonElement) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag)

Example 5 with Protocol1_9To1_8

use of com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8 in project ViaVersion by ViaVersion.

the class EntityPackets method register.

public static void register(Protocol1_9To1_8 protocol) {
    // Attach Entity Packet
    protocol.registerClientbound(ClientboundPackets1_8.ATTACH_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.INT);
            // 1 - Vehicle
            map(Type.INT);
            // Leash boolean is removed in new versions
            map(Type.BOOLEAN, new ValueTransformer<Boolean, Void>(Type.NOTHING) {

                @Override
                public Void transform(PacketWrapper wrapper, Boolean inputValue) throws Exception {
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    if (!inputValue) {
                        int passenger = wrapper.get(Type.INT, 0);
                        int vehicle = wrapper.get(Type.INT, 1);
                        // Don't send current packet
                        wrapper.cancel();
                        PacketWrapper passengerPacket = wrapper.create(ClientboundPackets1_9.SET_PASSENGERS);
                        if (vehicle == -1) {
                            if (!tracker.getVehicleMap().containsKey(passenger))
                                // Cancel
                                return null;
                            passengerPacket.write(Type.VAR_INT, tracker.getVehicleMap().remove(passenger));
                            passengerPacket.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] {});
                        } else {
                            passengerPacket.write(Type.VAR_INT, vehicle);
                            passengerPacket.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { passenger });
                            tracker.getVehicleMap().put(passenger, vehicle);
                        }
                        // Send the packet
                        passengerPacket.send(Protocol1_9To1_8.class);
                    }
                    return null;
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_TELEPORT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - X - Needs to be divide by 32
            map(Type.INT, SpawnPackets.toNewDouble);
            // 2 - Y - Needs to be divide by 32
            map(Type.INT, SpawnPackets.toNewDouble);
            // 3 - Z - Needs to be divide by 32
            map(Type.INT, SpawnPackets.toNewDouble);
            // 4 - Pitch
            map(Type.BYTE);
            // 5 - Yaw
            map(Type.BYTE);
            // 6 - On Ground
            map(Type.BOOLEAN);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    if (Via.getConfig().isHologramPatch()) {
                        EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                        if (tracker.getKnownHolograms().contains(entityID)) {
                            Double newValue = wrapper.get(Type.DOUBLE, 1);
                            newValue += (Via.getConfig().getHologramYOffset());
                            wrapper.set(Type.DOUBLE, 1, newValue);
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_POSITION_AND_ROTATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - X
            map(Type.BYTE, toNewShort);
            // 2 - Y
            map(Type.BYTE, toNewShort);
            // 3 - Z
            map(Type.BYTE, toNewShort);
            // 4 - Yaw
            map(Type.BYTE);
            // 5 - Pitch
            map(Type.BYTE);
            // 6 - On Ground
            map(Type.BOOLEAN);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - X
            map(Type.BYTE, toNewShort);
            // 2 - Y
            map(Type.BYTE, toNewShort);
            // 3 - Z
            map(Type.BYTE, toNewShort);
            // 4 - On Ground
            map(Type.BOOLEAN);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_EQUIPMENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Slot ID
            map(Type.SHORT, new ValueTransformer<Short, Integer>(Type.VAR_INT) {

                @Override
                public Integer transform(PacketWrapper wrapper, Short slot) throws Exception {
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    int receiverId = wrapper.user().getEntityTracker(Protocol1_9To1_8.class).clientEntityId();
                    // (In 1.9+, every client treats the received the same: 0=hand, 1=offhand, 2-5=armor)
                    if (entityId == receiverId) {
                        return slot.intValue() + 2;
                    }
                    return slot > 0 ? slot.intValue() + 1 : slot.intValue();
                }
            });
            // 2 - Item
            map(Type.ITEM);
            // Item Rewriter
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Item stack = wrapper.get(Type.ITEM, 0);
                    ItemRewriter.toClient(stack);
                }
            });
            // Blocking
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    Item stack = wrapper.get(Type.ITEM, 0);
                    if (stack != null) {
                        if (Protocol1_9To1_8.isSword(stack.identifier())) {
                            entityTracker.getValidBlocking().add(entityID);
                            return;
                        }
                    }
                    entityTracker.getValidBlocking().remove(entityID);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_METADATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Metadata List
            map(Types1_8.METADATA_LIST, Types1_9.METADATA_LIST);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    if (tracker.hasEntity(entityId)) {
                        protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, metadataList, wrapper.user());
                    } else {
                        // Buffer
                        tracker.addMetadataToBuffer(entityId, metadataList);
                        wrapper.cancel();
                    }
                }
            });
            // Handler for meta data
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.handleMetadata(entityID, metadataList);
                }
            });
            // Cancel packet if list empty
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    List<Metadata> metadataList = wrapper.get(Types1_9.METADATA_LIST, 0);
                    if (metadataList.isEmpty()) {
                        wrapper.cancel();
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Effect ID
            map(Type.BYTE);
            // 2 - Amplifier
            map(Type.BYTE);
            // 3 - Duration
            map(Type.VAR_INT);
            handler(new // Handle effect indicator
            PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // In 1.8 = true->Show particles : false->Hide particles
                    boolean showParticles = wrapper.read(Type.BOOLEAN);
                    boolean newEffect = Via.getConfig().isNewEffectIndicator();
                    // 0: hide, 1: shown without indictator, 2: shown with indicator, 3: hide with beacon indicator but we don't use it.
                    wrapper.write(Type.BYTE, (byte) (showParticles ? newEffect ? 2 : 1 : 0));
                }
            });
        }
    });
    protocol.cancelClientbound(ClientboundPackets1_8.UPDATE_ENTITY_NBT);
    protocol.registerClientbound(ClientboundPackets1_8.COMBAT_EVENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Event id
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (wrapper.get(Type.VAR_INT, 0) == 2) {
                        // entity dead
                        // Player id
                        wrapper.passthrough(Type.VAR_INT);
                        // Entity id
                        wrapper.passthrough(Type.INT);
                        Protocol1_9To1_8.FIX_JSON.write(wrapper, wrapper.read(Type.STRING));
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_PROPERTIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (!Via.getConfig().isMinimizeCooldown())
                        return;
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    if (wrapper.get(Type.VAR_INT, 0) != tracker.getProvidedEntityId()) {
                        return;
                    }
                    int propertiesToRead = wrapper.read(Type.INT);
                    Map<String, Pair<Double, List<Triple<UUID, Double, Byte>>>> properties = new HashMap<>(propertiesToRead);
                    for (int i = 0; i < propertiesToRead; i++) {
                        String key = wrapper.read(Type.STRING);
                        Double value = wrapper.read(Type.DOUBLE);
                        int modifiersToRead = wrapper.read(Type.VAR_INT);
                        List<Triple<UUID, Double, Byte>> modifiers = new ArrayList<>(modifiersToRead);
                        for (int j = 0; j < modifiersToRead; j++) {
                            modifiers.add(new Triple<>(wrapper.read(Type.UUID), // Amount
                            wrapper.read(Type.DOUBLE), // Operation
                            wrapper.read(Type.BYTE)));
                        }
                        properties.put(key, new Pair<>(value, modifiers));
                    }
                    // == Why 15.9? ==
                    // Higher values hides the cooldown but it bugs visual animation on hand
                    // when removing item from hand with inventory gui
                    properties.put("generic.attackSpeed", new Pair<Double, List<Triple<UUID, Double, Byte>>>(15.9, // Neutralize modifiers
                    ImmutableList.of(// Tool and weapon modifier
                    new Triple<>(UUID.fromString("FA233E1C-4180-4865-B01B-BCCE9785ACA3"), 0.0, (byte) 0), // Dig speed
                    new Triple<>(UUID.fromString("AF8B6E3F-3328-4C0A-AA36-5BA2BB9DBEF3"), 0.0, (byte) 2), // Dig slow down
                    new Triple<>(UUID.fromString("55FCED67-E92A-486E-9800-B47F202C4386"), 0.0, (byte) 2))));
                    wrapper.write(Type.INT, properties.size());
                    for (Map.Entry<String, Pair<Double, List<Triple<UUID, Double, Byte>>>> entry : properties.entrySet()) {
                        // Key
                        wrapper.write(Type.STRING, entry.getKey());
                        // Value
                        wrapper.write(Type.DOUBLE, entry.getValue().key());
                        wrapper.write(Type.VAR_INT, entry.getValue().value().size());
                        for (Triple<UUID, Double, Byte> modifier : entry.getValue().value()) {
                            wrapper.write(Type.UUID, modifier.first());
                            // Amount
                            wrapper.write(Type.DOUBLE, modifier.second());
                            // Operation
                            wrapper.write(Type.BYTE, modifier.third());
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.ENTITY_ANIMATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Animation
            map(Type.UNSIGNED_BYTE);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 3) {
                        wrapper.cancel();
                    }
                }
            });
        }
    });
    /* Incoming Packets */
    protocol.registerServerbound(ServerboundPackets1_9.ENTITY_ACTION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Player ID
            map(Type.VAR_INT);
            // 1 - Action
            map(Type.VAR_INT);
            // 2 - Jump
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int action = wrapper.get(Type.VAR_INT, 1);
                    if (action == 6 || action == 8)
                        wrapper.cancel();
                    if (action == 7) {
                        wrapper.set(Type.VAR_INT, 1, 6);
                    }
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.INTERACT_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID (Target)
            map(Type.VAR_INT);
            // 1 - Action Type
            map(Type.VAR_INT);
            // Cancel second hand to prevent double interact
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int type = wrapper.get(Type.VAR_INT, 1);
                    if (type == 2) {
                        // 2 - X
                        wrapper.passthrough(Type.FLOAT);
                        // 3 - Y
                        wrapper.passthrough(Type.FLOAT);
                        // 4 - Z
                        wrapper.passthrough(Type.FLOAT);
                    }
                    if (type == 0 || type == 2) {
                        // 2/5 - Hand
                        int hand = wrapper.read(Type.VAR_INT);
                        if (hand == 1)
                            wrapper.cancel();
                    }
                }
            });
        }
    });
}
Also used : HashMap(java.util.HashMap) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) ArrayList(java.util.ArrayList) Item(com.viaversion.viaversion.api.minecraft.item.Item) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) MetadataRewriter1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8) UUID(java.util.UUID) Pair(com.viaversion.viaversion.util.Pair) ValueTransformer(com.viaversion.viaversion.api.protocol.remapper.ValueTransformer) EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) Triple(com.viaversion.viaversion.util.Triple) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) HashMap(java.util.HashMap) Map(java.util.Map) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8)

Aggregations

PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)14 EntityTracker1_9 (com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9)10 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)7 Protocol1_9To1_8 (com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8)7 Item (com.viaversion.viaversion.api.minecraft.item.Item)6 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)6 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)5 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)5 Type (com.viaversion.viaversion.api.type.Type)4 Via (com.viaversion.viaversion.api.Via)3 ProtocolInfo (com.viaversion.viaversion.api.connection.ProtocolInfo)3 DataItem (com.viaversion.viaversion.api.minecraft.item.DataItem)3 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)3 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)2 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)2 JsonObject (com.google.gson.JsonObject)2 Position (com.viaversion.viaversion.api.minecraft.Position)2 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)2 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)2 Entity1_12Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types)2