1
|
|
|
package de.kleiner3.lasertag; |
2
|
|
|
|
3
|
|
|
import com.google.gson.Gson; |
4
|
|
|
import de.kleiner3.lasertag.networking.NetworkingConstants; |
5
|
|
|
import de.kleiner3.lasertag.networking.server.ServerEventSending; |
6
|
|
|
import de.kleiner3.lasertag.util.FileIO; |
7
|
|
|
import io.netty.buffer.Unpooled; |
8
|
|
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; |
9
|
|
|
import net.minecraft.network.PacketByteBuf; |
10
|
|
|
import net.minecraft.server.MinecraftServer; |
11
|
|
|
import net.minecraft.server.network.ServerPlayerEntity; |
12
|
|
|
|
13
|
|
|
import java.io.File; |
14
|
|
|
import java.io.IOException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* All configuration variables for lasertag. |
18
|
|
|
* |
19
|
|
|
* @author Étienne Muser |
20
|
|
|
*/ |
21
|
|
|
public class LasertagConfig { |
22
|
|
|
//region Properties |
23
|
|
|
|
24
|
|
|
// ===== Weapon settings ==================== |
25
|
|
|
/** |
26
|
|
|
* Weapon cooldown in game ticks |
27
|
|
|
*/ |
28
|
|
|
private int lasertagWeaponCooldown = 5; |
29
|
|
|
private int lasertagWeaponReach = 50; |
30
|
|
|
private boolean showLaserRays = true; |
31
|
|
|
|
32
|
|
|
// ===== General game settings ============== |
33
|
|
|
private final int maxTeamSize = 6; |
34
|
|
|
private boolean renderTeamList = true; |
35
|
|
|
private boolean renderTimer = true; |
36
|
|
|
private int lasertargetHitScore = 100; |
37
|
|
|
private int playerHitScore = 20; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The time in seconds from when you are teleported into the arena to when the game actually starts in seconds |
41
|
|
|
*/ |
42
|
|
|
private int startTime = 15; |
43
|
|
|
/** |
44
|
|
|
* The time the player is deactivated after being hit in seconds |
45
|
|
|
*/ |
46
|
|
|
private int deactivateTime = 15; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The time in seconds a lasertarget will be deactivated for after being hit |
50
|
|
|
*/ |
51
|
|
|
private int lasertargetDeactivatedTime = 10; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The play time in minutes |
55
|
|
|
*/ |
56
|
|
|
private int playTime = 10; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Flag if a game statistics file should be generated |
60
|
|
|
*/ |
61
|
|
|
private boolean generateStatsFile = true; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Flag if the game statistics file should be automatically opened after the game |
65
|
|
|
*/ |
66
|
|
|
private boolean autoOpenStatsFile = true; |
67
|
|
|
|
68
|
|
|
//endregion |
69
|
|
|
|
70
|
|
|
//region Getter and setter |
71
|
|
|
|
72
|
|
|
public int getLasertagWeaponCooldown() { |
73
|
|
|
return lasertagWeaponCooldown; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public void setLasertagWeaponCooldown(MinecraftServer s, Integer lasertagWeaponCooldown) { |
77
|
|
|
this.lasertagWeaponCooldown = lasertagWeaponCooldown; |
78
|
|
|
persist(s, "setLasertagWeaponCooldown", Integer.toString(lasertagWeaponCooldown)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public int getLasertagWeaponReach() { |
82
|
|
|
return lasertagWeaponReach; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public void setLasertagWeaponReach(MinecraftServer s, Integer lasertagWeaponReach) { |
86
|
|
|
this.lasertagWeaponReach = lasertagWeaponReach; |
87
|
|
|
persist(s, "setLasertagWeaponReach", Integer.toString(lasertagWeaponReach)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public boolean isShowLaserRays() { |
91
|
|
|
return showLaserRays; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public void setShowLaserRays(MinecraftServer s, Boolean showLaserRays) { |
95
|
|
|
this.showLaserRays = showLaserRays; |
96
|
|
|
persist(s, "setShowLaserRays", Boolean.toString(showLaserRays)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public int getMaxTeamSize() { |
100
|
|
|
return maxTeamSize; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public boolean isRenderTeamList() { |
104
|
|
|
return renderTeamList; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public void setRenderTeamList(MinecraftServer s, Boolean renderTeamList) { |
108
|
|
|
this.renderTeamList = renderTeamList; |
109
|
|
|
persist(s, "setRenderTeamList", Boolean.toString(renderTeamList)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public boolean isRenderTimer() { |
113
|
|
|
return renderTimer; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public void setRenderTimer(MinecraftServer s, Boolean renderTimer) { |
117
|
|
|
this.renderTimer = renderTimer; |
118
|
|
|
persist(s, "setRenderTimer", Boolean.toString(renderTimer)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public int getLasertargetHitScore() { |
122
|
|
|
return lasertargetHitScore; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public void setLasertargetHitScore(MinecraftServer s, Integer lasertargetHitScore) { |
126
|
|
|
this.lasertargetHitScore = lasertargetHitScore; |
127
|
|
|
persist(s, "setLasertargetHitScore", Integer.toString(lasertargetHitScore)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public int getPlayerHitScore() { |
131
|
|
|
return playerHitScore; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public void setPlayerHitScore(MinecraftServer s, Integer playerHitScore) { |
135
|
|
|
this.playerHitScore = playerHitScore; |
136
|
|
|
persist(s, "setPlayerHitScore", Integer.toString(playerHitScore)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public int getStartTime() { |
140
|
|
|
return startTime; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public void setStartTime(MinecraftServer s, Integer startTime) { |
144
|
|
|
this.startTime = startTime; |
145
|
|
|
persist(s, "setStartTime", Integer.toString(startTime)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public int getDeactivateTime() { |
149
|
|
|
return deactivateTime; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public void setDeactivateTime(MinecraftServer s, Integer deactivateTime) { |
153
|
|
|
this.deactivateTime = deactivateTime; |
154
|
|
|
persist(s, "setDeactivateTime", Integer.toString(deactivateTime)); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public int getLasertargetDeactivatedTime() { |
158
|
|
|
return lasertargetDeactivatedTime; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public void setLasertargetDeactivatedTime(MinecraftServer s, Integer lasertargetDeactivatedTime) { |
162
|
|
|
this.lasertargetDeactivatedTime = lasertargetDeactivatedTime; |
163
|
|
|
persist(s, "setLasertargetDeactivatedTime", Integer.toString(lasertargetDeactivatedTime)); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public int getPlayTime() { |
167
|
|
|
return playTime; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public void setPlayTime(MinecraftServer s, Integer playTime) { |
171
|
|
|
this.playTime = playTime; |
172
|
|
|
persist(s, "setPlayTime", Integer.toString(playTime)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public boolean getGenerateStatsFile() { |
176
|
|
|
return generateStatsFile; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public void setGenerateStatsFile(MinecraftServer s, Boolean generateStatsFile) { |
180
|
|
|
this.generateStatsFile = generateStatsFile; |
181
|
|
|
persist(s, "setGenerateStatsFile", Boolean.toString(generateStatsFile)); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public boolean getAutoOpenStatsFile() { |
185
|
|
|
return autoOpenStatsFile; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public void setAutoOpenStatsFile(MinecraftServer s, Boolean autoOpenStatsFile) { |
189
|
|
|
this.autoOpenStatsFile = autoOpenStatsFile; |
190
|
|
|
persist(s, "setAutoOpenStatsFile", Boolean.toString(autoOpenStatsFile)); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
//endregion |
194
|
|
|
|
195
|
|
|
//region Instance |
196
|
|
|
|
197
|
|
|
private LasertagConfig() { |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
private static LasertagConfig instance = null; |
201
|
|
|
|
202
|
|
|
public static LasertagConfig getInstance() { |
203
|
|
|
return instance; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public static void setInstance(LasertagConfig inst) { |
207
|
|
|
instance = inst; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
//endregion |
211
|
|
|
|
212
|
|
|
//region Synchronization |
213
|
|
|
|
214
|
|
|
public static void syncToPlayer(ServerPlayerEntity player) { |
215
|
|
|
// Serialize to json |
216
|
|
|
var json = new Gson().toJson(instance); |
217
|
|
|
|
218
|
|
|
// Create packet buffer |
219
|
|
|
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); |
220
|
|
|
|
221
|
|
|
// Write errorMessage to buffer |
222
|
|
|
buf.writeString(json); |
223
|
|
|
|
224
|
|
|
// Send to all clients |
225
|
|
|
ServerPlayNetworking.send(player, NetworkingConstants.LASERTAG_SETTINGS_SYNC, buf); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
//endregion |
229
|
|
|
|
230
|
|
|
//region Persistance |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Persist the config changes to the file system. |
234
|
|
|
* |
235
|
|
|
* @param server The server this is executed on. null if on the client |
236
|
|
|
* @param key The name of the setter method which executes the persist method |
237
|
|
|
* @param value The new value of the setting as a string |
238
|
|
|
*/ |
239
|
|
|
private static void persist(MinecraftServer server, String key, String value) { |
240
|
|
|
// Check if this is executed on client |
241
|
|
|
if (server == null) { |
242
|
|
|
// Do not persist lasertag config on client |
243
|
|
|
return; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
// Create packet |
247
|
|
|
var buf = new PacketByteBuf(Unpooled.buffer()); |
248
|
|
|
|
249
|
|
|
// Write to packet |
250
|
|
|
buf.writeString(key); |
251
|
|
|
buf.writeString(value); |
252
|
|
|
|
253
|
|
|
// Send update to clients |
254
|
|
|
ServerEventSending.sendToEveryone(server.getOverworld(), NetworkingConstants.LASERTAG_SETTINGS_CHANGED, buf); |
255
|
|
|
|
256
|
|
|
try { |
257
|
|
|
persistUnsafe(); |
258
|
|
|
} catch (IOException e) { |
259
|
|
|
LasertagMod.LOGGER.warn("Failed to persist lasertag config: " + e.getMessage()); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
private static void persistUnsafe() throws IOException { |
264
|
|
|
// Parse to json string |
265
|
|
|
var jsonString = new Gson().toJson(instance); |
266
|
|
|
|
267
|
|
|
// Write to file |
268
|
|
|
FileIO.writeAllFile(lasertagConfigFile, jsonString); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
// Get path to lasertag config file |
272
|
|
|
private static final String lasertagConfigFilePath = LasertagMod.configFolderPath + "\\lasertagConfig.json"; |
273
|
|
|
|
274
|
|
|
// Create file object |
275
|
|
|
private static final File lasertagConfigFile = new File(lasertagConfigFilePath); |
276
|
|
|
|
277
|
|
|
// Initialize lasertag game settings from file |
278
|
|
|
static { |
279
|
|
|
try { |
280
|
|
|
// Read config file |
281
|
|
|
var configFileContents = FileIO.readAllFile(lasertagConfigFile); |
282
|
|
|
|
283
|
|
|
// Parse file |
284
|
|
|
instance = new Gson().fromJson(configFileContents, LasertagConfig.class); |
285
|
|
|
} catch (IOException ioex) { |
286
|
|
|
LasertagMod.LOGGER.warn("Reading of lasertag config file failed: " + ioex.getMessage()); |
287
|
|
|
} catch (Exception ex) { |
288
|
|
|
LasertagMod.LOGGER.warn("Setting of lasertag config failed: " + ex.getMessage()); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
// If instance wasn't created |
292
|
|
|
if (instance == null) { |
293
|
|
|
// Use default config |
294
|
|
|
instance = new LasertagConfig(); |
295
|
|
|
|
296
|
|
|
// Log that default config is being used |
297
|
|
|
LasertagMod.LOGGER.info("Default lasertag config is being used."); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
try { |
301
|
|
|
// Create directory if not exists |
302
|
|
|
var dir = new File(LasertagMod.configFolderPath); |
303
|
|
|
if (!dir.exists()) { |
304
|
|
|
if (!dir.mkdir()) { |
305
|
|
|
throw new IOException("Make directory for lasertag config file failed!"); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
// If config file doesn't exist |
310
|
|
|
if (!lasertagConfigFile.exists()) { |
311
|
|
|
// Log that lasertag config file is being created |
312
|
|
|
LasertagMod.LOGGER.info("Lasertag config file is being created in '" + lasertagConfigFilePath + "'"); |
313
|
|
|
|
314
|
|
|
// Create new file |
315
|
|
|
if (!lasertagConfigFile.createNewFile()) { |
316
|
|
|
throw new IOException("Creation of file for lasertag config file failed!"); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
persistUnsafe(); |
320
|
|
|
} |
321
|
|
|
} catch (IOException e) { |
322
|
|
|
// Log that creation of new file failed |
323
|
|
|
LasertagMod.LOGGER.warn("Creation of new lasertag config file in '" + lasertagConfigFilePath + "' failed: " + e.getMessage()); |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
//endregion |
328
|
|
|
} |
329
|
|
|
|