Register Floodgate payload, updated Statistics, smaller jar, fixed bugs

Quite a lot of changes, but I was too lazy to split them in different commits (and they'll be squashed later anyway):
* Floodgate plugin message channels are now registered (because Spigot requires that, and I guess it's better practice)
* Updated the Statistics form to match the new Forms API
* The common jar is now much smaller, because Jackson isn't needed anymore in the common module
* Fixed some bugs in Forms where empty fields would lead to excluding them in the serialization (making Bedrock complain)
And a few other things, like a new boolean in RawSkin saying if the Skin is an Alex or Steve model.
This commit is contained in:
Tim203 2020-11-18 19:38:49 +01:00
parent 819ff09ee6
commit 7e3a736f20
No known key found for this signature in database
GPG key ID: 064EE9F5BF7C3EE8
23 changed files with 324 additions and 261 deletions

View file

@ -69,9 +69,9 @@ public class ModalForm extends Form {
}
public static final class Builder extends Form.Builder<Builder, ModalForm> {
private String content;
private String button1;
private String button2;
private String content = "";
private String button1 = "";
private String button2 = "";
public Builder content(String content) {
this.content = translate(content);

View file

@ -80,7 +80,7 @@ public final class SimpleForm extends Form {
public static final class Builder extends Form.Builder<Builder, SimpleForm> {
private final List<ButtonComponent> buttons = new ArrayList<>();
private String content;
private String content = "";
public Builder content(String content) {
this.content = translate(content);

View file

@ -30,14 +30,14 @@ import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* This class contains the raw data send by Geyser to Floodgate or from Floodgate to Floodgate.
* This class is only used internally, and you should look at FloodgatePlayer instead
* (FloodgatePlayer is present in the common module in the Floodgate repo)
* This class contains the raw data send by Geyser to Floodgate or from Floodgate to Floodgate. This
* class is only used internally, and you should look at FloodgatePlayer instead (FloodgatePlayer is
* present in the API module of the Floodgate repo)
*/
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public final class BedrockData {
public static final int EXPECTED_LENGTH = 9;
public static final int EXPECTED_LENGTH = 10;
private final String version;
private final String username;
@ -48,22 +48,21 @@ public final class BedrockData {
private final int inputMode;
private final String ip;
private final LinkedPlayer linkedPlayer;
private final boolean fromProxy;
private final int dataLength;
public BedrockData(String version, String username, String xuid, int deviceOs,
String languageCode, int uiProfile, int inputMode, String ip,
LinkedPlayer linkedPlayer) {
this(version, username, xuid, deviceOs, languageCode,
inputMode, uiProfile, ip, linkedPlayer, EXPECTED_LENGTH);
public static BedrockData of(String version, String username, String xuid, int deviceOs,
String languageCode, int uiProfile, int inputMode, String ip,
LinkedPlayer linkedPlayer, boolean fromProxy) {
return new BedrockData(version, username, xuid, deviceOs, languageCode, inputMode,
uiProfile, ip, linkedPlayer, fromProxy, EXPECTED_LENGTH);
}
public BedrockData(String version, String username, String xuid, int deviceOs,
String languageCode, int uiProfile, int inputMode, String ip) {
this(version, username, xuid, deviceOs, languageCode, uiProfile, inputMode, ip, null);
}
public boolean hasPlayerLink() {
return linkedPlayer != null;
public static BedrockData of(String version, String username, String xuid, int deviceOs,
String languageCode, int uiProfile, int inputMode, String ip) {
return of(version, username, xuid, deviceOs, languageCode,
uiProfile, inputMode, ip, null, false);
}
public static BedrockData fromString(String data) {
@ -77,19 +76,23 @@ public final class BedrockData {
return new BedrockData(
split[0], split[1], split[2], Integer.parseInt(split[3]), split[4],
Integer.parseInt(split[5]), Integer.parseInt(split[6]), split[7],
linkedPlayer, split.length
linkedPlayer, Boolean.parseBoolean(split[9]), split.length
);
}
private static BedrockData emptyData(int dataLength) {
return new BedrockData(null, null, null, -1, null, -1, -1, null, null, false, dataLength);
}
public boolean hasPlayerLink() {
return linkedPlayer != null;
}
@Override
public String toString() {
// The format is the same as the order of the fields in this class
return version + '\0' + username + '\0' + xuid + '\0' + deviceOs + '\0' +
languageCode + '\0' + uiProfile + '\0' + inputMode + '\0' + ip + '\0' +
(linkedPlayer != null ? linkedPlayer.toString() : "null");
}
private static BedrockData emptyData(int dataLength) {
return new BedrockData(null, null, null, -1, null, -1, -1, null, null, dataLength);
fromProxy + '\0' + (linkedPlayer != null ? linkedPlayer.toString() : "null");
}
}

View file

@ -25,7 +25,6 @@
package org.geysermc.floodgate.util;
import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
@ -34,7 +33,6 @@ import lombok.RequiredArgsConstructor;
*/
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum DeviceOs {
@JsonEnumDefaultValue
UNKNOWN("Unknown"),
ANDROID("Android"),
IOS("iOS"),
@ -57,6 +55,7 @@ public enum DeviceOs {
/**
* Get the DeviceOs instance from the identifier.
*
* @param id the DeviceOs identifier
* @return The DeviceOs or {@link #UNKNOWN} if the DeviceOs wasn't found
*/

View file

@ -26,10 +26,7 @@
package org.geysermc.floodgate.util;
import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
public enum InputMode {
@JsonEnumDefaultValue
UNKNOWN,
KEYBOARD_MOUSE,
TOUCH, // I guess Touch?
@ -40,6 +37,7 @@ public enum InputMode {
/**
* Get the InputMode instance from the identifier.
*
* @param id the InputMode identifier
* @return The InputMode or {@link #UNKNOWN} if the DeviceOs wasn't found
*/

View file

@ -26,11 +26,14 @@
package org.geysermc.floodgate.util;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import java.util.UUID;
@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public final class LinkedPlayer {
/**
* The Java username of the linked player
@ -45,19 +48,17 @@ public final class LinkedPlayer {
*/
private final UUID bedrockId;
/**
* If the LinkedPlayer is send from a different platform.
* For example the LinkedPlayer is from Bungee but the data has been sent to the Bukkit server.
* If the LinkedPlayer is send from a different platform. For example the LinkedPlayer is from
* Bungee but the data has been sent to the Bukkit server.
*/
private boolean fromDifferentPlatform = false;
public LinkedPlayer(String javaUsername, UUID javaUniqueId, UUID bedrockId) {
this.javaUsername = javaUsername;
this.javaUniqueId = javaUniqueId;
this.bedrockId = bedrockId;
public static LinkedPlayer of(String javaUsername, UUID javaUniqueId, UUID bedrockId) {
return new LinkedPlayer(javaUsername, javaUniqueId, bedrockId);
}
static LinkedPlayer fromString(String data) {
if (data.length() != 3) {
if (data.length() == 4) {
return null;
}

View file

@ -31,31 +31,36 @@ import lombok.ToString;
import java.nio.ByteBuffer;
import java.util.Base64;
import static java.lang.String.format;
@AllArgsConstructor
@ToString
public final class RawSkin {
public int width;
public int height;
public byte[] data;
public boolean alex;
private RawSkin() {}
private RawSkin() {
}
public static RawSkin decode(byte[] data) throws InvalidFormatException {
if (data == null) {
return null;
}
int maxEncodedLength = 4 * (((64 * 64 * 4 + 8) + 2) / 3);
int maxEncodedLength = 4 * (((64 * 64 * 4 + 9) + 2) / 3);
// if the RawSkin is longer then the max Java Edition skin length
if (data.length > maxEncodedLength) {
throw new InvalidFormatException(
"Encoded data cannot be longer then " + maxEncodedLength + " bytes!"
);
throw new InvalidFormatException(format(
"Encoded data cannot be longer then %s bytes! Got %s",
maxEncodedLength, data.length
));
}
// if the encoded data doesn't even contain the width and height (8 bytes, 2 ints)
if (data.length < 4 * ((8 + 2) / 3)) {
throw new InvalidFormatException("Encoded data must be at least 12 bytes long!");
// if the encoded data doesn't even contain the width, height (8 bytes, 2 ints) and isAlex
if (data.length < 4 * ((9 + 2) / 3)) {
throw new InvalidFormatException("Encoded data must be at least 16 bytes long!");
}
data = Base64.getDecoder().decode(data);
@ -65,23 +70,25 @@ public final class RawSkin {
RawSkin skin = new RawSkin();
skin.width = buffer.getInt();
skin.height = buffer.getInt();
if (buffer.remaining() != (skin.width * skin.height * 4)) {
throw new InvalidFormatException(String.format(
if (buffer.remaining() - 1 != (skin.width * skin.height * 4)) {
throw new InvalidFormatException(format(
"Expected skin length to be %s, got %s",
(skin.width * skin.height * 4), buffer.remaining()
));
}
skin.data = new byte[buffer.remaining()];
skin.data = new byte[buffer.remaining() - 1];
buffer.get(skin.data);
skin.alex = buffer.get() == 1;
return skin;
}
public byte[] encode() {
// 2 x int = 8 bytes
ByteBuffer buffer = ByteBuffer.allocate(8 + data.length);
// 2 x int + 1 = 9 bytes
ByteBuffer buffer = ByteBuffer.allocate(9 + data.length);
buffer.putInt(width);
buffer.putInt(height);
buffer.put(data);
buffer.put((byte) (alex ? 1 : 0));
return Base64.getEncoder().encode(buffer.array());
}
}

View file

@ -26,17 +26,14 @@
package org.geysermc.floodgate.util;
import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
public enum UiProfile {
@JsonEnumDefaultValue
CLASSIC,
POCKET;
CLASSIC, POCKET;
private static final UiProfile[] VALUES = values();
/**
* Get the UiProfile instance from the identifier.
*
* @param id the UiProfile identifier
* @return The UiProfile or {@link #CLASSIC} if the UiProfile wasn't found
*/