2016-12-24 05:55:17 +01:00
package net.ME1312.SubServers.Bungee.Host.Internal ;
2016-12-05 04:21:04 +01:00
2016-12-24 05:55:17 +01:00
import net.ME1312.SubServers.Bungee.Event.SubCreateEvent ;
2017-01-11 04:23:29 +01:00
import net.ME1312.SubServers.Bungee.Host.* ;
2016-12-28 01:15:36 +01:00
import net.ME1312.SubServers.Bungee.Library.Config.YAMLConfig ;
2016-12-24 05:55:17 +01:00
import net.ME1312.SubServers.Bungee.Library.Config.YAMLSection ;
import net.ME1312.SubServers.Bungee.Library.Container ;
2016-12-29 08:29:22 +01:00
import net.ME1312.SubServers.Bungee.Library.Exception.InvalidServerException ;
2017-07-23 06:32:57 +02:00
import net.ME1312.SubServers.Bungee.Library.Exception.SubCreatorException ;
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 ;
2017-07-23 06:32:57 +02:00
import net.ME1312.SubServers.Bungee.SubAPI ;
2016-12-28 01:15:36 +01:00
import net.ME1312.SubServers.Bungee.SubPlugin ;
2017-05-29 07:00:02 +02:00
import org.json.JSONObject ;
2016-12-15 22:04:39 +01: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
2016-12-15 22:04:39 +01:00
import javax.xml.parsers.DocumentBuilderFactory ;
import java.io.* ;
import java.net.URL ;
import java.nio.charset.Charset ;
2017-05-29 07:00:02 +02:00
import java.util.* ;
2017-07-23 06:32:57 +02:00
import java.util.logging.Logger ;
2016-12-05 04:21:04 +01:00
2017-01-07 20:06:54 +01:00
/ * *
* Internal SubCreator Class
* /
2016-12-05 04:21:04 +01:00
public class InternalSubCreator extends SubCreator {
2017-05-29 07:00:02 +02:00
private HashMap < String , ServerTemplate > templates = new HashMap < String , ServerTemplate > ( ) ;
2016-12-15 22:04:39 +01:00
private InternalHost host ;
private String gitBash ;
2017-07-23 06:32:57 +02:00
private TreeMap < String , NamedContainer < Thread , NamedContainer < InternalSubLogger , Process > > > thread ;
2016-12-05 04:21:04 +01:00
2017-01-07 20:06:54 +01:00
/ * *
* Creates an Internal SubCreator
*
* @param host Host
* @param gitBash Git Bash
* /
public InternalSubCreator ( InternalHost host , String gitBash ) {
2017-01-30 21:22:36 +01:00
if ( Util . isNull ( host , gitBash ) ) throw new NullPointerException ( ) ;
2017-01-07 20:06:54 +01:00
this . host = host ;
2016-12-15 22:04:39 +01:00
this . gitBash = gitBash ;
2017-07-23 06:32:57 +02:00
this . thread = new TreeMap < String , NamedContainer < Thread , NamedContainer < InternalSubLogger , Process > > > ( ) ;
2017-01-07 20:06:54 +01:00
2017-05-29 07:00:02 +02:00
if ( new UniversalFile ( host . plugin . dir , " SubServers:Templates " ) . exists ( ) ) for ( File file : new UniversalFile ( host . plugin . dir , " SubServers:Templates " ) . listFiles ( ) ) {
2017-01-07 20:06:54 +01:00
try {
2017-05-29 07:00:02 +02:00
if ( file . isDirectory ( ) ) {
YAMLSection config = ( new UniversalFile ( file , " template.yml " ) . exists ( ) ) ? new YAMLConfig ( new UniversalFile ( file , " template.yml " ) ) . get ( ) . getSection ( " Template " , new YAMLSection ( ) ) : new YAMLSection ( ) ;
2017-05-30 21:38:51 +02:00
ServerTemplate template = new ServerTemplate ( file . getName ( ) , config . getBoolean ( " Enabled " , true ) , config . getRawString ( " Icon " , " ::NULL:: " ) , file , config . getSection ( " Build " , new YAMLSection ( ) ) , config . getSection ( " Settings " , new YAMLSection ( ) ) ) ;
2017-05-29 07:00:02 +02:00
templates . put ( file . getName ( ) . toLowerCase ( ) , template ) ;
if ( config . getKeys ( ) . contains ( " Display " ) ) template . setDisplayName ( config . getString ( " Display " ) ) ;
}
2017-01-07 20:06:54 +01:00
} catch ( Exception e ) {
2017-05-29 07:00:02 +02:00
System . out . println ( host . getName ( ) + " /Creator > Couldn't load template: " + file . getName ( ) ) ;
2017-01-07 20:06:54 +01:00
e . printStackTrace ( ) ;
}
2017-05-29 07:00:02 +02:00
}
}
2017-01-07 20:06:54 +01:00
2017-07-23 06:32:57 +02:00
private YAMLSection build ( NamedContainer < InternalSubLogger , Process > thread , File dir , String name , ServerTemplate template , Version version , List < ServerTemplate > history ) throws SubCreatorException {
YAMLSection server = new YAMLSection ( ) ;
boolean error = false ;
if ( history . contains ( template ) ) throw new IllegalStateException ( " Template Import loop detected " ) ;
history . add ( template ) ;
for ( String other : template . getBuildOptions ( ) . getStringList ( " Import " , new ArrayList < String > ( ) ) ) {
if ( templates . keySet ( ) . contains ( other . toLowerCase ( ) ) ) {
YAMLSection config = build ( thread , dir , other , templates . get ( other . toLowerCase ( ) ) , version , history ) ;
if ( config = = null ) {
throw new SubCreatorException ( ) ;
} else {
server . setAll ( config ) ;
}
}
}
server . setAll ( template . getConfigOptions ( ) ) ;
2017-05-29 07:00:02 +02:00
try {
2017-07-23 06:32:57 +02:00
System . out . println ( name + " /Creator > Loading Template: " + template . getDisplayName ( ) ) ;
2017-05-29 07:00:02 +02:00
Util . copyDirectory ( template . getDirectory ( ) , dir ) ;
2017-07-23 20:21:05 +02:00
if ( template . getType ( ) = = ServerType . VANILLA ) {
String patch = " Patch " ;
if ( version . compareTo ( new Version ( " 1.12 " ) ) > = 0 ) patch + = " -v2 " ;
version = new Version ( version . toString ( ) + " " + patch ) ;
} else if ( template . getType ( ) = = ServerType . SPONGE ) {
2017-07-23 06:32:57 +02:00
System . out . println ( name + " /Creator > Searching Versions... " ) ;
2017-01-07 20:06:54 +01:00
Document spongexml = DocumentBuilderFactory . newInstance ( ) . newDocumentBuilder ( ) . parse ( new InputSource ( new StringReader ( Util . readAll ( new BufferedReader ( new InputStreamReader ( new URL ( " http://files.minecraftforge.net/maven/org/spongepowered/spongeforge/maven-metadata.xml " ) . openStream ( ) , Charset . forName ( " UTF-8 " ) ) ) ) ) ) ) ;
Document forgexml = DocumentBuilderFactory . newInstance ( ) . newDocumentBuilder ( ) . parse ( new InputSource ( new StringReader ( Util . readAll ( new BufferedReader ( new InputStreamReader ( new URL ( " http://files.minecraftforge.net/maven/net/minecraftforge/forge/maven-metadata.xml " ) . openStream ( ) , Charset . forName ( " UTF-8 " ) ) ) ) ) ) ) ;
NodeList spnodeList = spongexml . getElementsByTagName ( " version " ) ;
Version spversion = null ;
for ( int i = 0 ; i < spnodeList . getLength ( ) ; i + + ) {
Node node = spnodeList . item ( i ) ;
if ( node . getNodeType ( ) = = Node . ELEMENT_NODE ) {
if ( node . getTextContent ( ) . startsWith ( version . toString ( ) + '-' ) & & ( spversion = = null | | new Version ( node . getTextContent ( ) ) . compareTo ( spversion ) > = 0 ) ) {
spversion = new Version ( node . getTextContent ( ) ) ;
}
}
}
2017-05-29 07:00:02 +02:00
if ( spversion = = null )
throw new InvalidServerException ( " Cannot find sponge version for Minecraft " + version . toString ( ) ) ;
2017-07-23 06:32:57 +02:00
System . out . println ( name + " /Creator > Found \" spongeforge- " + spversion . toString ( ) + '"' ) ;
2017-01-07 20:06:54 +01:00
NodeList mcfnodeList = forgexml . getElementsByTagName ( " version " ) ;
Version mcfversion = null ;
for ( int i = 0 ; i < mcfnodeList . getLength ( ) ; i + + ) {
Node node = mcfnodeList . item ( i ) ;
if ( node . getNodeType ( ) = = Node . ELEMENT_NODE ) {
if ( node . getTextContent ( ) . contains ( spversion . toString ( ) . split ( " \\ - " ) [ 1 ] ) & & ( mcfversion = = null | | new Version ( node . getTextContent ( ) ) . compareTo ( mcfversion ) > = 0 ) ) {
mcfversion = new Version ( node . getTextContent ( ) ) ;
}
}
}
2017-05-29 07:00:02 +02:00
if ( mcfversion = = null )
throw new InvalidServerException ( " Cannot find forge version for Sponge " + spversion . toString ( ) ) ;
2017-07-23 06:32:57 +02:00
System . out . println ( name + " /Creator > Found \" forge- " + mcfversion . toString ( ) + '"' ) ;
2017-01-07 20:06:54 +01:00
2017-05-29 07:00:02 +02:00
version = new Version ( mcfversion . toString ( ) + " " + spversion . toString ( ) ) ;
2017-01-07 20:06:54 +01:00
}
2017-05-29 07:00:02 +02:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
2017-01-07 20:06:54 +01:00
}
2017-07-19 01:28:59 +02:00
if ( template . getBuildOptions ( ) . contains ( " Shell-Location " ) ) {
2017-05-29 07:00:02 +02:00
File gitBash = new File ( this . gitBash , " bin " + File . separatorChar + " bash.exe " ) ;
if ( ! ( System . getProperty ( " os.name " ) . toLowerCase ( ) . indexOf ( " win " ) > = 0 ) & & template . getBuildOptions ( ) . contains ( " Permission " ) ) {
try {
Process process = Runtime . getRuntime ( ) . exec ( " chmod " + template . getBuildOptions ( ) . getRawString ( " Permission " ) + ' ' + template . getBuildOptions ( ) . getRawString ( " Shell-Location " ) , null , dir ) ;
Thread . sleep ( 500 ) ;
2017-01-07 20:06:54 +01:00
if ( process . exitValue ( ) ! = 0 ) {
2017-07-23 06:32:57 +02:00
System . out . println ( name + " /Creator > Couldn't set " + template . getBuildOptions ( ) . getRawString ( " Permission " ) + " permissions to " + template . getBuildOptions ( ) . getRawString ( " Shell-Location " ) ) ;
2017-01-07 20:06:54 +01:00
}
2017-05-29 07:00:02 +02:00
} catch ( Exception e ) {
2017-07-23 06:32:57 +02:00
System . out . println ( name + " /Creator > Couldn't set " + template . getBuildOptions ( ) . getRawString ( " Permission " ) + " permissions to " + template . getBuildOptions ( ) . getRawString ( " Shell-Location " ) ) ;
2017-05-29 07:00:02 +02:00
e . printStackTrace ( ) ;
2017-01-07 20:06:54 +01:00
}
2017-05-29 07:00:02 +02:00
}
2017-01-07 20:06:54 +01:00
2017-05-29 07:00:02 +02:00
try {
2017-07-23 06:32:57 +02:00
System . out . println ( name + " /Creator > Launching " + template . getBuildOptions ( ) . getRawString ( " Shell-Location " ) ) ;
2017-07-23 23:05:33 +02:00
thread . set ( Runtime . getRuntime ( ) . exec ( ( System . getProperty ( " os.name " ) . toLowerCase ( ) . indexOf ( " win " ) > = 0 ) ? " \" " + gitBash + " \" --login -i -c \" bash " + template . getBuildOptions ( ) . getRawString ( " Shell-Location " ) + ' ' + version . toString ( ) + '\"' : ( " bash " + template . getBuildOptions ( ) . getRawString ( " Shell-Location " ) + ' ' + version . toString ( ) + " " + System . getProperty ( " user.home " ) ) , null , dir ) ) ;
2017-07-23 06:32:57 +02:00
thread . name ( ) . log . set ( host . plugin . config . get ( ) . getSection ( " Settings " ) . getBoolean ( " Log-Creator " ) ) ;
thread . name ( ) . file = new File ( dir , " SubCreator- " + template . getName ( ) + " - " + version . toString ( ) . replace ( " " , " @ " ) + " .log " ) ;
thread . name ( ) . process = thread . get ( ) ;
thread . name ( ) . start ( ) ;
thread . get ( ) . waitFor ( ) ;
2017-05-29 07:00:02 +02:00
Thread . sleep ( 500 ) ;
2017-07-23 06:32:57 +02:00
if ( thread . get ( ) . exitValue ( ) ! = 0 ) error = true ;
2017-07-23 23:05:33 +02:00
} catch ( InterruptedException e ) {
error = true ;
2017-05-29 07:00:02 +02:00
} catch ( Exception e ) {
error = true ;
e . printStackTrace ( ) ;
}
}
2017-05-30 21:38:51 +02:00
new UniversalFile ( dir , " template.yml " ) . delete ( ) ;
2017-07-23 06:32:57 +02:00
if ( error ) throw new SubCreatorException ( ) ;
return server ;
}
private void run ( UUID player , String name , ServerTemplate template , Version version , int port ) {
NamedContainer < InternalSubLogger , Process > thread = this . thread . get ( name . toLowerCase ( ) ) . get ( ) ;
UniversalFile dir = new UniversalFile ( new File ( host . getPath ( ) ) , name ) ;
dir . mkdirs ( ) ;
YAMLSection server ;
try {
server = build ( thread , dir , name , template , version , new LinkedList < > ( ) ) ;
generateProperties ( dir , port ) ;
generateClient ( dir , template . getType ( ) , name ) ;
} catch ( SubCreatorException e ) {
server = null ;
} catch ( Exception e ) {
server = null ;
e . printStackTrace ( ) ;
}
if ( server ! = null ) {
2017-05-29 07:00:02 +02:00
try {
2017-07-23 06:32:57 +02:00
System . out . println ( name + " /Creator > Saving... " ) ;
2017-05-29 07:00:02 +02:00
if ( host . plugin . exServers . keySet ( ) . contains ( name . toLowerCase ( ) ) ) host . plugin . exServers . remove ( name . toLowerCase ( ) ) ;
for ( String option : server . getKeys ( ) ) {
if ( server . isString ( option ) ) {
server . set ( option , server . getRawString ( option ) . replace ( " $name$ " , name ) . replace ( " $template$ " , template . getName ( ) ) . replace ( " $type$ " , template . getType ( ) . toString ( ) )
. replace ( " $version$ " , version . toString ( ) . replace ( " " , " @ " ) ) . replace ( " $port$ " , Integer . toString ( port ) ) ) ;
}
2017-01-07 20:06:54 +01:00
}
2017-05-29 07:00:02 +02:00
if ( ! server . contains ( " Enabled " ) ) server . set ( " Enabled " , true ) ;
if ( ! server . contains ( " Host " ) ) server . set ( " Host " , host . getName ( ) ) ;
if ( ! server . contains ( " Port " ) ) server . set ( " Port " , port ) ;
if ( ! server . contains ( " Motd " ) ) server . set ( " Motd " , " Some SubServer " ) ;
if ( ! server . contains ( " Log " ) ) server . set ( " Log " , true ) ;
if ( ! server . contains ( " Directory " ) ) server . set ( " Directory " , " . " + File . separatorChar + name ) ;
2017-06-30 15:36:16 +02:00
if ( ! server . contains ( " Executable " ) ) server . set ( " Executable " , " java -Xmx1024M -jar " + template . getType ( ) . toString ( ) + " .jar " ) ;
2017-05-29 07:00:02 +02:00
if ( ! server . contains ( " Stop-Command " ) ) server . set ( " Stop-Command " , " stop " ) ;
if ( ! server . contains ( " Run-On-Launch " ) ) server . set ( " Run-On-Launch " , false ) ;
if ( ! server . contains ( " Auto-Restart " ) ) server . set ( " Auto-Restart " , false ) ;
if ( ! server . contains ( " Hidden " ) ) server . set ( " Hidden " , false ) ;
if ( ! server . contains ( " Restricted " ) ) server . set ( " Restricted " , false ) ;
SubServer subserver = host . addSubServer ( player , name , server . getBoolean ( " Enabled " ) , port , server . getColoredString ( " Motd " , '&' ) , server . getBoolean ( " Log " ) , server . getRawString ( " Directory " ) ,
new Executable ( server . getRawString ( " Executable " ) ) , server . getRawString ( " Stop-Command " ) , false , server . getBoolean ( " Auto-Restart " ) , server . getBoolean ( " Hidden " ) , server . getBoolean ( " Restricted " ) , false ) ;
host . plugin . config . get ( ) . getSection ( " Servers " ) . set ( name , server ) ;
host . plugin . config . save ( ) ;
subserver . start ( player ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
2017-01-07 20:06:54 +01:00
}
2017-05-29 07:00:02 +02:00
} else {
2017-07-23 06:32:57 +02:00
System . out . println ( name + " /Creator > Couldn't build the server jar. Check the SubCreator logs for more detail. " ) ;
2017-01-07 20:06:54 +01:00
}
2017-07-23 06:32:57 +02:00
this . thread . remove ( name . toLowerCase ( ) ) ;
2017-01-07 20:06:54 +01:00
}
@Override
2017-05-29 07:00:02 +02:00
public boolean create ( UUID player , String name , ServerTemplate template , Version version , int port ) {
if ( Util . isNull ( name , template , version , port ) ) throw new NullPointerException ( ) ;
2017-07-23 06:32:57 +02:00
if ( template . isEnabled ( ) & & ! SubAPI . getInstance ( ) . getSubServers ( ) . keySet ( ) . contains ( name . toLowerCase ( ) ) & & ! SubCreator . isReserved ( name ) ) {
2017-05-29 07:00:02 +02:00
final SubCreateEvent event = new SubCreateEvent ( player , host , name , template , version , port ) ;
2017-01-07 20:06:54 +01:00
host . plugin . getPluginManager ( ) . callEvent ( event ) ;
if ( ! event . isCancelled ( ) ) {
2017-07-23 06:32:57 +02:00
NamedContainer < Thread , NamedContainer < InternalSubLogger , Process > > thread = new NamedContainer < Thread , NamedContainer < InternalSubLogger , Process > > ( new Thread ( ( ) - > InternalSubCreator . this . run ( player , name , event . getTemplate ( ) , event . getVersion ( ) , port ) ) , new NamedContainer < InternalSubLogger , Process > ( new InternalSubLogger ( null , this , name + " /Creator " , new Container < Boolean > ( false ) , null ) , null ) ) ;
this . thread . put ( name . toLowerCase ( ) , thread ) ;
thread . name ( ) . start ( ) ;
2017-01-07 20:06:54 +01:00
return true ;
} else return false ;
} else return false ;
}
@Override
public void terminate ( ) {
2017-07-23 06:32:57 +02:00
HashMap < String , NamedContainer < Thread , NamedContainer < InternalSubLogger , Process > > > temp = new HashMap < String , NamedContainer < Thread , NamedContainer < InternalSubLogger , Process > > > ( ) ;
temp . putAll ( thread ) ;
for ( String i : temp . keySet ( ) ) {
terminate ( i ) ;
}
}
@Override
public void terminate ( String name ) {
2017-07-23 23:05:33 +02:00
if ( this . thread . get ( name . toLowerCase ( ) ) . get ( ) . get ( ) ! = null & & this . thread . get ( name . toLowerCase ( ) ) . get ( ) . get ( ) . isAlive ( ) ) {
this . thread . get ( name . toLowerCase ( ) ) . get ( ) . get ( ) . destroyForcibly ( ) ;
2017-07-23 06:32:57 +02:00
}
2017-07-23 23:05:33 +02:00
if ( this . thread . get ( name . toLowerCase ( ) ) . name ( ) ! = null & & this . thread . get ( name . toLowerCase ( ) ) . name ( ) . isAlive ( ) ) {
this . thread . get ( name . toLowerCase ( ) ) . name ( ) . interrupt ( ) ;
2017-01-07 20:06:54 +01:00
}
}
@Override
public void waitFor ( ) throws InterruptedException {
2017-07-23 06:32:57 +02:00
HashMap < String , NamedContainer < Thread , NamedContainer < InternalSubLogger , Process > > > temp = new HashMap < String , NamedContainer < Thread , NamedContainer < InternalSubLogger , Process > > > ( ) ;
temp . putAll ( thread ) ;
for ( String i : temp . keySet ( ) ) {
waitFor ( i ) ;
}
}
@Override
public void waitFor ( String name ) throws InterruptedException {
2017-07-23 23:05:33 +02:00
while ( this . thread . get ( name . toLowerCase ( ) ) ! = null & & this . thread . get ( name . toLowerCase ( ) ) . name ( ) ! = null & & this . thread . get ( name . toLowerCase ( ) ) . name ( ) . isAlive ( ) ) {
2017-01-07 20:06:54 +01:00
Thread . sleep ( 250 ) ;
}
}
@Override
public Host getHost ( ) {
return host ;
}
@Override
2017-01-21 17:49:37 +01:00
public String getBashDirectory ( ) {
2017-01-07 20:06:54 +01:00
return gitBash ;
}
2017-01-11 04:23:29 +01:00
@Override
2017-07-23 06:32:57 +02:00
public List < SubLogger > getLogger ( ) {
List < SubLogger > loggers = new ArrayList < SubLogger > ( ) ;
HashMap < String , NamedContainer < Thread , NamedContainer < InternalSubLogger , Process > > > temp = new HashMap < String , NamedContainer < Thread , NamedContainer < InternalSubLogger , Process > > > ( ) ;
temp . putAll ( thread ) ;
for ( String i : temp . keySet ( ) ) {
loggers . add ( getLogger ( i ) ) ;
}
return loggers ;
}
@Override
2017-07-23 23:05:33 +02:00
public SubLogger getLogger ( String name ) {
return this . thread . get ( name . toLowerCase ( ) ) . get ( ) . name ( ) ;
2017-01-11 04:23:29 +01:00
}
2017-01-07 20:06:54 +01:00
@Override
2017-07-23 06:32:57 +02:00
public List < String > getReservedNames ( ) {
return new ArrayList < String > ( thread . keySet ( ) ) ;
2017-01-07 20:06:54 +01:00
}
2017-05-29 07:00:02 +02:00
@Override
public Map < String , ServerTemplate > getTemplates ( ) {
return new TreeMap < String , ServerTemplate > ( templates ) ;
2017-01-07 20:06:54 +01:00
}
2016-12-15 22:04:39 +01:00
2017-05-29 07:00:02 +02:00
@Override
public ServerTemplate getTemplate ( String name ) {
if ( Util . isNull ( name ) ) throw new NullPointerException ( ) ;
return getTemplates ( ) . get ( name . toLowerCase ( ) ) ;
}
2016-12-15 22:04:39 +01:00
2017-05-29 07:00:02 +02:00
private void generateClient ( File dir , ServerType type , String name ) throws IOException {
if ( new UniversalFile ( dir , " subservers.client " ) . exists ( ) ) {
if ( type = = ServerType . SPIGOT ) {
if ( ! new UniversalFile ( dir , " plugins " ) . exists ( ) ) new UniversalFile ( dir , " plugins " ) . mkdirs ( ) ;
Util . copyFromJar ( SubPlugin . class . getClassLoader ( ) , " net/ME1312/SubServers/Bungee/Library/Files/Client/spigot.jar " , new UniversalFile ( dir , " plugins:SubServers.Client.jar " ) . getPath ( ) ) ;
} else if ( type = = ServerType . SPONGE ) {
// TODO
2017-07-19 01:28:59 +02:00
// if (!new UniversalFile(dir, "mods").exists()) new UniversalFile(dir, "mods").mkdirs();
2017-05-29 07:00:02 +02:00
// Util.copyFromJar(SubPlugin.class.getClassLoader(), "net/ME1312/SubServers/Bungee/Library/Files/Client/sponge.jar", new UniversalFile(dir, "mods:SubServers.Client.jar").getPath());
}
2017-06-30 15:36:16 +02:00
JSONObject config = new JSONObject ( ) ;
2017-05-29 07:00:02 +02:00
FileWriter writer = new FileWriter ( new UniversalFile ( dir , " subservers.client " ) , false ) ;
config . put ( " Name " , name ) ;
config . put ( " Address " , host . plugin . config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Address " , " 127.0.0.1 " ) . replace ( " 0.0.0.0 " , " 127.0.0.1 " ) ) ;
config . put ( " Password " , host . plugin . config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Password " , " " ) ) ;
2017-06-30 15:36:16 +02:00
config . put ( " Encryption " , host . plugin . config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Encryption " , " NONE " ) ) ;
2017-05-29 07:00:02 +02:00
config . write ( writer ) ;
writer . close ( ) ;
}
2016-12-15 22:04:39 +01:00
}
2017-04-14 04:39:51 +02:00
private void generateProperties ( File dir , int port ) throws IOException {
File file = new File ( dir , " server.properties " ) ;
2017-05-29 07:00:02 +02:00
if ( ! file . exists ( ) ) file . createNewFile ( ) ;
2017-04-14 04:39:51 +02:00
String content = Util . readAll ( new BufferedReader ( new InputStreamReader ( new FileInputStream ( file ) ) ) ) . replace ( " server-port= " , " server-port= " + port ) . replace ( " server-ip= " , " server-ip= " + host . getAddress ( ) . toString ( ) . substring ( 1 ) ) ;
file . delete ( ) ;
PrintWriter writer = new PrintWriter ( file , " UTF-8 " ) ;
writer . write ( content ) ;
2016-12-15 22:04:39 +01:00
writer . close ( ) ;
}
2017-01-01 20:34:46 +01:00
private void copyFolder ( File source , File destination ) {
if ( source . isDirectory ( ) ) {
if ( ! destination . exists ( ) ) {
destination . mkdirs ( ) ;
}
String files [ ] = source . list ( ) ;
for ( String file : files ) {
File srcFile = new File ( source , file ) ;
File destFile = new File ( destination , file ) ;
copyFolder ( srcFile , destFile ) ;
}
} else {
InputStream in = null ;
OutputStream out = null ;
try {
in = new FileInputStream ( source ) ;
out = new FileOutputStream ( destination ) ;
byte [ ] buffer = new byte [ 1024 ] ;
int length ;
while ( ( length = in . read ( buffer ) ) > 0 ) {
out . write ( buffer , 0 , length ) ;
}
} catch ( Exception e ) {
try {
in . close ( ) ;
} catch ( IOException e1 ) {
e1 . printStackTrace ( ) ;
}
try {
out . close ( ) ;
} catch ( IOException e1 ) {
e1 . printStackTrace ( ) ;
}
}
}
}
2016-12-05 04:21:04 +01:00
}