us.myles.ViaVersion.packets.State Java Examples

The following examples show how to use us.myles.ViaVersion.packets.State. 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: Protocol1_7_6_10TO1_8.java    From ViaRewind with MIT License 6 votes vote down vote up
@Override
public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception {
	CompressionSendStorage compressionSendStorage = packetWrapper.user().get(CompressionSendStorage.class);
	if (compressionSendStorage.isCompressionSend()) {
		Channel channel = packetWrapper.user().getChannel();
		if (channel.pipeline().get("compress") != null) {
			channel.pipeline().replace("decompress", "decompress", new EmptyChannelHandler());
			channel.pipeline().replace("compress", "compress", new ForwardMessageToByteEncoder());
		} else if (channel.pipeline().get("compression-encoder") != null) { // Velocity
			channel.pipeline().replace("compression-decoder", "compression-decoder", new EmptyChannelHandler());
			channel.pipeline().replace("compression-encoder", "compression-encoder", new ForwardMessageToByteEncoder());
		}

		compressionSendStorage.setCompressionSend(false);
	}

	super.transform(direction, state, packetWrapper);
}
 
Example #2
Source File: Protocol1_9To1_8.java    From ViaVersion with 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 #3
Source File: Protocol.java    From ViaVersion with MIT License 6 votes vote down vote up
protected void registerOutgoingChannelIdChanges() {
    ClientboundPacketType[] newConstants = newClientboundPacketEnum.getEnumConstants();
    Map<String, ClientboundPacketType> newClientboundPackets = new HashMap<>(newConstants.length);
    for (ClientboundPacketType newConstant : newConstants) {
        newClientboundPackets.put(newConstant.name(), newConstant);
    }

    for (ClientboundPacketType packet : oldClientboundPacketEnum.getEnumConstants()) {
        ClientboundPacketType mappedPacket = newClientboundPackets.get(packet.name());
        int oldId = packet.ordinal();
        if (mappedPacket == null) {
            // Packet doesn't exist on new client
            Preconditions.checkArgument(hasRegisteredOutgoing(State.PLAY, oldId),
                    "Packet " + packet + " in " + getClass().getSimpleName() + " has no mapping - it needs to be manually cancelled or remapped!");
            continue;
        }

        int newId = mappedPacket.ordinal();
        if (!hasRegisteredOutgoing(State.PLAY, oldId)) {
            registerOutgoing(State.PLAY, oldId, newId);
        }
    }
}
 
Example #4
Source File: Protocol.java    From ViaVersion with MIT License 6 votes vote down vote up
protected void registerIncomingChannelIdChanges() {
    ServerboundPacketType[] oldConstants = oldServerboundPacketEnum.getEnumConstants();
    Map<String, ServerboundPacketType> oldServerboundConstants = new HashMap<>(oldConstants.length);
    for (ServerboundPacketType oldConstant : oldConstants) {
        oldServerboundConstants.put(oldConstant.name(), oldConstant);
    }

    for (ServerboundPacketType packet : newServerboundPacketEnum.getEnumConstants()) {
        ServerboundPacketType mappedPacket = oldServerboundConstants.get(packet.name());
        int newId = packet.ordinal();
        if (mappedPacket == null) {
            // Packet doesn't exist on old server
            Preconditions.checkArgument(hasRegisteredIncoming(State.PLAY, newId),
                    "Packet " + packet + " in " + getClass().getSimpleName() + " has no mapping - it needs to be manually cancelled or remapped!");
            continue;
        }

        int oldId = mappedPacket.ordinal();
        if (!hasRegisteredIncoming(State.PLAY, newId)) {
            registerIncoming(State.PLAY, oldId, newId);
        }
    }
}
 
Example #5
Source File: Protocol1_8TO1_9.java    From ViaRewind with MIT License 5 votes vote down vote up
@Override
protected void registerPackets() {
	EntityPackets.register(this);
	InventoryPackets.register(this);
	PlayerPackets.register(this);
	ScoreboardPackets.register(this);
	SpawnPackets.register(this);
	WorldPackets.register(this);

	//Keep Alive
	this.registerOutgoing(State.PLAY, 0x1F, 0x00);

	//Keep Alive
	this.registerIncoming(State.PLAY, 0x0B, 0x00);
}
 
Example #6
Source File: TranslatableRewriter.java    From ViaBackwards with MIT License 5 votes vote down vote up
public void registerPing() {
    protocol.registerOutgoing(State.LOGIN, 0x00, 0x00, new PacketRemapper() {
        @Override
        public void registerMap() {
            handler(wrapper -> processText(wrapper.passthrough(Type.COMPONENT)));
        }
    });
}
 
Example #7
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 #8
Source File: Protocol.java    From ViaVersion with 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 #9
Source File: Protocol.java    From ViaVersion with 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 #10
Source File: Protocol.java    From ViaVersion with 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 #11
Source File: Protocol.java    From ViaVersion with 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 #12
Source File: Protocol.java    From ViaVersion with 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 #13
Source File: ScoreboardPackets.java    From ViaRewind with 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 #14
Source File: BungeeMovementTransmitter.java    From ViaVersion with MIT License 5 votes vote down vote up
public void sendPlayer(UserConnection userConnection) {
    if (userConnection.getProtocolInfo().getState() == State.PLAY) {
        PacketWrapper wrapper = new PacketWrapper(0x03, null, userConnection);
        wrapper.write(Type.BOOLEAN, userConnection.get(MovementTracker.class).isGround());
        try {
            wrapper.sendToServer(Protocol1_9To1_8.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // PlayerPackets will increment idle
    }
}
 
Example #15
Source File: Protocol.java    From ViaVersion with 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 File: VelocityMovementTransmitter.java    From ViaVersion with MIT License 5 votes vote down vote up
public void sendPlayer(UserConnection userConnection) {
    if (userConnection.getProtocolInfo().getState() == State.PLAY) {
        PacketWrapper wrapper = new PacketWrapper(0x03, null, userConnection);
        wrapper.write(Type.BOOLEAN, userConnection.get(MovementTracker.class).isGround());
        try {
            wrapper.sendToServer(Protocol1_9To1_8.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // PlayerPackets will increment idle
    }
}
 
Example #17
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 #18
Source File: Protocol.java    From ViaVersion with MIT License 4 votes vote down vote up
public void cancelIncoming(State state, int newPacketID) {
    cancelIncoming(state, -1, newPacketID);
}
 
Example #19
Source File: ProtocolInfo.java    From ViaVersion with MIT License 4 votes vote down vote up
public void setState(State state) {
    this.state = state;
}
 
Example #20
Source File: ProtocolInfo.java    From ViaVersion with MIT License 4 votes vote down vote up
public State getState() {
    return state;
}
 
Example #21
Source File: BaseProtocol.java    From ViaVersion with MIT License 4 votes vote down vote up
@Override
protected void registerPackets() {
    /* Incoming Packets */

    // Handshake Packet
    registerIncoming(State.HANDSHAKE, 0x00, 0x00, new PacketRemapper() {
        @Override
        public void registerMap() {
            // select right protocol
            map(Type.VAR_INT); // 0 - Client Protocol Version
            map(Type.STRING); // 1 - Server Address
            map(Type.UNSIGNED_SHORT); // 2 - Server Port
            map(Type.VAR_INT); // 3 - Next State
            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int protVer = wrapper.get(Type.VAR_INT, 0);
                    int state = wrapper.get(Type.VAR_INT, 1);

                    ProtocolInfo info = wrapper.user().getProtocolInfo();
                    info.setProtocolVersion(protVer);
                    // Ensure the server has a version provider
                    if (Via.getManager().getProviders().get(VersionProvider.class) == null) {
                        wrapper.user().setActive(false);
                        return;
                    }
                    // Choose the pipe
                    int protocol = Via.getManager().getProviders().get(VersionProvider.class).getServerProtocol(wrapper.user());
                    info.setServerProtocolVersion(protocol);
                    List<Pair<Integer, Protocol>> protocols = null;

                    // Only allow newer clients or (1.9.2 on 1.9.4 server if the server supports it)
                    if (info.getProtocolVersion() >= protocol || Via.getPlatform().isOldClientsAllowed()) {
                        protocols = ProtocolRegistry.getProtocolPath(info.getProtocolVersion(), protocol);
                    }

                    ProtocolPipeline pipeline = wrapper.user().getProtocolInfo().getPipeline();
                    if (protocols != null) {
                        for (Pair<Integer, Protocol> prot : protocols) {
                            pipeline.add(prot.getValue());
                            // Ensure mapping data has already been loaded
                            ProtocolRegistry.completeMappingDataLoading(prot.getValue().getClass());
                        }
                        wrapper.set(Type.VAR_INT, 0, protocol);
                    }

                    // Add Base Protocol
                    pipeline.add(ProtocolRegistry.getBaseProtocol(protocol));

                    // Change state
                    if (state == 1) {
                        info.setState(State.STATUS);
                    }
                    if (state == 2) {
                        info.setState(State.LOGIN);
                    }
                }
            });
        }
    });
}
 
Example #22
Source File: Protocol.java    From ViaVersion with MIT License 4 votes vote down vote up
public State getState() {
    return state;
}
 
Example #23
Source File: Protocol.java    From ViaVersion with MIT License 4 votes vote down vote up
public ProtocolPacket(State state, int oldID, int newID, @Nullable PacketRemapper remapper) {
    this.state = state;
    this.oldID = oldID;
    this.newID = newID;
    this.remapper = remapper;
}
 
Example #24
Source File: Protocol.java    From ViaVersion with MIT License 4 votes vote down vote up
public State getState() {
    return state;
}
 
Example #25
Source File: Protocol.java    From ViaVersion with MIT License 4 votes vote down vote up
public Packet(State state, int packetId) {
    this.state = state;
    this.packetId = packetId;
}
 
Example #26
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();
    }
}
 
Example #27
Source File: Protocol.java    From ViaVersion with MIT License 4 votes vote down vote up
public void cancelIncoming(S2 packetType) {
    Preconditions.checkArgument(packetType.getClass() == newServerboundPacketEnum);
    cancelIncoming(State.PLAY, -1, packetType.ordinal());
}
 
Example #28
Source File: Protocol.java    From ViaVersion with MIT License 4 votes vote down vote up
public void cancelOutgoing(C1 packetType) {
    cancelOutgoing(State.PLAY, packetType.ordinal(), packetType.ordinal());
}
 
Example #29
Source File: Protocol.java    From ViaVersion with MIT License 4 votes vote down vote up
public void cancelOutgoing(State state, int oldPacketID) {
    cancelOutgoing(state, oldPacketID, -1);
}
 
Example #30
Source File: Protocol.java    From ViaVersion with MIT License 3 votes vote down vote up
/**
 * Registers an incoming protocol.
 *
 * @param packetType       serverbound packet type initially sent by the client
 * @param mappedPacketType serverbound packet type after transforming for the server
 * @param packetRemapper   remapper
 */
public void registerIncoming(S2 packetType, @Nullable S1 mappedPacketType, @Nullable PacketRemapper packetRemapper) {
    checkPacketType(packetType, packetType.getClass() == newServerboundPacketEnum);
    checkPacketType(mappedPacketType, mappedPacketType == null || mappedPacketType.getClass() == oldServerboundPacketEnum);

    registerIncoming(State.PLAY, mappedPacketType != null ? mappedPacketType.ordinal() : -1, packetType.ordinal(), packetRemapper);
}