2016-12-20 00:31:01 +01:00
package net.ME1312.SubServers.Client.Bukkit ;
2017-07-19 01:28:59 +02:00
import net.ME1312.SubServers.Client.Bukkit.Graphic.InternalUIHandler ;
2017-01-21 17:49:37 +01:00
import net.ME1312.SubServers.Client.Bukkit.Graphic.UIHandler ;
2016-12-20 00:31:01 +01:00
import net.ME1312.SubServers.Client.Bukkit.Library.Config.YAMLConfig ;
import net.ME1312.SubServers.Client.Bukkit.Library.Config.YAMLSection ;
import net.ME1312.SubServers.Client.Bukkit.Library.UniversalFile ;
2016-12-27 17:03:19 +01:00
import net.ME1312.SubServers.Client.Bukkit.Library.Util ;
2016-12-20 00:31:01 +01:00
import net.ME1312.SubServers.Client.Bukkit.Library.Version.Version ;
import net.ME1312.SubServers.Client.Bukkit.Network.SubDataClient ;
import org.bukkit.Bukkit ;
import org.bukkit.plugin.java.JavaPlugin ;
2017-05-29 07:00:02 +02:00
import org.json.JSONObject ;
2017-11-22 22:58:33 +01:00
import org.w3c.dom.Document ;
import org.w3c.dom.Node ;
import org.w3c.dom.NodeList ;
import org.xml.sax.InputSource ;
2016-12-20 00:31:01 +01:00
2017-11-22 22:58:33 +01:00
import javax.xml.parsers.DocumentBuilderFactory ;
2016-12-20 00:31:01 +01:00
import java.io.* ;
import java.net.InetAddress ;
2017-11-22 22:58:33 +01:00
import java.net.URL ;
import java.nio.charset.Charset ;
2016-12-20 00:31:01 +01:00
import java.nio.file.Files ;
2017-05-25 21:55:47 +02:00
import java.nio.file.StandardCopyOption ;
2017-11-22 22:58:33 +01:00
import java.util.concurrent.TimeUnit ;
2016-12-20 00:31:01 +01:00
2017-01-08 20:42:40 +01:00
/ * *
* SubServers Client Plugin Class
* /
2016-12-20 00:31:01 +01:00
public final class SubPlugin extends JavaPlugin {
2017-01-26 23:19:48 +01:00
public YAMLConfig config ;
2016-12-20 00:31:01 +01:00
public YAMLSection lang = null ;
public SubDataClient subdata = null ;
2017-01-21 17:49:37 +01:00
public UIHandler gui = null ;
2017-01-08 20:42:40 +01:00
public final Version version ;
2017-12-07 23:51:06 +01:00
public final Version bversion = new Version ( 1 ) ;
2017-01-21 17:49:37 +01:00
public final SubAPI api = new SubAPI ( this ) ;
2016-12-20 00:31:01 +01:00
2017-01-08 20:42:40 +01:00
public SubPlugin ( ) {
super ( ) ;
version = new Version ( getDescription ( ) . getVersion ( ) ) ;
}
/ * *
* Enable Plugin
* /
2016-12-20 00:31:01 +01:00
@Override
public void onEnable ( ) {
try {
2017-08-22 15:02:23 +02:00
Bukkit . getLogger ( ) . info ( " SubServers > Loading SubServers.Client.Bukkit v " + version . toString ( ) + " Libraries... " ) ;
2016-12-20 00:31:01 +01:00
getDataFolder ( ) . mkdirs ( ) ;
2017-05-25 21:55:47 +02:00
if ( new UniversalFile ( getDataFolder ( ) . getParentFile ( ) , " SubServers-Client:config.yml " ) . exists ( ) ) {
Files . move ( new UniversalFile ( getDataFolder ( ) . getParentFile ( ) , " SubServers-Client:config.yml " ) . toPath ( ) , new UniversalFile ( getDataFolder ( ) , " config.yml " ) . toPath ( ) , StandardCopyOption . REPLACE_EXISTING ) ;
Util . deleteDirectory ( new UniversalFile ( getDataFolder ( ) . getParentFile ( ) , " SubServers-Client " ) ) ;
}
2016-12-20 00:31:01 +01:00
if ( ! ( new UniversalFile ( getDataFolder ( ) , " config.yml " ) . exists ( ) ) ) {
2016-12-27 17:03:19 +01:00
Util . copyFromJar ( SubPlugin . class . getClassLoader ( ) , " config.yml " , new UniversalFile ( getDataFolder ( ) , " config.yml " ) . getPath ( ) ) ;
2016-12-20 00:31:01 +01:00
Bukkit . getLogger ( ) . info ( " SubServers > Created ~/plugins/SubServers/config.yml " ) ;
2016-12-28 01:15:36 +01:00
} else if ( ( new Version ( ( new YAMLConfig ( new UniversalFile ( getDataFolder ( ) , " config.yml " ) ) ) . get ( ) . getSection ( " Settings " ) . getString ( " Version " , " 0 " ) ) . compareTo ( new Version ( " 2.11.2a+ " ) ) ) ! = 0 ) {
2016-12-20 00:31:01 +01:00
Files . move ( new UniversalFile ( getDataFolder ( ) , " config.yml " ) . toPath ( ) , new UniversalFile ( getDataFolder ( ) , " config.old " + Math . round ( Math . random ( ) * 100000 ) + " .yml " ) . toPath ( ) ) ;
2016-12-27 17:03:19 +01:00
Util . copyFromJar ( SubPlugin . class . getClassLoader ( ) , " config.yml " , new UniversalFile ( getDataFolder ( ) , " config.yml " ) . getPath ( ) ) ;
2016-12-20 00:31:01 +01:00
Bukkit . getLogger ( ) . info ( " SubServers > Updated ~/plugins/SubServers/config.yml " ) ;
}
2017-01-26 23:19:48 +01:00
config = new YAMLConfig ( new UniversalFile ( getDataFolder ( ) , " config.yml " ) ) ;
2017-05-29 07:00:02 +02:00
if ( new UniversalFile ( new File ( System . getProperty ( " user.dir " ) ) , " subservers.client " ) . exists ( ) ) {
config . get ( ) . getSection ( " Settings " ) . set ( " SubData " , new JSONObject ( Util . readAll ( new FileReader ( new UniversalFile ( new File ( System . getProperty ( " user.dir " ) ) , " subservers.client " ) ) ) ) ) ;
config . save ( ) ;
new UniversalFile ( new File ( System . getProperty ( " user.dir " ) ) , " subservers.client " ) . delete ( ) ;
}
2017-06-30 15:36:16 +02:00
SubDataClient . Encryption encryption = SubDataClient . 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 ( ( ) - > SubDataClient . Encryption . valueOf ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Encryption " , " NONE " ) . replace ( '-' , '_' ) . replace ( ' ' , '_' ) . toUpperCase ( ) ) ) ) {
System . out . println ( " SubData > Unknown encryption type: " + SubDataClient . Encryption . valueOf ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Encryption " , " None " ) ) ) ;
} else {
encryption = SubDataClient . Encryption . valueOf ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Encryption " , " NONE " ) . replace ( '-' , '_' ) . replace ( ' ' , '_' ) . toUpperCase ( ) ) ;
}
2017-01-26 23:19:48 +01:00
subdata = new SubDataClient ( this , config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getString ( " Name " , " undefined " ) ,
InetAddress . getByName ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 0 ] ) ,
2017-06-30 15:36:16 +02:00
Integer . parseInt ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 1 ] ) , encryption ) ;
2016-12-20 00:31:01 +01:00
2017-01-26 23:19:48 +01:00
if ( config . get ( ) . getSection ( " Settings " ) . getBoolean ( " Ingame-Access " , true ) ) {
2017-07-19 01:28:59 +02:00
gui = new InternalUIHandler ( this ) ;
2017-01-26 23:19:48 +01:00
SubCommand cmd = new SubCommand ( this ) ;
getCommand ( " subservers " ) . setExecutor ( cmd ) ;
getCommand ( " subserver " ) . setExecutor ( cmd ) ;
getCommand ( " sub " ) . setExecutor ( cmd ) ;
}
2017-11-22 22:58:33 +01:00
Bukkit . getScheduler ( ) . runTaskTimerAsynchronously ( this , ( ) - > {
try {
2017-12-06 23:34:47 +01:00
Document updxml = DocumentBuilderFactory . newInstance ( ) . newDocumentBuilder ( ) . parse ( new InputSource ( new StringReader ( Util . readAll ( new BufferedReader ( new InputStreamReader ( new URL ( " https://src.me1312.net/maven/net/ME1312/SubServers/SubServers.Client.Bukkit/maven-metadata.xml " ) . openStream ( ) , Charset . forName ( " UTF-8 " ) ) ) ) ) ) ) ;
2017-11-22 22:58:33 +01:00
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.Client.Bukkit v " + updversion + " is available. You are " + updcount + " version " + ( ( updcount = = 1 ) ? " " : " s " ) + " behind. " ) ;
} catch ( Exception e ) { }
} , 0 , TimeUnit . DAYS . toSeconds ( 2 ) * 20 ) ;
2016-12-20 00:31:01 +01:00
} catch ( IOException e ) {
setEnabled ( false ) ;
e . printStackTrace ( ) ;
}
}
2017-01-08 20:42:40 +01:00
/ * *
* Disable Plugin
* /
2016-12-20 00:31:01 +01:00
@Override
public void onDisable ( ) {
2017-01-21 17:49:37 +01:00
if ( subdata ! = null ) try {
2017-07-19 01:28:59 +02:00
subdata . destroy ( 0 ) ;
2017-01-21 17:49:37 +01:00
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
2017-05-29 07:00:02 +02:00
setEnabled ( false ) ;
2016-12-20 00:31:01 +01:00
}
}