2016-12-24 05:55:17 +01:00
package net.ME1312.SubServers.Bungee ;
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 ;
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 ;
import net.md_5.bungee.api.config.ServerInfo ;
import java.io.* ;
import java.net.InetAddress ;
import java.net.InetSocketAddress ;
2016-12-24 05:55:17 +01:00
import java.net.SocketException ;
2016-12-05 04:21:04 +01:00
import java.nio.file.Files ;
import java.util.* ;
/ * *
* Main Plugin Class
*
* @author ME1312
* /
public final class SubPlugin extends BungeeCord {
protected final HashMap < String , Class < ? extends Host > > hostDrivers = new HashMap < String , Class < ? extends Host > > ( ) ;
public final HashMap < String , Server > exServers = new HashMap < String , Server > ( ) ;
public final HashMap < String , Host > hosts = new HashMap < String , Host > ( ) ;
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 ;
2016-12-24 05:55:17 +01:00
public final Version version = new Version ( " 2.11.2a " ) ;
protected Version bversion = new Version ( 4 ) ;
2016-12-05 04:21:04 +01:00
protected boolean running = false ;
public final SubAPI api = new SubAPI ( this ) ;
protected SubPlugin ( ) throws IOException {
enable ( ) ;
}
/ * *
* Enable Plugin
*
* @throws IOException
* /
protected void enable ( ) throws IOException {
if ( running ) throw new IllegalStateException ( " SubServers has already been loaded " ) ;
2016-12-19 01:38:02 +01:00
System . out . println ( " SubServers > Loading SubServers v " + version . toString ( ) + " Libraries... " ) ;
2016-12-05 04:21:04 +01:00
running = true ;
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 " ) ;
}
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 " ) ;
2016-12-24 05:55:17 +01:00
} else if ( ( new Version ( ( new YAMLConfig ( new UniversalFile ( dir , " lang.yml " ) ) ) . get ( ) . getString ( " Version " , " 0 " ) ) . compareTo ( new Version ( " 2.11.2a+ " ) ) ) ! = 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 " ) ;
}
if ( ! ( new UniversalFile ( dir , " build.sh " ) . exists ( ) ) ) {
2016-12-24 05:55:17 +01:00
Util . copyFromJar ( SubPlugin . class . getClassLoader ( ) , " net/ME1312/SubServers/Bungee/Library/Files/build.sh " , new UniversalFile ( dir , " build.sh " ) . getPath ( ) ) ;
2016-12-05 04:21:04 +01:00
System . out . println ( " SubServers > Created ~/SubServers/build.sh " ) ;
} else {
String Version = " null " ;
BufferedReader brText = new BufferedReader ( new FileReader ( new UniversalFile ( dir , " build.sh " ) ) ) ;
try {
Version = brText . readLine ( ) . split ( " Version: " ) [ 1 ] ;
} catch ( NullPointerException e ) { }
brText . close ( ) ;
if ( ! Version . equalsIgnoreCase ( " 2.11.0a+ " ) ) {
Files . move ( new UniversalFile ( dir , " build.sh " ) . toPath ( ) , new UniversalFile ( dir , " build.old " + Math . round ( Math . random ( ) * 100000 ) + " .sh " ) . toPath ( ) ) ;
2016-12-24 05:55:17 +01:00
Util . copyFromJar ( SubPlugin . class . getClassLoader ( ) , " net/ME1312/SubServers/Bungee/Library/Files/build.sh " , new UniversalFile ( dir , " build.sh " ) . getPath ( ) ) ;
2016-12-05 04:21:04 +01:00
System . out . println ( " SubServers > Updated ~/SubServers/build.sh " ) ;
}
}
2016-12-24 05:55:17 +01:00
hostDrivers . put ( " built-in " , net . ME1312 . SubServers . Bungee . Host . Internal . InternalHost . class ) ;
2016-12-05 04:21:04 +01:00
2016-12-19 01:38:02 +01:00
System . out . println ( " SubServers > Loading BungeeCord Libraries... " ) ;
2016-12-05 04:21:04 +01:00
}
/ * *
* Load Hosts , Servers , SubServers , and SubData .
* /
@Override
public void startListeners ( ) {
try {
config = new YAMLConfig ( new UniversalFile ( dir , " SubServers:config.yml " ) ) ;
lang = new YAMLConfig ( new UniversalFile ( dir , " SubServers:lang.yml " ) ) ;
2016-12-15 22:04:39 +01:00
subdata = new SubDataServer ( this , Integer . parseInt ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 1 ] ) , 10 ,
InetAddress . getByName ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 0 ] ) ) ;
2016-12-05 04:21:04 +01:00
System . out . println ( " SubServers > SubData Listening on " + subdata . getServer ( ) . getLocalSocketAddress ( ) . toString ( ) ) ;
loop ( ) ;
long begin = Calendar . getInstance ( ) . getTime ( ) . getTime ( ) ;
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 ) ;
Host host = hostDrivers . get ( config . get ( ) . getSection ( " Hosts " ) . getSection ( name ) . getRawString ( " Driver " ) . toLowerCase ( ) ) . getConstructor ( SubPlugin . class , String . class , Boolean . class , InetAddress . class , String . class , String . class ) . newInstance (
this , name , ( Boolean ) 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 " ) ) ;
2016-12-05 04:21:04 +01:00
this . hosts . put ( name . toLowerCase ( ) , host ) ;
2016-12-15 22:04:39 +01:00
SubDataServer . allowConnection ( host . getAddress ( ) ) ;
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 {
2016-12-15 22:04:39 +01:00
Server server = new Server ( name , new InetSocketAddress ( 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 " ) ) ;
2016-12-05 04:21:04 +01:00
exServers . put ( name . toLowerCase ( ) , server ) ;
2016-12-15 22:04:39 +01:00
SubDataServer . allowConnection ( server . getAddress ( ) . getAddress ( ) ) ;
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 {
if ( ! this . hosts . keySet ( ) . contains ( config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getString ( " Host " ) . toLowerCase ( ) ) ) throw new InvalidServerException ( " There is no host with this name: " + name ) ;
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 " ) ,
2016-12-24 05:55:17 +01:00
config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getBoolean ( " Run-On-Launch " ) , config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getBoolean ( " Hidden " ) , config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getBoolean ( " Auto-Restart " ) , config . get ( ) . getSection ( " Servers " ) . getSection ( name ) . getBoolean ( " Restricted " ) , false ) ;
2016-12-05 04:21:04 +01:00
subservers + + ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
System . out . println ( " SubServers > " + hosts + " Host(s), " + servers + " Server(s), and " + subservers + " SubServer(s) loaded in " + ( Calendar . getInstance ( ) . getTime ( ) . getTime ( ) - begin ) + " ms " ) ;
2016-12-24 05:55:17 +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 " ) ) ;
2016-12-05 04:21:04 +01:00
super . startListeners ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
2016-12-15 22:04:39 +01:00
private void loop ( ) {
new Thread ( ( ) - > {
while ( running & & subdata ! = null ) {
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 ( ) {
2016-12-15 22:04:39 +01:00
HashMap < String , ServerInfo > servers = new HashMap < String , ServerInfo > ( ) ;
for ( ServerInfo server : exServers . values ( ) ) servers . put ( server . getName ( ) , server ) ;
2016-12-05 04:21:04 +01:00
for ( Host host : this . hosts . values ( ) ) {
2016-12-15 22:04:39 +01:00
for ( ServerInfo server : host . getSubServers ( ) . values ( ) ) servers . put ( server . getName ( ) , server ) ;
2016-12-05 04:21:04 +01:00
}
return servers ;
}
/ * *
* Reset all changes made by startListeners
*
* @see SubPlugin # startListeners ( )
* /
@Override
public void stopListeners ( ) {
try {
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 ( ) ;
this . hosts . remove ( host ) ;
}
hosts . clear ( ) ;
exServers . clear ( ) ;
subdata . destroy ( ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
super . stopListeners ( ) ;
}
/ * *
* Disable Plugin
* /
protected void disable ( ) {
if ( running ) {
running = false ;
}
}
/ * *
* Override BungeeCord Stop Functions
* /
@Override
public void stop ( ) {
disable ( ) ;
super . stop ( ) ;
}
/ * *
* Override BungeeCord Stop Functions
*
* @param reason Reason
* /
@Override
public void stop ( String reason ) {
disable ( ) ;
super . stop ( reason ) ;
}
}