Created
October 18, 2021 20:49
-
-
Save MWHunter/0d01482be355a73c6b76ff66901a4969 to your computer and use it in GitHub Desktop.
Quick java program to wrap a minecraft server and run it anywhere java can be compiled an ran, such as repl.it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.lang.ProcessBuilder.Redirect; | |
import java.net.URL; | |
import java.util.Scanner; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipInputStream; | |
public class Main { | |
public static void main(String[] args) throws IOException, InterruptedException { | |
Scanner userInput = new Scanner(System.in); | |
try (BufferedInputStream in = new BufferedInputStream(new URL("https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip").openStream()); | |
FileOutputStream fileOutputStream = new FileOutputStream("ngrok.zip")) { | |
byte dataBuffer[] = new byte[1024]; | |
int bytesRead; | |
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) { | |
fileOutputStream.write(dataBuffer, 0, bytesRead); | |
} | |
} catch (IOException e) { | |
// handle exception | |
} | |
unzip("ngrok.zip", "ngrok"); | |
new File("ngrok.zip").delete(); | |
String token = "Your ngrok token here"; | |
System.out.println("Marking ngrok as executable"); | |
File ngrokFile = new File(System.getProperty("user.dir") + "/ngrok/ngrok"); | |
ngrokFile.setExecutable(true); | |
System.out.println("Setting auth token..."); | |
Process ngrokSetAuth = new ProcessBuilder(System.getProperty("user.dir") + "/ngrok/ngrok", "authtoken", token).start(); | |
ngrokSetAuth.waitFor(); | |
System.out.println("Auth token set!"); | |
System.out.println("Starting ngrok"); | |
ProcessBuilder ngrok = new ProcessBuilder(System.getProperty("user.dir") + "/ngrok/ngrok", "tcp", "25565"); | |
ngrok.redirectOutput(Redirect.INHERIT); | |
ngrok.redirectError(Redirect.INHERIT); | |
ngrok.start(); | |
try (BufferedInputStream in = new BufferedInputStream(new URL("https://ci.tivy.ca/job/Airplane-1.17/lastSuccessfulBuild/artifact/launcher-airplane.jar").openStream()); | |
FileOutputStream fileOutputStream = new FileOutputStream("paper.jar")) { | |
byte dataBuffer[] = new byte[1024]; | |
int bytesRead; | |
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) { | |
fileOutputStream.write(dataBuffer, 0, bytesRead); | |
} | |
} catch (IOException e) { | |
// handle exception | |
} | |
File eula = new File("eula.txt"); | |
eula.createNewFile(); | |
System.out.println("You agree to the Mojang EULA"); | |
FileWriter eulaAccepter = new FileWriter("eula.txt"); | |
eulaAccepter.write("eula=true"); | |
eulaAccepter.close(); | |
ProcessBuilder paper = new ProcessBuilder(System.getProperty("java.home") + "/bin/java", "-jar", "-Xmx2500M", "-Xms2500M", "paper.jar"); | |
//paper.directory(new File("server")); | |
paper.redirectOutput(Redirect.INHERIT); | |
paper.redirectInput(Redirect.INHERIT); | |
Process paperProcess = paper.start(); | |
paperProcess.waitFor(); | |
} | |
private static void unzip(String zipFilePath, String destDir) { | |
File dir = new File(destDir); | |
// create output directory if it doesn't exist | |
if (!dir.exists()) dir.mkdirs(); | |
FileInputStream fis; | |
//buffer for read and write data to file | |
byte[] buffer = new byte[1024]; | |
try { | |
fis = new FileInputStream(zipFilePath); | |
ZipInputStream zis = new ZipInputStream(fis); | |
ZipEntry ze = zis.getNextEntry(); | |
while (ze != null) { | |
String fileName = ze.getName(); | |
File newFile = new File(destDir + File.separator + fileName); | |
System.out.println("Unzipping to " + newFile.getAbsolutePath()); | |
//create directories for sub directories in zip | |
new File(newFile.getParent()).mkdirs(); | |
FileOutputStream fos = new FileOutputStream(newFile); | |
int len; | |
while ((len = zis.read(buffer)) > 0) { | |
fos.write(buffer, 0, len); | |
} | |
fos.close(); | |
//close this ZipEntry | |
zis.closeEntry(); | |
ze = zis.getNextEntry(); | |
} | |
//close last ZipEntry | |
zis.closeEntry(); | |
zis.close(); | |
fis.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment