Пример #1
0
 @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();
 }
Пример #2
0
  @SuppressWarnings({"CallToPrintStackTrace", "UseSpecificCatch"})
  public static void main(String[] args) throws IOException {
    {
      PluginManager.getPlugin(DefaultPlugin.class);
      if (IUpgrader.NOW_UPGRADER.parseArguments(getVersionNumber(), args)) return;

      System.setProperty("awt.useSystemAAFontSettings", "on");
      System.setProperty("swing.aatext", "true");
      System.setProperty("sun.java2d.noddraw", "true");
      System.setProperty("sun.java2d.dpiaware", "false");
      Thread.setDefaultUncaughtExceptionHandler(new CrashReporter(true));

      try {
        File file = new File("hmcl.log");
        if (!file.exists() && !file.createNewFile())
          HMCLog.warn("Failed to create log file " + file);
        Configuration.DEFAULT.appenders.add(
            new ConsoleAppender(
                "File", new DefaultLayout(), true, new FileOutputStream(file), true));
      } catch (IOException ex) {
        LOGGER.log(
            Level.SEVERE,
            "Failed to add log appender File because an error occurred while creating or opening hmcl.log",
            ex);
      }

      HMCLog.log("*** " + Main.makeTitle() + " ***");

      String s = Settings.getInstance().getLocalization();
      for (SupportedLocales sl : SupportedLocales.values())
        if (sl.name().equals(s)) {
          SupportedLocales.NOW_LOCALE = sl;
          Locale.setDefault(sl.self);
        }

      LogWindow.INSTANCE.clean();
      LogWindow.INSTANCE.setTerminateGame(GameLauncher.PROCESS_MANAGER::stopAllProcesses);

      try {
        LOOK_AND_FEEL = new HelloMinecraftLookAndFeel(Settings.getInstance().getTheme().settings);
        UIManager.setLookAndFeel(LOOK_AND_FEEL);
      } catch (ParseException | UnsupportedLookAndFeelException ex) {
        HMCLog.warn("Failed to set look and feel...", ex);
      }

      Settings.UPDATE_CHECKER.outdated.register(IUpgrader.NOW_UPGRADER);
      Settings.UPDATE_CHECKER.process(false).reg(t -> Main.invokeUpdate()).execute();

      if (StrUtils.isNotBlank(Settings.getInstance().getProxyHost())
          && StrUtils.isNotBlank(Settings.getInstance().getProxyPort())
          && MathUtils.canParseInt(Settings.getInstance().getProxyPort())) {
        HMCLog.log("Initializing customized proxy");
        System.setProperty("http.proxyHost", Settings.getInstance().getProxyHost());
        System.setProperty("http.proxyPort", Settings.getInstance().getProxyPort());
        if (StrUtils.isNotBlank(Settings.getInstance().getProxyUserName())
            && StrUtils.isNotBlank(Settings.getInstance().getProxyPassword()))
          Authenticator.setDefault(
              new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(
                      Settings.getInstance().getProxyUserName(),
                      Settings.getInstance().getProxyPassword().toCharArray());
                }
              });
      }

      try {
        PluginManager.plugin().showUI();
      } catch (Throwable t) {
        new CrashReporter(false).uncaughtException(Thread.currentThread(), t);
        System.exit(1);
      }
    }
  }