2016-12-20 00:31:01 +01:00
package net.ME1312.SubServers.Client.Bukkit ;
2018-04-15 03:53:51 +02:00
import com.google.gson.Gson ;
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 ;
2018-04-15 03:53:51 +02:00
import net.ME1312.SubServers.Client.Bukkit.Library.Config.YAMLSection ;
2018-03-21 21:45:59 +01:00
import net.ME1312.SubServers.Client.Bukkit.Library.Metrics ;
2018-01-02 23:29:25 +01:00
import net.ME1312.SubServers.Client.Bukkit.Library.NamedContainer ;
2016-12-20 00:31:01 +01:00
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 ;
2018-03-21 21:45:59 +01:00
import net.ME1312.SubServers.Client.Bukkit.Library.Version.VersionType ;
2018-01-05 21:37:23 +01:00
import net.ME1312.SubServers.Client.Bukkit.Network.Cipher ;
2016-12-20 00:31:01 +01:00
import net.ME1312.SubServers.Client.Bukkit.Network.SubDataClient ;
import org.bukkit.Bukkit ;
import org.bukkit.plugin.java.JavaPlugin ;
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.* ;
2017-12-14 01:04:58 +01:00
import java.lang.reflect.InvocationTargetException ;
2016-12-20 00:31:01 +01:00
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 ;
2018-07-29 20:39:42 +02:00
import java.util.Collections ;
import java.util.LinkedList ;
2017-12-14 01:04:58 +01:00
import java.util.List ;
2018-01-02 23:29:25 +01:00
import java.util.Map ;
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 {
2018-01-02 23:29:25 +01:00
protected NamedContainer < Long , Map < String , Map < String , String > > > lang = null ;
2017-01-26 23:19:48 +01:00
public YAMLConfig config ;
2016-12-20 00:31:01 +01:00
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-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 ( ) ;
2018-07-29 20:39:42 +02:00
version = Version . fromString ( getDescription ( ) . getVersion ( ) ) ;
2017-01-08 20:42:40 +01:00
}
/ * *
* Enable Plugin
* /
2016-12-20 00:31:01 +01:00
@Override
2018-04-15 03:53:51 +02:00
@SuppressWarnings ( " unchecked " )
2016-12-20 00:31:01 +01:00
public void onEnable ( ) {
try {
2017-12-13 18:29:50 +01:00
Bukkit . getLogger ( ) . info ( " SubServers > Loading SubServers.Client.Bukkit v " + version . toString ( ) + " Libraries (for Minecraft " + api . getGameVersion ( ) + " ) " ) ;
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 ( ) ) {
2018-01-29 07:04:36 +01:00
FileReader reader = new FileReader ( new UniversalFile ( new File ( System . getProperty ( " user.dir " ) ) , " subservers.client " ) ) ;
2018-04-15 03:53:51 +02:00
config . get ( ) . getSection ( " Settings " ) . set ( " SubData " , new YAMLSection ( new Gson ( ) . fromJson ( Util . readAll ( reader ) , Map . class ) ) ) ;
2017-05-29 07:00:02 +02:00
config . save ( ) ;
2018-01-29 07:04:36 +01:00
reader . close ( ) ;
2017-05-29 07:00:02 +02:00
new UniversalFile ( new File ( System . getProperty ( " user.dir " ) ) , " subservers.client " ) . delete ( ) ;
}
2017-12-14 01:04:58 +01:00
reload ( false ) ;
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
2018-03-21 21:45:59 +01:00
new Metrics ( this ) ;
2017-11-22 22:58:33 +01:00
Bukkit . getScheduler ( ) . runTaskTimerAsynchronously ( this , ( ) - > {
try {
2018-07-27 21:36:51 +02:00
YAMLSection tags = new YAMLSection ( new Gson ( ) . fromJson ( " { \" tags \" : " + Util . readAll ( new BufferedReader ( new InputStreamReader ( new URL ( " https://api.github.com/repos/ME1312/SubServers-2/git/refs/tags " ) . openStream ( ) , Charset . forName ( " UTF-8 " ) ) ) ) + '}' , Map . class ) ) ;
2018-07-29 20:39:42 +02:00
List < Version > versions = new LinkedList < Version > ( ) ;
2017-11-22 22:58:33 +01:00
Version updversion = version ;
2018-03-14 22:50:15 +01:00
int updcount = 0 ;
2018-07-29 20:39:42 +02:00
for ( YAMLSection tag : tags . getSectionList ( " tags " ) ) versions . add ( Version . fromString ( tag . getString ( " ref " ) . substring ( 10 ) ) ) ;
Collections . sort ( versions ) ;
for ( Version version : versions ) {
2018-07-29 05:02:47 +02:00
if ( version . compareTo ( updversion ) > 0 ) {
2018-07-27 21:36:51 +02:00
updversion = version ;
updcount + + ;
2017-11-22 22:58:33 +01:00
}
}
2018-07-23 05:44:20 +02:00
if ( updcount > 0 ) Bukkit . getLogger ( ) . info ( " SubServers > SubServers.Client.Bukkit v " + updversion + " is available. You are " + updcount + " version " + ( ( updcount = = 1 ) ? " " : " s " ) + " behind. " ) ;
2017-11-22 22:58:33 +01:00
} catch ( Exception e ) { }
} , 0 , TimeUnit . DAYS . toSeconds ( 2 ) * 20 ) ;
2016-12-20 00:31:01 +01:00
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
2017-12-14 01:04:58 +01:00
public void reload ( boolean notifyPlugins ) throws IOException {
if ( subdata ! = null )
subdata . destroy ( 0 ) ;
config . reload ( ) ;
2018-01-05 21:37:23 +01:00
Cipher cipher = null ;
if ( ! config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Encryption " , " NONE " ) . equalsIgnoreCase ( " NONE " ) ) {
if ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getString ( " Password " , " " ) . length ( ) = = 0 ) {
2018-07-23 05:44:20 +02:00
Bukkit . getLogger ( ) . info ( " SubData > Cannot encrypt connection without a password " ) ;
2018-01-12 22:56:22 +01:00
} else if ( ! SubDataClient . getCiphers ( ) . keySet ( ) . contains ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Encryption " ) . toUpperCase ( ) . replace ( '-' , '_' ) . replace ( ' ' , '_' ) ) ) {
2018-07-23 05:44:20 +02:00
Bukkit . getLogger ( ) . info ( " SubData > Unknown encryption type: " + config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Encryption " ) ) ;
2018-01-05 21:37:23 +01:00
} else {
cipher = SubDataClient . getCipher ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Encryption " ) ) ;
}
2017-12-14 01:04:58 +01:00
}
2018-01-29 07:04:36 +01:00
subdata = new SubDataClient ( this , config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getString ( " Name " , null ) ,
2017-12-14 01:04:58 +01:00
InetAddress . getByName ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 0 ] ) ,
2018-01-05 21:37:23 +01:00
Integer . parseInt ( config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 1 ] ) , cipher ) ;
2017-12-14 01:04:58 +01:00
if ( notifyPlugins ) {
List < Runnable > listeners = api . reloadListeners ;
if ( listeners . size ( ) > 0 ) {
for ( Object obj : listeners ) {
try {
( ( Runnable ) obj ) . run ( ) ;
} catch ( Throwable e ) {
new InvocationTargetException ( e , " Problem reloading plugin " ) . 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
}
}