Passed
Pull Request — master (#3)
by
unknown
02:41
created

net.labymod.serverapi.Addon.getAddons(JsonObject)   C

Complexity

Conditions 11

Size

Total Lines 21
Code Lines 14

Duplication

Lines 21
Ratio 100 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 14
c 4
b 0
f 0
dl 21
loc 21
rs 5.4
cc 11

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.Collections;
10
import java.util.List;
11
import java.util.UUID;
12
13
/**
14
 * An Addon represents a player's addon
15
 * The addons are being sent when a user joins the server
16
 * You can retrieve them by using LabyModPlayerJoinEvent#getAddons()
17
 *
18
 * @author Jan
19
 */
20
@AllArgsConstructor
21
@Getter
22
public class Addon {
23
24
    private UUID uuid;
25
    private String name;
26
27
    /**
28
     * Parses the addons from the INFO plugin message
29
     *
30
     * @param jsonObject the json object of the message
31
     * @return a list containing the message's addons
32
     */
33 View Code Duplication
    @Deprecated
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
34
    public static List<Addon> getAddons( JsonObject jsonObject ) {
35
        if ( !jsonObject.has( "addons" ) || !jsonObject.get( "addons" ).isJsonArray() )
36
            return Collections.emptyList();
37
38
        List<Addon> addons = new ArrayList<>();
39
40
        for ( JsonElement arrayElement : jsonObject.get( "addons" ).getAsJsonArray() ) {
41
            if ( !arrayElement.isJsonObject() )
42
                continue;
43
44
            JsonObject arrayObject = arrayElement.getAsJsonObject();
45
46
            if ( !arrayObject.has( "uuid" ) || !arrayObject.get( "uuid" ).isJsonPrimitive() || !arrayObject.get( "uuid" ).getAsJsonPrimitive().isString()
47
                    || !arrayObject.has( "name" ) || !arrayObject.get( "name" ).isJsonPrimitive() || !arrayObject.get( "name" ).getAsJsonPrimitive().isString() )
48
            continue;
49
50
            addons.add( new Addon( UUID.fromString( arrayObject.get( "uuid" ).getAsString() ), arrayObject.get( "name" ).getAsString() ) );
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...
51
        }
52
53
        return addons;
54
    }
55
56
}
57