130 Zeilen
3.8 KiB
Java
130 Zeilen
3.8 KiB
Java
/*
|
|
* This file is a part of the SteamWar software.
|
|
*
|
|
* Copyright (C) 2021 SteamWar.de-Serverteam
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program 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 Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package de.steamwar;
|
|
|
|
import lombok.experimental.UtilityClass;
|
|
import net.md_5.bungee.config.Configuration;
|
|
import net.md_5.bungee.config.ConfigurationProvider;
|
|
import net.md_5.bungee.config.YamlConfiguration;
|
|
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
|
|
@UtilityClass
|
|
public class ConfigSystem {
|
|
|
|
private File configFile;
|
|
|
|
private boolean enabled = false;
|
|
private long lastModified = 0;
|
|
|
|
private Configuration config;
|
|
|
|
private Set<String> whitelist = null;
|
|
|
|
static {
|
|
configFile = new File(SteamWarBungeeTeamserver.plugin.getDataFolder(), "config.yml");
|
|
if (!configFile.exists()) {
|
|
try {
|
|
configFile.getParentFile().mkdirs();
|
|
configFile.createNewFile();
|
|
FileOutputStream fileOutputStream = new FileOutputStream(configFile);
|
|
InputStream inputStream = SteamWarBungeeTeamserver.plugin.getResourceAsStream("config.yml");
|
|
byte[] buffer = new byte[1024];
|
|
int length;
|
|
while ((length = inputStream.read(buffer)) > 0) {
|
|
fileOutputStream.write(buffer, 0, length);
|
|
}
|
|
fileOutputStream.close();
|
|
inputStream.close();
|
|
enabled = true;
|
|
} catch (Exception e) {
|
|
// ignored
|
|
}
|
|
} else {
|
|
enabled = true;
|
|
}
|
|
}
|
|
|
|
void init() {
|
|
|
|
}
|
|
|
|
private synchronized void load() {
|
|
if (!enabled) return;
|
|
if (lastModified >= configFile.lastModified()) return;
|
|
try {
|
|
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
|
|
whitelist = null;
|
|
lastModified = configFile.lastModified();
|
|
} catch (IOException e) {
|
|
// Ignored
|
|
}
|
|
}
|
|
|
|
public boolean isEnabled() {
|
|
load();
|
|
if (config != null) {
|
|
return config.getBoolean("plugin-enabled", false) && enabled;
|
|
}
|
|
return enabled;
|
|
}
|
|
|
|
public boolean isOnlySteamWarJoin() {
|
|
load();
|
|
if (config != null) {
|
|
return config.getBoolean("force-only-steamwar-join", false);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public String getNotFromSteamWarMessage() {
|
|
load();
|
|
if (config != null) {
|
|
return config.getString("not-from-steamwar-message", "");
|
|
}
|
|
return "You are not allowed to join this server.";
|
|
}
|
|
|
|
public boolean isWhitelistEnabled() {
|
|
load();
|
|
if (config != null) {
|
|
return config.getBoolean("whitelist-enabled", false);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public Set<String> getWhitelist() {
|
|
load();
|
|
if (whitelist == null) {
|
|
if (config != null) {
|
|
whitelist = new HashSet<>(config.getStringList("whitelisted-players"));
|
|
} else {
|
|
whitelist = new HashSet<>();
|
|
}
|
|
}
|
|
return whitelist;
|
|
}
|
|
}
|