22
33import one .tranic .t .base .parse .uuid .UUIDParser ;
44import one .tranic .t .utils .Reflect ;
5+ import org .apache .commons .lang .Validate ;
56import org .checkerframework .checker .nullness .qual .NonNull ;
67import org .checkerframework .checker .nullness .qual .Nullable ;
78import org .geysermc .cumulus .form .Form ;
1516
1617import java .util .UUID ;
1718
18- public class BedrockPlayer {
19+ @ SuppressWarnings ("all" )
20+ public class BedrockPlayer <P > {
1921 private static final boolean geyser = Reflect .hasClass ("org.geysermc.geyser.api.GeyserApi" );
2022 private static final boolean floodgate = Reflect .hasClass ("org.geysermc.floodgate.api.FloodgateApi" );
2123
22- private final Player player ;
24+ private final Player < P > player ;
2325 private final Object bedrockPlayer ;
2426
25- protected BedrockPlayer (@ NotNull Player player ) {
27+ public BedrockPlayer (@ NotNull Player < P > player ) {
2628 this .player = player ;
2729 this .bedrockPlayer = getBedrockPlayer (player .getUniqueId ());
2830 }
@@ -33,7 +35,7 @@ protected BedrockPlayer(@NotNull Player player) {
3335 * @param player the {@link Player} to convert to a {@link BedrockPlayer}; must not be null
3436 * @return a new {@link BedrockPlayer} instance associated with the given {@link Player}
3537 */
36- public static BedrockPlayer of (@ NotNull Player player ) {
38+ public static @ NotNull BedrockPlayer of (@ NotNull Player player ) {
3739 return new BedrockPlayer (player );
3840 }
3941
@@ -50,14 +52,24 @@ public static boolean isBedrockPlayer(UUID uuid) {
5052 return isFloodgatePlayer (uuid );
5153 }
5254
55+ /**
56+ * Determines whether the specified player is a Bedrock player.
57+ *
58+ * @param player the player to check; must not be null
59+ * @return true if the specified player is a Bedrock player, otherwise false
60+ */
61+ public static boolean isBedrockPlayer (@ NotNull Player player ) {
62+ return isBedrockPlayer (player .getUniqueId ());
63+ }
64+
5365 /**
5466 * Checks if a given UUID string represents a Floodgate player by verifying
5567 * if it starts with the Floodgate UUID prefix.
5668 *
5769 * @param uuid the UUID string of the player, expected to contain dashes and not null
5870 * @return {@code true} if the UUID represents a Floodgate player, {@code false} otherwise
5971 */
60- public static boolean isFloodgatePlayer (String uuid ) {
72+ public static boolean isFloodgatePlayer (@ NotNull String uuid ) {
6173 final String FLOODGATE_UUID_PREFIX = "0000000000000000" ;
6274
6375 String str = UUIDParser .removeDashes (uuid );
@@ -70,17 +82,37 @@ public static boolean isFloodgatePlayer(String uuid) {
7082 * @param uuid the universally unique identifier (UUID) of the player to check
7183 * @return true if the player is a Floodgate player, false otherwise
7284 */
73- public static boolean isFloodgatePlayer (UUID uuid ) {
85+ public static boolean isFloodgatePlayer (@ NotNull UUID uuid ) {
7486 return isFloodgatePlayer (uuid .toString ());
7587 }
7688
89+ /**
90+ * Determines if the specified Bedrock player is a Floodgate player.
91+ *
92+ * @param player the Bedrock player to check; must not be null
93+ * @return true if the specified player is a Floodgate player, false otherwise
94+ */
95+ public static boolean isFloodgatePlayer (@ NotNull BedrockPlayer player ) {
96+ return isFloodgatePlayer (player .uuid ());
97+ }
98+
99+ /**
100+ * Determines whether the specified player is a Floodgate player.
101+ *
102+ * @param player the player to check; must not be null
103+ * @return true if the player is a Floodgate player, false otherwise
104+ */
105+ public static boolean isFloodgatePlayer (@ NotNull Player player ) {
106+ return isFloodgatePlayer (player .getUniqueId ());
107+ }
108+
77109 /**
78110 * Retrieves the Bedrock player associated with the given UUID.
79111 *
80112 * @param uuid the unique identifier of the player to retrieve
81113 * @return the Bedrock player object if found, otherwise null
82114 */
83- private static @ Nullable Object getBedrockPlayer (UUID uuid ) {
115+ private static @ Nullable Object getBedrockPlayer (@ NotNull UUID uuid ) {
84116 return floodgate ? FloodgateApi .getInstance ().getPlayer (uuid ) :
85117 geyser ? GeyserApi .api ().connectionByUuid (uuid ) : null ;
86118 }
@@ -108,7 +140,7 @@ public static boolean hasFloodgate() {
108140 *
109141 * @return the {@link Player} instance associated with this object; never null
110142 */
111- public @ NotNull Player player () {
143+ public @ NotNull Player < P > player () {
112144 return player ;
113145 }
114146
@@ -131,11 +163,8 @@ public boolean isFloodgatePlayer() {
131163 */
132164 public @ NonNull String platform () {
133165 if (bedrockPlayer != null ) {
134- if (floodgate ) {
135- return ((FloodgatePlayer ) bedrockPlayer ).getDeviceOs ().toString ();
136- } else if (geyser ) {
137- return ((GeyserConnection ) bedrockPlayer ).platform ().toString ();
138- }
166+ if (floodgate ) return ((FloodgatePlayer ) bedrockPlayer ).getDeviceOs ().toString ();
167+ if (geyser ) return ((GeyserConnection ) bedrockPlayer ).platform ().toString ();
139168 }
140169 return "Java Edition" ;
141170 }
@@ -150,9 +179,34 @@ public boolean isFloodgatePlayer() {
150179 * @return the subscription ID of the Floodgate player, or -1 if unavailable
151180 */
152181 public int subscribeId () {
153- if (bedrockPlayer != null && floodgate ) {
182+ if (bedrockPlayer != null && floodgate )
154183 return ((FloodgatePlayerImpl ) bedrockPlayer ).getSubscribeId ();
184+ return -1 ;
185+ }
186+
187+ /**
188+ * Retrieves the version of the Bedrock player, if applicable.
189+ *
190+ * @return the version of the Bedrock player as a {@code String}, or {@code null}
191+ * if the player is not a Bedrock player or the version could not be determined
192+ */
193+ public @ Nullable String version () {
194+ if (bedrockPlayer != null ) {
195+ if (geyser ) return ((GeyserConnection ) bedrockPlayer ).version ();
196+ if (floodgate ) return ((FloodgatePlayer ) bedrockPlayer ).getVersion ();
155197 }
198+ return null ;
199+ }
200+
201+ /**
202+ * Retrieves the protocol version of the Bedrock player if applicable.
203+ *
204+ * @return the protocol version as an integer if the player is a Geyser Bedrock player;
205+ * otherwise, returns -1 if the player is not a Bedrock player or Geyser is not available.
206+ */
207+ public int protocolVersion () {
208+ if (bedrockPlayer != null && geyser )
209+ return ((GeyserConnection ) bedrockPlayer ).protocolVersion ();
156210 return -1 ;
157211 }
158212
@@ -166,9 +220,8 @@ public int subscribeId() {
166220 * Bedrock player, Geyser is not enabled, or if the device ID is unavailable
167221 */
168222 public @ Nullable String deviceId () {
169- if (bedrockPlayer != null && geyser ) {
223+ if (bedrockPlayer != null && geyser )
170224 return ((GeyserSession ) bedrockPlayer ).getClientData ().getDeviceId ();
171- }
172225 return null ;
173226 }
174227
@@ -181,9 +234,8 @@ public int subscribeId() {
181234 * @return the device model of the Bedrock player as a string, or {@code null} if unavailable
182235 */
183236 public @ Nullable String deviceModel () {
184- if (bedrockPlayer != null && geyser ) {
237+ if (bedrockPlayer != null && geyser )
185238 return ((GeyserSession ) bedrockPlayer ).getClientData ().getDeviceModel ();
186- }
187239 return null ;
188240 }
189241
@@ -198,12 +250,8 @@ public int subscribeId() {
198250 */
199251 public @ Nullable String inputMode () {
200252 if (bedrockPlayer != null ) {
201- if (floodgate ) {
202- return ((FloodgatePlayer ) bedrockPlayer ).getInputMode ().name ();
203- }
204- if (geyser ) {
205- return ((GeyserConnection ) bedrockPlayer ).inputMode ().name ();
206- }
253+ if (floodgate ) return ((FloodgatePlayer ) bedrockPlayer ).getInputMode ().name ();
254+ if (geyser ) return ((GeyserConnection ) bedrockPlayer ).inputMode ().name ();
207255 }
208256 return null ;
209257 }
@@ -219,9 +267,8 @@ public int subscribeId() {
219267 * @return the client ID as a long if the player is a Geyser Bedrock player; -1 otherwise
220268 */
221269 public long clientID () {
222- if (bedrockPlayer != null && geyser ) {
270+ if (bedrockPlayer != null && geyser )
223271 return ((GeyserSession ) bedrockPlayer ).getClientData ().getClientRandomId ();
224- }
225272 return -1 ;
226273 }
227274
@@ -235,14 +282,27 @@ public long clientID() {
235282 */
236283 public @ Nullable String xuid () {
237284 if (bedrockPlayer != null ) {
238- if (floodgate ) {
239- return ((FloodgatePlayer ) bedrockPlayer ).getXuid ();
240- }
285+ if (floodgate ) return ((FloodgatePlayer ) bedrockPlayer ).getXuid ();
286+ if (geyser ) return ((GeyserConnection ) bedrockPlayer ).xuid ();
287+ }
288+ return null ;
289+ }
290+
291+ /**
292+ * Retrieves the unique identifier (UUID) associated with this player.
293+ *
294+ * @return the UUID of the player; never null
295+ */
296+ public @ NotNull UUID uuid () {
297+ if (bedrockPlayer != null ) {
298+ if (floodgate ) return ((FloodgatePlayer ) bedrockPlayer ).getCorrectUniqueId ();
241299 if (geyser ) {
242- return ((GeyserConnection ) bedrockPlayer ).xuid ();
300+ var uuid = ((GeyserConnection ) bedrockPlayer ).playerUuid ();
301+ if (uuid == null ) uuid = player .getUniqueId ();
302+ return uuid ;
243303 }
244304 }
245- return null ;
305+ return player . getUniqueId () ;
246306 }
247307
248308 /**
@@ -256,29 +316,20 @@ public long clientID() {
256316 @ SuppressWarnings ("all" )
257317 public boolean form (@ NotNull Form form ) {
258318 if (bedrockPlayer != null && form != null ) {
259- if (floodgate ) {
260- return ((FloodgatePlayer ) bedrockPlayer ).sendForm (form );
261- }
262- if (geyser ) {
263- return ((GeyserConnection ) bedrockPlayer ).sendForm (form );
264- }
319+ if (floodgate ) return ((FloodgatePlayer ) bedrockPlayer ).sendForm (form );
320+ if (geyser ) return ((GeyserConnection ) bedrockPlayer ).sendForm (form );
265321 }
266322 return false ;
267323 }
268324
269325 /**
270326 * Retrieves the ping value of a Bedrock player if applicable.
271- * <p>
272- * This method checks if the associated player is a Bedrock player and if
273- * the Geyser integration is available.
274327 *
275328 * @return the ping value in milliseconds for a Bedrock player, or -1 if
276329 * the player is not a Bedrock player or Geyser is not available.
277330 */
278331 public long ping () {
279- if (bedrockPlayer != null && geyser ) {
280- return ((GeyserConnection ) bedrockPlayer ).ping ();
281- }
282- return -1 ;
332+ if (bedrockPlayer != null && geyser ) return ((GeyserConnection ) bedrockPlayer ).ping ();
333+ return player .getPing ();
283334 }
284335}
0 commit comments