|
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
|
|
|
|