summaryrefslogtreewikicommitdiff
path: root/src/main/java/cat/freya/khs/configuration/Config.java
blob: 1bbbefa0bff413e22811782f44b61cd02e8179a2 (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
package cat.freya.khs.configuration;

import com.cryptomorin.xseries.XItemStack;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound;
import cat.freya.khs.Main;
import cat.freya.khs.game.util.CountdownDisplay;
import cat.freya.khs.util.Location;
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.jetbrains.annotations.Nullable;

import java.util.ArrayList;
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,
		locale,
		leaveServer,
		placeholderError,
		placeholderNoData,
		databaseType,
		databaseHost,
		databasePort,
		databaseUser,
		databasePass,
		databaseName;

	public static boolean
		nameTagsVisible,
		permissionsRequired,
		announceMessagesToNonPlayers,
		tauntEnabled,
		tauntCountdown,
		tauntLast,
		alwaysGlow,
		glowEnabled,
		glowStackable,
		pvpEnabled,
		autoJoin,
		teleportToExit,
		lobbyCountdownEnabled,
		seekerPing,
		bungeeLeave,
		lobbyItemStartAdmin,
		leaveOnEnd,
		mapSaveEnabled,
		allowNaturalCauses,
		saveInventory,
		delayedRespawn,
        dontRewardQuit,
		spawnPatch,
		dropItems,
        respawnAsSpectator,
        waitTillNoneLeft,
        gameOverTitle,
		regenHealth;

	public static int
		minPlayers,
		gameLength,
		tauntDelay,
		glowLength,
		countdown,
		changeCountdown,
		lobbyMin,
		lobbyMax,
		seekerPingLevel1,
		seekerPingLevel2,
		seekerPingLevel3,
		lobbyItemLeavePosition,
		lobbyItemStartPosition,
		flightToggleItemPosition,
		teleportItemPosition,
        startingSeekerCount,
		delayedRespawnDelay,
		hidingTimer,
        endGameDelay;

	public static float
		seekerPingLeadingVolume,
		seekerPingVolume,
		seekerPingPitch;

	public static List<String>
		blockedCommands,
		blockedInteracts;

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

	public static XSound
		ringingSound,
		heartbeatSound;

	public static CountdownDisplay
		countdownDisplay;

	public static Location
		exitPosition;

	public static void loadConfig() {

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

		announceMessagesToNonPlayers = config.getBoolean("announceMessagesToNonPlayers");

		//Prefix
		char SYMBOL = '§';
		String SYMBOL_STRING = String.valueOf(SYMBOL);

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

		// Locations
		exitPosition = new Location(
				config.getString("exit.world"),
				config.getInt("exit.x"),
				config.getInt("exit.y"),
				config.getInt("exit.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
        startingSeekerCount = Math.max(1, config.getInt("startingSeekerCount"));
        waitTillNoneLeft = config.getBoolean("waitTillNoneLeft");
		minPlayers = Math.max(1 + startingSeekerCount + (waitTillNoneLeft ? 0 : 1), 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");
		saveInventory = config.getBoolean("saveInventory");
        respawnAsSpectator = config.getBoolean("respawnAsSpectator");
        dontRewardQuit = config.getBoolean("dontRewardQuit");
        endGameDelay = Math.max(0,config.getInt("endGameDelay"));
        gameOverTitle = config.getBoolean("gameOverTitle");
		hidingTimer = Math.max(10, config.getInt("hidingTimer"));

		try {
			countdownDisplay = CountdownDisplay.valueOf(config.getString("hideCountdownDisplay"));
		} catch (IllegalArgumentException e) {
			throw new RuntimeException("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");

		//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")){
			throw new RuntimeException("databaseType: "+databaseType+" is not a valid configuration option!");
		}

		delayedRespawn = config.getBoolean("delayedRespawn.enabled");
		delayedRespawnDelay = Math.max(0,config.getInt("delayedRespawn.delay"));

		spawnPatch = config.getBoolean("spawnPatch");
		dropItems = config.getBoolean("dropItems");
		regenHealth = config.getBoolean("regenHealth");

	}

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

}