Add method in Connection API for transferring connections (#2891)

This commit is contained in:
Konicai 2022-03-18 18:59:32 -04:00 committed by GitHub
parent 9c7210ef92
commit f8e983887e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View file

@ -26,6 +26,7 @@
package org.geysermc.api.session;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.common.value.qual.IntRange;
import java.util.UUID;
@ -55,5 +56,13 @@ public interface Connection {
*/
String xuid();
/**
* Transfer the connection to a server. A Bedrock player can successfully transfer to the same server they are
* currently playing on.
*
* @param address The address of the server
* @param port The port of the server
* @return true if the transfer was a success
*/
boolean transfer(@NonNull String address, @IntRange(from = 0, to = 65535) int port);
}

View file

@ -78,6 +78,7 @@ import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.checkerframework.common.value.qual.IntRange;
import org.geysermc.common.PlatformType;
import org.geysermc.cumulus.Form;
import org.geysermc.cumulus.util.FormBuilder;
@ -1310,6 +1311,21 @@ public class GeyserSession implements GeyserConnection, CommandSender {
return authData.xuid();
}
@SuppressWarnings("ConstantConditions") // Need to enforce the parameter annotations
@Override
public boolean transfer(@NonNull String address, @IntRange(from = 0, to = 65535) int port) {
if (address == null || address.isBlank()) {
throw new IllegalArgumentException("Server address cannot be null or blank");
} else if (port < 0 || port > 65535) {
throw new IllegalArgumentException("Server port must be between 0 and 65535, was " + port);
}
TransferPacket transferPacket = new TransferPacket();
transferPacket.setAddress(address);
transferPacket.setPort(port);
sendUpstreamPacket(transferPacket);
return true;
}
@Override
public void sendMessage(String message) {
TextPacket textPacket = new TextPacket();