net.labymod.serverapi.Addon.getAddons(JsonObject)   D
last analyzed

Complexity

Conditions 12

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 28
c 2
b 0
f 0
rs 4.8
cc 12

How to fix   Complexity   

Complexity

Complex classes like net.labymod.serverapi.Addon.getAddons(JsonObject) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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
Bug introduced by
This line will not be executed conditionally; only the first line of this 3-line block will be. The rest will execute unconditionally.

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.

Loading history...
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