2016-12-24 05:55:17 +01:00
package net.ME1312.SubServers.Bungee ;
2017-08-15 11:58:48 +02:00
import net.ME1312.SubServers.Bungee.Event.SubStoppedEvent ;
2016-12-24 05:55:17 +01:00
import net.ME1312.SubServers.Bungee.Host.Executable ;
import net.ME1312.SubServers.Bungee.Host.Server ;
import net.ME1312.SubServers.Bungee.Library.Config.YAMLConfig ;
import net.ME1312.SubServers.Bungee.Library.Exception.InvalidHostException ;
import net.ME1312.SubServers.Bungee.Library.Exception.InvalidServerException ;
import net.ME1312.SubServers.Bungee.Host.Host ;
import net.ME1312.SubServers.Bungee.Host.SubServer ;
2017-08-17 01:29:02 +02:00
import net.ME1312.SubServers.Bungee.Library.Metrics ;
2017-01-26 23:19:48 +01:00
import net.ME1312.SubServers.Bungee.Library.NamedContainer ;
2016-12-24 05:55:17 +01:00
import net.ME1312.SubServers.Bungee.Library.UniversalFile ;
import net.ME1312.SubServers.Bungee.Library.Util ;
import net.ME1312.SubServers.Bungee.Library.Version.Version ;
import net.ME1312.SubServers.Bungee.Network.SubDataServer ;
2016-12-05 04:21:04 +01:00
import net.md_5.bungee.BungeeCord ;
2017-04-16 19:02:14 +02:00
import net.md_5.bungee.BungeeServerInfo ;
2016-12-05 04:21:04 +01:00
import net.md_5.bungee.api.config.ServerInfo ;
2017-08-18 11:58:06 +02:00
import net.md_5.bungee.api.event.ServerConnectEvent ;
2017-08-15 11:58:48 +02:00
import net.md_5.bungee.api.plugin.Listener ;
import net.md_5.bungee.event.EventHandler ;
2017-01-01 20:34:46 +01:00
import org.json.JSONObject ;
2017-08-18 11:58:06 +02:00
import org.w3c.dom.Document ;
import org.w3c.dom.Node ;
import org.w3c.dom.NodeList ;
import org.xml.sax.InputSource ;
2016-12-05 04:21:04 +01:00
2017-08-18 11:58:06 +02:00
import javax.xml.parsers.DocumentBuilderFactory ;
2016-12-05 04:21:04 +01:00
import java.io.* ;
2017-01-30 21:22:36 +01:00
import java.lang.reflect.InvocationTargetException ;
2016-12-05 04:21:04 +01:00
import java.net.InetAddress ;
2017-04-16 19:02:14 +02:00
import java.net.InetSocketAddress ;
2016-12-24 05:55:17 +01:00
import java.net.SocketException ;
2017-08-18 11:58:06 +02:00
import java.net.URL ;
import java.nio.charset.Charset ;
2016-12-05 04:21:04 +01:00
import java.nio.file.Files ;
2017-05-30 21:38:51 +02:00
import java.text.DecimalFormat ;
2016-12-05 04:21:04 +01:00
import java.util.* ;
2017-01-01 20:34:46 +01:00
import java.util.concurrent.TimeUnit ;
2016-12-05 04:21:04 +01:00
/ * *
* Main Plugin Class
* /
2017-08-15 11:58:48 +02:00
public final class SubPlugin extends BungeeCord implements Listener {
2016-12-05 04:21:04 +01:00
protected final HashMap < String , Class < ? extends Host > > hostDrivers = new HashMap < String , Class < ? extends Host > > ( ) ;
public final HashMap < String , Host > hosts = new HashMap < String , Host > ( ) ;
2017-04-16 19:02:14 +02:00
public final HashMap < String , Server > exServers = new HashMap < String , Server > ( ) ;
private final HashMap < String , ServerInfo > legServers = new HashMap < String , ServerInfo > ( ) ;
2016-12-05 04:21:04 +01:00
2017-01-08 03:30:03 +01:00
public final PrintStream out ;
2016-12-15 22:04:39 +01:00
public final UniversalFile dir = new UniversalFile ( new File ( System . getProperty ( " user.dir " ) ) ) ;
2016-12-05 04:21:04 +01:00
public YAMLConfig config ;
public YAMLConfig lang ;
2016-12-19 01:38:02 +01:00
public HashMap < String , String > exLang = new HashMap < String , String > ( ) ;
2016-12-15 22:04:39 +01:00
public SubDataServer subdata = null ;
2017-08-15 11:58:48 +02:00
public SubServer sudo = null ;
2017-01-08 03:30:03 +01:00
public final Version version = new Version ( SubPlugin . class . getPackage ( ) . getImplementationVersion ( ) ) ;
public final Version bversion = ( SubPlugin . class . getPackage ( ) . getSpecificationVersion ( ) . equals ( " 0 " ) ) ? null : new Version ( SubPlugin . class . getPackage ( ) . getSpecificationVersion ( ) ) ;
2016-12-05 04:21:04 +01:00
2017-04-16 19:02:14 +02:00
private boolean running = false ;
2017-08-18 11:58:06 +02:00
private boolean posted = false ;
2016-12-05 04:21:04 +01:00
public final SubAPI api = new SubAPI ( this ) ;
2017-01-08 03:30:03 +01:00
protected SubPlugin ( PrintStream out ) throws IOException {
2017-08-22 15:02:23 +02:00
System . out . println ( " SubServers > Loading SubServers.Bungee v " + version . toString ( ) + " Libraries... " ) ;
2017-01-08 03:30:03 +01:00
2017-01-30 21:22:36 +01:00
this . out = out ;
2016-12-24 05:55:17 +01:00
if ( ! ( new UniversalFile ( dir , " config.yml " ) . exists ( ) ) ) {
Util . copyFromJar ( SubPlugin . class . getClassLoader ( ) , " net/ME1312/SubServers/Bungee/Library/Files/bungee.yml " , new UniversalFile ( dir , " config.yml " ) . getPath ( ) ) ;
YAMLConfig tmp = new YAMLConfig ( new UniversalFile ( " config.yml " ) ) ;
tmp . get ( ) . set ( " stats " , UUID . randomUUID ( ) . toString ( ) ) ;
tmp . save ( ) ;
System . out . println ( " SubServers > Created ~/config.yml " ) ;
}
if ( ! ( new UniversalFile ( dir , " modules.yml " ) . exists ( ) ) ) {
Util . copyFromJar ( SubPlugin . class . getClassLoader ( ) , " net/ME1312/SubServers/Bungee/Library/Files/modules.yml " , new UniversalFile ( dir , " modules.yml " ) . getPath ( ) ) ;
System . out . println ( " SubServers > Created ~/modules.yml " ) ;
}
2016-12-05 04:21:04 +01:00
UniversalFile dir = new UniversalFile ( this . dir , " SubServers " ) ;
dir . mkdir ( ) ;
if ( ! ( new UniversalFile ( dir , " config.yml " ) . exists ( ) ) ) {
2016-12-24 05:55:17 +01:00
Util . copyFromJar ( SubPlugin . class . getClassLoader ( ) , " net/ME1312/SubServers/Bungee/Library/Files/config.yml " , new UniversalFile ( dir , " config.yml " ) . getPath ( ) ) ;
2016-12-05 04:21:04 +01:00
System . out . println ( " SubServers > Created ~/SubServers/config.yml " ) ;
2016-12-24 05:55:17 +01:00
} else if ( ( new Version ( ( new YAMLConfig ( new UniversalFile ( dir , " config.yml " ) ) ) . get ( ) . getSection ( " Settings " ) . getString ( " Version " , " 0 " ) ) . compareTo ( new Version ( " 2.11.2a+ " ) ) ) ! = 0 ) {
2016-12-05 04:21:04 +01:00
Files . move ( new UniversalFile ( dir , " config.yml " ) . toPath ( ) , new UniversalFile ( dir , " config.old " + Math . round ( Math . random ( ) * 100000 ) + " .yml " ) . toPath ( ) ) ;
2016-12-24 05:55:17 +01:00
Util . copyFromJar ( SubPlugin . class . getClassLoader ( ) , " net/ME1312/SubServers/Bungee/Library/Files/config.yml " , new UniversalFile ( dir , " config.yml " ) . getPath ( ) ) ;
2016-12-05 04:21:04 +01:00
System . out . println ( " SubServers > Updated ~/SubServers/config.yml " ) ;
}
2017-04-16 19:02:14 +02:00
config = new YAMLConfig ( new UniversalFile ( dir , " config.yml " ) ) ;
2016-12-05 04:21:04 +01:00
if ( ! ( new UniversalFile ( dir , " lang.yml " ) . exists ( ) ) ) {
2016-12-24 05:55:17 +01:00
Util . copyFromJar ( SubPlugin . class . getClassLoader ( ) , " net/ME1312/SubServers/Bungee/Library/Files/lang.yml " , new UniversalFile ( dir , " lang.yml " ) . getPath ( ) ) ;
2016-12-05 04:21:04 +01:00
System . out . println ( " SubServers > Created ~/SubServers/lang.yml " ) ;
2017-08-18 11:58:06 +02:00
} else if ( ( new Version ( ( new YAMLConfig ( new UniversalFile ( dir , " lang.yml " ) ) ) . get ( ) . getString ( " Version " , " 0 " ) ) . compareTo ( new Version ( " 2.12.1c+ " ) ) ) ! = 0 ) {
2016-12-05 04:21:04 +01:00
Files . move ( new UniversalFile ( dir , " lang.yml " ) . toPath ( ) , new UniversalFile ( dir , " lang.old " + Math . round ( Math . random ( ) * 100000 ) + " .yml " ) . toPath ( ) ) ;
2016-12-24 05:55:17 +01:00
Util . copyFromJar ( SubPlugin . class . getClassLoader ( ) , " net/ME1312/SubServers/Bungee/Library/Files/lang.yml " , new UniversalFile ( dir , " lang.yml " ) . getPath ( ) ) ;
2016-12-05 04:21:04 +01:00
System . out . println ( " SubServers > Updated ~/SubServers/lang.yml " ) ;
}
2017-04-16 19:02:14 +02:00
lang = new YAMLConfig ( new UniversalFile ( dir , " lang.yml " ) ) ;
2016-12-05 04:21:04 +01:00
2017-07-30 02:41:02 +02:00
if ( ! ( new UniversalFile ( dir , " Templates " ) . exists ( ) ) ) {
new UniversalFile ( dir , " Templates " ) . mkdirs ( ) ;
2017-07-23 06:32:57 +02:00
Util . unzip ( SubPlugin . class . getResourceAsStream ( " /net/ME1312/SubServers/Bungee/Library/Files/Templates/vanilla.zip " ) , new UniversalFile ( dir , " Templates " ) ) ;
2017-05-29 07:00:02 +02:00
System . out . println ( " SubServers > Created ~/SubServers/Templates/Vanilla " ) ;
2017-07-30 02:41:02 +02:00
2017-07-23 06:32:57 +02:00
Util . unzip ( SubPlugin . class . getResourceAsStream ( " /net/ME1312/SubServers/Bungee/Library/Files/Templates/spigot.zip " ) , new UniversalFile ( dir , " Templates " ) ) ;
2017-05-29 07:00:02 +02:00
System . out . println ( " SubServers > Created ~/SubServers/Templates/Spigot " ) ;
2017-07-30 02:41:02 +02:00
2017-07-23 06:32:57 +02:00
Util . unzip ( SubPlugin . class . getResourceAsStream ( " /net/ME1312/SubServers/Bungee/Library/Files/Templates/sponge.zip " ) , new UniversalFile ( dir , " Templates " ) ) ;
2017-05-29 07:00:02 +02:00
System . out . println ( " SubServers > Created ~/SubServers/Templates/Sponge " ) ;
2017-07-30 02:41:02 +02:00
} else {
if ( new UniversalFile ( dir , " Templates:Vanilla:template.yml " ) . exists ( ) & & ( new Version ( ( new YAMLConfig ( new UniversalFile ( dir , " Templates:Vanilla:template.yml " ) ) ) . get ( ) . getString ( " Version " , " 0 " ) ) . compareTo ( new Version ( " 2.12b+ " ) ) ) ! = 0 ) {
Files . move ( new UniversalFile ( dir , " Templates:Vanilla " ) . toPath ( ) , new UniversalFile ( dir , " Templates:Vanilla.old " + Math . round ( Math . random ( ) * 100000 ) ) . toPath ( ) ) ;
Util . unzip ( SubPlugin . class . getResourceAsStream ( " /net/ME1312/SubServers/Bungee/Library/Files/Templates/vanilla.zip " ) , new UniversalFile ( dir , " Templates " ) ) ;
System . out . println ( " SubServers > Updated ~/SubServers/Templates/Vanilla " ) ;
}
if ( new UniversalFile ( dir , " Templates:Spigot:template.yml " ) . exists ( ) & & ( new Version ( ( new YAMLConfig ( new UniversalFile ( dir , " Templates:Spigot:template.yml " ) ) ) . get ( ) . getString ( " Version " , " 0 " ) ) . compareTo ( new Version ( " 2.11.2m+ " ) ) ) ! = 0 ) {
Files . move ( new UniversalFile ( dir , " Templates:Vanilla " ) . toPath ( ) , new UniversalFile ( dir , " Templates:Spigot.old " + Math . round ( Math . random ( ) * 100000 ) ) . toPath ( ) ) ;
Util . unzip ( SubPlugin . class . getResourceAsStream ( " /net/ME1312/SubServers/Bungee/Library/Files/Templates/spigot.zip " ) , new UniversalFile ( dir , " Templates " ) ) ;
System . out . println ( " SubServers > Updated ~/SubServers/Templates/Spigot " ) ;
}
if ( new UniversalFile ( dir , " Templates:Sponge:template.yml " ) . exists ( ) & & ( new Version ( ( new YAMLConfig ( new UniversalFile ( dir , " Templates:Sponge:template.yml " ) ) ) . get ( ) . getString ( " Version " , " 0 " ) ) . compareTo ( new Version ( " 2.11.2m+ " ) ) ) ! = 0 ) {
Files . move ( new UniversalFile ( dir , " Templates:Vanilla " ) . toPath ( ) , new UniversalFile ( dir , " Templates:Sponge.old " + Math . round ( Math . random ( ) * 100000 ) ) . toPath ( ) ) ;
Util . unzip ( SubPlugin . class . getResourceAsStream ( " /net/ME1312/SubServers/Bungee/Library/Files/Templates/sponge.zip " ) , new UniversalFile ( dir , " Templates " ) ) ;
System . out . println ( " SubServers > Updated ~/SubServers/Templates/Sponge " ) ;
}
2017-01-01 20:34:46 +01:00
}
if ( new UniversalFile ( dir , " Recently Deleted " ) . exists ( ) ) {
int f = new UniversalFile ( dir , " Recently Deleted " ) . listFiles ( ) . length ;
for ( File file : new UniversalFile ( dir , " Recently Deleted " ) . listFiles ( ) ) {
try {
if ( file . isDirectory ( ) ) {
if ( new UniversalFile ( dir , " Recently Deleted: " + file . getName ( ) + " :info.json " ) . exists ( ) ) {
JSONObject json = new JSONObject ( Util . readAll ( new FileReader ( new UniversalFile ( dir , " Recently Deleted: " + file . getName ( ) + " :info.json " ) ) ) ) ;
if ( json . keySet ( ) . contains ( " Timestamp " ) ) {
if ( TimeUnit . MILLISECONDS . toDays ( Calendar . getInstance ( ) . getTime ( ) . getTime ( ) - json . getLong ( " Timestamp " ) ) > = 7 ) {
Util . deleteDirectory ( file ) ;
f - - ;
System . out . println ( " SubServers > Removed ~/SubServers/Recently Deleted/ " + file . getName ( ) ) ;
}
} else {
Util . deleteDirectory ( file ) ;
f - - ;
System . out . println ( " SubServers > Removed ~/SubServers/Recently Deleted/ " + file . getName ( ) ) ;
}
} else {
Util . deleteDirectory ( file ) ;
f - - ;
System . out . println ( " SubServers > Removed ~/SubServers/Recently Deleted/ " + file . getName ( ) ) ;
}
} else {
Files . delete ( file . toPath ( ) ) ;
f - - ;
System . out . println ( " SubServers > Removed ~/SubServers/Recently Deleted/ " + file . getName ( ) ) ;
}
} catch ( Exception e ) {
2017-05-29 07:00:02 +02:00
System . out . println ( " SubServers > Problem scanning ~/SubServers/Recently Deleted/ " + file . getName ( ) ) ;
2017-01-01 20:34:46 +01:00
e . printStackTrace ( ) ;
2017-05-29 07:00:02 +02:00
Files . delete ( file . toPath ( ) ) ;
2017-01-01 20:34:46 +01:00
}
}
2017-01-30 21:22:36 +01:00
if ( f < = 0 ) {
2017-01-01 20:34:46 +01:00
Files . delete ( new UniversalFile ( dir , " Recently Deleted " ) . toPath ( ) ) ;
}
}
2017-08-22 15:02:23 +02:00
api . addHostDriver ( net . ME1312 . SubServers . Bungee . Host . Internal . InternalHost . class , " built-in " ) ;
api . addHostDriver ( net . ME1312 . SubServers . Bungee . Host . External . ExternalHost . class , " network " ) ;
2016-12-05 04:21:04 +01:00
2017-08-15 11:58:48 +02:00
getPluginManager ( ) . registerListener ( null , this ) ;
2017-01-08 03:30:03 +01:00
getPluginManager ( ) . registerCommand ( null , new SubCommand . BungeeServer ( this , " server " ) ) ;
getPluginManager ( ) . registerCommand ( null , new SubCommand . BungeeList ( this , " glist " ) ) ;
getPluginManager ( ) . registerCommand ( null , new SubCommand ( this , " subservers " ) ) ;
getPluginManager ( ) . registerCommand ( null , new SubCommand ( this , " subserver " ) ) ;
getPluginManager ( ) . registerCommand ( null , new SubCommand ( this , " sub " ) ) ;
2017-08-17 01:29:02 +02:00
System . out . println ( " SubServers > Pre-Parsing Config... " ) ;
2017-04-16 19:02:14 +02:00
for ( String name : config . get ( ) . getSection ( " Servers " ) . getKeys ( ) ) {
2017-08-18 11:58:06 +02:00
try {
if ( ! config . get ( ) . getSection ( " Hosts " ) . contains ( config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getString ( " Host " ) ) ) throw new InvalidServerException ( " There is no host with this name: " + config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getString ( " Host " ) ) ;
legServers . put ( name , new BungeeServerInfo ( name , new InetSocketAddress ( InetAddress . getByName ( config . get ( ) . getSection ( " Hosts " ) . getSection ( config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getString ( " Host " ) ) . getRawString ( " Address " ) ) , config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getInt ( " Port " ) ) , config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getColoredString ( " Motd " , '&' ) , config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getBoolean ( " Restricted " ) ) ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2017-04-16 19:02:14 +02:00
}
2017-08-17 01:29:02 +02:00
System . out . println ( " SubServers > Loading BungeeCord Libraries... " ) ;
2016-12-05 04:21:04 +01:00
}
/ * *
2017-01-08 03:30:03 +01:00
* Load Hosts , Servers , SubServers , and SubData Direct
2016-12-05 04:21:04 +01:00
* /
@Override
public void startListeners ( ) {
try {
2017-01-21 17:49:37 +01:00
long begin = Calendar . getInstance ( ) . getTime ( ) . getTime ( ) ;
2017-04-16 19:02:14 +02:00
config . reload ( ) ;
lang . reload ( ) ;
2017-06-30 15:36:16 +02:00
SubDataServer . Encryption encryption = SubDataServer . Encryption . NONE ;
if ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getString ( " Password " , " " ) . length ( ) = = 0 ) {
System . out . println ( " SubData > Cannot encrypt connection without a password " ) ;
} else if ( Util . isException ( ( ) - > SubDataServer . Encryption . valueOf ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Encryption " , " NONE " ) . replace ( '-' , '_' ) . replace ( ' ' , '_' ) . toUpperCase ( ) ) ) ) {
System . out . println ( " SubData > Unknown encryption type: " + SubDataServer . Encryption . valueOf ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Encryption " , " None " ) ) ) ;
} else {
encryption = SubDataServer . Encryption . valueOf ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Encryption " , " NONE " ) . replace ( '-' , '_' ) . replace ( ' ' , '_' ) . toUpperCase ( ) ) ;
}
2017-07-19 01:28:59 +02:00
subdata = new SubDataServer ( this , Integer . parseInt ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 1 ] ) ,
2017-06-30 15:36:16 +02:00
( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 0 ] . equals ( " 0.0.0.0 " ) ) ? null : InetAddress . getByName ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 0 ] ) ,
encryption ) ;
2017-08-22 15:02:23 +02:00
System . out . println ( " SubServers > SubData Direct Listening on " + subdata . getServer ( ) . getLocalSocketAddress ( ) . toString ( ) ) ;
2016-12-05 04:21:04 +01:00
loop ( ) ;
int hosts = 0 ;
System . out . println ( " SubServers > Loading Hosts... " ) ;
for ( String name : config . get ( ) . getSection ( " Hosts " ) . getKeys ( ) ) {
try {
2016-12-15 22:04:39 +01:00
if ( ! hostDrivers . keySet ( ) . contains ( config . get ( ) . getSection ( " Hosts " ) . getSection ( name ) . getRawString ( " Driver " ) . toLowerCase ( ) ) ) throw new InvalidHostException ( " Invalid Driver for host: " + name ) ;
2017-08-22 15:02:23 +02:00
Host host = api . addHost ( config . get ( ) . getSection ( " Hosts " ) . getSection ( name ) . getRawString ( " Driver " ) . toLowerCase ( ) , name , config . get ( ) . getSection ( " Hosts " ) . getSection ( name ) . getBoolean ( " Enabled " ) , InetAddress . getByName ( config . get ( ) . getSection ( " Hosts " ) . getSection ( name ) . getRawString ( " Address " ) ) ,
config . get ( ) . getSection ( " Hosts " ) . getSection ( name ) . getRawString ( " Directory " ) , config . get ( ) . getSection ( " Hosts " ) . getSection ( name ) . getRawString ( " Git-Bash " ) ) ;
2017-05-29 07:00:02 +02:00
if ( config . get ( ) . getSection ( " Hosts " ) . getSection ( name ) . getKeys ( ) . contains ( " Display " ) ) host . setDisplayName ( config . get ( ) . getSection ( " Hosts " ) . getSection ( name ) . getString ( " Display " ) ) ;
2017-07-21 22:45:41 +02:00
if ( config . get ( ) . getSection ( " Hosts " ) . getSection ( name ) . getKeys ( ) . contains ( " Extra " ) ) for ( String extra : config . get ( ) . getSection ( " Hosts " ) . getSection ( name ) . getSection ( " Extra " ) . getKeys ( ) ) host . addExtra ( extra , config . get ( ) . getSection ( " Hosts " ) . getSection ( name ) . getSection ( " Extra " ) . getObject ( extra ) ) ;
2016-12-05 04:21:04 +01:00
hosts + + ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
int servers = 0 ;
System . out . println ( " SubServers > Loading Servers... " ) ;
YAMLConfig bungee = new YAMLConfig ( new UniversalFile ( dir , " config.yml " ) ) ;
for ( String name : bungee . get ( ) . getSection ( " servers " ) . getKeys ( ) ) {
try {
2017-01-08 03:30:03 +01:00
Server server = api . addServer ( name , InetAddress . getByName ( bungee . get ( ) . getSection ( " servers " ) . getSection ( name ) . getRawString ( " address " ) . split ( " : " ) [ 0 ] ) ,
Integer . parseInt ( bungee . get ( ) . getSection ( " servers " ) . getSection ( name ) . getRawString ( " address " ) . split ( " : " ) [ 1 ] ) , bungee . get ( ) . getSection ( " servers " ) . getSection ( name ) . getColoredString ( " motd " , '&' ) ,
2016-12-19 01:38:02 +01:00
bungee . get ( ) . getSection ( " servers " ) . getSection ( name ) . getBoolean ( " hidden " , false ) , bungee . get ( ) . getSection ( " servers " ) . getSection ( name ) . getBoolean ( " restricted " ) ) ;
2017-05-29 07:00:02 +02:00
if ( bungee . get ( ) . getSection ( " servers " ) . getSection ( name ) . getKeys ( ) . contains ( " display " ) ) server . setDisplayName ( bungee . get ( ) . getSection ( " servers " ) . getSection ( name ) . getString ( " display " ) ) ;
2017-07-21 22:45:41 +02:00
if ( bungee . get ( ) . getSection ( " servers " ) . getSection ( name ) . getKeys ( ) . contains ( " extra " ) ) for ( String extra : config . get ( ) . getSection ( " servers " ) . getSection ( name ) . getSection ( " extra " ) . getKeys ( ) ) server . addExtra ( extra , config . get ( ) . getSection ( " servers " ) . getSection ( name ) . getSection ( " extra " ) . getObject ( extra ) ) ;
2016-12-05 04:21:04 +01:00
servers + + ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
int subservers = 0 ;
System . out . println ( " SubServers > Loading SubServers... " ) ;
for ( String name : config . get ( ) . getSection ( " Servers " ) . getKeys ( ) ) {
try {
2017-01-01 20:34:46 +01:00
if ( ! this . hosts . keySet ( ) . contains ( config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getString ( " Host " ) . toLowerCase ( ) ) ) throw new InvalidServerException ( " There is no host with this name: " + config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getString ( " Host " ) ) ;
2016-12-15 22:04:39 +01:00
if ( exServers . keySet ( ) . contains ( name . toLowerCase ( ) ) ) {
exServers . remove ( name . toLowerCase ( ) ) ;
servers - - ;
}
2016-12-05 04:21:04 +01:00
SubServer server = this . hosts . get ( config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getString ( " Host " ) . toLowerCase ( ) ) . addSubServer ( name , config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getBoolean ( " Enabled " ) ,
2016-12-15 22:04:39 +01:00
config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getInt ( " Port " ) , config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getColoredString ( " Motd " , '&' ) , config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getBoolean ( " Log " ) ,
config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getRawString ( " Directory " ) , new Executable ( config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getRawString ( " Executable " ) ) , config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getRawString ( " Stop-Command " ) ,
2017-01-02 17:54:01 +01:00
config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getBoolean ( " Run-On-Launch " ) , config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getBoolean ( " Auto-Restart " ) , config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getBoolean ( " Hidden " ) , config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getBoolean ( " Restricted " ) , false ) ;
2017-05-29 07:00:02 +02:00
if ( config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getKeys ( ) . contains ( " Display " ) ) server . setDisplayName ( config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getString ( " Display " ) ) ;
2017-07-21 22:45:41 +02:00
if ( config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getKeys ( ) . contains ( " Extra " ) ) for ( String extra : config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getSection ( " Extra " ) . getKeys ( ) ) server . addExtra ( extra , config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getSection ( " Extra " ) . getObject ( extra ) ) ;
2016-12-05 04:21:04 +01:00
subservers + + ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
2017-07-21 22:45:41 +02:00
for ( SubServer server : api . getSubServers ( ) . values ( ) ) {
for ( String name : config . get ( ) . getSection ( " Servers " ) . getSection ( server . getName ( ) ) . getRawStringList ( " Incompatible " , new ArrayList < > ( ) ) ) {
SubServer other = api . getSubServer ( name ) ;
if ( other ! = null & & server . isCompatible ( other ) ) server . toggleCompatibility ( other ) ;
}
}
2017-04-16 19:02:14 +02:00
running = true ;
legServers . clear ( ) ;
2016-12-05 04:21:04 +01:00
2017-01-26 23:19:48 +01:00
int plugins = 0 ;
if ( api . listeners . size ( ) > 0 ) {
System . out . println ( " SubServers > Loading SubAPI Plugins... " ) ;
for ( NamedContainer < Runnable , Runnable > listener : api . listeners ) {
try {
if ( listener . name ( ) ! = null ) {
listener . name ( ) . run ( ) ;
plugins + + ;
}
} catch ( Throwable e ) {
2017-01-30 21:22:36 +01:00
new InvocationTargetException ( e , " Problem enabling plugin " ) . printStackTrace ( ) ;
2017-01-26 23:19:48 +01:00
}
}
}
2017-07-19 01:28:59 +02:00
System . out . println ( " SubServers > " + ( ( plugins > 0 ) ? plugins + " Plugin " + ( ( plugins = = 1 ) ? " " : " s " ) + " , " : " " ) + hosts + " Host " + ( ( hosts = = 1 ) ? " " : " s " ) + " , " + servers + " Server " + ( ( servers = = 1 ) ? " " : " s " ) + " , and " + subservers + " SubServer " + ( ( subservers = = 1 ) ? " " : " s " ) + " loaded in " + new DecimalFormat ( " 0.000 " ) . format ( ( Calendar . getInstance ( ) . getTime ( ) . getTime ( ) - begin ) / 1000D ) + " s " ) ;
2016-12-05 04:21:04 +01:00
super . startListeners ( ) ;
2017-08-18 11:58:06 +02:00
if ( ! posted ) {
post ( ) ;
posted = true ;
}
2016-12-05 04:21:04 +01:00
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
2017-08-18 11:58:06 +02:00
private void post ( ) {
new Metrics ( this ) ;
new Timer ( ) . schedule ( new TimerTask ( ) {
@Override
public void run ( ) {
try {
Document updxml = DocumentBuilderFactory . newInstance ( ) . newDocumentBuilder ( ) . parse ( new InputSource ( new StringReader ( Util . readAll ( new BufferedReader ( new InputStreamReader ( new URL ( " http://src.me1312.net/maven/net/ME1312/SubServers/SubServers.Bungee/maven-metadata.xml " ) . openStream ( ) , Charset . forName ( " UTF-8 " ) ) ) ) ) ) ) ;
NodeList updnodeList = updxml . getElementsByTagName ( " version " ) ;
Version updversion = version ;
int updcount = - 1 ;
for ( int i = 0 ; i < updnodeList . getLength ( ) ; i + + ) {
Node node = updnodeList . item ( i ) ;
if ( node . getNodeType ( ) = = Node . ELEMENT_NODE ) {
if ( ! node . getTextContent ( ) . startsWith ( " - " ) & & new Version ( node . getTextContent ( ) ) . compareTo ( updversion ) > = 0 ) {
updversion = new Version ( node . getTextContent ( ) ) ;
updcount + + ;
}
}
}
if ( ! updversion . equals ( version ) ) System . out . println ( " SubServers > SubServers.Bungee v " + updversion + " is available. You are " + updcount + " version " + ( ( updcount = = 1 ) ? " " : " s " ) + " behind. " ) ;
} catch ( Exception e ) { }
}
} , 0 , TimeUnit . DAYS . toMillis ( 2 ) ) ;
}
2016-12-15 22:04:39 +01:00
private void loop ( ) {
new Thread ( ( ) - > {
2017-04-16 19:02:14 +02:00
while ( subdata ! = null ) {
2016-12-15 22:04:39 +01:00
try {
subdata . addClient ( subdata . getServer ( ) . accept ( ) ) ;
} catch ( IOException e ) {
2016-12-24 05:55:17 +01:00
if ( ! ( e instanceof SocketException ) ) e . printStackTrace ( ) ;
2016-12-05 04:21:04 +01:00
}
}
2016-12-15 22:04:39 +01:00
} ) . start ( ) ;
2016-12-05 04:21:04 +01:00
}
/ * *
2016-12-15 22:04:39 +01:00
* Emulate BungeeCord ' s getServers ( )
2016-12-05 04:21:04 +01:00
*
* @see SubAPI # getServers ( )
* @return Server Map
* /
@Override
public Map < String , ServerInfo > getServers ( ) {
2017-04-16 19:02:14 +02:00
HashMap < String , ServerInfo > servers = new HashMap < String , ServerInfo > ( ) ;
2017-01-08 03:30:03 +01:00
if ( ! running ) {
2017-07-21 22:45:41 +02:00
servers . putAll ( super . getServers ( ) ) ;
2017-08-17 01:29:02 +02:00
servers . putAll ( legServers ) ;
2017-01-08 03:30:03 +01:00
} else {
for ( ServerInfo server : exServers . values ( ) ) servers . put ( server . getName ( ) , server ) ;
for ( Host host : this . hosts . values ( ) ) {
for ( ServerInfo server : host . getSubServers ( ) . values ( ) ) servers . put ( server . getName ( ) , server ) ;
}
2016-12-05 04:21:04 +01:00
}
2017-04-16 19:02:14 +02:00
return servers ;
2016-12-05 04:21:04 +01:00
}
/ * *
* Reset all changes made by startListeners
*
* @see SubPlugin # startListeners ( )
* /
@Override
public void stopListeners ( ) {
try {
2017-04-16 19:02:14 +02:00
legServers . clear ( ) ;
legServers . putAll ( getServers ( ) ) ;
2017-01-26 23:19:48 +01:00
if ( api . listeners . size ( ) > 0 ) {
System . out . println ( " SubServers > Resetting SubAPI Plugins... " ) ;
for ( NamedContainer < Runnable , Runnable > listener : api . listeners ) {
try {
if ( listener . get ( ) ! = null ) listener . get ( ) . run ( ) ;
} catch ( Throwable e ) {
2017-01-30 21:22:36 +01:00
new InvocationTargetException ( e , " Problem disabling plugin " ) . printStackTrace ( ) ;
2017-01-26 23:19:48 +01:00
}
}
}
2017-01-08 03:30:03 +01:00
running = false ;
2016-12-05 04:21:04 +01:00
System . out . println ( " SubServers > Resetting Hosts and Server Data " ) ;
List < String > hosts = new ArrayList < String > ( ) ;
hosts . addAll ( this . hosts . keySet ( ) ) ;
for ( String host : hosts ) {
List < String > subservers = new ArrayList < String > ( ) ;
subservers . addAll ( this . hosts . get ( host ) . getSubServers ( ) . keySet ( ) ) ;
for ( String server : subservers ) {
this . hosts . get ( host ) . removeSubServer ( server ) ;
}
subservers . clear ( ) ;
2017-07-23 06:32:57 +02:00
this . hosts . get ( host ) . getCreator ( ) . terminate ( ) ;
this . hosts . get ( host ) . getCreator ( ) . waitFor ( ) ;
2016-12-05 04:21:04 +01:00
this . hosts . remove ( host ) ;
}
hosts . clear ( ) ;
exServers . clear ( ) ;
subdata . destroy ( ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
super . stopListeners ( ) ;
}
2017-08-15 11:58:48 +02:00
2017-08-18 11:58:06 +02:00
@EventHandler ( priority = Byte . MAX_VALUE )
public void reroute ( ServerConnectEvent e ) {
Map < String , ServerInfo > servers = new TreeMap < String , ServerInfo > ( api . getServers ( ) ) ;
if ( servers . keySet ( ) . contains ( e . getTarget ( ) . getName ( ) . toLowerCase ( ) ) & & e . getTarget ( ) ! = servers . get ( e . getTarget ( ) . getName ( ) . toLowerCase ( ) ) ) {
e . setTarget ( servers . get ( e . getTarget ( ) . getName ( ) . toLowerCase ( ) ) ) ;
} else {
servers = getServers ( ) ;
if ( servers . keySet ( ) . contains ( e . getTarget ( ) . getName ( ) ) & & e . getTarget ( ) ! = servers . get ( e . getTarget ( ) . getName ( ) ) ) {
e . setTarget ( servers . get ( e . getTarget ( ) . getName ( ) ) ) ;
}
}
}
@EventHandler ( priority = Byte . MIN_VALUE )
2017-08-15 11:58:48 +02:00
public void resetSudo ( SubStoppedEvent e ) {
if ( sudo = = e . getServer ( ) ) {
sudo = null ;
System . out . println ( " SubServers > Reverting to the BungeeCord Console " ) ;
}
}
2016-12-05 04:21:04 +01:00
}