Java Code Examples for us.myles.ViaVersion.api.PacketWrapper#getId()

The following examples show how to use us.myles.ViaVersion.api.PacketWrapper#getId() . 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 File: ProtocolPipeline.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception {
    int originalID = packetWrapper.getId();
    List<Protocol> protocols = new ArrayList<>(protocolList);
    // Other way if outgoing
    if (direction == Direction.OUTGOING)
        Collections.reverse(protocols);

    // Apply protocols
    packetWrapper.apply(direction, state, 0, protocols);
    super.transform(direction, state, packetWrapper);

    if (Via.getManager().isDebug()) {
        // Debug packet
        int serverProtocol = userConnection.getProtocolInfo().getServerProtocolVersion();
        int clientProtocol = userConnection.getProtocolInfo().getProtocolVersion();
        ViaPlatform platform = Via.getPlatform();

        String actualUsername = packetWrapper.user().getProtocolInfo().getUsername();
        String username = actualUsername != null ? actualUsername + " " : "";

        platform.getLogger().log(Level.INFO, "{0}{1} {2}: {3} (0x{4}) -> {5} (0x{6}) [{7}] {8}",
                new Object[]{
                        username,
                        direction,
                        state,
                        originalID,
                        Integer.toHexString(originalID),
                        packetWrapper.getId(),
                        Integer.toHexString(packetWrapper.getId()),
                        Integer.toString(clientProtocol),
                        packetWrapper
                });
    }
}
 
Example 2
Source File: BaseProtocol.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception {
    super.transform(direction, state, packetWrapper);
    if (direction == Direction.INCOMING && state == State.HANDSHAKE) {
        // Disable if it isn't a handshake packet.
        if (packetWrapper.getId() != 0) {
            packetWrapper.user().setActive(false);
        }
    }
}
 
Example 3
Source File: EntityPackets1_14.java    From ViaBackwards with MIT License 5 votes vote down vote up
@Override
protected void addTrackedEntity(PacketWrapper wrapper, int entityId, EntityType type) throws Exception {
    super.addTrackedEntity(wrapper, entityId, type);

    // Cache the position for every newly tracked entity
    if (type == Entity1_14Types.EntityType.PAINTING) {
        final Position position = wrapper.get(Type.POSITION, 0);
        positionHandler.cacheEntityPosition(wrapper, position.getX(), position.getY(), position.getZ(), true, false);
    } else if (wrapper.getId() != 0x25) { // ignore join game
        positionHandler.cacheEntityPosition(wrapper, true, false);
    }
}
 
Example 4
Source File: Protocol.java    From ViaVersion with MIT License 4 votes vote down vote up
/**
 * Transform a packet using this protocol
 *
 * @param direction     The direction the packet is going in
 * @param state         The current protocol state
 * @param packetWrapper The packet wrapper to transform
 * @throws Exception Throws exception if it fails to transform
 */
public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception {
    Packet statePacket = new Packet(state, packetWrapper.getId());
    Map<Packet, ProtocolPacket> packetMap = (direction == Direction.OUTGOING ? outgoing : incoming);
    ProtocolPacket protocolPacket = packetMap.get(statePacket);
    if (protocolPacket == null) {
        return;
    }

    // Write packet id
    int oldId = packetWrapper.getId();
    int newId = direction == Direction.OUTGOING ? protocolPacket.getNewID() : protocolPacket.getOldID();
    packetWrapper.setId(newId);
    if (protocolPacket.getRemapper() == null) {
        return;
    }

    // Remap
    try {
        protocolPacket.getRemapper().remap(packetWrapper);
    } catch (Exception e) {
        if (e instanceof CancelException) {
            throw e;
        }

        Class<? extends PacketType> packetTypeClass = state == State.PLAY ? (direction == Direction.OUTGOING ? oldClientboundPacketEnum : newServerboundPacketEnum) : null;
        if (packetTypeClass != null) {
            PacketType[] enumConstants = packetTypeClass.getEnumConstants();
            PacketType packetType = oldId < enumConstants.length && oldId >= 0 ? enumConstants[oldId] : null;
            Via.getPlatform().getLogger().warning("ERROR IN " + getClass().getSimpleName() + " IN REMAP OF " + packetType + " (" + toNiceHex(oldId) + ")");
        } else {
            Via.getPlatform().getLogger().warning("ERROR IN " + getClass().getSimpleName()
                    + " IN REMAP OF " + toNiceHex(oldId) + "->" + toNiceHex(newId));
        }
        throw e;
    }

    if (packetWrapper.isCancelled()) {
        throw CancelException.generate();
    }
}