Search in sources :

Example 21 with Type

use of com.viaversion.viaversion.api.type.Type in project ViaVersion by ViaVersion.

the class InventoryPackets method registerPackets.

@Override
public void registerPackets() {
    registerSetSlot(ClientboundPackets1_13.SET_SLOT, Type.FLAT_ITEM);
    registerWindowItems(ClientboundPackets1_13.WINDOW_ITEMS, Type.FLAT_ITEM_ARRAY);
    registerAdvancements(ClientboundPackets1_13.ADVANCEMENTS, Type.FLAT_ITEM);
    registerSetCooldown(ClientboundPackets1_13.COOLDOWN);
    protocol.registerClientbound(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    String channel = wrapper.get(Type.STRING, 0);
                    if (channel.equals("minecraft:trader_list") || channel.equals("trader_list")) {
                        // 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.FLAT_ITEM));
                            // Output Item
                            handleItemToClient(wrapper.passthrough(Type.FLAT_ITEM));
                            // Has second item
                            boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
                            if (secondItem) {
                                // Second Item
                                handleItemToClient(wrapper.passthrough(Type.FLAT_ITEM));
                            }
                            // Trade disabled
                            wrapper.passthrough(Type.BOOLEAN);
                            // Number of tools uses
                            wrapper.passthrough(Type.INT);
                            // Maximum number of trade uses
                            wrapper.passthrough(Type.INT);
                        }
                    }
                }
            });
        }
    });
    registerEntityEquipment(ClientboundPackets1_13.ENTITY_EQUIPMENT, Type.FLAT_ITEM);
    RecipeRewriter recipeRewriter = new RecipeRewriter1_13_2(protocol);
    protocol.registerClientbound(ClientboundPackets1_13.DECLARE_RECIPES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int size = wrapper.passthrough(Type.VAR_INT);
                for (int i = 0; i < size; i++) {
                    // First id, then type
                    String id = wrapper.passthrough(Type.STRING);
                    String type = wrapper.passthrough(Type.STRING).replace("minecraft:", "");
                    recipeRewriter.handle(wrapper, type);
                }
            });
        }
    });
    registerClickWindow(ServerboundPackets1_13.CLICK_WINDOW, Type.FLAT_ITEM);
    registerCreativeInvAction(ServerboundPackets1_13.CREATIVE_INVENTORY_ACTION, Type.FLAT_ITEM);
    registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, Type.FLAT_ITEM, Type.FLOAT);
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13) ItemRewriter(com.viaversion.viaversion.rewriter.ItemRewriter) Type(com.viaversion.viaversion.api.type.Type) ServerboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) RecipeRewriter1_13_2(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2) Protocol1_13_1To1_13(com.viaversion.viaversion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13) RecipeRewriter(com.viaversion.viaversion.rewriter.RecipeRewriter) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) RecipeRewriter(com.viaversion.viaversion.rewriter.RecipeRewriter) RecipeRewriter1_13_2(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2)

Example 22 with Type

use of com.viaversion.viaversion.api.type.Type in project ViaVersion by ViaVersion.

the class PlayerPackets method register.

public static void register(Protocol1_9To1_8 protocol) {
    protocol.registerClientbound(ClientboundPackets1_8.CHAT_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Chat Message (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 1 - Chat Positon
            map(Type.BYTE);
            handler(wrapper -> {
                try {
                    JsonObject obj = (JsonObject) wrapper.get(Type.COMPONENT, 0);
                    ChatRewriter.toClient(obj, wrapper.user());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.TAB_LIST, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Header
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 1 - Footer
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.DISCONNECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Reason
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.TITLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Action
            map(Type.VAR_INT);
            // We only handle if the title or subtitle is set then just write through.
            handler(wrapper -> {
                int action = wrapper.get(Type.VAR_INT, 0);
                if (action == 0 || action == 1) {
                    Protocol1_9To1_8.FIX_JSON.write(wrapper, wrapper.read(Type.STRING));
                }
            });
        // Everything else is handled.
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.PLAYER_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Player X
            map(Type.DOUBLE);
            // 1 - Player Y
            map(Type.DOUBLE);
            // 2 - Player Z
            map(Type.DOUBLE);
            // 3 - Player Yaw
            map(Type.FLOAT);
            // 4 - Player Pitch
            map(Type.FLOAT);
            // 5 - Player Flags
            map(Type.BYTE);
            handler(wrapper -> {
                // 6 - Teleport ID was added
                wrapper.write(Type.VAR_INT, 0);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.TEAMS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Team Name
            map(Type.STRING);
            // 1 - Mode
            map(Type.BYTE);
            handler(wrapper -> {
                // Mode
                byte mode = wrapper.get(Type.BYTE, 0);
                if (mode == 0 || mode == 2) {
                    // Display Name
                    wrapper.passthrough(Type.STRING);
                    // Prefix
                    wrapper.passthrough(Type.STRING);
                    // Suffix
                    wrapper.passthrough(Type.STRING);
                    // Friendly Fire
                    wrapper.passthrough(Type.BYTE);
                    // Name tag visibility
                    wrapper.passthrough(Type.STRING);
                    wrapper.write(Type.STRING, Via.getConfig().isPreventCollision() ? "never" : "");
                    // Colour
                    wrapper.passthrough(Type.BYTE);
                }
                if (mode == 0 || mode == 3 || mode == 4) {
                    // Players
                    String[] players = wrapper.passthrough(Type.STRING_ARRAY);
                    final EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    String myName = wrapper.user().getProtocolInfo().getUsername();
                    String teamName = wrapper.get(Type.STRING, 0);
                    for (String player : players) {
                        if (entityTracker.isAutoTeam() && player.equalsIgnoreCase(myName)) {
                            if (mode == 4) {
                                // since removing add to auto team
                                // Workaround for packet order issue
                                wrapper.send(Protocol1_9To1_8.class);
                                wrapper.cancel();
                                entityTracker.sendTeamPacket(true, true);
                                entityTracker.setCurrentTeam("viaversion");
                            } else {
                                // since adding remove from auto team
                                entityTracker.sendTeamPacket(false, true);
                                entityTracker.setCurrentTeam(teamName);
                            }
                        }
                    }
                }
                if (mode == 1) {
                    // Remove team
                    final EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    String teamName = wrapper.get(Type.STRING, 0);
                    if (entityTracker.isAutoTeam() && teamName.equals(entityTracker.getCurrentTeam())) {
                        // team was removed
                        // Workaround for packet order issue
                        wrapper.send(Protocol1_9To1_8.class);
                        wrapper.cancel();
                        entityTracker.sendTeamPacket(true, true);
                        entityTracker.setCurrentTeam("viaversion");
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Player ID
            map(Type.INT);
            // Parse this info
            handler(wrapper -> {
                int entityId = wrapper.get(Type.INT, 0);
                EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                tracker.addEntity(entityId, Entity1_10Types.EntityType.PLAYER);
                tracker.setClientEntityId(entityId);
            });
            // 1 - Player Gamemode
            map(Type.UNSIGNED_BYTE);
            // 2 - Player Dimension
            map(Type.BYTE);
            // 3 - World Difficulty
            map(Type.UNSIGNED_BYTE);
            // 4 - Max Players (Tab)
            map(Type.UNSIGNED_BYTE);
            // 5 - Level Type
            map(Type.STRING);
            // 6 - Reduced Debug info
            map(Type.BOOLEAN);
            handler(wrapper -> {
                EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                // Set player gamemode
                tracker.setGameMode(GameMode.getById(wrapper.get(Type.UNSIGNED_BYTE, 0)));
            });
            // Track player's dimension
            handler(wrapper -> {
                ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                int dimensionId = wrapper.get(Type.BYTE, 0);
                clientWorld.setEnvironment(dimensionId);
            });
            // Gotta fake their op
            handler(wrapper -> {
                CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
                provider.sendPermission(wrapper.user());
            });
            // Scoreboard will be cleared when join game is received
            handler(wrapper -> {
                EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                if (Via.getConfig().isAutoTeam()) {
                    entityTracker.setAutoTeam(true);
                    // Workaround for packet order issue
                    wrapper.send(Protocol1_9To1_8.class);
                    wrapper.cancel();
                    entityTracker.sendTeamPacket(true, true);
                    entityTracker.setCurrentTeam("viaversion");
                } else {
                    entityTracker.setAutoTeam(false);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.PLAYER_INFO, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Action
            map(Type.VAR_INT);
            // 1 - Player Count
            map(Type.VAR_INT);
            // Due to this being a complex data structure we just use a handler.
            handler(wrapper -> {
                int action = wrapper.get(Type.VAR_INT, 0);
                int count = wrapper.get(Type.VAR_INT, 1);
                for (int i = 0; i < count; i++) {
                    // Player UUID
                    wrapper.passthrough(Type.UUID);
                    if (action == 0) {
                        // add player
                        // Player Name
                        wrapper.passthrough(Type.STRING);
                        int properties = wrapper.passthrough(Type.VAR_INT);
                        // loop through properties
                        for (int j = 0; j < properties; j++) {
                            // name
                            wrapper.passthrough(Type.STRING);
                            // value
                            wrapper.passthrough(Type.STRING);
                            boolean isSigned = wrapper.passthrough(Type.BOOLEAN);
                            if (isSigned) {
                                // signature
                                wrapper.passthrough(Type.STRING);
                            }
                        }
                        // gamemode
                        wrapper.passthrough(Type.VAR_INT);
                        // ping
                        wrapper.passthrough(Type.VAR_INT);
                        boolean hasDisplayName = wrapper.passthrough(Type.BOOLEAN);
                        if (hasDisplayName) {
                            // display name
                            Protocol1_9To1_8.FIX_JSON.write(wrapper, wrapper.read(Type.STRING));
                        }
                    } else if ((action == 1) || (action == 2)) {
                        // update gamemode || update latency
                        wrapper.passthrough(Type.VAR_INT);
                    } else if (action == 3) {
                        // update display name
                        boolean hasDisplayName = wrapper.passthrough(Type.BOOLEAN);
                        if (hasDisplayName) {
                            // display name
                            Protocol1_9To1_8.FIX_JSON.write(wrapper, wrapper.read(Type.STRING));
                        }
                    } else if (action == 4) {
                    // remove player
                    // no fields
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.PLUGIN_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Channel Name
            map(Type.STRING);
            handler(wrapper -> {
                String name = wrapper.get(Type.STRING, 0);
                if (name.equalsIgnoreCase("MC|BOpen")) {
                    // Not used anymore
                    wrapper.read(Type.REMAINING_BYTES);
                    wrapper.write(Type.VAR_INT, 0);
                }
                if (name.equalsIgnoreCase("MC|TrList")) {
                    // ID
                    wrapper.passthrough(Type.INT);
                    Short size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                    for (int i = 0; i < size; ++i) {
                        Item item1 = wrapper.passthrough(Type.ITEM);
                        ItemRewriter.toClient(item1);
                        Item item2 = wrapper.passthrough(Type.ITEM);
                        ItemRewriter.toClient(item2);
                        boolean present = wrapper.passthrough(Type.BOOLEAN);
                        if (present) {
                            Item item3 = wrapper.passthrough(Type.ITEM);
                            ItemRewriter.toClient(item3);
                        }
                        wrapper.passthrough(Type.BOOLEAN);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.INT);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Dimension
            map(Type.INT);
            // 1 - Difficulty
            map(Type.UNSIGNED_BYTE);
            // 2 - GameMode
            map(Type.UNSIGNED_BYTE);
            // 3 - Level Type
            map(Type.STRING);
            // Track player's dimension
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    int dimensionId = wrapper.get(Type.INT, 0);
                    clientWorld.setEnvironment(dimensionId);
                }
            });
            handler(wrapper -> {
                // Client unloads chunks on respawn
                wrapper.user().get(ClientChunks.class).getLoadedChunks().clear();
                int gamemode = wrapper.get(Type.UNSIGNED_BYTE, 0);
                EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                tracker.setGameMode(GameMode.getById(gamemode));
            });
            // Fake permissions to get Commandblocks working
            handler(wrapper -> {
                CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
                provider.sendPermission(wrapper.user());
                provider.unloadChunks(wrapper.user());
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.GAME_EVENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Reason
            map(Type.UNSIGNED_BYTE);
            // 1 - Value
            map(Type.FLOAT);
            handler(wrapper -> {
                short reason = wrapper.get(Type.UNSIGNED_BYTE, 0);
                if (reason == 3) {
                    // Change gamemode
                    int gamemode = wrapper.get(Type.FLOAT, 0).intValue();
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.setGameMode(GameMode.getById(gamemode));
                } else if (reason == 4) {
                    // Open credits screen
                    wrapper.set(Type.FLOAT, 0, 1F);
                }
            });
        }
    });
    /* Removed packets */
    protocol.registerClientbound(ClientboundPackets1_8.SET_COMPRESSION, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.cancel();
                CompressionProvider provider = Via.getManager().getProviders().get(CompressionProvider.class);
                provider.handlePlayCompression(wrapper.user(), wrapper.read(Type.VAR_INT));
            });
        }
    });
    /* Incoming Packets */
    protocol.registerServerbound(ServerboundPackets1_9.TAB_COMPLETE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Requested Command
            map(Type.STRING);
            // 1 - Is Command Block
            map(Type.BOOLEAN, Type.NOTHING);
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.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, Type.BYTE);
            // 3 - If Chat Colours on
            map(Type.BOOLEAN);
            // 4 - Skin Parts
            map(Type.UNSIGNED_BYTE);
            handler(wrapper -> {
                int hand = wrapper.read(Type.VAR_INT);
                if (Via.getConfig().isLeftHandedHandling()) {
                    // Add 0x80 if left handed
                    if (hand == 0)
                        wrapper.set(Type.UNSIGNED_BYTE, 0, (short) (wrapper.get(Type.UNSIGNED_BYTE, 0).intValue() | 0x80));
                }
                wrapper.sendToServer(Protocol1_9To1_8.class);
                wrapper.cancel();
                Via.getManager().getProviders().get(MainHandProvider.class).setMainHand(wrapper.user(), hand);
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.ANIMATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Hand
            map(Type.VAR_INT, Type.NOTHING);
        }
    });
    protocol.cancelServerbound(ServerboundPackets1_9.TELEPORT_CONFIRM);
    protocol.cancelServerbound(ServerboundPackets1_9.VEHICLE_MOVE);
    protocol.cancelServerbound(ServerboundPackets1_9.STEER_BOAT);
    protocol.registerServerbound(ServerboundPackets1_9.PLUGIN_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Channel Name
            map(Type.STRING);
            handler(wrapper -> {
                String name = wrapper.get(Type.STRING, 0);
                if (name.equalsIgnoreCase("MC|BSign")) {
                    Item item = wrapper.passthrough(Type.ITEM);
                    if (item != null) {
                        // Written Book
                        item.setIdentifier(387);
                        ItemRewriter.rewriteBookToServer(item);
                    }
                }
                if (name.equalsIgnoreCase("MC|AutoCmd")) {
                    wrapper.set(Type.STRING, 0, "MC|AdvCdm");
                    wrapper.write(Type.BYTE, (byte) 0);
                    // X
                    wrapper.passthrough(Type.INT);
                    // Y
                    wrapper.passthrough(Type.INT);
                    // Z
                    wrapper.passthrough(Type.INT);
                    // Command
                    wrapper.passthrough(Type.STRING);
                    // Flag
                    wrapper.passthrough(Type.BOOLEAN);
                    wrapper.clearInputBuffer();
                }
                if (name.equalsIgnoreCase("MC|AdvCmd")) {
                    wrapper.set(Type.STRING, 0, "MC|AdvCdm");
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.CLIENT_STATUS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Action ID
            map(Type.VAR_INT);
            handler(wrapper -> {
                int action = wrapper.get(Type.VAR_INT, 0);
                if (action == 2) {
                    // cancel any blocking >.>
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    if (tracker.isBlocking()) {
                        if (!Via.getConfig().isShowShieldWhenSwordInHand()) {
                            tracker.setSecondHand(null);
                        }
                        tracker.setBlocking(false);
                    }
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.PLAYER_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - X
            map(Type.DOUBLE);
            // 1 - Y
            map(Type.DOUBLE);
            // 2 - Z
            map(Type.DOUBLE);
            // 3 - Ground
            map(Type.BOOLEAN);
            handler(new PlayerMovementMapper());
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.PLAYER_POSITION_AND_ROTATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - X
            map(Type.DOUBLE);
            // 1 - Y
            map(Type.DOUBLE);
            // 2 - Z
            map(Type.DOUBLE);
            // 3 - Yaw
            map(Type.FLOAT);
            // 4 - Pitch
            map(Type.FLOAT);
            // 5 - Ground
            map(Type.BOOLEAN);
            handler(new PlayerMovementMapper());
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.PLAYER_ROTATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Yaw
            map(Type.FLOAT);
            // 1 - Pitch
            map(Type.FLOAT);
            // 2 - Ground
            map(Type.BOOLEAN);
            handler(new PlayerMovementMapper());
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.PLAYER_MOVEMENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Ground
            map(Type.BOOLEAN);
            handler(new PlayerMovementMapper());
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PlayerMovementMapper(com.viaversion.viaversion.protocols.protocol1_9to1_8.PlayerMovementMapper) JsonObject(com.google.gson.JsonObject) GameMode(com.viaversion.viaversion.protocols.protocol1_9to1_8.chat.GameMode) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) ChatRewriter(com.viaversion.viaversion.protocols.protocol1_9to1_8.chat.ChatRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Via(com.viaversion.viaversion.api.Via) Entity1_10Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_10Types) Item(com.viaversion.viaversion.api.minecraft.item.Item) CommandBlockProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CommandBlockProvider) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) MainHandProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.MainHandProvider) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8) EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) ServerboundPackets1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.ServerboundPackets1_9) CompressionProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CompressionProvider) ClientboundPackets1_8(com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8) ItemRewriter(com.viaversion.viaversion.protocols.protocol1_9to1_8.ItemRewriter) ClientChunks(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.ClientChunks) EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) ClientChunks(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.ClientChunks) PlayerMovementMapper(com.viaversion.viaversion.protocols.protocol1_9to1_8.PlayerMovementMapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) JsonObject(com.google.gson.JsonObject) CommandBlockProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CommandBlockProvider) CompressionProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CompressionProvider) Item(com.viaversion.viaversion.api.minecraft.item.Item) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8)

Example 23 with Type

use of com.viaversion.viaversion.api.type.Type in project ViaVersion by ViaVersion.

the class Protocol1_17To1_16_4 method registerPackets.

@Override
protected void registerPackets() {
    entityRewriter.register();
    itemRewriter.register();
    WorldPackets.register(this);
    registerClientbound(ClientboundPackets1_16_2.TAGS, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Tags are now generically written with resource location - 5 different Vanilla types
                wrapper.write(Type.VAR_INT, 5);
                for (RegistryType type : RegistryType.getValues()) {
                    // Prefix with resource location
                    wrapper.write(Type.STRING, type.resourceLocation());
                    // Id conversion
                    tagRewriter.handle(wrapper, tagRewriter.getRewriter(type), tagRewriter.getNewTags(type));
                    // Stop iterating after entity types
                    if (type == RegistryType.ENTITY) {
                        break;
                    }
                }
                // New Game Event tags type
                wrapper.write(Type.STRING, RegistryType.GAME_EVENT.resourceLocation());
                wrapper.write(Type.VAR_INT, NEW_GAME_EVENT_TAGS.length);
                for (String tag : NEW_GAME_EVENT_TAGS) {
                    wrapper.write(Type.STRING, tag);
                    wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[0]);
                }
            });
        }
    });
    new StatisticsRewriter(this).register(ClientboundPackets1_16_2.STATISTICS);
    SoundRewriter soundRewriter = new SoundRewriter(this);
    soundRewriter.registerSound(ClientboundPackets1_16_2.SOUND);
    soundRewriter.registerSound(ClientboundPackets1_16_2.ENTITY_SOUND);
    registerClientbound(ClientboundPackets1_16_2.RESOURCE_PACK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.passthrough(Type.STRING);
                wrapper.passthrough(Type.STRING);
                // Required
                wrapper.write(Type.BOOLEAN, Via.getConfig().isForcedUse1_17ResourcePack());
                // Prompt message
                wrapper.write(Type.OPTIONAL_COMPONENT, Via.getConfig().get1_17ResourcePackPrompt());
            });
        }
    });
    registerClientbound(ClientboundPackets1_16_2.MAP_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.passthrough(Type.VAR_INT);
                wrapper.passthrough(Type.BYTE);
                // Tracking position removed
                wrapper.read(Type.BOOLEAN);
                wrapper.passthrough(Type.BOOLEAN);
                int size = wrapper.read(Type.VAR_INT);
                // Write whether markers exists or not
                if (size != 0) {
                    wrapper.write(Type.BOOLEAN, true);
                    wrapper.write(Type.VAR_INT, size);
                } else {
                    wrapper.write(Type.BOOLEAN, false);
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_16_2.TITLE, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Title packet actions have been split into individual packets (the content hasn't changed)
                int type = wrapper.read(Type.VAR_INT);
                ClientboundPacketType packetType;
                switch(type) {
                    case 0:
                        packetType = ClientboundPackets1_17.TITLE_TEXT;
                        break;
                    case 1:
                        packetType = ClientboundPackets1_17.TITLE_SUBTITLE;
                        break;
                    case 2:
                        packetType = ClientboundPackets1_17.ACTIONBAR;
                        break;
                    case 3:
                        packetType = ClientboundPackets1_17.TITLE_TIMES;
                        break;
                    case 4:
                        packetType = ClientboundPackets1_17.CLEAR_TITLES;
                        // Reset times
                        wrapper.write(Type.BOOLEAN, false);
                        break;
                    case 5:
                        packetType = ClientboundPackets1_17.CLEAR_TITLES;
                        // Reset times
                        wrapper.write(Type.BOOLEAN, true);
                        break;
                    default:
                        throw new IllegalArgumentException("Invalid title type received: " + type);
                }
                wrapper.setId(packetType.getId());
            });
        }
    });
    registerClientbound(ClientboundPackets1_16_2.EXPLOSION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // X
            map(Type.FLOAT);
            // Y
            map(Type.FLOAT);
            // Z
            map(Type.FLOAT);
            // Strength
            map(Type.FLOAT);
            handler(wrapper -> {
                // Collection length is now a var int
                wrapper.write(Type.VAR_INT, wrapper.read(Type.INT));
            });
        }
    });
    registerClientbound(ClientboundPackets1_16_2.SPAWN_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14);
            handler(wrapper -> {
                // Angle (which Mojang just forgot to write to the buffer, lol)
                wrapper.write(Type.FLOAT, 0f);
            });
        }
    });
    registerServerbound(ServerboundPackets1_17.CLIENT_SETTINGS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Locale
            map(Type.STRING);
            // View distance
            map(Type.BYTE);
            // Chat mode
            map(Type.VAR_INT);
            // Chat colors
            map(Type.BOOLEAN);
            // Chat flags
            map(Type.UNSIGNED_BYTE);
            // Main hand
            map(Type.VAR_INT);
            handler(wrapper -> {
                // Text filtering
                wrapper.read(Type.BOOLEAN);
            });
        }
    });
}
Also used : MappingData(com.viaversion.viaversion.api.data.MappingData) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) InventoryPackets(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets.InventoryPackets) ParticleType(com.viaversion.viaversion.api.type.types.minecraft.ParticleType) InventoryAcknowledgements(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.storage.InventoryAcknowledgements) WorldPackets(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets.WorldPackets) Via(com.viaversion.viaversion.api.Via) AbstractProtocol(com.viaversion.viaversion.api.protocol.AbstractProtocol) Types1_17(com.viaversion.viaversion.api.type.types.version.Types1_17) MappingDataBase(com.viaversion.viaversion.api.data.MappingDataBase) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) ClientboundPacketType(com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Entity1_17Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types) ServerboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ServerboundPackets1_16_2) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ItemRewriter(com.viaversion.viaversion.api.rewriter.ItemRewriter) EntityRewriter(com.viaversion.viaversion.api.rewriter.EntityRewriter) EntityPackets(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets.EntityPackets) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) ClientboundPacketType(com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType)

Example 24 with Type

use of com.viaversion.viaversion.api.type.Type in project ViaBackwards by ViaVersion.

the class EntityPackets1_14 method registerRewrites.

@Override
protected void registerRewrites() {
    mapTypes(Entity1_14Types.values(), Entity1_13Types.EntityType.class);
    mapEntityTypeWithData(Entity1_14Types.CAT, Entity1_14Types.OCELOT).jsonName();
    mapEntityTypeWithData(Entity1_14Types.TRADER_LLAMA, Entity1_14Types.LLAMA).jsonName();
    mapEntityTypeWithData(Entity1_14Types.FOX, Entity1_14Types.WOLF).jsonName();
    mapEntityTypeWithData(Entity1_14Types.PANDA, Entity1_14Types.POLAR_BEAR).jsonName();
    mapEntityTypeWithData(Entity1_14Types.PILLAGER, Entity1_14Types.VILLAGER).jsonName();
    mapEntityTypeWithData(Entity1_14Types.WANDERING_TRADER, Entity1_14Types.VILLAGER).jsonName();
    mapEntityTypeWithData(Entity1_14Types.RAVAGER, Entity1_14Types.COW).jsonName();
    filter().handler((event, meta) -> {
        int typeId = meta.metaType().typeId();
        if (typeId <= 15) {
            meta.setMetaType(Types1_13_2.META_TYPES.byId(typeId));
        }
        MetaType type = meta.metaType();
        if (type == Types1_13_2.META_TYPES.itemType) {
            Item item = (Item) meta.getValue();
            meta.setValue(protocol.getItemRewriter().handleItemToClient(item));
        } else if (type == Types1_13_2.META_TYPES.blockStateType) {
            int blockstate = meta.value();
            meta.setValue(protocol.getMappingData().getNewBlockStateId(blockstate));
        }
    });
    filter().type(Entity1_14Types.PILLAGER).cancel(15);
    filter().type(Entity1_14Types.FOX).cancel(15);
    filter().type(Entity1_14Types.FOX).cancel(16);
    filter().type(Entity1_14Types.FOX).cancel(17);
    filter().type(Entity1_14Types.FOX).cancel(18);
    filter().type(Entity1_14Types.PANDA).cancel(15);
    filter().type(Entity1_14Types.PANDA).cancel(16);
    filter().type(Entity1_14Types.PANDA).cancel(17);
    filter().type(Entity1_14Types.PANDA).cancel(18);
    filter().type(Entity1_14Types.PANDA).cancel(19);
    filter().type(Entity1_14Types.PANDA).cancel(20);
    filter().type(Entity1_14Types.CAT).cancel(18);
    filter().type(Entity1_14Types.CAT).cancel(19);
    filter().type(Entity1_14Types.CAT).cancel(20);
    filter().handler((event, meta) -> {
        EntityType type = event.entityType();
        if (type == null)
            return;
        if (type.isOrHasParent(Entity1_14Types.ABSTRACT_ILLAGER_BASE) || type == Entity1_14Types.RAVAGER || type == Entity1_14Types.WITCH) {
            int index = event.index();
            if (index == 14) {
                event.cancel();
            } else if (index > 14) {
                event.setIndex(index - 1);
            }
        }
    });
    filter().type(Entity1_14Types.AREA_EFFECT_CLOUD).index(10).handler((event, meta) -> {
        rewriteParticle((Particle) meta.getValue());
    });
    filter().type(Entity1_14Types.FIREWORK_ROCKET).index(8).handler((event, meta) -> {
        meta.setMetaType(Types1_13_2.META_TYPES.varIntType);
        Integer value = (Integer) meta.getValue();
        if (value == null) {
            meta.setValue(0);
        }
    });
    filter().filterFamily(Entity1_14Types.ABSTRACT_ARROW).removeIndex(9);
    // Head shake timer
    filter().type(Entity1_14Types.VILLAGER).cancel(15);
    MetaHandler villagerDataHandler = (event, meta) -> {
        VillagerData villagerData = (VillagerData) meta.getValue();
        meta.setTypeAndValue(Types1_13_2.META_TYPES.varIntType, villagerDataToProfession(villagerData));
        if (meta.id() == 16) {
            // decreased by 2 again in one of the following handlers
            event.setIndex(15);
        }
    };
    filter().type(Entity1_14Types.ZOMBIE_VILLAGER).index(18).handler(villagerDataHandler);
    filter().type(Entity1_14Types.VILLAGER).index(16).handler(villagerDataHandler);
    // Holding arms up - from bitfield into own boolean
    filter().filterFamily(Entity1_14Types.ABSTRACT_SKELETON).index(13).handler((event, meta) -> {
        byte value = (byte) meta.getValue();
        if ((value & 4) != 0) {
            event.createExtraMeta(new Metadata(14, Types1_13_2.META_TYPES.booleanType, true));
        }
    });
    filter().filterFamily(Entity1_14Types.ZOMBIE).index(13).handler((event, meta) -> {
        byte value = (byte) meta.getValue();
        if ((value & 4) != 0) {
            event.createExtraMeta(new Metadata(16, Types1_13_2.META_TYPES.booleanType, true));
        }
    });
    filter().filterFamily(Entity1_14Types.ZOMBIE).addIndex(16);
    // Remove bed location
    filter().filterFamily(Entity1_14Types.LIVINGENTITY).handler((event, meta) -> {
        int index = event.index();
        if (index == 12) {
            Position position = (Position) meta.getValue();
            if (position != null) {
                // Use bed
                PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_13.USE_BED, null, event.user());
                wrapper.write(Type.VAR_INT, event.entityId());
                wrapper.write(Type.POSITION, position);
                try {
                    wrapper.scheduleSend(Protocol1_13_2To1_14.class);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            event.cancel();
        } else if (index > 12) {
            event.setIndex(index - 1);
        }
    });
    // Pose
    filter().removeIndex(6);
    filter().type(Entity1_14Types.OCELOT).index(13).handler((event, meta) -> {
        event.setIndex(15);
        meta.setTypeAndValue(Types1_13_2.META_TYPES.varIntType, 0);
    });
    filter().type(Entity1_14Types.CAT).handler((event, meta) -> {
        if (event.index() == 15) {
            meta.setValue(1);
        } else if (event.index() == 13) {
            meta.setValue((byte) ((byte) meta.getValue() & 0x4));
        }
    });
    filter().handler((event, meta) -> {
        if (meta.metaType().typeId() > 15) {
            throw new IllegalArgumentException("Unhandled metadata: " + meta);
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types) EntityPositionStorage1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14) Item(com.viaversion.viaversion.api.minecraft.item.Item) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) Types1_14(com.viaversion.viaversion.api.type.types.version.Types1_14) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) ChunkLightStorage(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage) Protocol1_13_2To1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14) EntityData(com.viaversion.viabackwards.api.entities.storage.EntityData) Particle(com.viaversion.viaversion.api.type.types.Particle) LegacyEntityRewriter(com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter) Position(com.viaversion.viaversion.api.minecraft.Position) Types1_13_2(com.viaversion.viaversion.api.type.types.version.Types1_13_2) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) VillagerData(com.viaversion.viaversion.api.minecraft.VillagerData) EntityPositionHandler(com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler) ClientboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13) DifficultyStorage(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.DifficultyStorage) MetaHandler(com.viaversion.viaversion.rewriter.meta.MetaHandler) ViaBackwards(com.viaversion.viabackwards.ViaBackwards) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) Entity1_14Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types) Position(com.viaversion.viaversion.api.minecraft.Position) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) Item(com.viaversion.viaversion.api.minecraft.item.Item) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) VillagerData(com.viaversion.viaversion.api.minecraft.VillagerData) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types) MetaHandler(com.viaversion.viaversion.rewriter.meta.MetaHandler) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType)

Example 25 with Type

use of com.viaversion.viaversion.api.type.Type in project ViaBackwards by ViaVersion.

the class EntityPackets1_14 method registerPackets.

@Override
protected void registerPackets() {
    positionHandler = new EntityPositionHandler(this, EntityPositionStorage1_14.class, EntityPositionStorage1_14::new);
    protocol.registerClientbound(ClientboundPackets1_14.ENTITY_STATUS, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int entityId = wrapper.passthrough(Type.INT);
                byte status = wrapper.passthrough(Type.BYTE);
                // Check for death status
                if (status != 3)
                    return;
                EntityTracker tracker = tracker(wrapper.user());
                EntityType entityType = tracker.entityType(entityId);
                if (entityType != Entity1_14Types.PLAYER)
                    return;
                // Remove equipment, else the client will see ghost items
                for (int i = 0; i <= 5; i++) {
                    PacketWrapper equipmentPacket = wrapper.create(ClientboundPackets1_13.ENTITY_EQUIPMENT);
                    equipmentPacket.write(Type.VAR_INT, entityId);
                    equipmentPacket.write(Type.VAR_INT, i);
                    equipmentPacket.write(Type.FLAT_VAR_INT_ITEM, null);
                    equipmentPacket.send(Protocol1_13_2To1_14.class);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.ENTITY_TELEPORT, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            handler(wrapper -> positionHandler.cacheEntityPosition(wrapper, false, false));
        }
    });
    PacketRemapper relativeMoveHandler = new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    double x = wrapper.get(Type.SHORT, 0) / EntityPositionHandler.RELATIVE_MOVE_FACTOR;
                    double y = wrapper.get(Type.SHORT, 1) / EntityPositionHandler.RELATIVE_MOVE_FACTOR;
                    double z = wrapper.get(Type.SHORT, 2) / EntityPositionHandler.RELATIVE_MOVE_FACTOR;
                    positionHandler.cacheEntityPosition(wrapper, x, y, z, false, true);
                }
            });
        }
    };
    protocol.registerClientbound(ClientboundPackets1_14.ENTITY_POSITION, relativeMoveHandler);
    protocol.registerClientbound(ClientboundPackets1_14.ENTITY_POSITION_AND_ROTATION, relativeMoveHandler);
    protocol.registerClientbound(ClientboundPackets1_14.SPAWN_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // 1 - UUID
            map(Type.UUID);
            // 2 - Type
            map(Type.VAR_INT, Type.BYTE);
            // 3 - X
            map(Type.DOUBLE);
            // 4 - Y
            map(Type.DOUBLE);
            // 5 - Z
            map(Type.DOUBLE);
            // 6 - Pitch
            map(Type.BYTE);
            // 7 - Yaw
            map(Type.BYTE);
            // 8 - Data
            map(Type.INT);
            // 9 - Velocity X
            map(Type.SHORT);
            // 10 - Velocity Y
            map(Type.SHORT);
            // 11 - Velocity Z
            map(Type.SHORT);
            handler(getObjectTrackerHandler());
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.BYTE, 0);
                    int mappedId = newEntityId(id);
                    Entity1_13Types.EntityType entityType = Entity1_13Types.getTypeFromId(mappedId, false);
                    Entity1_13Types.ObjectType objectType;
                    if (entityType.isOrHasParent(Entity1_13Types.EntityType.MINECART_ABSTRACT)) {
                        objectType = Entity1_13Types.ObjectType.MINECART;
                        int data = 0;
                        switch(entityType) {
                            case CHEST_MINECART:
                                data = 1;
                                break;
                            case FURNACE_MINECART:
                                data = 2;
                                break;
                            case TNT_MINECART:
                                data = 3;
                                break;
                            case SPAWNER_MINECART:
                                data = 4;
                                break;
                            case HOPPER_MINECART:
                                data = 5;
                                break;
                            case COMMAND_BLOCK_MINECART:
                                data = 6;
                                break;
                        }
                        if (data != 0)
                            wrapper.set(Type.INT, 0, data);
                    } else {
                        objectType = Entity1_13Types.ObjectType.fromEntityType(entityType).orElse(null);
                    }
                    if (objectType == null)
                        return;
                    wrapper.set(Type.BYTE, 0, (byte) objectType.getId());
                    int data = wrapper.get(Type.INT, 0);
                    if (objectType == Entity1_13Types.ObjectType.FALLING_BLOCK) {
                        int blockState = wrapper.get(Type.INT, 0);
                        int combined = protocol.getMappingData().getNewBlockStateId(blockState);
                        wrapper.set(Type.INT, 0, combined);
                    } else if (entityType.isOrHasParent(Entity1_13Types.EntityType.ABSTRACT_ARROW)) {
                        wrapper.set(Type.INT, 0, data + 1);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.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_14.METADATA_LIST, Types1_13_2.METADATA_LIST);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int type = wrapper.get(Type.VAR_INT, 1);
                    EntityType entityType = Entity1_14Types.getTypeFromId(type);
                    addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), entityType);
                    int oldId = newEntityId(type);
                    if (oldId == -1) {
                        EntityData entityData = entityDataForType(entityType);
                        if (entityData == null) {
                            ViaBackwards.getPlatform().getLogger().warning("Could not find 1.13.2 entity type for 1.14 entity type " + type + "/" + entityType);
                            wrapper.cancel();
                        } else {
                            wrapper.set(Type.VAR_INT, 1, entityData.replacementId());
                        }
                    } else {
                        wrapper.set(Type.VAR_INT, 1, oldId);
                    }
                }
            });
            // Handle entity type & metadata
            handler(getMobSpawnRewriter(Types1_13_2.METADATA_LIST));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.SPAWN_EXPERIENCE_ORB, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // Needs to be mapped for the position cache
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            handler(wrapper -> addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), Entity1_14Types.EXPERIENCE_ORB));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.SPAWN_GLOBAL_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            map(Type.BYTE);
            // Needs to be mapped for the position cache
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            handler(wrapper -> addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), Entity1_14Types.LIGHTNING_BOLT));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.SPAWN_PAINTING, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.UUID);
            map(Type.VAR_INT);
            map(Type.POSITION1_14, Type.POSITION);
            map(Type.BYTE);
            // Track entity
            handler(wrapper -> addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), Entity1_14Types.PAINTING));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.SPAWN_PLAYER, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Player UUID
            map(Type.UUID);
            // 2 - X
            map(Type.DOUBLE);
            // 3 - Y
            map(Type.DOUBLE);
            // 4 - Z
            map(Type.DOUBLE);
            // 5 - Yaw
            map(Type.BYTE);
            // 6 - Pitch
            map(Type.BYTE);
            // 7 - Metadata
            map(Types1_14.METADATA_LIST, Types1_13_2.METADATA_LIST);
            handler(getTrackerAndMetaHandler(Types1_13_2.METADATA_LIST, Entity1_14Types.PLAYER));
            handler(wrapper -> positionHandler.cacheEntityPosition(wrapper, true, false));
        }
    });
    registerRemoveEntities(ClientboundPackets1_14.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_14.ENTITY_METADATA, Types1_14.METADATA_LIST, Types1_13_2.METADATA_LIST);
    protocol.registerClientbound(ClientboundPackets1_14.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(getTrackerHandler(Entity1_14Types.PLAYER, Type.INT));
            handler(getDimensionHandler(1));
            handler(wrapper -> {
                short difficulty = wrapper.user().get(DifficultyStorage.class).getDifficulty();
                wrapper.write(Type.UNSIGNED_BYTE, difficulty);
                // Max Players
                wrapper.passthrough(Type.UNSIGNED_BYTE);
                // Level Type
                wrapper.passthrough(Type.STRING);
                // Read View Distance
                wrapper.read(Type.VAR_INT);
            // TODO Track client position
            // Manually add position storage
            /*int entitiyId = wrapper.get(Type.INT, 0);
                    StoredEntityData storedEntity = protocol.getEntityRewriter().tracker(wrapper.user()).entityData(entitiyId);
                    storedEntity.put(new EntityPositionStorage1_14());*/
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Dimension ID
            map(Type.INT);
            handler(wrapper -> {
                ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                int dimensionId = wrapper.get(Type.INT, 0);
                clientWorld.setEnvironment(dimensionId);
                short difficulty = wrapper.user().get(DifficultyStorage.class).getDifficulty();
                wrapper.write(Type.UNSIGNED_BYTE, difficulty);
                wrapper.user().get(ChunkLightStorage.class).clear();
            });
        }
    });
}
Also used : EntityPositionHandler(com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types) EntityPositionStorage1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14) Item(com.viaversion.viaversion.api.minecraft.item.Item) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) Types1_14(com.viaversion.viaversion.api.type.types.version.Types1_14) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) ChunkLightStorage(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage) Protocol1_13_2To1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14) EntityData(com.viaversion.viabackwards.api.entities.storage.EntityData) Particle(com.viaversion.viaversion.api.type.types.Particle) LegacyEntityRewriter(com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter) Position(com.viaversion.viaversion.api.minecraft.Position) Types1_13_2(com.viaversion.viaversion.api.type.types.version.Types1_13_2) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) VillagerData(com.viaversion.viaversion.api.minecraft.VillagerData) EntityPositionHandler(com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler) ClientboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13) DifficultyStorage(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.DifficultyStorage) MetaHandler(com.viaversion.viaversion.rewriter.meta.MetaHandler) ViaBackwards(com.viaversion.viabackwards.ViaBackwards) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) Entity1_14Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types) EntityPositionStorage1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) EntityData(com.viaversion.viabackwards.api.entities.storage.EntityData) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) Protocol1_13_2To1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14)

Aggregations

Type (com.viaversion.viaversion.api.type.Type)36 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)33 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)28 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)23 Via (com.viaversion.viaversion.api.Via)14 Item (com.viaversion.viaversion.api.minecraft.item.Item)14 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)13 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)11 ClientboundPackets1_13 (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13)11 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)10 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)9 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)8 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)8 Position (com.viaversion.viaversion.api.minecraft.Position)8 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)8 ClientboundPackets1_14 (com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14)8 List (java.util.List)8 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)7 ClientboundPackets1_15 (com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15)7 ArrayList (java.util.ArrayList)7