summaryrefslogtreewikicommitdiff
path: root/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java
blob: 4aeac6841f496bbcd7e53e435acdc8864adc7613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * This file is part of Kenshins Hide and Seek
 *
 * Copyright (c) 2021 Tyler Murphy.
 *
 * Kenshins Hide and Seek free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * he Free Software Foundation version 3.
 *
 * Kenshins Hide and Seek is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

package net.tylermurphy.hideAndSeek.configuration;

import com.cryptomorin.xseries.XItemStack;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound;
import net.tylermurphy.hideAndSeek.Main;
import net.tylermurphy.hideAndSeek.game.util.CountdownDisplay;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

public class Config {

	private static ConfigManager config;

	public static String 
		messagePrefix,
		errorPrefix,
		tauntPrefix,
		worldBorderPrefix,
		abortPrefix,
		gameOverPrefix,
		warningPrefix,
		spawnWorld,
		exitWorld,
		lobbyWorld,
		locale,
		leaveServer,
		placeholderError,
		placeholderNoData,
		databaseType,
		databaseHost,
		databasePort,
		databaseUser,
		databasePass,
		databaseName;
	
	public static Vector
		spawnPosition,
		lobbyPosition,
		exitPosition,
		worldBorderPosition;
	
	public static boolean
		nameTagsVisible,
		permissionsRequired,
		announceMessagesToNonPlayers,
		worldBorderEnabled,
		tauntEnabled,
		tauntCountdown,
		tauntLast,
		alwaysGlow,
		glowEnabled,
		glowStackable,
		pvpEnabled,
		autoJoin,
		teleportToExit,
		lobbyCountdownEnabled,
		seekerPing,
		bungeeLeave,
		lobbyItemStartAdmin,
		leaveOnEnd,
		mapSaveEnabled,
		allowNaturalCauses;
	
	public static int 
		minPlayers,
		worldBorderSize,
		worldBorderDelay,
		currentWorldborderSize,
		worldBorderChange,
		gameLength,
		saveMinX,
		saveMinZ,
		saveMaxX,
		saveMaxZ,
		tauntDelay,
		glowLength,
		countdown,
		changeCountdown,
		lobbyMin,
		lobbyMax,
		seekerPingLevel1,
		seekerPingLevel2,
		seekerPingLevel3,
		lobbyItemLeavePosition,
		lobbyItemStartPosition,
		flightToggleItemPosition,
		teleportItemPosition;

	public static float
		seekerPingLeadingVolume,
		seekerPingVolume,
		seekerPingPitch;

	public static List<String>
		blockedCommands,
		blockedInteracts;

	public static String
		LOBBY_TITLE,
		GAME_TITLE,
		COUNTDOWN_WAITING,
		COUNTDOWN_COUNTING,
		COUNTDOWN_ADMINSTART,
		TAUNT_COUNTING,
		TAUNT_ACTIVE,
		TAUNT_EXPIRED,
		GLOW_ACTIVE,
		GLOW_INACTIVE,
		BORDER_COUNTING,
		BORDER_DECREASING;

	public static List<String>
		LOBBY_CONTENTS,
		GAME_CONTENTS;

	public static ItemStack
		lobbyLeaveItem,
		lobbyStartItem,
		glowPowerupItem,
		flightToggleItem,
		teleportItem;

	public static XSound
		ringingSound,
		heartbeatSound;

	public static CountdownDisplay
		countdownDisplay;
	
	public static void loadConfig() {

		config = ConfigManager.create("config.yml");
		config.saveConfig();
		ConfigManager leaderboard = ConfigManager.create("leaderboard.yml");

		//Spawn
		spawnPosition = new Vector(
				config.getDouble("spawns.game.x"),
				Math.max(Main.getInstance().supports(18) ? -64 : 0, Math.min(255, config.getDouble("spawns.game.y"))),
				config.getDouble("spawns.game.z")
		);
		spawnWorld = config.getString("spawns.game.world");

		///Lobby
		lobbyPosition = new Vector(
				config.getDouble("spawns.lobby.x"),
				Math.max(Main.getInstance().supports(18) ? -64 : 0, Math.min(255, config.getDouble("spawns.lobby.y"))),
				config.getDouble("spawns.lobby.z")
		);
		lobbyWorld = config.getString("spawns.lobby.world");

		announceMessagesToNonPlayers = config.getBoolean("announceMessagesToNonPlayers");

		exitPosition = new Vector(
				config.getDouble("spawns.exit.x"),
				Math.max(Main.getInstance().supports(18) ? -64 : 0, Math.min(255, config.getDouble("spawns.exit.y"))),
				config.getDouble("spawns.exit.z")
		);
		exitWorld = config.getString("spawns.exit.world");

		//World border
		worldBorderPosition = new Vector(
				config.getInt("worldBorder.x"),
				0,
				config.getInt("worldBorder.z")
		);
		worldBorderSize = Math.max(100, config.getInt("worldBorder.size"));
		worldBorderDelay = Math.max(1, config.getInt("worldBorder.delay"));
		worldBorderEnabled = config.getBoolean("worldBorder.enabled");
		worldBorderChange = config.getInt("worldBorder.moveAmount");

		//Prefix
		char SYMBOLE = '\u00A7';
		String SYMBOLE_STRING = String.valueOf(SYMBOLE);

		messagePrefix = config.getString("prefix.default").replace("&", SYMBOLE_STRING);
		errorPrefix = config.getString("prefix.error").replace("&", SYMBOLE_STRING);
		tauntPrefix = config.getString("prefix.taunt").replace("&", SYMBOLE_STRING);
		worldBorderPrefix = config.getString("prefix.border").replace("&", SYMBOLE_STRING);
		abortPrefix = config.getString("prefix.abort").replace("&", SYMBOLE_STRING);
		gameOverPrefix = config.getString("prefix.gameover").replace("&", SYMBOLE_STRING);
		warningPrefix = config.getString("prefix.warning").replace("&", SYMBOLE_STRING);

		//Map Bounds
		saveMinX = config.getInt("bounds.min.x");
		saveMinZ = config.getInt("bounds.min.z");
		saveMaxX = config.getInt("bounds.max.x");
		saveMaxZ = config.getInt("bounds.max.z");
		mapSaveEnabled = config.getBoolean("mapSaveEnabled");

		//Taunt
		tauntEnabled = config.getBoolean("taunt.enabled");
		tauntCountdown = config.getBoolean("taunt.showCountdown");
		tauntDelay = Math.max(60, config.getInt("taunt.delay"));
		tauntLast = config.getBoolean("taunt.whenLastPerson");

		//Glow
		alwaysGlow = config.getBoolean("alwaysGlow") && Main.getInstance().supports(9);
		glowLength = Math.max(1, config.getInt("glow.time"));
		glowStackable = config.getBoolean("glow.stackable");
		glowEnabled = config.getBoolean("glow.enabled") && Main.getInstance().supports(9) && !alwaysGlow;
		if (glowEnabled) {
			glowPowerupItem = createItemStack("glow");
		}

		//Lobby
		minPlayers = Math.max(2, config.getInt("minPlayers"));
		countdown = Math.max(10, config.getInt("lobby.countdown"));
		changeCountdown = Math.max(minPlayers, config.getInt("lobby.changeCountdown"));
		lobbyMin = Math.max(minPlayers, config.getInt("lobby.min"));
		lobbyMax = config.getInt("lobby.max");
		lobbyCountdownEnabled = config.getBoolean("lobby.enabled");

		//SeekerPing
		seekerPing = config.getBoolean("seekerPing.enabled");
		seekerPingLevel1 = config.getInt("seekerPing.distances.level1");
		seekerPingLevel2 = config.getInt("seekerPing.distances.level2");
		seekerPingLevel3 = config.getInt("seekerPing.distances.level3");
		seekerPingLeadingVolume = config.getFloat("seekerPing.sounds.leadingVolume");
		seekerPingVolume = config.getFloat("seekerPing.sounds.volume");
		seekerPingPitch = config.getFloat("seekerPing.sounds.pitch");
		Optional<XSound> heartbeatOptional = XSound.matchXSound(config.getString("seekerPing.sounds.heartbeatNoise"));
		heartbeatSound = heartbeatOptional.orElse(XSound.BLOCK_NOTE_BLOCK_BASEDRUM);
		Optional<XSound> ringingOptional = XSound.matchXSound(config.getString("seekerPing.sounds.ringingNoise"));
		ringingSound = ringingOptional.orElse(XSound.BLOCK_NOTE_BLOCK_PLING);

		//Other
		nameTagsVisible = config.getBoolean("nametagsVisible");
		permissionsRequired = config.getBoolean("permissionsRequired");
		gameLength = config.getInt("gameLength");
		pvpEnabled = config.getBoolean("pvp");
		allowNaturalCauses = config.getBoolean("allowNaturalCauses");
		autoJoin = config.getBoolean("autoJoin");
		teleportToExit = config.getBoolean("teleportToExit");
		locale = config.getString("locale", "local");
		blockedCommands = config.getStringList("blockedCommands");
		leaveOnEnd = config.getBoolean("leaveOnEnd");
		placeholderError = config.getString("placeholder.incorrect");
		placeholderNoData = config.getString("placeholder.noData");
		try {
			countdownDisplay = CountdownDisplay.valueOf(config.getString("hideCountdownDisplay"));
		} catch (IllegalArgumentException e) {
			countdownDisplay = CountdownDisplay.CHAT;
			Main.getInstance().getLogger().warning("hideCountdownDisplay: "+config.getString("hideCountdownDisplay")+" is not a valid configuration option!");
		}
		blockedInteracts = new ArrayList<>();
		List<String> tempInteracts = config.getStringList("blockedInteracts");
		for(String id : tempInteracts) {
			Optional<XMaterial> optional_mat = XMaterial.matchXMaterial(id);
			if (optional_mat.isPresent()) {
				Material mat = optional_mat.get().parseMaterial();
				if (mat != null) {
					blockedInteracts.add(mat.name());
				}
			}
		}
		bungeeLeave = config.getString("leaveType") == null || config.getString("leaveType").equalsIgnoreCase("proxy");
		leaveServer = config.getString("leaveServer");

		//Leaderboard
		LOBBY_TITLE = leaderboard.getString("lobby.title");
		GAME_TITLE = leaderboard.getString("game.title");
		LOBBY_CONTENTS = leaderboard.getStringList("lobby.content");
		Collections.reverse(LOBBY_CONTENTS);
		GAME_CONTENTS = leaderboard.getStringList("game.content");
		Collections.reverse(GAME_CONTENTS);
		COUNTDOWN_WAITING = leaderboard.getString("countdown.waiting");
		COUNTDOWN_COUNTING = leaderboard.getString("countdown.counting");
		COUNTDOWN_ADMINSTART = leaderboard.getString("countdown.adminStart");
		TAUNT_COUNTING = leaderboard.getString("taunt.counting");
		TAUNT_ACTIVE = leaderboard.getString("taunt.active");
		TAUNT_EXPIRED = leaderboard.getString("taunt.expired");
		GLOW_ACTIVE = leaderboard.getString("glow.active");
		GLOW_INACTIVE = leaderboard.getString("glow.inactive");
		BORDER_COUNTING = leaderboard.getString("border.counting");
		BORDER_DECREASING = leaderboard.getString("border.decreasing");

		//Lobby Items
		if (config.getBoolean("lobbyItems.leave.enabled")) {
			lobbyLeaveItem = createItemStack("lobbyItems.leave");
			lobbyItemLeavePosition = config.getInt("lobbyItems.leave.position");
		}
		if (config.getBoolean("lobbyItems.start.enabled")) {
			lobbyStartItem = createItemStack("lobbyItems.start");
			lobbyItemStartAdmin = config.getBoolean("lobbyItems.start.adminOnly");
			lobbyItemStartPosition = config.getInt("lobbyItems.start.position");
		}

		//Spectator Items
		flightToggleItem = createItemStack("spectatorItems.flight");
		flightToggleItemPosition = config.getInt("spectatorItems.flight.position");

		teleportItem = createItemStack("spectatorItems.teleport");
		teleportItemPosition = config.getInt("spectatorItems.teleport.position");

		//Database
		databaseHost = config.getString("databaseHost");
		databasePort = config.getString("databasePort");
		databaseUser = config.getString("databaseUser");
		databasePass = config.getString("databasePass");
		databaseName = config.getString("databaseName");

		databaseType = config.getString("databaseType").toUpperCase();
		if(!databaseType.equals("SQLITE") && !databaseType.equals("MYSQL")){
			Main.getInstance().getLogger().warning("databaseType: "+databaseType+" is not a valid configuration option!");
			databaseType = "SQLITE";
		}
	}
	
	public static void addToConfig(String path, Object value) {
		config.set(path, value);
	}

	public static void saveConfig() {
		config.saveConfig();
	}

	@Nullable
	private static ItemStack createItemStack(String path){
		ConfigurationSection item = new YamlConfiguration().createSection("temp");
		item.set("name", ChatColor.translateAlternateColorCodes('&',config.getString(path+".name")));
		item.set("material", config.getString(path+".material"));
		if (Main.getInstance().supports(14)) {
			if (config.contains(path+".model-data") && config.getInt(path+".model-data") != 0) {
				item.set("model-data", config.getInt(path+".model-data"));
			}
		}
		List<String> lore = config.getStringList(path+".lore");
		if (lore != null && !lore.isEmpty()) item.set("lore", lore);
		ItemStack temp = null;
		try{ temp = XItemStack.deserialize(item); } catch(Exception ignored) {}
		return temp;
	}
	
}