FlexConnect/src/main/java/fi/flexplex/connect/FlexConnect.java

107 lines
3.5 KiB
Java

package fi.flexplex.connect;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import fi.flexplex.connect.util.FileChangeListener;
public final class FlexConnect extends JavaPlugin {
private final Set<String> proxyAddresses = new HashSet<>();
private String token = "";
private FlexPlexGraphQLApi flexPlexGraphQLApi;
public String getToken() {
return this.token;
}
public FlexPlexGraphQLApi getFlexPlexGraphQLApi() {
return this.flexPlexGraphQLApi;
}
public Set<String> getProxyAddresses() {
return this.proxyAddresses;
}
@Override
public void onEnable() {
// Plugin own configs
this.saveDefaultConfig();
this.token = this.getConfig().getString("token");
if (this.token.equals("default")) {
this.getLogger().warning("Token for FlexPlex server network has not been set into plugins config file. Please request token from FlexPlex staff members if you don't already have one!");
}
// GraphQL Api
this.flexPlexGraphQLApi = new FlexPlexGraphQLApi(this, "https://api.flexplex.fi/graphql");
boolean configsModified = false;
// Check online mode
if (this.getServer().getOnlineMode()) {
final Properties properties = new Properties();
try {
properties.load(new FileInputStream("server.properties"));
properties.setProperty("online-mode", "false");
properties.store(new FileOutputStream("server.properties"), null);
} catch (final IOException e) {
e.printStackTrace();
}
this.getLogger().warning("Automatic changes has been made to server.properties file.");
configsModified = true;
}
// Check spigot config
boolean spigotConfigModified = false;
final YamlConfiguration spigotConfig = this.getServer().spigot().getSpigotConfig();
if (!spigotConfig.getBoolean("settings.bungeecord")) {
spigotConfig.set("settings.bungeecord", true);
spigotConfigModified = true;
}
if (spigotConfigModified) {
try {
spigotConfig.save("spigot.yml");
} catch (final IOException e) {
e.printStackTrace();
}
this.getLogger().warning("Automatic changes has been made to spigot.yml config file.");
configsModified = true;
}
// Actions if configs were modified
if (configsModified) {
this.getLogger().warning("Changes has been made to config files. Server will now shutdown automatically.");
this.getServer().shutdown();
return;
}
// Register listener
this.getServer().getPluginManager().registerEvents(new FlexConnectListener(this), this);
// Register file change listener
new FileChangeListener(this, "ops.json", "whitelist.json");
// Update whitelists for flexplex api
this.flexPlexGraphQLApi.updateWhitelist();
// Load real FlexPlex proxy addresses from FlexPlex API
this.proxyAddresses.addAll(this.flexPlexGraphQLApi.getProxyAddresses());
// Load allowed proxy addresses from config
this.proxyAddresses.addAll(this.getConfig().getStringList("allowedProxies"));
}
}