Passed
Branch dev (d24a17)
by Etienne
01:34
created

getSerializer()   A

Complexity

Conditions 2

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.75
c 0
b 0
f 0
eloc 13
cc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
de.kleiner3.lasertag.lasertaggame.teammanagement.serialize.TeamDtoSerializer.$JsonSerializer$.serialize(TeamDto,Type,JsonSerializationContext) 0 13 ?
1
package de.kleiner3.lasertag.lasertaggame.teammanagement.serialize;
2
3
import com.google.gson.*;
4
import de.kleiner3.lasertag.lasertaggame.teammanagement.TeamDto;
5
import net.minecraft.util.registry.Registry;
6
7
import java.lang.reflect.Type;
8
9
/**
10
 * Util to get a serializer for the TeamDto class
11
 *
12
 * @author Étienne Muser
13
 */
14
public class TeamDtoSerializer {
15
    /**
16
     * Build a GsonBuilder for the Team
17
     * @return The GsonBuilder designed for the Team
18
     */
19
    public static GsonBuilder getSerializer() {
20
        // Create builder
21
        var gsonBuilder = new GsonBuilder();
22
23
        // Create serializer for TeamDto
24
        var serializer = new JsonSerializer<TeamDto>() {
25
            @Override
26
            public JsonElement serialize(TeamDto teamDto, Type type, JsonSerializationContext jsonSerializationContext) {
27
                var jsonObject = new JsonObject();
28
29
                // Add RGB
30
                jsonObject.addProperty("red", teamDto.color().r());
31
                jsonObject.addProperty("green", teamDto.color().g());
32
                jsonObject.addProperty("blue", teamDto.color().b());
33
34
                // Add spawnpoint block
35
                jsonObject.addProperty("spawnpointBlock", Registry.BLOCK.getId(teamDto.spawnpointBlock()).toString());
36
37
                return jsonObject;
38
            }
39
        };
40
41
        // Register serializer for TeamDto
42
        gsonBuilder.registerTypeAdapter(TeamDto.class, serializer);
43
44
        return gsonBuilder;
45
    }
46
}
47