Search in sources :

Example 1 with PacketType

use of com.viaversion.viaversion.api.protocol.packet.PacketType in project ViaVersion by ViaVersion.

the class AbstractProtocol method throwRemapError.

private void throwRemapError(Direction direction, State state, int oldId, int newId, InformativeException e) throws InformativeException {
    // Don't print errors during handshake
    if (state == State.HANDSHAKE) {
        throw e;
    }
    Class<? extends PacketType> packetTypeClass = state == State.PLAY ? (direction == Direction.CLIENTBOUND ? 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;
}
Also used : ServerboundPacketType(com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType) PacketType(com.viaversion.viaversion.api.protocol.packet.PacketType) ClientboundPacketType(com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType)

Example 2 with PacketType

use of com.viaversion.viaversion.api.protocol.packet.PacketType in project ViaVersion by ViaVersion.

the class PacketWrapperImpl method read.

@Override
public <T> T read(Type<T> type) throws Exception {
    if (type == Type.NOTHING)
        return null;
    if (readableObjects.isEmpty()) {
        Preconditions.checkNotNull(inputBuffer, "This packet does not have an input buffer.");
        // We could in the future log input read values, but honestly for things like bulk maps, mem waste D:
        try {
            return type.read(inputBuffer);
        } catch (Exception e) {
            throw new InformativeException(e).set("Type", type.getTypeName()).set("Packet ID", getId()).set("Packet Type", packetType).set("Data", packetValues);
        }
    }
    Pair<Type, Object> read = readableObjects.poll();
    Type rtype = read.key();
    if (rtype == type || (type.getBaseClass() == rtype.getBaseClass() && type.getOutputClass() == rtype.getOutputClass())) {
        return (T) read.value();
    } else if (rtype == Type.NOTHING) {
        // retry
        return read(type);
    } else {
        Exception e = new IOException("Unable to read type " + type.getTypeName() + ", found " + read.key().getTypeName());
        throw new InformativeException(e).set("Type", type.getTypeName()).set("Packet ID", getId()).set("Packet Type", packetType).set("Data", packetValues);
    }
}
Also used : PacketType(com.viaversion.viaversion.api.protocol.packet.PacketType) Type(com.viaversion.viaversion.api.type.Type) IOException(java.io.IOException) InformativeException(com.viaversion.viaversion.exception.InformativeException) IOException(java.io.IOException) InformativeException(com.viaversion.viaversion.exception.InformativeException) NoSuchElementException(java.util.NoSuchElementException) CancelException(com.viaversion.viaversion.exception.CancelException)

Example 3 with PacketType

use of com.viaversion.viaversion.api.protocol.packet.PacketType in project ViaVersion by ViaVersion.

the class PacketWrapperImpl method get.

@Override
public <T> T get(Type<T> type, int index) throws Exception {
    int currentIndex = 0;
    for (Pair<Type, Object> packetValue : packetValues) {
        if (packetValue.key() != type)
            continue;
        if (currentIndex == index) {
            return (T) packetValue.value();
        }
        currentIndex++;
    }
    Exception e = new ArrayIndexOutOfBoundsException("Could not find type " + type.getTypeName() + " at " + index);
    throw new InformativeException(e).set("Type", type.getTypeName()).set("Index", index).set("Packet ID", getId()).set("Packet Type", packetType).set("Data", packetValues);
}
Also used : PacketType(com.viaversion.viaversion.api.protocol.packet.PacketType) Type(com.viaversion.viaversion.api.type.Type) InformativeException(com.viaversion.viaversion.exception.InformativeException) IOException(java.io.IOException) InformativeException(com.viaversion.viaversion.exception.InformativeException) NoSuchElementException(java.util.NoSuchElementException) CancelException(com.viaversion.viaversion.exception.CancelException)

Example 4 with PacketType

use of com.viaversion.viaversion.api.protocol.packet.PacketType in project ViaVersion by ViaVersion.

the class VersionedPacketTransformerImpl method transformPacket.

private void transformPacket(PacketWrapper packet) throws Exception {
    // If clientbound: Constructor given inputProtocolVersion → Client version
    // If serverbound: Constructor given inputProtocolVersion → Server version
    PacketType packetType = packet.getPacketType();
    UserConnection connection = packet.user();
    boolean clientbound = packetType.direction() == Direction.CLIENTBOUND;
    int serverProtocolVersion = clientbound ? this.inputProtocolVersion : connection.getProtocolInfo().getServerProtocolVersion();
    int clientProtocolVersion = clientbound ? connection.getProtocolInfo().getProtocolVersion() : this.inputProtocolVersion;
    // Construct protocol pipeline
    List<ProtocolPathEntry> path = Via.getManager().getProtocolManager().getProtocolPath(clientProtocolVersion, serverProtocolVersion);
    List<Protocol> protocolList = null;
    if (path != null) {
        protocolList = new ArrayList<>(path.size());
        for (ProtocolPathEntry entry : path) {
            protocolList.add(entry.protocol());
        }
    } else if (serverProtocolVersion != clientProtocolVersion) {
        throw new RuntimeException("No protocol path between client version " + clientProtocolVersion + " and server version " + serverProtocolVersion);
    }
    if (protocolList != null) {
        // Reset reader and apply pipeline
        packet.resetReader();
        try {
            packet.apply(packetType.direction(), State.PLAY, 0, protocolList, clientbound);
        } catch (Exception e) {
            throw new Exception("Exception trying to transform packet between client version " + clientProtocolVersion + " and server version " + serverProtocolVersion + ". Are you sure you used the correct input version and packet write types?", e);
        }
    }
}
Also used : ProtocolPathEntry(com.viaversion.viaversion.api.protocol.ProtocolPathEntry) ServerboundPacketType(com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType) PacketType(com.viaversion.viaversion.api.protocol.packet.PacketType) ClientboundPacketType(com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType) Protocol(com.viaversion.viaversion.api.protocol.Protocol) UserConnection(com.viaversion.viaversion.api.connection.UserConnection)

Example 5 with PacketType

use of com.viaversion.viaversion.api.protocol.packet.PacketType in project ViaVersion by ViaVersion.

the class PacketWrapperImpl method set.

@Override
public <T> void set(Type<T> type, int index, T value) throws Exception {
    int currentIndex = 0;
    for (Pair<Type, Object> packetValue : packetValues) {
        if (packetValue.key() != type)
            continue;
        if (currentIndex == index) {
            packetValue.setValue(attemptTransform(type, value));
            return;
        }
        currentIndex++;
    }
    Exception e = new ArrayIndexOutOfBoundsException("Could not find type " + type.getTypeName() + " at " + index);
    throw new InformativeException(e).set("Type", type.getTypeName()).set("Index", index).set("Packet ID", getId()).set("Packet Type", packetType);
}
Also used : PacketType(com.viaversion.viaversion.api.protocol.packet.PacketType) Type(com.viaversion.viaversion.api.type.Type) InformativeException(com.viaversion.viaversion.exception.InformativeException) IOException(java.io.IOException) InformativeException(com.viaversion.viaversion.exception.InformativeException) NoSuchElementException(java.util.NoSuchElementException) CancelException(com.viaversion.viaversion.exception.CancelException)

Aggregations

PacketType (com.viaversion.viaversion.api.protocol.packet.PacketType)5 Type (com.viaversion.viaversion.api.type.Type)3 CancelException (com.viaversion.viaversion.exception.CancelException)3 InformativeException (com.viaversion.viaversion.exception.InformativeException)3 IOException (java.io.IOException)3 NoSuchElementException (java.util.NoSuchElementException)3 ClientboundPacketType (com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType)2 ServerboundPacketType (com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType)2 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)1 Protocol (com.viaversion.viaversion.api.protocol.Protocol)1 ProtocolPathEntry (com.viaversion.viaversion.api.protocol.ProtocolPathEntry)1