1
|
|
|
package de.kleiner3.lasertag.lasertaggame.management.team; |
2
|
|
|
|
3
|
|
|
import com.google.gson.Gson; |
4
|
|
|
import com.google.gson.GsonBuilder; |
5
|
|
|
import com.google.gson.reflect.TypeToken; |
6
|
|
|
import de.kleiner3.lasertag.LasertagMod; |
7
|
|
|
import de.kleiner3.lasertag.lasertaggame.management.IManager; |
8
|
|
|
import de.kleiner3.lasertag.lasertaggame.management.team.serialize.TeamConfigManagerDeserializer; |
9
|
|
|
import de.kleiner3.lasertag.common.util.FileIO; |
10
|
|
|
import de.kleiner3.lasertag.lasertaggame.management.team.serialize.TeamDtoSerializer; |
11
|
|
|
import net.minecraft.block.Blocks; |
12
|
|
|
import net.minecraft.server.MinecraftServer; |
13
|
|
|
import net.minecraft.server.network.ServerPlayerEntity; |
14
|
|
|
|
15
|
|
|
import java.io.File; |
16
|
|
|
import java.io.IOException; |
17
|
|
|
import java.util.HashMap; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class to manage the lasertag teams |
21
|
|
|
* |
22
|
|
|
* @author Étienne Muser |
23
|
|
|
*/ |
24
|
|
|
public class TeamConfigManager implements IManager { |
25
|
|
|
/** |
26
|
|
|
* The path to the team config file |
27
|
|
|
*/ |
28
|
|
|
private static final String teamConfigFilePath = LasertagMod.configFolderPath + "\\teamConfig.json"; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The actual in-memory team config |
32
|
|
|
*/ |
33
|
|
|
public HashMap<String, TeamDto> teamConfig = null; |
34
|
|
|
|
35
|
|
|
public TeamConfigManager() { |
36
|
|
|
// Get config file |
37
|
|
|
var teamConfigFile = new File(teamConfigFilePath); |
38
|
|
|
|
39
|
|
|
// If the config file exists |
40
|
|
|
if (teamConfigFile.exists()) { |
41
|
|
|
try { |
42
|
|
|
// Read config file |
43
|
|
|
var configFileContents = FileIO.readAllFile(teamConfigFile); |
44
|
|
|
|
45
|
|
|
// get gson builder |
46
|
|
|
var gsonBuilder = new GsonBuilder(); |
47
|
|
|
|
48
|
|
|
// Get deserializer |
49
|
|
|
var deserializer = TeamConfigManagerDeserializer.getDeserializer(); |
50
|
|
|
|
51
|
|
|
// Register type |
52
|
|
|
gsonBuilder.registerTypeAdapter(HashMap.class, deserializer); |
53
|
|
|
|
54
|
|
|
// Parse |
55
|
|
|
teamConfig = gsonBuilder.create().fromJson(configFileContents, new TypeToken<HashMap<String, TeamDto>>() { |
56
|
|
|
}.getType()); |
57
|
|
|
} catch (IOException ex) { |
58
|
|
|
LasertagMod.LOGGER.warn("Reading of team config file failed: " + ex.getMessage()); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// If config couldn't be loaded from file |
63
|
|
|
if (teamConfig == null) { |
64
|
|
|
// Create map |
65
|
|
|
teamConfig = new HashMap<>(); |
66
|
|
|
|
67
|
|
|
// Fill map with default values |
68
|
|
|
teamConfig.put("Red", new TeamDto("Red", 255, 0, 0, Blocks.RED_CONCRETE)); |
69
|
|
|
teamConfig.put("Green", new TeamDto("Green", 0, 255, 0, Blocks.LIME_CONCRETE)); |
70
|
|
|
teamConfig.put("Blue", new TeamDto("Blue", 0, 0, 255, Blocks.BLUE_CONCRETE)); |
71
|
|
|
teamConfig.put("Orange", new TeamDto("Orange", 255, 128, 0, Blocks.ORANGE_CONCRETE)); |
72
|
|
|
teamConfig.put("Teal", new TeamDto("Teal", 0, 128, 255, Blocks.LIGHT_BLUE_CONCRETE)); |
73
|
|
|
teamConfig.put("Pink", new TeamDto("Pink", 255, 0, 255, Blocks.PINK_CONCRETE)); |
74
|
|
|
|
75
|
|
|
// Get gson builder |
76
|
|
|
var gsonBuilder = new GsonBuilder(); |
77
|
|
|
|
78
|
|
|
// Register type adapter |
79
|
|
|
gsonBuilder.registerTypeAdapter(TeamDto.class, TeamDtoSerializer.getSerializer()); |
80
|
|
|
|
81
|
|
|
// Serialize |
82
|
|
|
var configJson = gsonBuilder.setPrettyPrinting().create().toJson(teamConfig); |
83
|
|
|
|
84
|
|
|
// Persist |
85
|
|
|
try { |
86
|
|
|
var dir = new File(LasertagMod.configFolderPath); |
87
|
|
|
|
88
|
|
|
// Create directory if not exists |
89
|
|
|
if (!dir.exists() && !dir.mkdir()) { |
90
|
|
|
throw new IOException("Make directory for team config failed!"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Create file if not exists |
94
|
|
|
if (!teamConfigFile.exists() && !teamConfigFile.createNewFile()) { |
95
|
|
|
throw new IOException("Creation of file for team config failed!"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Write to file |
99
|
|
|
FileIO.writeAllFile(teamConfigFile, configJson); |
100
|
|
|
} catch (IOException e) { |
101
|
|
|
LasertagMod.LOGGER.error("Writing to team config file failed: " + e.getMessage()); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
@Override |
107
|
|
|
public void dispose() { |
108
|
|
|
// Nothing to dispose |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public String toJson() { |
112
|
|
|
return new Gson().toJson(this); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public static TeamConfigManager fromJson(String jsonString) { |
116
|
|
|
return new Gson().fromJson(jsonString, TeamConfigManager.class); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
@Override |
120
|
|
|
public void syncToClient(ServerPlayerEntity client, MinecraftServer server) { |
121
|
|
|
// Do not sync! |
122
|
|
|
throw new UnsupportedOperationException("TeamConfigManager should not be synced on its own."); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|