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

The following examples show how to use us.myles.ViaVersion.api.remapper.PacketRemapper. 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: BlockRewriter.java    License: MIT License 6 votes vote down vote up
public void registerBlockAction(ClientboundPacketType packetType) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(positionType); // Location
            map(Type.UNSIGNED_BYTE); // Action id
            map(Type.UNSIGNED_BYTE); // Action param
            map(Type.VAR_INT); // Block id - /!\ NOT BLOCK STATE
            handler(wrapper -> {
                int id = wrapper.get(Type.VAR_INT, 0);
                int mappedId = blockRewriter.rewrite(id);
                if (mappedId == -1) {
                    // Block (action) has been removed
                    wrapper.cancel();
                    return;
                }

                wrapper.set(Type.VAR_INT, 0, mappedId);
            });
        }
    });
}
 
Example #2
Source Project: ViaVersion   Author: ViaVersion   File: BlockRewriter.java    License: MIT License 6 votes vote down vote up
public void registerEffect(ClientboundPacketType packetType, int playRecordId, int blockBreakId, IdRewriteFunction itemIdRewriteFunction) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.INT); // Effect Id
            map(positionType); // Location
            map(Type.INT); // Data
            handler(wrapper -> {
                int id = wrapper.get(Type.INT, 0);
                int data = wrapper.get(Type.INT, 1);
                if (id == playRecordId) { // Play record
                    wrapper.set(Type.INT, 1, itemIdRewriteFunction.rewrite(data));
                } else if (id == blockBreakId) { // Block break + block break sound
                    wrapper.set(Type.INT, 1, blockStateRewriter.rewrite(data));
                }
            });
        }
    });
}
 
Example #3
Source Project: ViaVersion   Author: ViaVersion   File: SoundRewriter.java    License: MIT License 6 votes vote down vote up
public void registerSound(ClientboundPacketType packetType) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // Sound Id
            handler(wrapper -> {
                int soundId = wrapper.get(Type.VAR_INT, 0);
                int mappedId = idRewriter.rewrite(soundId);
                if (mappedId == -1) {
                    wrapper.cancel();
                } else if (soundId != mappedId) {
                    wrapper.set(Type.VAR_INT, 0, mappedId);
                }
            });
        }
    });
}
 
Example #4
Source Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: MIT License 6 votes vote down vote up
public void registerEntityEquipmentArray(ClientboundPacketType packetType, Type<Item> type) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Entity ID

            handler(wrapper -> {
                byte slot;
                do {
                    slot = wrapper.passthrough(Type.BYTE);
                     // & 0x7F into an extra variable if slot is needed
                    toClient.rewrite(wrapper.passthrough(type));
                } while ((slot & 0xFFFFFF80) != 0);
            });
        }
    });
}
 
Example #5
Source Project: ViaVersion   Author: ViaVersion   File: TagRewriter.java    License: MIT License 6 votes vote down vote up
public void register(ClientboundPacketType packetType) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            handler(wrapper -> {
                handle(wrapper, blockRewriter, newBlockTags);
                handle(wrapper, itemRewriter, newItemTags);

                if (entityRewriter == null && newEntityTags.isEmpty()) return;

                int fluidTagsSize = wrapper.passthrough(Type.VAR_INT);
                for (int i = 0; i < fluidTagsSize; i++) {
                    wrapper.passthrough(Type.STRING);
                    wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE);
                }

                handle(wrapper, entityRewriter, newEntityTags);
            });
        }
    });
}
 
Example #6
Source Project: ViaVersion   Author: ViaVersion   File: MetadataRewriter.java    License: MIT License 6 votes vote down vote up
public void registerJoinGame(ClientboundPacketType packetType, @Nullable EntityType playerType) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.INT); // 0 - Entity ID
            map(Type.UNSIGNED_BYTE); // 1 - Gamemode
            map(Type.INT); // 2 - Dimension
            handler(wrapper -> {
                ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
                int dimensionId = wrapper.get(Type.INT, 1);
                clientChunks.setEnvironment(dimensionId);

                if (playerType != null) {
                    wrapper.user().get(entityTrackerClass).addEntity(wrapper.get(Type.INT, 0), playerType);
                }
            });
        }
    });
}
 
Example #7
Source Project: ViaVersion   Author: ViaVersion   File: MetadataRewriter.java    License: MIT License 6 votes vote down vote up
public void registerSpawnTrackerWithData(ClientboundPacketType packetType, EntityType fallingBlockType, IdRewriteFunction itemRewriter) {
    protocol.registerOutgoing(packetType, 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 - Pitch
            map(Type.BYTE); // 7 - Yaw
            map(Type.INT); // 8 - Data
            handler(getTracker());
            handler(wrapper -> {
                int entityId = wrapper.get(Type.VAR_INT, 0);
                EntityType entityType = wrapper.user().get(entityTrackerClass).getEntity(entityId);
                if (entityType == fallingBlockType) {
                    wrapper.set(Type.INT, 0, itemRewriter.rewrite(wrapper.get(Type.INT, 0)));
                }
            });
        }
    });
}
 
Example #8
Source Project: ViaVersion   Author: ViaVersion   File: MetadataRewriter.java    License: MIT License 6 votes vote down vote up
public void registerMetadataRewriter(ClientboundPacketType packetType, @Nullable Type<List<Metadata>> oldMetaType, Type<List<Metadata>> newMetaType) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Entity ID
            if (oldMetaType != null) {
                map(oldMetaType, newMetaType);
            } else {
                map(newMetaType);
            }
            handler(wrapper -> {
                int entityId = wrapper.get(Type.VAR_INT, 0);
                List<Metadata> metadata = wrapper.get(newMetaType, 0);
                handleMetadata(entityId, metadata, wrapper.user());
            });
        }
    });
}
 
Example #9
Source Project: ViaVersion   Author: ViaVersion   File: Protocol1_12_2To1_12_1.java    License: MIT License 6 votes vote down vote up
@Override
protected void registerPackets() {
    registerOutgoing(ClientboundPackets1_12_1.KEEP_ALIVE, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT, Type.LONG);
        }
    });

    registerIncoming(ServerboundPackets1_12_1.KEEP_ALIVE, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.LONG, Type.VAR_INT);
        }
    });
}
 
Example #10
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 #11
Source Project: ViaVersion   Author: ViaVersion   File: Protocol1_9To1_8.java    License: MIT License 6 votes vote down vote up
@Override
protected void registerPackets() {
    new MetadataRewriter1_9To1_8(this);

    // Disconnect workaround (JSON!)
    registerOutgoing(State.LOGIN, 0x00, 0x00, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 0 - Reason
        }
    });

    // Other Handlers
    SpawnPackets.register(this);
    InventoryPackets.register(this);
    EntityPackets.register(this);
    PlayerPackets.register(this);
    WorldPackets.register(this);
}
 
Example #12
Source Project: ViaBackwards   Author: ViaVersion   File: EntityRewriter.java    License: MIT License 6 votes vote down vote up
public void registerSpawnTrackerWithData(ClientboundPacketType packetType, EntityType fallingBlockType, IdRewriteFunction blockStateRewriter) {
    protocol.registerOutgoing(packetType, 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 - Pitch
            map(Type.BYTE); // 7 - Yaw
            map(Type.INT); // 8 - Data
            handler(wrapper -> {
                EntityType entityType = setOldEntityId(wrapper);
                if (entityType == fallingBlockType) {
                    int blockState = wrapper.get(Type.INT, 0);
                    wrapper.set(Type.INT, 0, blockStateRewriter.rewrite(blockState));
                }
            });
        }
    });
}
 
Example #13
Source Project: ViaBackwards   Author: ViaVersion   File: SoundRewriter.java    License: MIT License 6 votes vote down vote up
public void registerNamedSound(ClientboundPacketType packetType) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.STRING); // Sound identifier
            handler(wrapper -> {
                String soundId = wrapper.get(Type.STRING, 0);
                String mappedId = stringIdRewriter.apply(soundId);
                if (mappedId == null) return;
                if (!mappedId.isEmpty()) {
                    wrapper.set(Type.STRING, 0, mappedId);
                } else {
                    wrapper.cancel();
                }
            });
        }
    });
}
 
Example #14
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 #15
Source Project: ViaVersion   Author: ViaVersion   File: Protocol.java    License: MIT License 5 votes vote down vote up
public void registerIncoming(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper, boolean override) {
    ProtocolPacket protocolPacket = new ProtocolPacket(state, oldPacketID, newPacketID, packetRemapper);
    Packet packet = new Packet(state, newPacketID);
    if (!override && incoming.containsKey(packet)) {
        Via.getPlatform().getLogger().log(Level.WARNING, packet + " already registered!" +
                " If this override is intentional, set override to true. Stacktrace: ", new Exception());
    }
    incoming.put(packet, protocolPacket);
}
 
Example #16
Source Project: ViaVersion   Author: ViaVersion   File: Protocol.java    License: MIT License 5 votes vote down vote up
public void cancelIncoming(State state, int oldPacketID, int newPacketID) {
    registerIncoming(state, oldPacketID, newPacketID, new PacketRemapper() {
        @Override
        public void registerMap() {
            handler(PacketWrapper::cancel);
        }
    });
}
 
Example #17
Source Project: ViaVersion   Author: ViaVersion   File: Protocol.java    License: MIT License 5 votes vote down vote up
public void cancelOutgoing(State state, int oldPacketID, int newPacketID) {
    registerOutgoing(state, oldPacketID, newPacketID, new PacketRemapper() {
        @Override
        public void registerMap() {
            handler(PacketWrapper::cancel);
        }
    });
}
 
Example #18
Source Project: ViaVersion   Author: ViaVersion   File: Protocol.java    License: MIT License 5 votes vote down vote up
public void registerOutgoing(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper, boolean override) {
    ProtocolPacket protocolPacket = new ProtocolPacket(state, oldPacketID, newPacketID, packetRemapper);
    Packet packet = new Packet(state, oldPacketID);
    if (!override && outgoing.containsKey(packet)) {
        Via.getPlatform().getLogger().log(Level.WARNING, packet + " already registered!" +
                " If override is intentional, set override to true. Stacktrace: ", new Exception());
    }
    outgoing.put(packet, protocolPacket);
}
 
Example #19
Source Project: ViaVersion   Author: ViaVersion   File: Protocol.java    License: MIT License 5 votes vote down vote up
/**
 * Registers an outgoing protocol and automatically maps it to the new id.
 *
 * @param packetType     clientbound packet type the server sends
 * @param packetRemapper remapper
 */
public void registerOutgoing(C1 packetType, @Nullable PacketRemapper packetRemapper) {
    checkPacketType(packetType, packetType.getClass() == oldClientboundPacketEnum);

    ClientboundPacketType mappedPacket = oldClientboundPacketEnum == newClientboundPacketEnum ? packetType
            : Arrays.stream(newClientboundPacketEnum.getEnumConstants()).filter(en -> en.name().equals(packetType.name())).findAny().orElse(null);
    Preconditions.checkNotNull(mappedPacket, "Packet type " + packetType + " in " + packetType.getClass().getSimpleName() + " could not be automatically mapped!");

    int oldId = packetType.ordinal();
    int newId = mappedPacket.ordinal();
    registerOutgoing(State.PLAY, oldId, newId, packetRemapper);
}
 
Example #20
Source Project: ViaVersion   Author: ViaVersion   File: Protocol.java    License: MIT License 5 votes vote down vote up
/**
 * Registers an incoming protocol and automatically maps it to the server's id.
 *
 * @param packetType     serverbound packet type the client sends
 * @param packetRemapper remapper
 */
public void registerIncoming(S2 packetType, @Nullable PacketRemapper packetRemapper) {
    checkPacketType(packetType, packetType.getClass() == newServerboundPacketEnum);

    ServerboundPacketType mappedPacket = oldServerboundPacketEnum == newServerboundPacketEnum ? packetType
            : Arrays.stream(oldServerboundPacketEnum.getEnumConstants()).filter(en -> en.name().equals(packetType.name())).findAny().orElse(null);
    Preconditions.checkNotNull(mappedPacket, "Packet type " + packetType + " in " + packetType.getClass().getSimpleName() + " could not be automatically mapped!");

    int oldId = mappedPacket.ordinal();
    int newId = packetType.ordinal();
    registerIncoming(State.PLAY, oldId, newId, packetRemapper);
}
 
Example #21
Source Project: ViaVersion   Author: ViaVersion   File: BlockRewriter.java    License: MIT License 5 votes vote down vote up
public void registerBlockChange(ClientboundPacketType packetType) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(positionType);
            map(Type.VAR_INT);
            handler(wrapper -> wrapper.set(Type.VAR_INT, 0, blockStateRewriter.rewrite(wrapper.get(Type.VAR_INT, 0))));
        }
    });
}
 
Example #22
Source Project: ViaVersion   Author: ViaVersion   File: BlockRewriter.java    License: MIT License 5 votes vote down vote up
public void registerMultiBlockChange(ClientboundPacketType packetType) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.INT); // 0 - Chunk X
            map(Type.INT); // 1 - Chunk Z
            map(Type.BLOCK_CHANGE_RECORD_ARRAY); // 2 - Records
            handler(wrapper -> {
                for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) {
                    record.setBlockId(blockStateRewriter.rewrite(record.getBlockId()));
                }
            });
        }
    });
}
 
Example #23
Source Project: ViaVersion   Author: ViaVersion   File: BlockRewriter.java    License: MIT License 5 votes vote down vote up
public void registerSpawnParticle(ClientboundPacketType packetType, int blockId, int fallingDustId, int itemId,
                                  @Nullable IdRewriteFunction particleRewriteFunction, ItemRewriter.RewriteFunction itemRewriteFunction, Type<Item> itemType, Type<?> coordType) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.INT); // 0 - Particle ID
            map(Type.BOOLEAN); // 1 - Long Distance
            map(coordType); // 2 - X
            map(coordType); // 3 - Y
            map(coordType); // 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(wrapper -> {
                int id = wrapper.get(Type.INT, 0);
                if (id == -1) return;
                if (id == blockId || id == fallingDustId) {
                    int data = wrapper.passthrough(Type.VAR_INT);
                    wrapper.set(Type.VAR_INT, 0, blockStateRewriter.rewrite(data));
                } else if (id == itemId) {
                    // Has to be like this, until we make *everything* object oriented inside of each protocol :(
                    itemRewriteFunction.rewrite(wrapper.passthrough(itemType));
                }

                if (particleRewriteFunction != null) {
                    int newId = particleRewriteFunction.rewrite(id);
                    if (newId != id) {
                        wrapper.set(Type.INT, 0, newId);
                    }
                }
            });
        }
    });
}
 
Example #24
Source Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: MIT License 5 votes vote down vote up
public void registerWindowItems(ClientboundPacketType packetType, Type<Item[]> type) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.UNSIGNED_BYTE); // 0 - Window ID
            map(type); // 1 - Window Values

            handler(itemArrayHandler(type));
        }
    });
}
 
Example #25
Source Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: MIT License 5 votes vote down vote up
public void registerSetSlot(ClientboundPacketType packetType, Type<Item> type) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.BYTE); // 0 - Window ID
            map(Type.SHORT); // 1 - Slot ID
            map(type); // 2 - Slot Value

            handler(itemToClientHandler(type));
        }
    });
}
 
Example #26
Source Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: MIT License 5 votes vote down vote up
public void registerEntityEquipment(ClientboundPacketType packetType, Type<Item> type) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Entity ID
            map(Type.VAR_INT); // 1 - Slot ID
            map(type); // 2 - Item

            handler(itemToClientHandler(type));
        }
    });
}
 
Example #27
Source Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: MIT License 5 votes vote down vote up
public void registerCreativeInvAction(ServerboundPacketType packetType, Type<Item> type) {
    protocol.registerIncoming(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.SHORT); // 0 - Slot
            map(type); // 1 - Clicked Item

            handler(itemToServerHandler(type));
        }
    });
}
 
Example #28
Source Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: MIT License 5 votes vote down vote up
public void registerClickWindow(ServerboundPacketType packetType, Type<Item> type) {
    protocol.registerIncoming(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.UNSIGNED_BYTE); // 0 - Window ID
            map(Type.SHORT); // 1 - Slot
            map(Type.BYTE); // 2 - Button
            map(Type.SHORT); // 3 - Action number
            map(Type.VAR_INT); // 4 - Mode
            map(type); // 5 - Clicked Item

            handler(itemToServerHandler(type));
        }
    });
}
 
Example #29
Source Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: MIT License 5 votes vote down vote up
public void registerSetCooldown(ClientboundPacketType packetType, IdRewriteFunction itemIDRewriteFunction) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            handler(wrapper -> {
                int itemId = wrapper.read(Type.VAR_INT);
                wrapper.write(Type.VAR_INT, itemIDRewriteFunction.rewrite(itemId));
            });
        }
    });
}
 
Example #30
Source Project: ViaVersion   Author: ViaVersion   File: ComponentRewriter.java    License: MIT License 5 votes vote down vote up
public void registerChatMessage(ClientboundPacketType packetType) {
    protocol.registerOutgoing(packetType, new PacketRemapper() {
        @Override
        public void registerMap() {
            handler(wrapper -> processText(wrapper.passthrough(Type.COMPONENT)));
        }
    });
}