0

I've got an issue sending commands to a Bukkit server for Minecraft. My code work perfectly for Vanilla but when I try it with Bukkit it doesn't seem to want to work any longer. I still can receive all the output from the server I just can't send commands like "Help" or "Stop." This is my class for handling the server thread.

public class Sender{

ConsoleWriter cWriter = new ConsoleWriter();
    private BufferedWriter writer;
private OutputStreamWriter out;
public void execute(){
    this.ui = ui;
    try{
        ProcessBuilder pb = new ProcessBuilder(path_to_server+"launch.bat");
        Process process = pb.start();

        StreamGobbler sgError = new StreamGobbler(process.getErrorStream());

        new Thread( sgError ).start();
        out = new OutputStreamWriter(process.getOutputStream());
        writer = new PrintWriter(out);

    } catch ( IOException e ){
        e.printStackTrace();
    }
}

private class StreamGobbler implements Runnable 
{
    private InputStream is;
    public StreamGobbler( InputStream is ){
        this.is = is;
    }
    @Override
    public void run() {
        try {
            InputStreamReader isr = new InputStreamReader( is );
            BufferedReader br = new BufferedReader( isr );
            String line = null;
            while ( ( line = br.readLine() ) != null ){
                cWriter.writer(line, ui);
            }
        } catch ( IOException e ){
            e.printStackTrace();
        }
    }
}

}

So what is going on here? why does it work fine for Vanilla but not Bukkit?

Thanks!

Brilliance Of Ben
  • 91
  • 1
  • 3
  • 11
  • Folks over at http://gaming.stackexchange.com/ might have more experience with bukkit-specific questions. – Chris Zhang Jan 22 '14 at 05:01
  • @ChrisZhang In looking at that site, it doesn't seem as much about code but about issues with the games themselves.. Which isn't what I'm looking for. Thanks for the suggestion though. – Brilliance Of Ben Jan 22 '14 at 22:07

1 Answers1

1

Vanilla code is completely different from Bukkit code. For commands you have to use

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){

So if you wanted to create a simple Say command (as help and stop are already included in bukkit), you could do something like this:

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(cmd.getName().equalsIgnoreCase("say")){
        if(args.length != 0){
            Bukkit.getServer().broadcastMessage(args);
        }
        return true;
    }
    return false;
}  

You would have to create a plugin.yml file, too, and declare your command there using

commands:
    say:
        description: Say something
        usage: /say

A great way to learn the Bukkit API is by going to these sites:

Official Bukkit Plugin Tutorial: http://wiki.bukkit.org/Plugin_Tutorial

Bukkit API Docs: http://jd.bukkit.org/dev/apidocs/

Again, Bukkit is way different from Vanilla , so you will have to modify almost all of your code. Good luck!

Jojodmo
  • 23,357
  • 13
  • 65
  • 107
  • I'm not writing a plugin. It is an external program to manage and run both types of servers. I know the Bukkit API but that isn't the most helpful in this circumstance. – Brilliance Of Ben Feb 02 '14 at 02:06
  • 1
    @BrillianceOfBen Again, though, the reason it isn't working is because Bukkit and Vanilla are very different. – Jojodmo Feb 02 '14 at 02:27
  • While you're answer wasn't what I was looking for, it did get me thinking about how Bukkit interacts differently than Vanilla. I found that unlike Vanilla it doesn't bind to System.in/out like Vanilla. So I was able to write a super simple plugin to do just that and it has solved my issue completely. – Brilliance Of Ben Feb 02 '14 at 03:42