Search in sources :

Example 26 with PacketHandler

use of com.viaversion.viaversion.api.protocol.remapper.PacketHandler in project ViaVersion by ViaVersion.

the class Protocol1_11To1_10 method registerPackets.

@Override
protected void registerPackets() {
    entityRewriter.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(entityRewriter.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.UNSIGNED_BYTE, 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_9.METADATA_LIST);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    // Change Type :)
                    int type = wrapper.get(Type.VAR_INT, 1);
                    Entity1_11Types.EntityType entType = MetadataRewriter1_11To1_10.rewriteEntityType(type, wrapper.get(Types1_9.METADATA_LIST, 0));
                    if (entType != null) {
                        wrapper.set(Type.VAR_INT, 1, entType.getId());
                        // Register Type ID
                        wrapper.user().getEntityTracker(Protocol1_11To1_10.class).addEntity(entityId, entType);
                        entityRewriter.handleMetadata(entityId, wrapper.get(Types1_9.METADATA_LIST, 0), wrapper.user());
                    }
                }
            });
        }
    });
    new SoundRewriter(this, this::getNewSoundId).registerSound(ClientboundPackets1_9_3.SOUND);
    registerClientbound(ClientboundPackets1_9_3.COLLECT_ITEM, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Collected entity id
            map(Type.VAR_INT);
            // 1 - Collector entity id
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // 2 - Pickup Count
                    wrapper.write(Type.VAR_INT, 1);
                }
            });
        }
    });
    entityRewriter.registerMetadataRewriter(ClientboundPackets1_9_3.ENTITY_METADATA, Types1_9.METADATA_LIST);
    registerClientbound(ClientboundPackets1_9_3.ENTITY_TELEPORT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // 1 - x
            map(Type.DOUBLE);
            // 2 - y
            map(Type.DOUBLE);
            // 3 - z
            map(Type.DOUBLE);
            // 4 - yaw
            map(Type.BYTE);
            // 5 - pitch
            map(Type.BYTE);
            // 6 - onGround
            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_11 tracker = wrapper.user().getEntityTracker(Protocol1_11To1_10.class);
                        if (tracker.isHologram(entityID)) {
                            Double newValue = wrapper.get(Type.DOUBLE, 1);
                            newValue -= (Via.getConfig().getHologramYOffset());
                            wrapper.set(Type.DOUBLE, 1, newValue);
                        }
                    }
                }
            });
        }
    });
    entityRewriter.registerRemoveEntities(ClientboundPackets1_9_3.DESTROY_ENTITIES);
    registerClientbound(ClientboundPackets1_9_3.TITLE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int action = wrapper.get(Type.VAR_INT, 0);
                    // Handle the new ActionBar
                    if (action >= 2) {
                        wrapper.set(Type.VAR_INT, 0, action + 1);
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.BLOCK_ACTION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Position
            map(Type.POSITION);
            // 1 - Action ID
            map(Type.UNSIGNED_BYTE);
            // 2 - Action Param
            map(Type.UNSIGNED_BYTE);
            // 3 - Block Type
            map(Type.VAR_INT);
            // Cheap hack to ensure it's always right block
            handler(new PacketHandler() {

                @Override
                public void handle(final PacketWrapper actionWrapper) throws Exception {
                    if (Via.getConfig().isPistonAnimationPatch()) {
                        int id = actionWrapper.get(Type.VAR_INT, 0);
                        if (id == 33 || id == 29) {
                            actionWrapper.cancel();
                        }
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Position
            map(Type.POSITION);
            // 1 - Action
            map(Type.UNSIGNED_BYTE);
            // 2 - NBT data
            map(Type.NBT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    CompoundTag tag = wrapper.get(Type.NBT, 0);
                    if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 1)
                        EntityIdRewriter.toClientSpawner(tag);
                    if (tag.contains("id"))
                        // Handle new identifier
                        ((StringTag) tag.get("id")).setValue(BlockEntityRewriter.toNewIdentifier((String) tag.get("id").getValue()));
                }
            });
        }
    });
    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);
                    Chunk chunk = wrapper.passthrough(new Chunk1_9_3_4Type(clientWorld));
                    if (chunk.getBlockEntities() == null)
                        return;
                    for (CompoundTag tag : chunk.getBlockEntities()) {
                        if (tag.contains("id")) {
                            String identifier = ((StringTag) tag.get("id")).getValue();
                            if (identifier.equals("MobSpawner")) {
                                EntityIdRewriter.toClientSpawner(tag);
                            }
                            // Handle new identifier
                            ((StringTag) tag.get("id")).setValue(BlockEntityRewriter.toNewIdentifier(identifier));
                        }
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.INT);
            // 1 - Gamemode
            map(Type.UNSIGNED_BYTE);
            // 2 - Dimension
            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);
            });
        }
    });
    this.registerClientbound(ClientboundPackets1_9_3.EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // effectID
            this.map(Type.INT);
            // pos
            this.map(Type.POSITION);
            // effectData
            this.map(Type.INT);
            // serverwide / global
            this.map(Type.BOOLEAN);
            handler(packetWrapper -> {
                int effectID = packetWrapper.get(Type.INT, 0);
                if (effectID == 2002) {
                    int data = packetWrapper.get(Type.INT, 1);
                    boolean isInstant = false;
                    Pair<Integer, Boolean> newData = PotionColorMapping.getNewData(data);
                    if (newData == null) {
                        Via.getPlatform().getLogger().warning("Received unknown 1.11 -> 1.10.2 potion data (" + data + ")");
                        data = 0;
                    } else {
                        data = newData.key();
                        isInstant = newData.value();
                    }
                    if (isInstant) {
                        packetWrapper.set(Type.INT, 0, 2007);
                    }
                    packetWrapper.set(Type.INT, 1, data);
                }
            });
        }
    });
    /*
            INCOMING PACKETS
        */
    registerServerbound(ServerboundPackets1_9_3.PLAYER_BLOCK_PLACEMENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Location
            map(Type.POSITION);
            // 1 - Face
            map(Type.VAR_INT);
            // 2 - Hand
            map(Type.VAR_INT);
            map(Type.FLOAT, toOldByte);
            map(Type.FLOAT, toOldByte);
            map(Type.FLOAT, toOldByte);
        }
    });
    registerServerbound(ServerboundPackets1_9_3.CHAT_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Message
            map(Type.STRING);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // 100 character limit on older servers
                    String msg = wrapper.get(Type.STRING, 0);
                    if (msg.length() > 100) {
                        wrapper.set(Type.STRING, 0, msg.substring(0, 100));
                    }
                }
            });
        }
    });
}
Also used : EntityTracker1_11(com.viaversion.viaversion.protocols.protocol1_11to1_10.storage.EntityTracker1_11) 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) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ValueTransformer(com.viaversion.viaversion.api.protocol.remapper.ValueTransformer) ItemRewriter(com.viaversion.viaversion.api.rewriter.ItemRewriter) EntityRewriter(com.viaversion.viaversion.rewriter.EntityRewriter) ServerboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3) 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) MetadataRewriter1_11To1_10(com.viaversion.viaversion.protocols.protocol1_11to1_10.metadata.MetadataRewriter1_11To1_10) Via(com.viaversion.viaversion.api.Via) AbstractProtocol(com.viaversion.viaversion.api.protocol.AbstractProtocol) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Types1_9(com.viaversion.viaversion.api.type.types.version.Types1_9) PotionColorMapping(com.viaversion.viaversion.protocols.protocol1_11to1_10.data.PotionColorMapping) Pair(com.viaversion.viaversion.util.Pair) EntityTracker1_11(com.viaversion.viaversion.protocols.protocol1_11to1_10.storage.EntityTracker1_11) Entity1_11Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_11Types) ClientboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3) InventoryPackets(com.viaversion.viaversion.protocols.protocol1_11to1_10.packets.InventoryPackets) 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) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) Entity1_11Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_11Types) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) Pair(com.viaversion.viaversion.util.Pair)

Example 27 with PacketHandler

use of com.viaversion.viaversion.api.protocol.remapper.PacketHandler in project ViaVersion by ViaVersion.

the class InventoryPackets method registerPackets.

@Override
public void registerPackets() {
    registerSetSlot(ClientboundPackets1_9_3.SET_SLOT, Type.ITEM);
    registerWindowItems(ClientboundPackets1_9_3.WINDOW_ITEMS, Type.ITEM_ARRAY);
    registerEntityEquipment(ClientboundPackets1_9_3.ENTITY_EQUIPMENT, Type.ITEM);
    // Plugin message Packet -> Trading
    protocol.registerClientbound(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Channel
            map(Type.STRING);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (wrapper.get(Type.STRING, 0).equalsIgnoreCase("MC|TrList")) {
                        // Passthrough Window ID
                        wrapper.passthrough(Type.INT);
                        int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                        for (int i = 0; i < size; i++) {
                            // Input Item
                            EntityIdRewriter.toClientItem(wrapper.passthrough(Type.ITEM));
                            // Output Item
                            EntityIdRewriter.toClientItem(wrapper.passthrough(Type.ITEM));
                            // Has second item
                            boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
                            if (secondItem)
                                // Second Item
                                EntityIdRewriter.toClientItem(wrapper.passthrough(Type.ITEM));
                            // Trade disabled
                            wrapper.passthrough(Type.BOOLEAN);
                            // Number of tools uses
                            wrapper.passthrough(Type.INT);
                            // Maximum number of trade uses
                            wrapper.passthrough(Type.INT);
                        }
                    }
                }
            });
        }
    });
    registerClickWindow(ServerboundPackets1_9_3.CLICK_WINDOW, Type.ITEM);
    registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM);
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)

Example 28 with PacketHandler

use of com.viaversion.viaversion.api.protocol.remapper.PacketHandler in project ViaVersion by ViaVersion.

the class BaseProtocol1_7 method registerPackets.

@Override
protected void registerPackets() {
    /* Outgoing Packets */
    // Status Response Packet
    registerClientbound(ClientboundStatusPackets.STATUS_RESPONSE, new // Status Response Packet
    PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ProtocolInfo info = wrapper.user().getProtocolInfo();
                    String originalStatus = wrapper.get(Type.STRING, 0);
                    try {
                        JsonElement json = GsonUtil.getGson().fromJson(originalStatus, JsonElement.class);
                        JsonObject version;
                        // Unknown!
                        int protocolVersion = 0;
                        if (json.isJsonObject()) {
                            if (json.getAsJsonObject().has("version")) {
                                version = json.getAsJsonObject().get("version").getAsJsonObject();
                                if (version.has("protocol")) {
                                    protocolVersion = ((Long) version.get("protocol").getAsLong()).intValue();
                                }
                            } else {
                                json.getAsJsonObject().add("version", version = new JsonObject());
                            }
                        } else {
                            // Format properly
                            json = new JsonObject();
                            json.getAsJsonObject().add("version", version = new JsonObject());
                        }
                        if (Via.getConfig().isSendSupportedVersions()) {
                            // Send supported versions
                            version.add("supportedVersions", GsonUtil.getGson().toJsonTree(Via.getAPI().getSupportedVersions()));
                        }
                        if (!Via.getAPI().getServerVersion().isKnown()) {
                            // Set the Server protocol if the detection on startup failed
                            ProtocolManagerImpl protocolManager = (ProtocolManagerImpl) Via.getManager().getProtocolManager();
                            protocolManager.setServerProtocol(new ServerProtocolVersionSingleton(ProtocolVersion.getProtocol(protocolVersion).getVersion()));
                        }
                        // Ensure the server has a version provider
                        VersionProvider versionProvider = Via.getManager().getProviders().get(VersionProvider.class);
                        if (versionProvider == null) {
                            wrapper.user().setActive(false);
                            return;
                        }
                        int closestServerProtocol = versionProvider.getClosestServerProtocol(wrapper.user());
                        List<ProtocolPathEntry> protocols = null;
                        if (info.getProtocolVersion() >= closestServerProtocol || Via.getPlatform().isOldClientsAllowed()) {
                            protocols = Via.getManager().getProtocolManager().getProtocolPath(info.getProtocolVersion(), closestServerProtocol);
                        }
                        if (protocols != null) {
                            if (protocolVersion == closestServerProtocol || protocolVersion == 0) {
                                // Fix ServerListPlus
                                ProtocolVersion prot = ProtocolVersion.getProtocol(info.getProtocolVersion());
                                version.addProperty("protocol", prot.getOriginalVersion());
                            }
                        } else {
                            // not compatible :(, *plays very sad violin*
                            wrapper.user().setActive(false);
                        }
                        if (Via.getConfig().blockedProtocolVersions().contains(info.getProtocolVersion())) {
                            // Show blocked versions as outdated
                            version.addProperty("protocol", -1);
                        }
                        // Update value
                        wrapper.set(Type.STRING, 0, GsonUtil.getGson().toJson(json));
                    } catch (JsonParseException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    });
    // Login Success Packet
    registerClientbound(ClientboundLoginPackets.GAME_PROFILE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ProtocolInfo info = wrapper.user().getProtocolInfo();
                    info.setState(State.PLAY);
                    UUID uuid = passthroughLoginUUID(wrapper);
                    info.setUuid(uuid);
                    String username = wrapper.passthrough(Type.STRING);
                    info.setUsername(username);
                    // Add to ported clients
                    Via.getManager().getConnectionManager().onLoginSuccess(wrapper.user());
                    if (!info.getPipeline().hasNonBaseProtocols()) {
                        // Only base protocol
                        wrapper.user().setActive(false);
                    }
                    if (Via.getManager().isDebug()) {
                        // Print out the route to console
                        Via.getPlatform().getLogger().log(Level.INFO, "{0} logged in with protocol {1}, Route: {2}", new Object[] { username, info.getProtocolVersion(), Joiner.on(", ").join(info.getPipeline().pipes(), ", ") });
                    }
                }
            });
        }
    });
    /* Incoming Packets */
    // Login Start Packet
    registerServerbound(ServerboundLoginPackets.HELLO, new PacketRemapper() {

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

                @Override
                public void handle(final PacketWrapper wrapper) throws Exception {
                    int protocol = wrapper.user().getProtocolInfo().getProtocolVersion();
                    if (Via.getConfig().blockedProtocolVersions().contains(protocol)) {
                        if (!wrapper.user().getChannel().isOpen())
                            return;
                        if (!wrapper.user().shouldApplyBlockProtocol())
                            return;
                        // Disconnect Packet
                        PacketWrapper disconnectPacket = PacketWrapper.create(ClientboundLoginPackets.LOGIN_DISCONNECT, wrapper.user());
                        Protocol1_9To1_8.FIX_JSON.write(disconnectPacket, ChatColorUtil.translateAlternateColorCodes(Via.getConfig().getBlockedDisconnectMsg()));
                        // cancel current
                        wrapper.cancel();
                        // Send and close
                        ChannelFuture future = disconnectPacket.sendFuture(BaseProtocol.class);
                        future.addListener(f -> wrapper.user().getChannel().close());
                    }
                }
            });
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ProtocolPathEntry(com.viaversion.viaversion.api.protocol.ProtocolPathEntry) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) JsonObject(com.google.gson.JsonObject) ProtocolVersion(com.viaversion.viaversion.api.protocol.version.ProtocolVersion) JsonParseException(com.google.gson.JsonParseException) VersionProvider(com.viaversion.viaversion.api.protocol.version.VersionProvider) ProtocolManagerImpl(com.viaversion.viaversion.protocol.ProtocolManagerImpl) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) JsonElement(com.google.gson.JsonElement) ProtocolInfo(com.viaversion.viaversion.api.connection.ProtocolInfo) UUID(java.util.UUID) ServerProtocolVersionSingleton(com.viaversion.viaversion.protocol.ServerProtocolVersionSingleton)

Example 29 with PacketHandler

use of com.viaversion.viaversion.api.protocol.remapper.PacketHandler 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 30 with PacketHandler

use of com.viaversion.viaversion.api.protocol.remapper.PacketHandler in project ViaVersion by ViaVersion.

the class InventoryPackets method registerPackets.

@Override
public void registerPackets() {
    registerSetSlot(ClientboundPackets1_9_3.SET_SLOT, Type.ITEM);
    registerWindowItems(ClientboundPackets1_9_3.WINDOW_ITEMS, Type.ITEM_ARRAY);
    registerEntityEquipment(ClientboundPackets1_9_3.ENTITY_EQUIPMENT, Type.ITEM);
    // Plugin message Packet -> Trading
    protocol.registerClientbound(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Channel
            map(Type.STRING);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (wrapper.get(Type.STRING, 0).equalsIgnoreCase("MC|TrList")) {
                        // Passthrough Window ID
                        wrapper.passthrough(Type.INT);
                        int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                        for (int i = 0; i < size; i++) {
                            // Input Item
                            handleItemToClient(wrapper.passthrough(Type.ITEM));
                            // Output Item
                            handleItemToClient(wrapper.passthrough(Type.ITEM));
                            // Has second item
                            boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
                            if (secondItem) {
                                // Second Item
                                handleItemToClient(wrapper.passthrough(Type.ITEM));
                            }
                            // Trade disabled
                            wrapper.passthrough(Type.BOOLEAN);
                            // Number of tools uses
                            wrapper.passthrough(Type.INT);
                            // Maximum number of trade uses
                            wrapper.passthrough(Type.INT);
                        }
                    }
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_12.CLICK_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Window ID
            map(Type.UNSIGNED_BYTE);
            // 1 - Slot
            map(Type.SHORT);
            // 2 - Button
            map(Type.BYTE);
            // 3 - Action number
            map(Type.SHORT);
            // 4 - Mode
            map(Type.VAR_INT);
            // 5 - Clicked Item
            map(Type.ITEM);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Item item = wrapper.get(Type.ITEM, 0);
                    if (!Via.getConfig().is1_12QuickMoveActionFix()) {
                        handleItemToServer(item);
                        return;
                    }
                    byte button = wrapper.get(Type.BYTE, 0);
                    int mode = wrapper.get(Type.VAR_INT, 0);
                    // QUICK_MOVE PATCH (Shift + (click/double click))
                    if (mode == 1 && button == 0 && item == null) {
                        short windowId = wrapper.get(Type.UNSIGNED_BYTE, 0);
                        short slotId = wrapper.get(Type.SHORT, 0);
                        short actionId = wrapper.get(Type.SHORT, 1);
                        InventoryQuickMoveProvider provider = Via.getManager().getProviders().get(InventoryQuickMoveProvider.class);
                        boolean succeed = provider.registerQuickMoveAction(windowId, slotId, actionId, wrapper.user());
                        if (succeed) {
                            wrapper.cancel();
                        }
                    // otherwise just pass through so the server sends the PacketPlayOutTransaction packet.
                    } else {
                        handleItemToServer(item);
                    }
                }
            });
        }
    });
    // Creative Inventory Action
    registerCreativeInvAction(ServerboundPackets1_12.CREATIVE_INVENTORY_ACTION, Type.ITEM);
}
Also used : Item(com.viaversion.viaversion.api.minecraft.item.Item) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) InventoryQuickMoveProvider(com.viaversion.viaversion.protocols.protocol1_12to1_11_1.providers.InventoryQuickMoveProvider)

Aggregations

PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)64 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)64 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)62 Type (com.viaversion.viaversion.api.type.Type)26 Item (com.viaversion.viaversion.api.minecraft.item.Item)25 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)20 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)17 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)13 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)12 ClientboundPackets1_13 (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13)12 Via (com.viaversion.viaversion.api.Via)11 Position (com.viaversion.viaversion.api.minecraft.Position)11 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)10 Chunk1_9_3_4Type (com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type)10 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)9 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)8 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)7 Entity1_13Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types)7 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)7 List (java.util.List)7