Skip to content

Commit 92faaee

Browse files
authored
Added method to get RemoteChatSession from player (#2406)
1 parent 339b2ef commit 92faaee

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/main/java/com/comphenix/protocol/events/AbstractStructure.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ public StructureModifier<World> getWorldKeys() {
940940
);
941941
}
942942

943+
943944
/**
944945
* Retrieve a read/write structure for SectionPositions in 1.16.2+
945946
* @return The Structure Modifier

src/main/java/com/comphenix/protocol/wrappers/WrappedProfilePublicKey.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
import java.time.Instant;
1212
import java.util.Base64;
1313
import java.util.Base64.Encoder;
14+
15+
import com.comphenix.protocol.utility.MinecraftVersion;
1416
import org.bukkit.entity.Player;
1517

18+
import javax.annotation.Nullable;
19+
1620
/**
1721
* A wrapper around the profile public key.
1822
*
@@ -57,7 +61,12 @@ public WrappedProfilePublicKey(WrappedProfileKeyData keyData) {
5761
* @param player the player to get the key of.
5862
* @return a wrapper around the public key of the given player.
5963
*/
64+
@Nullable
6065
public static WrappedProfilePublicKey ofPlayer(Player player) {
66+
if(MinecraftVersion.FEATURE_PREVIEW_2.atOrAbove()) {
67+
WrappedRemoteChatSessionData data = WrappedRemoteChatSessionData.fromPlayer(player);
68+
return data == null ? null : new WrappedProfilePublicKey(data.getProfilePublicKey());
69+
}
6170
FieldAccessor accessor = PROFILE_KEY_ACCESSOR;
6271
if (accessor == null) {
6372
accessor = Accessors.getFieldAccessor(MinecraftReflection.getEntityHumanClass(),

src/main/java/com/comphenix/protocol/wrappers/WrappedRemoteChatSessionData.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package com.comphenix.protocol.wrappers;
22

3+
import com.comphenix.protocol.reflect.ExactReflection;
4+
import com.comphenix.protocol.reflect.FuzzyReflection;
35
import com.comphenix.protocol.reflect.StructureModifier;
46
import com.comphenix.protocol.reflect.accessors.Accessors;
57
import com.comphenix.protocol.reflect.accessors.ConstructorAccessor;
8+
import com.comphenix.protocol.reflect.accessors.FieldAccessor;
9+
import com.comphenix.protocol.reflect.accessors.MethodAccessor;
610
import com.comphenix.protocol.utility.MinecraftReflection;
11+
import org.bukkit.entity.Player;
712

13+
import javax.annotation.Nullable;
814
import java.util.UUID;
915

1016
/**
@@ -16,6 +22,9 @@ public class WrappedRemoteChatSessionData extends AbstractWrapper {
1622
private final static Class<?> HANDLE_TYPE = MinecraftReflection.getRemoteChatSessionDataClass();
1723
private static ConstructorAccessor CONSTRUCTOR;
1824
private StructureModifier<Object> modifier;
25+
private static MethodAccessor PLAYER_GET_HANDLE;
26+
private static FieldAccessor REMOTE_CHAT_SESSION_FIELD;
27+
private static MethodAccessor AS_DATA_METHOD;
1928

2029
/**
2130
* Constructs a new profile public key wrapper directly from a nms RemoteChatSession.Data/RemoteChatSession.a object.
@@ -93,4 +102,29 @@ public String toString() {
93102
public int hashCode() {
94103
return handle.hashCode();
95104
}
105+
106+
/**
107+
* Retrieves the current session data of the player
108+
* @since 1.19.4
109+
* @return Wrapper for session data or null if not present
110+
*/
111+
@Nullable
112+
public static WrappedRemoteChatSessionData fromPlayer(Player player) {
113+
if(PLAYER_GET_HANDLE == null) {
114+
PLAYER_GET_HANDLE = Accessors.getMethodAccessor(MinecraftReflection.getCraftPlayerClass(), "getHandle");
115+
}
116+
if(REMOTE_CHAT_SESSION_FIELD == null) {
117+
REMOTE_CHAT_SESSION_FIELD = Accessors.getFieldAccessor(MinecraftReflection.getEntityPlayerClass(), MinecraftReflection.getRemoteChatSessionClass(), true);
118+
}
119+
if(AS_DATA_METHOD == null) {
120+
AS_DATA_METHOD = Accessors.getMethodAccessor(FuzzyReflection.fromClass(MinecraftReflection.getRemoteChatSessionClass(), true).getMethodListByParameters(HANDLE_TYPE).get(0));
121+
}
122+
Object handle = PLAYER_GET_HANDLE.invoke(player);
123+
Object remoteChatSession = REMOTE_CHAT_SESSION_FIELD.get(handle);
124+
if(remoteChatSession == null) {
125+
return null;
126+
}
127+
Object remoteChatSessionData = AS_DATA_METHOD.invoke(remoteChatSession);
128+
return remoteChatSessionData == null ? null : new WrappedRemoteChatSessionData(remoteChatSessionData);
129+
}
96130
}

0 commit comments

Comments
 (0)