1212import java .io .Closeable ;
1313import java .io .File ;
1414import java .util .UUID ;
15+ import java .util .stream .Stream ;
1516
1617public class ServerContainer extends Thread {
1718 private final String prefix ;
@@ -24,15 +25,17 @@ public class ServerContainer extends Thread {
2425 private String serverPath ;
2526 private int port ;
2627 private int memory ;
28+ private String [] environmentVariables ;
2729
28- public ServerContainer (String prefix , DockerClient dockerClient , String basePath , String platform , String version , int port , int memory ) {
30+ public ServerContainer (String prefix , DockerClient dockerClient , String basePath , String platform , String version , int port , int memory , String [] environmentVariables ) {
2931 this .prefix = prefix ;
3032 this .dockerClient = dockerClient ;
3133 this .basePath = basePath ;
3234 this .platform = platform ;
3335 this .version = version ;
3436 this .port = port ;
3537 this .memory = memory ;
38+ this .environmentVariables = environmentVariables ;
3639 }
3740
3841 public ServerContainer (String prefix , DockerClient dockerClient , String basePath , String containerId , String uniqueId ) {
@@ -59,24 +62,21 @@ public String createAndStartContainer() {
5962 ExposedPort containerPort = ExposedPort .tcp (25565 );
6063 Ports portBindings = new Ports ();
6164 portBindings .bind (containerPort , Ports .Binding .bindPort (this .port ));
65+ String [] result = Stream .concat (Stream .of (environmentVariables ), Stream .of (
66+ "EULA=TRUE" ,
67+ "TYPE=" + this .platform .toUpperCase (),
68+ "VERSION=" + this .version ,
69+ "MEMORY=" + this .memory + "M" ,
70+ "ONLINE_MODE=TRUE" ,
71+ "AIKAR_FLAGS=TRUE"
72+ )).toArray (String []::new );
6273 CreateContainerResponse container = this .dockerClient .createContainerCmd (imageName )
6374 .withName (containerName )
6475 .withHostConfig (HostConfig .newHostConfig ()
6576 .withBinds (new Bind (this .serverPath , serverVolume ))
6677 .withPortBindings (portBindings )
6778 .withRestartPolicy (RestartPolicy .alwaysRestart ()))
68- .withEnv (
69- "EULA=TRUE" ,
70- "TYPE=" + this .platform .toUpperCase (),
71- "VERSION=" + this .version ,
72- "MEMORY=" + this .memory + "M" ,
73- "VIEW_DISTANCE=6" ,
74- "SIMULATION_DISTANCE=4" ,
75- "MAX_PLAYERS=4" ,
76- "MOTD=Server hosted by 2weeksmc.com" ,
77- "ONLINE_MODE=TRUE" ,
78- "AIKAR_FLAGS=TRUE"
79- )
79+ .withEnv (result )
8080 .exec ();
8181 this .containerId = container .getId ();
8282 System .out .println (this .containerId );
@@ -134,13 +134,21 @@ public String recreateAndStartContainerFromDirectory(String uniqueId) {
134134 ExposedPort containerPort = ExposedPort .tcp (25565 );
135135 Ports portBindings = new Ports ();
136136 portBindings .bind (containerPort , Ports .Binding .bindPort (this .port ));
137+ String [] result = Stream .concat (Stream .of (environmentVariables ), Stream .of (
138+ "EULA=TRUE" ,
139+ "TYPE=" + this .platform .toUpperCase (),
140+ "VERSION=" + this .version ,
141+ "MEMORY=" + this .memory + "M" ,
142+ "ONLINE_MODE=TRUE" ,
143+ "AIKAR_FLAGS=TRUE"
144+ )).toArray (String []::new );
137145 CreateContainerResponse container = this .dockerClient .createContainerCmd ("itzg/minecraft-server" )
138146 .withName (containerName )
139147 .withHostConfig (HostConfig .newHostConfig ()
140148 .withBinds (new Bind (serverPath , serverVolume ))
141149 .withPortBindings (portBindings )
142150 .withRestartPolicy (RestartPolicy .alwaysRestart ()))
143- .withEnv ("EULA=TRUE" , "TYPE=" + this . platform . toUpperCase (), "VERSION=" + this . version , "MEMORY=" + this . memory + "M" )
151+ .withEnv (result )
144152 .exec ();
145153 this .containerId = container .getId ();
146154 this .dockerClient .startContainerCmd (this .containerId ).exec ();
0 commit comments