1 | package net.labymod.serverapi; |
||
2 | |||
3 | import com.google.gson.JsonElement; |
||
4 | import com.google.gson.JsonObject; |
||
5 | import lombok.AllArgsConstructor; |
||
6 | import lombok.Getter; |
||
7 | |||
8 | import java.util.ArrayList; |
||
9 | import java.util.List; |
||
10 | import java.util.UUID; |
||
11 | |||
12 | /** |
||
13 | * An Addon represents a player's addon |
||
14 | * The addons are being sent when a user joins the server |
||
15 | * You can retrieve them by using LabyModPlayerJoinEvent#getAddons() |
||
16 | * |
||
17 | * @author Jan |
||
18 | */ |
||
19 | @AllArgsConstructor |
||
20 | @Getter |
||
21 | public class Addon { |
||
22 | |||
23 | private UUID uuid; |
||
24 | private String name; |
||
25 | |||
26 | /** |
||
27 | * Parses the addons from the INFO plugin message |
||
28 | * |
||
29 | * @param jsonObject the json object of the message |
||
30 | * @return a list containing the message's addons |
||
31 | */ |
||
32 | public static List<Addon> getAddons( JsonObject jsonObject ) { |
||
33 | if ( !jsonObject.has( "addons" ) || !jsonObject.get( "addons" ).isJsonArray() ) |
||
34 | return new ArrayList<>(); |
||
35 | |||
36 | List<Addon> addons = new ArrayList<>(); |
||
37 | |||
38 | for ( JsonElement arrayElement : jsonObject.get( "addons" ).getAsJsonArray() ) { |
||
39 | if ( !arrayElement.isJsonObject() ) |
||
40 | continue; |
||
41 | |||
42 | JsonObject arrayObject = arrayElement.getAsJsonObject(); |
||
43 | |||
44 | if ( !arrayObject.has( "uuid" ) || !arrayObject.get( "uuid" ).isJsonPrimitive() || !arrayObject.get( "uuid" ).getAsJsonPrimitive().isString() |
||
45 | || !arrayObject.has( "name" ) || !arrayObject.get( "name" ).isJsonPrimitive() || !arrayObject.get( "name" ).getAsJsonPrimitive().isString() ) |
||
46 | continue; |
||
47 | |||
48 | UUID uuid = null; |
||
0 ignored issues
–
show
|
|||
49 | |||
50 | try { |
||
51 | uuid = UUID.fromString( arrayObject.get( "uuid" ).getAsString() ); |
||
52 | } catch ( IllegalArgumentException ex ) { |
||
53 | continue; |
||
54 | } |
||
55 | |||
56 | addons.add( new Addon( uuid, arrayObject.get( "name" ).getAsString() ) ); |
||
57 | } |
||
58 | |||
59 | return addons; |
||
60 | } |
||
61 | |||
62 | } |
||
63 |
Even if your block only consists of one line right now, it is good practice to enclose it in curly braces. It makes your code much more readable.