us.myles.ViaVersion.exception.CancelException Java Examples

The following examples show how to use us.myles.ViaVersion.exception.CancelException. 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: UserConnection.java    From ViaVersion with MIT License 6 votes vote down vote up
private void transform(ByteBuf buf, Direction direction, Function<Throwable, Exception> cancelSupplier) throws Exception {
    int id = Type.VAR_INT.readPrimitive(buf);
    if (id == PacketWrapper.PASSTHROUGH_ID) return;

    PacketWrapper wrapper = new PacketWrapper(id, buf, this);
    try {
        protocolInfo.getPipeline().transform(direction, protocolInfo.getState(), wrapper);
    } catch (CancelException ex) {
        throw cancelSupplier.apply(ex);
    }

    ByteBuf transformed = buf.alloc().buffer();
    try {
        wrapper.writeToBuffer(transformed);
        buf.clear().writeBytes(transformed);
    } finally {
        transformed.release();
    }
}
 
Example #2
Source File: PacketUtil.java    From ViaRewind with MIT License 5 votes vote down vote up
public static void sendToServer(PacketWrapper packet, Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, boolean currentThread) {
	try {
		packet.sendToServer(packetProtocol, skipCurrentPipeline, currentThread);
	} catch (CancelException ignored) {
		;
	} catch (Exception ex) {
		ex.printStackTrace();
	}
}
 
Example #3
Source File: PacketUtil.java    From ViaRewind with MIT License 5 votes vote down vote up
public static boolean sendPacket(PacketWrapper packet, Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, boolean currentThread) {
	try {
		packet.send(packetProtocol, skipCurrentPipeline, currentThread);
		return true;
	} catch (CancelException ignored) {
		;
	} catch (Exception ex) {
		ex.printStackTrace();
	}
	return false;
}
 
Example #4
Source File: PacketWrapper.java    From ViaVersion with MIT License 5 votes vote down vote up
/**
 * Send this packet to the associated user.
 * Be careful not to send packets twice.
 * (Sends it after current)
 *
 * @param packetProtocol      The protocol version of the packet.
 * @param skipCurrentPipeline Skip the current pipeline
 * @param currentThread       Run in the same thread
 * @throws Exception if it fails to write
 */
public void send(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, boolean currentThread) throws Exception {
    if (!isCancelled()) {
        try {
            ByteBuf output = constructPacket(packetProtocol, skipCurrentPipeline, Direction.OUTGOING);
            user().sendRawPacket(output, currentThread);
        } catch (Exception e) {
            if (!PipelineUtil.containsCause(e, CancelException.class)) {
                throw e;
            }
        }
    }
}
 
Example #5
Source File: PacketWrapper.java    From ViaVersion with MIT License 5 votes vote down vote up
/**
 * Send this packet to the server.
 *
 * @param packetProtocol      The protocol version of the packet.
 * @param skipCurrentPipeline Skip the current pipeline
 * @param currentThread       Run in the same thread
 * @throws Exception if it fails to write
 */
public void sendToServer(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, boolean currentThread) throws Exception {
    if (!isCancelled()) {
        try {
            ByteBuf output = constructPacket(packetProtocol, skipCurrentPipeline, Direction.INCOMING);
            user().sendRawPacketToServer(output, currentThread);
        } catch (Exception e) {
            if (!PipelineUtil.containsCause(e, CancelException.class)) {
                throw e;
            }
        }
    }
}
 
Example #6
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();
    }
}