de.pewpewproject.lasertag.lasertaggame.team.serialize.TeamDtoSerializer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
$JsonSerializer$.serialize(TeamDto,Type,JsonSerializationContext) 0 21 ?
A getSerializer() 0 28 3
1
package de.pewpewproject.lasertag.lasertaggame.team.serialize;
2
3
import com.google.gson.JsonElement;
4
import com.google.gson.JsonObject;
5
import com.google.gson.JsonSerializationContext;
6
import com.google.gson.JsonSerializer;
7
import de.pewpewproject.lasertag.lasertaggame.team.TeamDto;
8
import net.minecraft.util.registry.Registry;
9
10
import java.lang.reflect.Type;
11
12
/**
13
 * Util to get a serializer for the TeamDto class
14
 *
15
 * @author Étienne Muser
16
 */
17
public class TeamDtoSerializer {
18
    /**
19
     * Build a GsonBuilder for the Team
20
     * @return The GsonBuilder designed for the Team
21
     */
22
    public static JsonSerializer<TeamDto> getSerializer() {
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 id
30
                jsonObject.addProperty("id", teamDto.id());
31
32
                // Add RGB
33
                jsonObject.addProperty("red", teamDto.color().r());
34
                jsonObject.addProperty("green", teamDto.color().g());
35
                jsonObject.addProperty("blue", teamDto.color().b());
36
37
                // Add spawnpoint block
38
                var spawnPointBlockId = "null";
39
                if (teamDto.spawnpointBlock() != null) {
40
                    
41
                    spawnPointBlockId = Registry.BLOCK.getId(teamDto.spawnpointBlock()).toString();
42
                }
43
                jsonObject.addProperty("spawnpointBlock", spawnPointBlockId);
44
45
                return jsonObject;
46
            }
47
        };
48
49
        return serializer;
50
    }
51
}
52