通过java将命令发送到minecraft服务器

时间:2014-06-30 12:48:55

标签: java multithreading process

您好我想向正在运行的Minecraft服务器发送命令并观察它的响应。 一切正常,但在服务器启动后,我无法发送任何命令:

这是我的代码:

import java.io.*;
import java.util.Scanner;

public class Server{
    private String name;
    private StartCommand command;
    private boolean on = false;
    private Process p = null;

    public Server(StartCommand c, String name){
        this.command = c;
        this.name = name;
    }

    public String getName(){
        return name;
    }

    public void start(){
    try {
      p = Runtime.getRuntime().exec(command.getCommand());
      System.out.println("Server: " + name + " is on now!");
      on = true;
    } catch (IOException e) {
      e.printStackTrace();
      on = false;
    }
  }

  public void watch(){
   System.out.println("threads:");
   Thread t1 = new Thread(new proc1(p));
   Thread t2 = new Thread(new proc2(p));
   System.out.println("start1");
   t1.start();
   System.out.println("start2");
   t2.start();
 }
}


  //-----Processes-----//

class proc1 implements Runnable{
  private Process p;
  private static String line = null;

  public proc1(Process p){
    this.p = p;
  }

  @Override
  public void run(){
    try{

      BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));

      while ((line = in.readLine()) != null) {
       System.out.println(line);
     }
   } catch (IOException e) {
    e.printStackTrace();
      //on = false;
  }
}
}

class proc2 implements Runnable{
  private Process p;

  public proc2(Process p){
    this.p = p;
  }

  @Override
  public void run(){
    Scanner in = new Scanner(System.in);
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
    //OutputStream out = p.getOutputStream();
    System.out.println("start while (type commands now!!!!)");

    while(in.hasNextLine()){
      String command = in.nextLine();

      if(command.equals("ext")){
           //coming soon
        return;
      }else{
       try{
         out.write(command);
         out.flush();
       } catch (IOException e) {
         e.printStackTrace();
         return;
       }
     }
   }
 }
}

我知道必须有某事。我的proc2类错了。这实际上是我第一次使用'流程'类

谢谢!

2 个答案:

答案 0 :(得分:1)

您正在使用Scanner.next()来检索下一个命令。你确定你想做什么?这将读取由空格分隔的所有内容作为单独的输入,并在没有空格的情况下写入(删除分隔符)。

EG:" /给予玩家项目"会出现在" / giveplayeritem"要么在一条线上捣碎在一起,最后没有回报,要么" /给\ nplayer \ nitem"取决于您如何设置缓冲输出以将命令发送到服务器。

编辑: 可以在他的自我回答中看到有效的修复。

基本上,冲洗没有正常工作,使用打印机自动冲洗打印机然后冲洗以某种方式修复。

答案 1 :(得分:0)

好看,它现在工作正常,但我不得不使用flush()方法。

这是我的新代码。我希望我以正确的方式实现了一切:

class proc2 implements Runnable{
  private Process p;

  public proc2(Process p){
    this.p = p;
  }

  @Override
  public void run(){
    Scanner in = new Scanner(System.in);
    //BufferedWriter out = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
    PrintWriter out = new PrintWriter(p.getOutputStream());
    System.out.println("start while (type commands now!!!!)");

    while(in.hasNextLine()){
      String command = in.nextLine();
      System.out.println("test: " + command);

      if(command.equals("ext")){
           //FILL IN
           return;
      }else{
    //try{
      out.println(command);
      out.flush();
      /*out.flush();
      out.close();*/
    /*} catch (IOException e) {
      e.printStackTrace();
      return;
        }*/
      }
    }
  }
}

我应该像现在一样离开它吗?或者还有更好的方法吗?

相关问题