@Override public void onStatus(String status) { System.out.println(status); if (isGettingPlayerNumber == 1) { Pattern p = Pattern.compile("There are [0-9]*/[0-9]* players online"); Matcher m = p.matcher(status); if (!m.find()) return; String s = m.group(0); s = s.substring(10, s.length() - 15); playerNumber = s; isGettingPlayerNumber = 2; return; } else if (isGettingPlayerNumber == 2) { try { status = status.substring(status.lastIndexOf("]") + 1); status = status.substring(status.indexOf(":") + 1); } catch (Exception e) { HMCLog.warn("Failed to substring status.", e); } String[] s; if (StrUtils.isNotBlank(status)) s = status.trim().split(", "); else s = new String[0]; Pair<String, String[]> p = new Pair<>(playerNumber, s); isGettingPlayerNumber = 0; gettingPlayerNumber.accept(p); return; } if (isDone == false) { Pattern p = Pattern.compile( "\\[INFO\\] Done \\([0-9]*\\.[0-9]*s\\)! For help, type \"help\" or \"\\?\""); Matcher m = p.matcher(status); if (m.find()) { stoppedEvent.execute(null); timer = new Timer(); timerTasks.clear(); for (int i = 0; i < schedules.size(); i++) { if (schedules.get(i).timeType == Schedule.TIME_TYPE_SERVER_STARTED) { ScheduleTranslator.translate(this, schedules.get(i)).run(); continue; } if (schedules.get(i).timeType != Schedule.TIME_TYPE_PER) continue; long mill = (long) Math.floor(schedules.get(i).per * 60 * 1000); timerTasks.add(ScheduleTranslator.translate(this, schedules.get(i))); timer.schedule(timerTasks.get(i), mill, mill); } pastTimer = new javax.swing.Timer(1000, this); pastTimer.start(); System.out.println("Server started!"); sendStatus("*** 服务端已启动完成 ***"); isDone = true; } } if (status.length() > 20) if (status.substring(20).contains("[SEVERE] This crash report has been saved to: ")) for (Schedule schedule : schedules) if (schedule.timeType == Schedule.TIME_TYPE_SERVER_CRASHED) ScheduleTranslator.translate(this, schedule).run(); }
public void run() throws IOException { String jvmPath; if (StrUtils.isBlank(SettingsManager.settings.javaDir)) jvmPath = IOUtils.getJavaDir(); else jvmPath = SettingsManager.settings.javaDir; String[] puts = new String[] { jvmPath, "-Xmx" + memory + "m", "-jar", SettingsManager.settings.mainjar, "nogui", "-nojline" }; ProcessBuilder pb = new ProcessBuilder(puts); pb.directory(new File(SettingsManager.settings.mainjar).getParentFile()); try { disactiveMods( SettingsManager.settings.inactiveExtMods, SettingsManager.settings.inactiveCoreMods, SettingsManager.settings.inactivePlugins); server = pb.start(); registerThread(threadA, server.getInputStream()); registerThread(threadB, server.getErrorStream()); registerThreadC(server); try { bw = new BufferedWriter( new OutputStreamWriter( server.getOutputStream(), System.getProperty("sun.jnu.encoding", "utf-8"))); } catch (UnsupportedEncodingException ex) { bw = new BufferedWriter(new OutputStreamWriter(server.getOutputStream())); } isRunning = true; startedEvent.execute(null); sendStatus("*** 启动服务端中 ***"); } catch (IOException ex) { Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex); isRunning = false; } }