fix exceptions on older server versions (#1645)

This commit is contained in:
Pasqual Koschmieder 2022-06-14 18:54:45 +02:00 committed by GitHub
parent 59ca841ed5
commit 4e105c59ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 18 deletions

View File

@ -177,9 +177,12 @@ public class PlayerInfoData {
MinecraftReflection.getIChatBaseComponentClass(), BukkitConverters.getWrappedChatComponentConverter());
WrappedChatComponent displayName = displayNames.read(0);
StructureModifier<WrappedProfileKeyData> keyData = modifier.withType(
MinecraftReflection.getProfilePublicKeyDataClass(), BukkitConverters.getWrappedPublicKeyDataConverter());
WrappedProfileKeyData key = keyData.optionRead(0).orElse(null);
WrappedProfileKeyData key = null;
if (MinecraftVersion.WILD_UPDATE.atOrAbove()) {
StructureModifier<WrappedProfileKeyData> keyData = modifier.withType(
MinecraftReflection.getProfilePublicKeyDataClass(), BukkitConverters.getWrappedPublicKeyDataConverter());
key = keyData.read(0);
}
return new PlayerInfoData(gameProfile, latency, gameMode, displayName, key);
}

View File

@ -20,11 +20,8 @@ import org.bukkit.entity.Player;
*/
public class WrappedProfilePublicKey extends AbstractWrapper {
private static final Class<?> HANDLE_TYPE = MinecraftReflection.getProfilePublicKeyClass();
private static final Class<?> KEY_DATA_TYPE = MinecraftReflection.getProfilePublicKeyDataClass();
private static final ConstructorAccessor CONSTRUCTOR = Accessors.getConstructorAccessor(HANDLE_TYPE, KEY_DATA_TYPE);
private static final FieldAccessor DATA_ACCESSOR = Accessors.getFieldAccessor(HANDLE_TYPE, KEY_DATA_TYPE, true);
private static ConstructorAccessor CONSTRUCTOR;
private static FieldAccessor DATA_ACCESSOR;
// lazy initialized when needed
private static FieldAccessor PROFILE_KEY_ACCESSOR;
@ -35,7 +32,7 @@ public class WrappedProfilePublicKey extends AbstractWrapper {
* @param handle the handle to create the wrapper from.
*/
public WrappedProfilePublicKey(Object handle) {
super(HANDLE_TYPE);
super(MinecraftReflection.getProfilePublicKeyClass());
this.setHandle(handle);
}
@ -45,7 +42,13 @@ public class WrappedProfilePublicKey extends AbstractWrapper {
* @param keyData the data of the key.
*/
public WrappedProfilePublicKey(WrappedProfileKeyData keyData) {
this(CONSTRUCTOR.invoke(keyData.getHandle()));
super(MinecraftReflection.getProfilePublicKeyClass());
if (CONSTRUCTOR == null) {
CONSTRUCTOR = Accessors.getConstructorAccessor(this.getHandleType(),
MinecraftReflection.getProfilePublicKeyDataClass());
}
this.setHandle(CONSTRUCTOR.invoke(keyData.getHandle()));
}
/**
@ -57,7 +60,8 @@ public class WrappedProfilePublicKey extends AbstractWrapper {
public static WrappedProfilePublicKey ofPlayer(Player player) {
FieldAccessor accessor = PROFILE_KEY_ACCESSOR;
if (accessor == null) {
accessor = Accessors.getFieldAccessor(MinecraftReflection.getEntityHumanClass(), HANDLE_TYPE, true);
accessor = Accessors.getFieldAccessor(MinecraftReflection.getEntityHumanClass(),
MinecraftReflection.getProfilePublicKeyClass(), true);
PROFILE_KEY_ACCESSOR = accessor;
}
@ -71,6 +75,10 @@ public class WrappedProfilePublicKey extends AbstractWrapper {
* @return the data of this key.
*/
public WrappedProfileKeyData getKeyData() {
if (DATA_ACCESSOR == null) {
DATA_ACCESSOR = Accessors.getFieldAccessor(this.getHandleType(),
MinecraftReflection.getProfilePublicKeyDataClass(), true);
}
return new WrappedProfileKeyData(DATA_ACCESSOR.get(this.getHandle()));
}
@ -81,6 +89,10 @@ public class WrappedProfilePublicKey extends AbstractWrapper {
* @param keyData the new data of this key.
*/
public void setKeyData(WrappedProfileKeyData keyData) {
if (DATA_ACCESSOR == null) {
DATA_ACCESSOR = Accessors.getFieldAccessor(this.getHandleType(),
MinecraftReflection.getProfilePublicKeyDataClass(), true);
}
DATA_ACCESSOR.set(this.getHandle(), keyData.getHandle());
}
@ -91,9 +103,7 @@ public class WrappedProfilePublicKey extends AbstractWrapper {
*/
public static class WrappedProfileKeyData extends AbstractWrapper {
private static final ConstructorAccessor CONSTRUCTOR = Accessors.getConstructorAccessor(
KEY_DATA_TYPE,
Instant.class, PublicKey.class, byte[].class);
private static ConstructorAccessor CONSTRUCTOR;
private static final Encoder MIME_ENCODER = Base64.getMimeEncoder(76, "\n".getBytes(StandardCharsets.UTF_8));
private final StructureModifier<Object> modifier;
@ -104,9 +114,9 @@ public class WrappedProfilePublicKey extends AbstractWrapper {
* @param handle the handle to create the wrapper from.
*/
public WrappedProfileKeyData(Object handle) {
super(KEY_DATA_TYPE);
super(MinecraftReflection.getProfilePublicKeyDataClass());
this.setHandle(handle);
this.modifier = new StructureModifier<>(KEY_DATA_TYPE).withTarget(handle);
this.modifier = new StructureModifier<>(MinecraftReflection.getProfilePublicKeyDataClass()).withTarget(handle);
}
/**
@ -117,7 +127,16 @@ public class WrappedProfilePublicKey extends AbstractWrapper {
* @param signature the signature of the public key.
*/
public WrappedProfileKeyData(Instant expireTime, PublicKey key, byte[] signature) {
this(CONSTRUCTOR.invoke(expireTime, key, signature));
super(MinecraftReflection.getProfilePublicKeyDataClass());
if (CONSTRUCTOR == null) {
CONSTRUCTOR = Accessors.getConstructorAccessor(
this.getHandleType(),
Instant.class, PublicKey.class, byte[].class);
}
this.setHandle(CONSTRUCTOR.invoke(expireTime, key, signature));
this.modifier = new StructureModifier<>(MinecraftReflection.getProfilePublicKeyDataClass()).withTarget(this.handle);
}
/**

View File

@ -51,7 +51,7 @@ public class WrappedServerPing extends AbstractWrapper implements ClonableWrappe
private static FieldAccessor PLAYERS = Accessors.getFieldAccessor(SERVER_PING, MinecraftReflection.getServerPingPlayerSampleClass(), true);
private static FieldAccessor VERSION = Accessors.getFieldAccessor(SERVER_PING, MinecraftReflection.getServerPingServerDataClass(), true);
private static FieldAccessor FAVICON = Accessors.getFieldAccessor(SERVER_PING, String.class, true);
private static FieldAccessor PREVIEWS_CHAT = Accessors.getFieldAccessor(SERVER_PING, boolean.class, true);
private static FieldAccessor PREVIEWS_CHAT;
// For converting to the underlying array
private static EquivalentConverter<Iterable<? extends WrappedGameProfile>> PROFILE_CONVERT =
@ -188,6 +188,11 @@ public class WrappedServerPing extends AbstractWrapper implements ClonableWrappe
* @since 1.19
*/
public boolean isChatPreviewEnabled() {
if (PREVIEWS_CHAT == null) {
// TODO: at some point we should make everything nullable to make updates easier
// see https://github.com/dmulloy2/ProtocolLib/issues/1644 for an example reference
PREVIEWS_CHAT = Accessors.getFieldAccessor(SERVER_PING, boolean.class, true);
}
return (Boolean) PREVIEWS_CHAT.get(handle);
}
@ -197,6 +202,9 @@ public class WrappedServerPing extends AbstractWrapper implements ClonableWrappe
* @since 1.19
*/
public void setChatPreviewEnabled(boolean chatPreviewEnabled) {
if (PREVIEWS_CHAT == null) {
PREVIEWS_CHAT = Accessors.getFieldAccessor(SERVER_PING, boolean.class, true);
}
PREVIEWS_CHAT.set(handle, chatPreviewEnabled);
}