How to set all cb output to a gui

Discussion in 'Plugin Development' started by MCMatters, Jul 28, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    MCMatters

    Help! How can I display all bukkit console output in a gui, and how can I run a bat script from a java gui?
     
  2. Offline

    Garris0n

  3. Offline

    jthort

    MCMatters you can grab the output from the text file that logs all the output or create a new process and get all the output through that.
     
  4. Offline

    MCMatters

  5. Offline

    Dragonphase

  6. Offline

    MCMatters

    HOW
    how
     
  7. Offline

    Garris0n

    By learning how to use Java, Google's search, and Bukkit's search...
     
  8. Offline

    MCMatters

    Bukkits search gave me 3 unanswered topics asking for help,
    Link to the apidocs showing it?
    I'll google it now
     
  9. Offline

    jthort

    For reading from the flat file that your server provides it's under the logs folder, last file called latest. Create an output stream and you can read from that. If you don't know what i'm talking about, perhaps you should look into Java Networking

    OutputStream method

    http://unserializableone.blogspot.com/2009/01/redirecting-systemout-and-systemerr-to.html

    https://forums.bukkit.org/threads/redirect-console-output-to-a-jtextarea.44265/
    (Just so you know this was the first result on google and the OP figured it out.)

    Process method
    http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html

    Logger method (Never used, not sure how it works) You will have to do some research for this one
    https://forums.bukkit.org/threads/how-to-get-all-console-output.226532/

    My Method (this is how I did it, worked great)
    http://stackoverflow.com/questions/14181957/redirected-standard-output-datad
     
  10. Offline

    ChrisCreed2007

    I was thinking about this before, and had some free time to look into it now.
    It's not the best example of code(or best practice it's only an example) but it works, it captures all logging from the plugins/players on the server. Think you will have to change the code abit more to get everything outputted!

    Hope this helps!

    LoggerTester Class:
    Code:java
    1. package io.github.ChrisCreed2007.PluginLogger;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class LoggerTester extends JavaPlugin {
    6.  
    7. LoggerRunner loggerRunner;
    8.  
    9. public void onEnable() {
    10. loggerRunner = new LoggerRunner(this);
    11. }
    12.  
    13. public void onDisable() {
    14. loggerRunner.disableLogger();
    15. }
    16. }


    LoggerRunner Class:
    Code:java
    1. package io.github.ChrisCreed2007.PluginLogger;
    2.  
    3. import java.io.IOException;
    4. import java.util.logging.Formatter;
    5. import java.util.logging.Handler;
    6. import java.util.logging.Level;
    7. import java.util.logging.LogRecord;
    8. import java.util.logging.Logger;
    9. import java.util.logging.SocketHandler;
    10.  
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.AsyncPlayerChatEvent;
    14.  
    15. public class LoggerRunner implements Listener {
    16.  
    17. private static Logger bukkitLogger;
    18. private Handler handler;
    19.  
    20. public LoggerRunner(LoggerTester plugin) {
    21. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    22. try {
    23. bukkitLogger = Logger.getLogger(""); //root logger
    24. handler = new SocketHandler("localhost", 5000);
    25. handler.setFormatter(new outputFormatter());
    26. bukkitLogger.addHandler(handler);
    27. //bukkitLogger.setLevel(Level.ALL);
    28. } catch (IOException e) {
    29. e.printStackTrace();
    30. }
    31. }
    32.  
    33. public void disableLogger() {
    34. handler.close();
    35. }
    36.  
    37. @EventHandler
    38. public void playerChat(AsyncPlayerChatEvent message){
    39. bukkitLogger.log(Level.INFO, "<" + message.getPlayer().getName() + "> " + message.getMessage());
    40. }
    41.  
    42. public class outputFormatter extends Formatter {
    43.  
    44. @Override
    45. public String format(LogRecord record) {
    46. return "[" + record.getLevel() + "] [" + record.getSourceClassName()+ "] - " + record.getMessage() + "\n";
    47. }
    48. }
    49. }


    And somewhere for reading the data from:
    Code:java
    1. import java.io.BufferedReader;
    2. import java.io.IOException;
    3. import java.io.InputStreamReader;
    4. import java.net.ServerSocket;
    5. import java.net.Socket;
    6.  
    7.  
    8. public class Main {
    9.  
    10. public static void main(String[] args) {
    11. try {
    12. while(true){
    13. ServerSocket serverSocket = new ServerSocket(5000);
    14. Socket clientSocket = serverSocket.accept();
    15. BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    16. String line;
    17. while ((line = in.readLine()) != null) {
    18. System.out.println(line);
    19. }
    20. in.close();
    21. clientSocket.close();
    22. serverSocket.close();
    23. }
    24. } catch (IOException e) {
    25. e.printStackTrace();
    26. }
    27. }
    28. }
     
  11. Offline

    MCMatters

    Code:java
    1. package me.dillan.bukkitgui;
    2.  
    3. import java.awt.BorderLayout;
    4. import java.awt.Font;
    5. import java.util.logging.Handler;
    6. import java.util.logging.LogRecord;
    7.  
    8. import javax.swing.JTextPane;
    9.  
    10. public class Console extends Handler{
    11. @Override
    12. public void close() throws SecurityException {/*Ignore this method*/}
    13.  
    14. @Override
    15. public void flush() {/*Ignore this method*/}
    16.  
    17. @Override
    18. public void publish(LogRecord logRecord) {
    19. //Do something with the logRecord
    20. //Interesting methods of the logRecord object may be:
    21. //logRecord.getLevel().toString();//SEVERE / WARNING / INFO etc
    22. //logRecord.getMillis(); //The the unix timestamp of the logRecord
    23. //logRecord.getMessage(); //The logged message
    24. String level = logRecord.getLevel().toString();
    25. String logmsg = logRecord.getMessage();
    26. String logmsg2 = logmsg.replace(',', '`');
    27. String logmsg3 = logmsg2.replace('[', '{');
    28. String logmsg4 = logmsg3.replace(']', '}');
    29. JTextPane consoles = new JTextPane();
    30. consoles.setFont(new Font("Simplified Arabic Fixed", Font.PLAIN, 12));
    31. consoles.setEditable(false);
    32. Main.console.add(logRecord.getMillis() + " {" + level + "} " + logmsg4);
    33. String console2 = "" + Main.console;
    34. String console3 = console2.replace('[', ' ');
    35. String console4 = console3.replace(']', ' ');
    36. String console5 = console4.replace(',', '\n');
    37. String console6 = console5.replace('`', ',');
    38. String console7 = console6.replace('}', ']');
    39. String console8 = console7.replace('{', '[');
    40. consoles.setText(console8);
    41. Main.frame.getContentPane().add(consoles, BorderLayout.CENTER);
    42. Main.frame.setVisible(false);
    43. Main.frame.setVisible(true);
    44. }
    45. }
    46.  
    jthort Garris0n
    Thats my code, but is there a way that i dont have to frame.setVisible(false); and frame.setVisible(true); in order to update the gui?
     
  12. Offline

    jthort

    I had some trouble getting the GUI to update, it kept freezing, but when I clicked on the pane it popped up. Maybe try running it on a separate thread? Never took the time to figure it out, I would be interested to know what you do to solve this
     
  13. Offline

    MCMatters

    How? im a noob at this

    jthort

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  14. Offline

    jthort

    Your really getting irritating. Not because you are new, but because you lack the motivation to solve these issues yourself

    Create a new class that extends Thread. Inside the run method run the separate process or however you are getting the server output through there. Then once you get the output, send it to the TextArea or whatever you are using.

    Take a look at here for more information http://docs.oracle.com/javase/tutorial/uiswing/concurrency/

    This is my last post on this thread
     
Thread Status:
Not open for further replies.

Share This Page