Java Code Examples for us.myles.ViaVersion.api.remapper.PacketHandler

The following examples show how to use us.myles.ViaVersion.api.remapper.PacketHandler. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: ViaVersion   Author: ViaVersion   File: MetadataRewriter.java    License: MIT License 6 votes vote down vote up
/**
 * Returns a packethandler to track and rewrite an entity.
 *
 * @param metaType type of the metadata list
 * @return handler for tracking and rewriting entities
 */
public PacketHandler getTrackerAndRewriter(@Nullable Type<List<Metadata>> metaType) {
    return wrapper -> {
        int entityId = wrapper.get(Type.VAR_INT, 0);
        int type = wrapper.get(Type.VAR_INT, 1);

        int newType = getNewEntityId(type);
        if (newType != type) {
            wrapper.set(Type.VAR_INT, 1, newType);
        }

        EntityType entType = getTypeFromId(newType);
        // Register Type ID
        wrapper.user().get(entityTrackerClass).addEntity(entityId, entType);

        if (metaType != null) {
            handleMetadata(entityId, wrapper.get(metaType, 0), wrapper.user());
        }
    };
}
 
Example #2
Source Project: ViaVersion   Author: ViaVersion   File: WorldPackets.java    License: MIT License 6 votes vote down vote up
public static void register(Protocol protocol) {
    protocol.registerOutgoing(ClientboundPackets1_13.SPAWN_PARTICLE, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.INT); // 0 - Particle ID
            map(Type.BOOLEAN); // 1 - Long Distance
            map(Type.FLOAT); // 2 - X
            map(Type.FLOAT); // 3 - Y
            map(Type.FLOAT); // 4 - Z
            map(Type.FLOAT); // 5 - Offset X
            map(Type.FLOAT); // 6 - Offset Y
            map(Type.FLOAT); // 7 - Offset Z
            map(Type.FLOAT); // 8 - Particle Data
            map(Type.INT); // 9 - Particle Count
            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.INT, 0);
                    if (id == 27) {
                        wrapper.write(Type.FLAT_VAR_INT_ITEM, wrapper.read(Type.FLAT_ITEM));
                    }
                }
            });
        }
    });
}
 
Example #3
Source Project: ViaBackwards   Author: ViaVersion   File: LegacyEntityRewriter.java    License: MIT License 6 votes vote down vote up
protected PacketHandler getMobSpawnRewriter(Type<List<Metadata>> metaType) {
    return wrapper -> {
        int entityId = wrapper.get(Type.VAR_INT, 0);
        EntityType type = getEntityType(wrapper.user(), entityId);

        MetaStorage storage = new MetaStorage(wrapper.get(metaType, 0));
        handleMeta(wrapper.user(), entityId, storage);

        EntityData entityData = getEntityData(type);
        if (entityData != null) {
            wrapper.set(Type.VAR_INT, 1, entityData.getReplacementId());
            if (entityData.hasBaseMeta()) {
                entityData.getDefaultMeta().createMeta(storage);
            }
        }

        // Rewrite Metadata
        wrapper.set(metaType, 0, storage.getMetaDataList());
    };
}
 
Example #4
Source Project: ViaBackwards   Author: ViaVersion   File: LegacyEntityRewriter.java    License: MIT License 6 votes vote down vote up
protected PacketHandler getObjectRewriter(Function<Byte, ObjectType> objectGetter) {
    return wrapper -> {
        ObjectType type = objectGetter.apply(wrapper.get(Type.BYTE, 0));
        if (type == null) {
            ViaBackwards.getPlatform().getLogger().warning("Could not find Entity Type" + wrapper.get(Type.BYTE, 0));
            return;
        }

        EntityData data = getObjectData(type);
        if (data != null) {
            wrapper.set(Type.BYTE, 0, (byte) data.getReplacementId());
            if (data.getObjectData() != -1) {
                wrapper.set(Type.INT, 0, data.getObjectData());
            }
        }
    };
}
 
Example #5
Source Project: ViaRewind   Author: ViaVersion   File: ScoreboardPackets.java    License: MIT License 5 votes vote down vote up
public static void register(Protocol protocol) {
	/*  OUTGOING  */

	//Display Scoreboard
	protocol.registerOutgoing(State.PLAY, 0x38, 0x3D);

	//Scoreboard Objective
	protocol.registerOutgoing(State.PLAY, 0x3F, 0x3B);

	//Scoreboard Team
	protocol.registerOutgoing(State.PLAY, 0x41, 0x3E, new PacketRemapper() {
		@Override
		public void registerMap() {
			map(Type.STRING);
			map(Type.BYTE);
			handler(new PacketHandler() {
				@Override
				public void handle(PacketWrapper packetWrapper) throws Exception {
					byte mode = packetWrapper.get(Type.BYTE, 0);
					if (mode == 0 || mode == 2) {
						packetWrapper.passthrough(Type.STRING);  //Display Name
						packetWrapper.passthrough(Type.STRING);  //Prefix
						packetWrapper.passthrough(Type.STRING);  //Suffix
						packetWrapper.passthrough(Type.BYTE);  //Friendly Flags
						packetWrapper.passthrough(Type.STRING);  //Name Tag Visibility
						packetWrapper.read(Type.STRING);  //Skip Collision Rule
						packetWrapper.passthrough(Type.BYTE);  //Friendly Flags
					}

					if (mode == 0 || mode == 3 || mode == 4) {
						packetWrapper.passthrough(Type.STRING_ARRAY);
					}
				}
			});
		}
	});

	//Update Score
	protocol.registerOutgoing(State.PLAY, 0x42, 0x3C);
}
 
Example #6
Source Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: MIT License 5 votes vote down vote up
public PacketHandler itemArrayHandler(Type<Item[]> type) {
    return wrapper -> {
        Item[] items = wrapper.get(type, 0);
        for (Item item : items) {
            toClient.rewrite(item);
        }
    };
}
 
Example #7
Source Project: ViaVersion   Author: ViaVersion   File: MetadataRewriter.java    License: MIT License 5 votes vote down vote up
public PacketHandler getTrackerAndRewriter(@Nullable Type<List<Metadata>> metaType, EntityType entityType) {
    return wrapper -> {
        int entityId = wrapper.get(Type.VAR_INT, 0);
        // Register Type ID
        wrapper.user().get(entityTrackerClass).addEntity(entityId, entityType);

        if (metaType != null) {
            handleMetadata(entityId, wrapper.get(metaType, 0), wrapper.user());
        }
    };
}
 
Example #8
Source Project: ViaVersion   Author: ViaVersion   File: MetadataRewriter.java    License: MIT License 5 votes vote down vote up
/**
 * Returns a packethandler to track an object entity.
 *
 * @return handler for tracking and rewriting entities
 */
public PacketHandler getObjectTracker() {
    return wrapper -> {
        int entityId = wrapper.get(Type.VAR_INT, 0);
        byte type = wrapper.get(Type.BYTE, 0);

        EntityType entType = getObjectTypeFromId(type);
        // Register Type ID
        wrapper.user().get(entityTrackerClass).addEntity(entityId, entType);
    };
}
 
Example #9
Source Project: ViaVersion   Author: ViaVersion   File: Protocol1_14_4To1_14_3.java    License: MIT License 5 votes vote down vote up
@Override
protected void registerPackets() {
    registerOutgoing(ClientboundPackets1_14.TRADE_LIST, new PacketRemapper() {
        @Override
        public void registerMap() {
            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    wrapper.passthrough(Type.VAR_INT);
                    int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                    for (int i = 0; i < size; i++) {
                        wrapper.passthrough(Type.FLAT_VAR_INT_ITEM);
                        wrapper.passthrough(Type.FLAT_VAR_INT_ITEM);
                        if (wrapper.passthrough(Type.BOOLEAN)) {
                            wrapper.passthrough(Type.FLAT_VAR_INT_ITEM);
                        }
                        wrapper.passthrough(Type.BOOLEAN);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.FLOAT);
                        wrapper.write(Type.INT, 0); // demand value added in pre5
                    }
                }
            });
        }
    });
}
 
Example #10
Source Project: ViaVersion   Author: ViaVersion   File: Protocol1_9To1_9_1.java    License: MIT License 5 votes vote down vote up
@Override
protected void registerPackets() {
    // Currently supports 1.9.1 and 1.9.2

    registerOutgoing(ClientboundPackets1_9.JOIN_GAME, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.INT); // 0 - Player ID
            map(Type.UNSIGNED_BYTE); // 1 - Player Gamemode
            // 1.9.1 PRE 2 Changed this
            map(Type.INT, Type.BYTE); // 2 - Player Dimension
            map(Type.UNSIGNED_BYTE); // 3 - World Difficulty
            map(Type.UNSIGNED_BYTE); // 4 - Max Players (Tab)
            map(Type.STRING); // 5 - Level Type
            map(Type.BOOLEAN); // 6 - Reduced Debug info
        }
    });

    registerOutgoing(ClientboundPackets1_9.SOUND, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Sound ID

            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int sound = wrapper.get(Type.VAR_INT, 0);

                    if (sound == 415) // Stop the Elytra sound for 1.9 (It's introduced in 1.9.2)
                        wrapper.cancel();
                    else if (sound >= 416) // Act like the Elytra sound never existed
                        wrapper.set(Type.VAR_INT, 0, sound - 1);
                }
            });
        }
    });
}
 
Example #11
Source Project: ViaVersion   Author: ViaVersion   File: Protocol1_14_3To1_14_2.java    License: MIT License 5 votes vote down vote up
@Override
protected void registerPackets() {
    registerOutgoing(ClientboundPackets1_14.TRADE_LIST, new PacketRemapper() {
        @Override
        public void registerMap() {
            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    wrapper.passthrough(Type.VAR_INT);
                    int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                    for (int i = 0; i < size; i++) {
                        wrapper.passthrough(Type.FLAT_VAR_INT_ITEM);
                        wrapper.passthrough(Type.FLAT_VAR_INT_ITEM);
                        if (wrapper.passthrough(Type.BOOLEAN)) {
                            wrapper.passthrough(Type.FLAT_VAR_INT_ITEM);
                        }
                        wrapper.passthrough(Type.BOOLEAN);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.FLOAT);
                    }
                    wrapper.passthrough(Type.VAR_INT);
                    wrapper.passthrough(Type.VAR_INT);
                    boolean regularVillager = wrapper.passthrough(Type.BOOLEAN);
                    wrapper.write(Type.BOOLEAN, regularVillager); // new boolean added in pre-1
                }
            });
        }
    });
}
 
Example #12
Source Project: ViaVersion   Author: ViaVersion   File: Protocol1_9_1To1_9.java    License: MIT License 5 votes vote down vote up
@Override
protected void registerPackets() {
    // Currently supports 1.9.1 and 1.9.2

    registerOutgoing(ClientboundPackets1_9.JOIN_GAME, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.INT); // 0 - Player ID
            map(Type.UNSIGNED_BYTE); // 1 - Player Gamemode
            // 1.9.1 PRE 2 Changed this
            map(Type.BYTE, Type.INT); // 2 - Player Dimension
            map(Type.UNSIGNED_BYTE); // 3 - World Difficulty
            map(Type.UNSIGNED_BYTE); // 4 - Max Players (Tab)
            map(Type.STRING); // 5 - Level Type
            map(Type.BOOLEAN); // 6 - Reduced Debug info
        }
    });

    registerOutgoing(ClientboundPackets1_9.SOUND, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Sound ID

            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int sound = wrapper.get(Type.VAR_INT, 0);

                    if (sound >= 415) // Add 1 to every sound id since there is no Elytra sound on a 1.9 server
                        wrapper.set(Type.VAR_INT, 0, sound + 1);
                }
            });
        }
    });
}
 
Example #13
Source Project: ViaBackwards   Author: ViaVersion   File: WorldPackets1_13_2.java    License: MIT License 5 votes vote down vote up
public static void register(Protocol1_13_1To1_13_2 protocol) {
    protocol.registerOutgoing(ClientboundPackets1_13.SPAWN_PARTICLE, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.INT); // 0 - Particle ID
            map(Type.BOOLEAN); // 1 - Long Distance
            map(Type.FLOAT); // 2 - X
            map(Type.FLOAT); // 3 - Y
            map(Type.FLOAT); // 4 - Z
            map(Type.FLOAT); // 5 - Offset X
            map(Type.FLOAT); // 6 - Offset Y
            map(Type.FLOAT); // 7 - Offset Z
            map(Type.FLOAT); // 8 - Particle Data
            map(Type.INT); // 9 - Particle Count

            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.INT, 0);
                    if (id == 27) {
                        wrapper.write(Type.FLAT_ITEM, wrapper.read(Type.FLAT_VAR_INT_ITEM));
                    }
                }
            });
        }
    });
}
 
Example #14
Source Project: ViaBackwards   Author: ViaVersion   File: WorldPackets1_13_1.java    License: MIT License 5 votes vote down vote up
public static void register(Protocol protocol) {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION, Protocol1_13To1_13_1::getNewBlockStateId, Protocol1_13To1_13_1::getNewBlockId);

    protocol.registerOutgoing(ClientboundPackets1_13.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_13Type(clientWorld));

                    for (ChunkSection section : chunk.getSections()) {
                        if (section != null) {
                            for (int i = 0; i < section.getPaletteSize(); i++) {
                                section.setPaletteEntry(i, Protocol1_13To1_13_1.getNewBlockStateId(section.getPaletteEntry(i)));
                            }
                        }
                    }
                }
            });
        }
    });

    blockRewriter.registerBlockAction(ClientboundPackets1_13.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_13.BLOCK_CHANGE);
    blockRewriter.registerMultiBlockChange(ClientboundPackets1_13.MULTI_BLOCK_CHANGE);
    blockRewriter.registerEffect(ClientboundPackets1_13.EFFECT, 1010, 2001, InventoryPackets1_13_1::getOldItemId);
    blockRewriter.registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, 3, 20, 27, InventoryPackets1_13_1::toClient, Type.FLAT_ITEM, Type.FLOAT);
}
 
Example #15
Source Project: ViaBackwards   Author: ViaVersion   File: LegacyEntityRewriter.java    License: MIT License 5 votes vote down vote up
protected PacketHandler getTrackerAndMetaHandler(Type<List<Metadata>> metaType, EntityType entityType) {
    return wrapper -> {
        addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), entityType);

        List<Metadata> metaDataList = handleMeta(wrapper.user(), wrapper.get(Type.VAR_INT, 0),
                new MetaStorage(wrapper.get(metaType, 0))).getMetaDataList();
        wrapper.set(metaType, 0, metaDataList);
    };
}
 
Example #16
Source Project: ViaBackwards   Author: ViaVersion   File: EntityRewriterBase.java    License: MIT License 5 votes vote down vote up
protected PacketHandler getDimensionHandler(int index) {
    return wrapper -> {
        ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
        int dimensionId = wrapper.get(Type.INT, index);
        clientWorld.setEnvironment(dimensionId);
    };
}
 
Example #17
Source Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: MIT License 4 votes vote down vote up
public PacketHandler itemToClientHandler(Type<Item> type) {
    return wrapper -> toClient.rewrite(wrapper.get(type, 0));
}
 
Example #18
Source Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: MIT License 4 votes vote down vote up
public PacketHandler itemToServerHandler(Type<Item> type) {
    return wrapper -> toServer.rewrite(wrapper.get(type, 0));
}
 
Example #19
Source Project: ViaVersion   Author: ViaVersion   File: MetadataRewriter.java    License: MIT License 4 votes vote down vote up
public PacketHandler getTracker() {
    return getTrackerAndRewriter(null);
}
 
Example #20
Source Project: ViaVersion   Author: ViaVersion   File: BaseProtocol.java    License: MIT License 4 votes vote down vote up
@Override
protected void registerPackets() {
    /* Incoming Packets */

    // Handshake Packet
    registerIncoming(State.HANDSHAKE, 0x00, 0x00, new PacketRemapper() {
        @Override
        public void registerMap() {
            // select right protocol
            map(Type.VAR_INT); // 0 - Client Protocol Version
            map(Type.STRING); // 1 - Server Address
            map(Type.UNSIGNED_SHORT); // 2 - Server Port
            map(Type.VAR_INT); // 3 - Next State
            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int protVer = wrapper.get(Type.VAR_INT, 0);
                    int state = wrapper.get(Type.VAR_INT, 1);

                    ProtocolInfo info = wrapper.user().getProtocolInfo();
                    info.setProtocolVersion(protVer);
                    // Ensure the server has a version provider
                    if (Via.getManager().getProviders().get(VersionProvider.class) == null) {
                        wrapper.user().setActive(false);
                        return;
                    }
                    // Choose the pipe
                    int protocol = Via.getManager().getProviders().get(VersionProvider.class).getServerProtocol(wrapper.user());
                    info.setServerProtocolVersion(protocol);
                    List<Pair<Integer, Protocol>> protocols = null;

                    // Only allow newer clients or (1.9.2 on 1.9.4 server if the server supports it)
                    if (info.getProtocolVersion() >= protocol || Via.getPlatform().isOldClientsAllowed()) {
                        protocols = ProtocolRegistry.getProtocolPath(info.getProtocolVersion(), protocol);
                    }

                    ProtocolPipeline pipeline = wrapper.user().getProtocolInfo().getPipeline();
                    if (protocols != null) {
                        for (Pair<Integer, Protocol> prot : protocols) {
                            pipeline.add(prot.getValue());
                            // Ensure mapping data has already been loaded
                            ProtocolRegistry.completeMappingDataLoading(prot.getValue().getClass());
                        }
                        wrapper.set(Type.VAR_INT, 0, protocol);
                    }

                    // Add Base Protocol
                    pipeline.add(ProtocolRegistry.getBaseProtocol(protocol));

                    // Change state
                    if (state == 1) {
                        info.setState(State.STATUS);
                    }
                    if (state == 2) {
                        info.setState(State.LOGIN);
                    }
                }
            });
        }
    });
}
 
Example #21
Source Project: ViaVersion   Author: ViaVersion   File: EntityPackets.java    License: MIT License 4 votes vote down vote up
public static void register(Protocol1_13_1To1_13 protocol) {
    MetadataRewriter1_13_1To1_13 metadataRewriter = protocol.get(MetadataRewriter1_13_1To1_13.class);

    protocol.registerOutgoing(ClientboundPackets1_13.SPAWN_ENTITY, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Entity id
            map(Type.UUID); // 1 - UUID
            map(Type.BYTE); // 2 - Type
            map(Type.DOUBLE); // 3 - X
            map(Type.DOUBLE); // 4 - Y
            map(Type.DOUBLE); // 5 - Z
            map(Type.BYTE); // 6 - Pitch
            map(Type.BYTE); // 7 - Yaw
            map(Type.INT); // 8 - Data

            // Track Entity
            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    byte type = wrapper.get(Type.BYTE, 0);
                    Entity1_13Types.EntityType entType = Entity1_13Types.getTypeFromId(type, true);

                    if (entType != null) {
                        if (entType.is(Entity1_13Types.EntityType.FALLING_BLOCK)) {
                            int data = wrapper.get(Type.INT, 0);
                            wrapper.set(Type.INT, 0, Protocol1_13_1To1_13.getNewBlockStateId(data));
                        }
                        // Register Type ID
                        wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType);
                    }
                }
            });
        }
    });

    protocol.registerOutgoing(ClientboundPackets1_13.SPAWN_MOB, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Entity ID
            map(Type.UUID); // 1 - Entity UUID
            map(Type.VAR_INT); // 2 - Entity Type
            map(Type.DOUBLE); // 3 - X
            map(Type.DOUBLE); // 4 - Y
            map(Type.DOUBLE); // 5 - Z
            map(Type.BYTE); // 6 - Yaw
            map(Type.BYTE); // 7 - Pitch
            map(Type.BYTE); // 8 - Head Pitch
            map(Type.SHORT); // 9 - Velocity X
            map(Type.SHORT); // 10 - Velocity Y
            map(Type.SHORT); // 11 - Velocity Z
            map(Types1_13.METADATA_LIST); // 12 - Metadata

            handler(metadataRewriter.getTrackerAndRewriter(Types1_13.METADATA_LIST));
        }
    });

    protocol.registerOutgoing(ClientboundPackets1_13.SPAWN_PLAYER, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Entity ID
            map(Type.UUID); // 1 - Player UUID
            map(Type.DOUBLE); // 2 - X
            map(Type.DOUBLE); // 3 - Y
            map(Type.DOUBLE); // 4 - Z
            map(Type.BYTE); // 5 - Yaw
            map(Type.BYTE); // 6 - Pitch
            map(Types1_13.METADATA_LIST); // 7 - Metadata

            handler(metadataRewriter.getTrackerAndRewriter(Types1_13.METADATA_LIST, Entity1_13Types.EntityType.PLAYER));
        }
    });

    metadataRewriter.registerEntityDestroy(ClientboundPackets1_13.DESTROY_ENTITIES);
    metadataRewriter.registerMetadataRewriter(ClientboundPackets1_13.ENTITY_METADATA, Types1_13.METADATA_LIST);
}
 
Example #22
Source Project: ViaVersion   Author: ViaVersion   File: Protocol1_13_2To1_13_1.java    License: MIT License 4 votes vote down vote up
@Override
protected void registerPackets() {
    InventoryPackets.register(this);
    WorldPackets.register(this);
    EntityPackets.register(this);

    registerIncoming(ServerboundPackets1_13.EDIT_BOOK, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.FLAT_VAR_INT_ITEM, Type.FLAT_ITEM);
        }
    });

    registerOutgoing(ClientboundPackets1_13.ADVANCEMENTS, new PacketRemapper() {
        @Override
        public void registerMap() {
            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    wrapper.passthrough(Type.BOOLEAN); // Reset/clear
                    int size = wrapper.passthrough(Type.VAR_INT); // Mapping size

                    for (int i = 0; i < size; i++) {
                        wrapper.passthrough(Type.STRING); // Identifier

                        // Parent
                        if (wrapper.passthrough(Type.BOOLEAN))
                            wrapper.passthrough(Type.STRING);

                        // Display data
                        if (wrapper.passthrough(Type.BOOLEAN)) {
                            wrapper.passthrough(Type.COMPONENT); // Title
                            wrapper.passthrough(Type.COMPONENT); // Description
                            Item icon = wrapper.read(Type.FLAT_ITEM);
                            wrapper.write(Type.FLAT_VAR_INT_ITEM, icon);
                            wrapper.passthrough(Type.VAR_INT); // Frame type
                            int flags = wrapper.passthrough(Type.INT); // Flags
                            if ((flags & 1) != 0)
                                wrapper.passthrough(Type.STRING); // Background texture
                            wrapper.passthrough(Type.FLOAT); // X
                            wrapper.passthrough(Type.FLOAT); // Y
                        }

                        wrapper.passthrough(Type.STRING_ARRAY); // Criteria

                        int arrayLength = wrapper.passthrough(Type.VAR_INT);
                        for (int array = 0; array < arrayLength; array++) {
                            wrapper.passthrough(Type.STRING_ARRAY); // String array
                        }
                    }
                }
            });
        }
    });
}
 
Example #23
Source Project: ViaVersion   Author: ViaVersion   File: EntityPackets.java    License: MIT License 4 votes vote down vote up
public static void register(Protocol protocol) {
    final PacketHandler metaTypeHandler = wrapper -> {
        for (Metadata metadata : wrapper.get(Types1_13_2.METADATA_LIST, 0)) {
            metadata.setMetaType(MetaType1_13_2.byId(metadata.getMetaType().getTypeID()));
        }
    };

    protocol.registerOutgoing(ClientboundPackets1_13.SPAWN_MOB, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Entity ID
            map(Type.UUID); // 1 - Entity UUID
            map(Type.VAR_INT); // 2 - Entity Type
            map(Type.DOUBLE); // 3 - X
            map(Type.DOUBLE); // 4 - Y
            map(Type.DOUBLE); // 5 - Z
            map(Type.BYTE); // 6 - Yaw
            map(Type.BYTE); // 7 - Pitch
            map(Type.BYTE); // 8 - Head Pitch
            map(Type.SHORT); // 9 - Velocity X
            map(Type.SHORT); // 10 - Velocity Y
            map(Type.SHORT); // 11 - Velocity Z
            map(Types1_13.METADATA_LIST, Types1_13_2.METADATA_LIST); // 12 - Metadata

            handler(metaTypeHandler);
        }
    });

    protocol.registerOutgoing(ClientboundPackets1_13.SPAWN_PLAYER, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Entity ID
            map(Type.UUID); // 1 - Player UUID
            map(Type.DOUBLE); // 2 - X
            map(Type.DOUBLE); // 3 - Y
            map(Type.DOUBLE); // 4 - Z
            map(Type.BYTE); // 5 - Yaw
            map(Type.BYTE); // 6 - Pitch
            map(Types1_13.METADATA_LIST, Types1_13_2.METADATA_LIST); // 7 - Metadata

            handler(metaTypeHandler);
        }
    });

    protocol.registerOutgoing(ClientboundPackets1_13.ENTITY_METADATA, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Entity ID
            map(Types1_13.METADATA_LIST, Types1_13_2.METADATA_LIST); // 1 - Metadata list

            handler(metaTypeHandler);
        }
    });
}
 
Example #24
Source Project: ViaVersion   Author: ViaVersion   File: InventoryPackets.java    License: MIT License 4 votes vote down vote up
public static void register(Protocol1_11To1_10 protocol) {
    ItemRewriter itemRewriter = new ItemRewriter(protocol, EntityIdRewriter::toClientItem, EntityIdRewriter::toServerItem);

    itemRewriter.registerSetSlot(ClientboundPackets1_9_3.SET_SLOT, Type.ITEM);
    itemRewriter.registerWindowItems(ClientboundPackets1_9_3.WINDOW_ITEMS, Type.ITEM_ARRAY);
    itemRewriter.registerEntityEquipment(ClientboundPackets1_9_3.ENTITY_EQUIPMENT, Type.ITEM);

    // Plugin message Packet -> Trading
    protocol.registerOutgoing(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.STRING); // 0 - Channel

            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (wrapper.get(Type.STRING, 0).equalsIgnoreCase("MC|TrList")) {
                        wrapper.passthrough(Type.INT); // Passthrough Window ID

                        int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                        for (int i = 0; i < size; i++) {
                            EntityIdRewriter.toClientItem(wrapper.passthrough(Type.ITEM)); // Input Item
                            EntityIdRewriter.toClientItem(wrapper.passthrough(Type.ITEM)); // Output Item

                            boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item
                            if (secondItem)
                                EntityIdRewriter.toClientItem(wrapper.passthrough(Type.ITEM)); // Second Item

                            wrapper.passthrough(Type.BOOLEAN); // Trade disabled
                            wrapper.passthrough(Type.INT); // Number of tools uses
                            wrapper.passthrough(Type.INT); // Maximum number of trade uses
                        }
                    }
                }
            });
        }
    });

    itemRewriter.registerClickWindow(ServerboundPackets1_9_3.CLICK_WINDOW, Type.ITEM);
    itemRewriter.registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM);
}
 
Example #25
Source Project: ViaBackwards   Author: ViaVersion   File: SoundPackets1_12.java    License: MIT License 4 votes vote down vote up
@Override
protected void registerPackets() {
    protocol.registerOutgoing(ClientboundPackets1_12.NAMED_SOUND, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.STRING); // 0 - Sound name
            map(Type.VAR_INT); // 1 - Sound Category
            map(Type.INT); // 2 - x
            map(Type.INT); // 3 - y
            map(Type.INT); // 4 - z
            map(Type.FLOAT); // 5 - Volume
            map(Type.FLOAT); // 6 - Pitch
        }
    });

    protocol.registerOutgoing(ClientboundPackets1_12.SOUND, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Sound name
            map(Type.VAR_INT); // 1 - Sound Category
            map(Type.INT); // 2 - x
            map(Type.INT); // 3 - y
            map(Type.INT); // 4 - z
            map(Type.FLOAT); // 5 - Volume
            map(Type.FLOAT); // 6 - Pitch

            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int oldId = wrapper.get(Type.VAR_INT, 0);
                    int newId = handleSounds(oldId);
                    if (newId == -1) {
                        wrapper.cancel();
                        return;
                    }

                    if (hasPitch(oldId)) {
                        wrapper.set(Type.FLOAT, 1, handlePitch(oldId));
                    }
                    wrapper.set(Type.VAR_INT, 0, newId);
                }
            });
        }
    });
}
 
Example #26
Source Project: ViaBackwards   Author: ViaVersion   File: Protocol1_13_1To1_13_2.java    License: MIT License 4 votes vote down vote up
@Override
protected void registerPackets() {
    InventoryPackets1_13_2.register(this);
    WorldPackets1_13_2.register(this);
    EntityPackets1_13_2.register(this);

    registerIncoming(ServerboundPackets1_13.EDIT_BOOK, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.FLAT_ITEM, Type.FLAT_VAR_INT_ITEM);
        }
    });

    registerOutgoing(ClientboundPackets1_13.ADVANCEMENTS, new PacketRemapper() {
        @Override
        public void registerMap() {
            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    wrapper.passthrough(Type.BOOLEAN); // Reset/clear
                    int size = wrapper.passthrough(Type.VAR_INT); // Mapping size

                    for (int i = 0; i < size; i++) {
                        wrapper.passthrough(Type.STRING); // Identifier

                        // Parent
                        if (wrapper.passthrough(Type.BOOLEAN))
                            wrapper.passthrough(Type.STRING);

                        // Display data
                        if (wrapper.passthrough(Type.BOOLEAN)) {
                            wrapper.passthrough(Type.COMPONENT); // Title
                            wrapper.passthrough(Type.COMPONENT); // Description
                            Item icon = wrapper.read(Type.FLAT_VAR_INT_ITEM);
                            wrapper.write(Type.FLAT_ITEM, icon);
                            wrapper.passthrough(Type.VAR_INT); // Frame type
                            int flags = wrapper.passthrough(Type.INT); // Flags
                            if ((flags & 1) != 0)
                                wrapper.passthrough(Type.STRING); // Background texture
                            wrapper.passthrough(Type.FLOAT); // X
                            wrapper.passthrough(Type.FLOAT); // Y
                        }

                        wrapper.passthrough(Type.STRING_ARRAY); // Criteria

                        int arrayLength = wrapper.passthrough(Type.VAR_INT);
                        for (int array = 0; array < arrayLength; array++) {
                            wrapper.passthrough(Type.STRING_ARRAY); // String array
                        }
                    }
                }
            });
        }
    });
}
 
Example #27
Source Project: ViaBackwards   Author: ViaVersion   File: InventoryPackets1_13_1.java    License: MIT License 4 votes vote down vote up
public static void register(Protocol protocol) {
    ItemRewriter itemRewriter = new ItemRewriter(protocol, InventoryPackets1_13_1::toClient, InventoryPackets1_13_1::toServer);

    itemRewriter.registerSetCooldown(ClientboundPackets1_13.COOLDOWN, InventoryPackets1_13_1::getOldItemId);
    itemRewriter.registerWindowItems(ClientboundPackets1_13.WINDOW_ITEMS, Type.FLAT_ITEM_ARRAY);
    itemRewriter.registerSetSlot(ClientboundPackets1_13.SET_SLOT, Type.FLAT_ITEM);

    protocol.registerOutgoing(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {
        @Override
        public void registerMap() {
            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    String channel = wrapper.passthrough(Type.STRING);
                    if (channel.equals("minecraft:trader_list")) {
                        wrapper.passthrough(Type.INT); //Passthrough Window ID

                        int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                        for (int i = 0; i < size; i++) {
                            //Input Item
                            Item input = wrapper.passthrough(Type.FLAT_ITEM);
                            toClient(input);
                            //Output Item
                            Item output = wrapper.passthrough(Type.FLAT_ITEM);
                            toClient(output);

                            boolean secondItem = wrapper.passthrough(Type.BOOLEAN); //Has second item
                            if (secondItem) {
                                //Second Item
                                Item second = wrapper.passthrough(Type.FLAT_ITEM);
                                toClient(second);
                            }

                            wrapper.passthrough(Type.BOOLEAN); //Trade disabled
                            wrapper.passthrough(Type.INT); //Number of tools uses
                            wrapper.passthrough(Type.INT); //Maximum number of trade uses
                        }
                    }
                }
            });
        }
    });

    itemRewriter.registerEntityEquipment(ClientboundPackets1_13.ENTITY_EQUIPMENT, Type.FLAT_ITEM);
    itemRewriter.registerClickWindow(ServerboundPackets1_13.CLICK_WINDOW, Type.FLAT_ITEM);
    itemRewriter.registerCreativeInvAction(ServerboundPackets1_13.CREATIVE_INVENTORY_ACTION, Type.FLAT_ITEM);
}
 
Example #28
Source Project: ViaBackwards   Author: ViaVersion   File: SoundPackets1_10.java    License: MIT License 4 votes vote down vote up
@Override
protected void registerPackets() {
    protocol.registerOutgoing(ClientboundPackets1_9_3.NAMED_SOUND, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.STRING); // 0 - Sound name
            map(Type.VAR_INT); // 1 - Sound Category
            map(Type.INT); // 2 - x
            map(Type.INT); // 3 - y
            map(Type.INT); // 4 - z
            map(Type.FLOAT); // 5 - Volume
            map(Type.FLOAT, toOldPitch); // 6 - Pitch
        }
    });

    protocol.registerOutgoing(ClientboundPackets1_9_3.SOUND, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Sound name
            map(Type.VAR_INT); // 1 - Sound Category
            map(Type.INT); // 2 - x
            map(Type.INT); // 3 - y
            map(Type.INT); // 4 - z
            map(Type.FLOAT); // 5 - Volume
            map(Type.FLOAT, toOldPitch); // 6 - Pitch

            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int oldId = wrapper.get(Type.VAR_INT, 0);
                    int newId = handleSounds(oldId);
                    if (newId == -1) {
                        wrapper.cancel();
                        return;
                    }

                    if (hasPitch(oldId)) {
                        wrapper.set(Type.UNSIGNED_BYTE, 0, (short) Math.round(handlePitch(oldId) * 63.5F));
                    }
                    wrapper.set(Type.VAR_INT, 0, newId);
                }
            });
        }
    });
}
 
Example #29
Source Project: ViaBackwards   Author: ViaVersion   File: ItemPackets1_11_1.java    License: MIT License 4 votes vote down vote up
@Override
protected void registerPackets() {
    ItemRewriter itemRewriter = new ItemRewriter(protocol, this::handleItemToClient, this::handleItemToServer);

    itemRewriter.registerSetSlot(ClientboundPackets1_9_3.SET_SLOT, Type.ITEM);
    itemRewriter.registerWindowItems(ClientboundPackets1_9_3.WINDOW_ITEMS, Type.ITEM_ARRAY);
    itemRewriter.registerEntityEquipment(ClientboundPackets1_9_3.ENTITY_EQUIPMENT, Type.ITEM);

    // Plugin message Packet -> Trading
    protocol.registerOutgoing(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.STRING); // 0 - Channel

            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (wrapper.get(Type.STRING, 0).equalsIgnoreCase("MC|TrList")) {
                        wrapper.passthrough(Type.INT); // Passthrough Window ID

                        int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                        for (int i = 0; i < size; i++) {
                            wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM))); // Input Item
                            wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM))); // Output Item

                            boolean secondItem = wrapper.passthrough(Type.BOOLEAN); // Has second item
                            if (secondItem) {
                                wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM))); // Second Item
                            }

                            wrapper.passthrough(Type.BOOLEAN); // Trade disabled
                            wrapper.passthrough(Type.INT); // Number of tools uses
                            wrapper.passthrough(Type.INT); // Maximum number of trade uses
                        }
                    }
                }
            });
        }
    });

    itemRewriter.registerClickWindow(ServerboundPackets1_9_3.CLICK_WINDOW, Type.ITEM);
    itemRewriter.registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM);

    // Handle item metadata
    protocol.getEntityPackets().registerMetaHandler().handle(e -> {
        Metadata data = e.getData();

        if (data.getMetaType().getType().equals(Type.ITEM)) { // Is Item
            data.setValue(handleItemToClient((Item) data.getValue()));
        }

        return data;
    });
}
 
Example #30
Source Project: ViaBackwards   Author: ViaVersion   File: PlayerPackets1_11.java    License: MIT License 4 votes vote down vote up
public void register(Protocol1_10To1_11 protocol) {
    protocol.registerOutgoing(ClientboundPackets1_9_3.TITLE, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Action

            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) {
                        // Convert to the old actionbar way
                        PacketWrapper actionbar = new PacketWrapper(0x0F, null, wrapper.user()); // Chat Message packet
                        JsonElement msg = wrapper.read(Type.COMPONENT);
                        actionbar.write(Type.COMPONENT, msg);
                        actionbar.write(Type.BYTE, (byte) 2); // Above hotbar

                        actionbar.send(Protocol1_10To1_11.class);

                        wrapper.cancel(); // Cancel the title packet
                        return;
                    }

                    if (action > 2) {
                        wrapper.set(Type.VAR_INT, 0, action - 1); // Move everything one position down
                    }
                }
            });
        }
    });

    protocol.registerOutgoing(ClientboundPackets1_9_3.COLLECT_ITEM, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Collected entity id
            map(Type.VAR_INT); // 1 - Collector entity id

            handler(wrapper -> wrapper.read(Type.VAR_INT)); // Ignore item pickup count
        }
    });


    protocol.registerIncoming(ServerboundPackets1_9_3.PLAYER_BLOCK_PLACEMENT, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.POSITION); // 0 - Location
            map(Type.VAR_INT); // 1 - Face
            map(Type.VAR_INT); // 2 - Hand

            map(Type.UNSIGNED_BYTE, TO_NEW_FLOAT);
            map(Type.UNSIGNED_BYTE, TO_NEW_FLOAT);
            map(Type.UNSIGNED_BYTE, TO_NEW_FLOAT);
        }
    });
}