2022-04-30 21:51:44 +00:00
package fi.flexplex.connect ;
import java.io.FileInputStream ;
import java.io.FileOutputStream ;
import java.io.IOException ;
2022-08-29 11:50:02 +00:00
import java.util.HashSet ;
2022-04-30 21:51:44 +00:00
import java.util.Properties ;
2022-08-29 11:50:02 +00:00
import java.util.Set ;
2022-04-30 21:51:44 +00:00
import org.bukkit.configuration.file.YamlConfiguration ;
import org.bukkit.plugin.java.JavaPlugin ;
2022-05-02 17:15:25 +00:00
import fi.flexplex.connect.util.FileChangeListener ;
2022-04-30 21:51:44 +00:00
public final class FlexConnect extends JavaPlugin {
2022-08-29 11:50:02 +00:00
private final Set < String > proxyAddresses = new HashSet < > ( ) ;
2022-04-30 21:51:44 +00:00
private String token = "" ;
2022-05-02 17:15:25 +00:00
private FlexPlexGraphQLApi flexPlexGraphQLApi ;
2022-04-30 21:51:44 +00:00
public String getToken ( ) {
return this . token ;
}
2022-05-02 17:15:25 +00:00
public FlexPlexGraphQLApi getFlexPlexGraphQLApi ( ) {
return this . flexPlexGraphQLApi ;
}
2022-08-29 11:50:02 +00:00
public Set < String > getProxyAddresses ( ) {
return this . proxyAddresses ;
}
2022-04-30 21:51:44 +00:00
@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!" ) ;
}
2022-05-02 17:15:25 +00:00
// GraphQL Api
this . flexPlexGraphQLApi = new FlexPlexGraphQLApi ( this , "https://api.flexplex.fi/graphql" ) ;
2022-04-30 21:51:44 +00:00
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 ;
}
2022-08-29 09:27:07 +00:00
// Check spigot config
boolean spigotConfigModified = false ;
final YamlConfiguration spigotConfig = this . getServer ( ) . spigot ( ) . getSpigotConfig ( ) ;
2022-07-27 14:38:32 +00:00
2022-08-29 09:27:07 +00:00
if ( ! spigotConfig . getBoolean ( "settings.bungeecord" ) ) {
spigotConfig . set ( "settings.bungeecord" , true ) ;
spigotConfigModified = true ;
2022-07-27 14:38:32 +00:00
}
2022-08-29 09:27:07 +00:00
if ( spigotConfigModified ) {
2022-04-30 21:51:44 +00:00
try {
2022-08-29 09:27:07 +00:00
spigotConfig . save ( "spigot.yml" ) ;
2022-04-30 21:51:44 +00:00
} catch ( final IOException e ) {
e . printStackTrace ( ) ;
}
2022-08-29 09:27:07 +00:00
this . getLogger ( ) . warning ( "Automatic changes has been made to spigot.yml config file." ) ;
2022-04-30 21:51:44 +00:00
configsModified = true ;
}
2022-08-29 09:27:07 +00:00
2022-04-30 21:51:44 +00:00
// 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 ) ;
2022-05-02 17:15:25 +00:00
// Register file change listener
new FileChangeListener ( this , "ops.json" , "whitelist.json" ) ;
2022-05-04 09:55:51 +00:00
// Update whitelists for flexplex api
this . flexPlexGraphQLApi . updateWhitelist ( ) ;
2022-08-29 11:50:02 +00:00
// 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" ) ) ;
2022-04-30 21:51:44 +00:00
}
}