Search in sources :

Example 1 with InformativeException

use of com.viaversion.viaversion.exception.InformativeException in project ViaVersion by ViaVersion.

the class AbstractProtocol method transform.

@Override
public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception {
    Packet statePacket = new Packet(state, packetWrapper.getId());
    Map<Packet, ProtocolPacket> packetMap = direction == Direction.CLIENTBOUND ? clientbound : serverbound;
    ProtocolPacket protocolPacket = packetMap.get(statePacket);
    if (protocolPacket == null) {
        return;
    }
    // Write packet id
    int unmappedId = packetWrapper.getId();
    if (protocolPacket.isMappedOverTypes()) {
        packetWrapper.setPacketType(protocolPacket.getMappedPacketType());
    } else {
        int mappedId = direction == Direction.CLIENTBOUND ? protocolPacket.getNewId() : protocolPacket.getOldId();
        if (unmappedId != mappedId) {
            packetWrapper.setId(mappedId);
        }
    }
    PacketRemapper remapper = protocolPacket.getRemapper();
    if (remapper != null) {
        try {
            remapper.remap(packetWrapper);
        } catch (InformativeException e) {
            // Catch InformativeExceptions, pass through CancelExceptions
            throwRemapError(direction, state, unmappedId, packetWrapper.getId(), e);
            return;
        }
        if (packetWrapper.isCancelled()) {
            throw CancelException.generate();
        }
    }
}
Also used : PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) InformativeException(com.viaversion.viaversion.exception.InformativeException)

Example 2 with InformativeException

use of com.viaversion.viaversion.exception.InformativeException 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 InformativeException

use of com.viaversion.viaversion.exception.InformativeException 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 InformativeException

use of com.viaversion.viaversion.exception.InformativeException 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

InformativeException (com.viaversion.viaversion.exception.InformativeException)4 PacketType (com.viaversion.viaversion.api.protocol.packet.PacketType)3 Type (com.viaversion.viaversion.api.type.Type)3 CancelException (com.viaversion.viaversion.exception.CancelException)3 IOException (java.io.IOException)3 NoSuchElementException (java.util.NoSuchElementException)3 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)1