2016-12-20 00:31:01 +01:00
package net.ME1312.SubServers.Client.Bukkit ;
2019-04-18 16:02:09 +02:00
import net.ME1312.Galaxi.Library.Map.ObjectMap ;
import net.ME1312.SubData.Client.Encryption.AES ;
import net.ME1312.SubData.Client.Encryption.RSA ;
import net.ME1312.SubData.Client.SubDataProtocol ;
import net.ME1312.SubServers.Client.Bukkit.Event.SubNetworkDisconnectEvent ;
2018-08-12 23:23:21 +02:00
import net.ME1312.SubServers.Client.Bukkit.Graphic.DefaultUIHandler ;
2017-01-21 17:49:37 +01:00
import net.ME1312.SubServers.Client.Bukkit.Graphic.UIHandler ;
2019-04-18 16:02:09 +02:00
import net.ME1312.Galaxi.Library.Config.YAMLConfig ;
import net.ME1312.Galaxi.Library.Config.YAMLSection ;
2018-03-21 21:45:59 +01:00
import net.ME1312.SubServers.Client.Bukkit.Library.Metrics ;
2019-04-18 16:02:09 +02:00
import net.ME1312.Galaxi.Library.NamedContainer ;
import net.ME1312.Galaxi.Library.UniversalFile ;
import net.ME1312.Galaxi.Library.Util ;
import net.ME1312.Galaxi.Library.Version.Version ;
import net.ME1312.SubData.Client.SubDataClient ;
import net.ME1312.SubServers.Client.Bukkit.Network.Packet.PacketLinkServer ;
import net.ME1312.SubServers.Client.Bukkit.Network.SubProtocol ;
2016-12-20 00:31:01 +01:00
import org.bukkit.Bukkit ;
import org.bukkit.plugin.java.JavaPlugin ;
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 ;
2019-04-18 16:02:09 +02:00
public SubDataProtocol subprotocol ;
2016-12-20 00:31:01 +01:00
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 ( ) ) ;
2018-10-24 07:37:55 +02:00
Bukkit . getLogger ( ) . info ( " SubServers > Created ~/plugins/SubServers-Client-Bukkit/config.yml " ) ;
2019-04-18 16:02:09 +02:00
} else if ( ( ( new YAMLConfig ( new UniversalFile ( getDataFolder ( ) , " config.yml " ) ) ) . get ( ) . getMap ( " Settings " ) . getVersion ( " Version " , new 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 ( ) ) ;
2018-10-24 07:37:55 +02:00
Bukkit . getLogger ( ) . info ( " SubServers > Updated ~/plugins/SubServers-Client-Bukkit/config.yml " ) ;
2016-12-20 00:31:01 +01:00
}
2017-01-26 23:19:48 +01:00
config = new YAMLConfig ( new UniversalFile ( getDataFolder ( ) , " config.yml " ) ) ;
2019-04-18 16:02:09 +02:00
if ( new UniversalFile ( new File ( System . getProperty ( " user.dir " ) ) , " subdata.json " ) . exists ( ) ) {
FileReader reader = new FileReader ( new UniversalFile ( new File ( System . getProperty ( " user.dir " ) ) , " subdata.json " ) ) ;
config . get ( ) . getMap ( " Settings " ) . set ( " SubData " , new YAMLSection ( parseJSON ( Util . readAll ( reader ) ) ) ) ;
2017-05-29 07:00:02 +02:00
config . save ( ) ;
2018-01-29 07:04:36 +01:00
reader . close ( ) ;
2019-04-18 16:02:09 +02:00
new UniversalFile ( new File ( System . getProperty ( " user.dir " ) ) , " subdata.json " ) . delete ( ) ;
}
if ( new UniversalFile ( new File ( System . getProperty ( " user.dir " ) ) , " subdata.rsa.key " ) . exists ( ) ) {
Files . move ( new UniversalFile ( new File ( System . getProperty ( " user.dir " ) ) , " subdata.rsa.key " ) . toPath ( ) , new UniversalFile ( getDataFolder ( ) , " subdata.rsa.key " ) . toPath ( ) ) ;
2017-05-29 07:00:02 +02:00
}
2017-12-14 01:04:58 +01:00
2019-04-18 16:02:09 +02:00
subprotocol = SubProtocol . get ( ) ;
2017-12-14 01:04:58 +01:00
reload ( false ) ;
2016-12-20 00:31:01 +01:00
2019-04-18 16:02:09 +02:00
if ( config . get ( ) . getMap ( " Settings " ) . getBoolean ( " Ingame-Access " , true ) ) {
gui = new DefaultUIHandler ( this ) ;
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-08-04 02:43:02 +02:00
YAMLSection tags = new YAMLSection ( parseJSON ( " { \" 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 " ) ) ) ) + '}' ) ) ;
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 ;
2019-04-18 16:02:09 +02:00
for ( ObjectMap < String > tag : tags . getMapList ( " tags " ) ) versions . add ( Version . fromString ( tag . getString ( " ref " ) . substring ( 10 ) ) ) ;
2018-07-29 20:39:42 +02:00
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 ) ;
2018-08-04 02:43:02 +02:00
} catch ( Exception e ) {
2016-12-20 00:31:01 +01:00
e . printStackTrace ( ) ;
}
}
2017-12-14 01:04:58 +01:00
public void reload ( boolean notifyPlugins ) throws IOException {
if ( subdata ! = null )
2019-04-18 16:02:09 +02:00
subdata . close ( ) ;
2017-12-14 01:04:58 +01:00
config . reload ( ) ;
2019-04-18 16:02:09 +02:00
subprotocol . unregisterCipher ( " AES " ) ;
subprotocol . unregisterCipher ( " AES-128 " ) ;
subprotocol . unregisterCipher ( " AES-192 " ) ;
subprotocol . unregisterCipher ( " AES-256 " ) ;
subprotocol . unregisterCipher ( " RSA " ) ;
api . name = config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getString ( " Name " , null ) ;
if ( config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Password " , " " ) . length ( ) > 0 ) {
subprotocol . registerCipher ( " AES " , new AES ( 128 , config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Password " ) ) ) ;
subprotocol . registerCipher ( " AES-128 " , new AES ( 128 , config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Password " ) ) ) ;
subprotocol . registerCipher ( " AES-192 " , new AES ( 192 , config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Password " ) ) ) ;
subprotocol . registerCipher ( " AES-256 " , new AES ( 256 , config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Password " ) ) ) ;
System . out . println ( " SubData > AES Encryption Available " ) ;
}
if ( new UniversalFile ( getDataFolder ( ) , " subdata.rsa.key " ) . exists ( ) ) {
try {
subprotocol . registerCipher ( " RSA " , new RSA ( new UniversalFile ( getDataFolder ( ) , " subdata.rsa.key " ) ) ) ;
System . out . println ( " SubData > RSA Encryption Available " ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
2018-01-05 21:37:23 +01:00
}
2017-12-14 01:04:58 +01:00
}
2019-04-18 16:02:09 +02:00
System . out . println ( " SubData > " ) ;
subdata = subprotocol . open ( ( config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 0 ] . equals ( " 0.0.0.0 " ) ) ? null : InetAddress . getByName ( config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 0 ] ) ,
Integer . parseInt ( config . get ( ) . getMap ( " Settings " ) . getMap ( " SubData " ) . getRawString ( " Address " , " 127.0.0.1:4391 " ) . split ( " : " ) [ 1 ] ) ) ;
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 {
2019-04-18 16:02:09 +02:00
subdata . close ( ) ;
2017-01-21 17:49:37 +01:00
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
2016-12-20 00:31:01 +01:00
}
2018-08-04 02:43:02 +02:00
/ * *
* Use reflection to access Gson for parsing
*
* @param json JSON to parse
* @return JSON as a map
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws InstantiationException
* @throws ClassNotFoundException
* /
@SuppressWarnings ( " unchecked " )
public Map < String , ? > parseJSON ( String json ) throws NoSuchMethodException , IllegalAccessException , InvocationTargetException , InstantiationException , ClassNotFoundException {
Class < ? > gson = Class . forName ( ( ( Util . getDespiteException ( ( ) - > Class . forName ( " com.google.gson.Gson " ) ! = null , false ) ? " " : " org.bukkit.craftbukkit.libs. " ) ) + " com.google.gson.Gson " ) ;
//Class<?> gson = com.google.gson.Gson.class;
return ( Map < String , ? > ) gson . getMethod ( " fromJson " , String . class , Class . class ) . invoke ( gson . newInstance ( ) , json , Map . class ) ;
}
2016-12-20 00:31:01 +01:00
}