MinecraftServerGui.java

net.minecraft.server.gui.MinecraftServerGui

信息

  • 全限定名:net.minecraft.server.gui.MinecraftServerGui
  • 类型:public class
  • 包:net.minecraft.server.gui
  • 源码路径:src/main/java/net/minecraft/server/gui/MinecraftServerGui.java
  • 起始行号:L34
  • 继承:JComponent
  • 职责:

    TODO

字段/常量

  • MONOSPACED

    • 类型: Font
    • 修饰符: private static final
    • 源码定位: L35
    • 说明:

      TODO

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L36
    • 说明:

      TODO

  • TITLE

    • 类型: String
    • 修饰符: private static final
    • 源码定位: L37
    • 说明:

      TODO

  • SHUTDOWN_TITLE

    • 类型: String
    • 修饰符: private static final
    • 源码定位: L38
    • 说明:

      TODO

  • server

    • 类型: DedicatedServer
    • 修饰符: private final
    • 源码定位: L39
    • 说明:

      TODO

  • logAppenderThread

    • 类型: Thread
    • 修饰符: private
    • 源码定位: L40
    • 说明:

      TODO

  • finalizers

    • 类型: Collection<Runnable>
    • 修饰符: private final
    • 源码定位: L41
    • 说明:

      TODO

  • isClosing

    • 类型: AtomicBoolean
    • 修饰符: private final
    • 源码定位: L42
    • 说明:

      TODO

内部类/嵌套类型

构造器

private MinecraftServerGui(DedicatedServer server) @ L72

  • 构造器名:MinecraftServerGui
  • 源码定位:L72
  • 修饰符:private

参数:

  • server: DedicatedServer

说明:

TODO

方法

下面的方法块按源码顺序生成。

public static MinecraftServerGui showFrameFor(DedicatedServer server) @ L44

  • 方法名:showFrameFor
  • 源码定位:L44
  • 返回类型:MinecraftServerGui
  • 修饰符:public static

参数:

  • server: DedicatedServer

说明:

TODO

public void addFinalizer(Runnable finalizer) @ L85

  • 方法名:addFinalizer
  • 源码定位:L85
  • 返回类型:void
  • 修饰符:public

参数:

  • finalizer: Runnable

说明:

TODO

private JComponent buildInfoPanel() @ L89

  • 方法名:buildInfoPanel
  • 源码定位:L89
  • 返回类型:JComponent
  • 修饰符:private

参数:

说明:

TODO

private JComponent buildPlayerPanel() @ L99

  • 方法名:buildPlayerPanel
  • 源码定位:L99
  • 返回类型:JComponent
  • 修饰符:private

参数:

说明:

TODO

private JComponent buildChatPanel() @ L106

  • 方法名:buildChatPanel
  • 源码定位:L106
  • 返回类型:JComponent
  • 修饰符:private

参数:

说明:

TODO

public void start() @ L144

  • 方法名:start
  • 源码定位:L144
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

public void close() @ L148

  • 方法名:close
  • 源码定位:L148
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

private void runFinalizers() @ L154

  • 方法名:runFinalizers
  • 源码定位:L154
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

public void print(JTextArea console, JScrollPane scrollPane, String line) @ L158

  • 方法名:print
  • 源码定位:L158
  • 返回类型:void
  • 修饰符:public

参数:

  • console: JTextArea
  • scrollPane: JScrollPane
  • line: String

说明:

TODO

代码

public class MinecraftServerGui extends JComponent {
    private static final Font MONOSPACED = new Font("Monospaced", 0, 12);
    private static final Logger LOGGER = LogUtils.getLogger();
    private static final String TITLE = "Minecraft server";
    private static final String SHUTDOWN_TITLE = "Minecraft server - shutting down!";
    private final DedicatedServer server;
    private Thread logAppenderThread;
    private final Collection<Runnable> finalizers = Lists.newArrayList();
    private final AtomicBoolean isClosing = new AtomicBoolean();
 
    public static MinecraftServerGui showFrameFor(DedicatedServer server) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception var3) {
        }
 
        final JFrame frame = new JFrame("Minecraft server");
        final MinecraftServerGui gui = new MinecraftServerGui(server);
        frame.setDefaultCloseOperation(2);
        frame.add(gui);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent event) {
                if (!gui.isClosing.getAndSet(true)) {
                    frame.setTitle("Minecraft server - shutting down!");
                    server.halt(true);
                    gui.runFinalizers();
                }
            }
        });
        gui.addFinalizer(frame::dispose);
        gui.start();
        return gui;
    }
 
    private MinecraftServerGui(DedicatedServer server) {
        this.server = server;
        this.setPreferredSize(new Dimension(854, 480));
        this.setLayout(new BorderLayout());
 
        try {
            this.add(this.buildChatPanel(), "Center");
            this.add(this.buildInfoPanel(), "West");
        } catch (Exception var3) {
            LOGGER.error("Couldn't build server GUI", (Throwable)var3);
        }
    }
 
    public void addFinalizer(Runnable finalizer) {
        this.finalizers.add(finalizer);
    }
 
    private JComponent buildInfoPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        StatsComponent comp = new StatsComponent(this.server);
        this.finalizers.add(comp::close);
        panel.add(comp, "North");
        panel.add(this.buildPlayerPanel(), "Center");
        panel.setBorder(new TitledBorder(new EtchedBorder(), "Stats"));
        return panel;
    }
 
    private JComponent buildPlayerPanel() {
        JList<?> playerList = new PlayerListComponent(this.server);
        JScrollPane scrollPane = new JScrollPane(playerList, 22, 30);
        scrollPane.setBorder(new TitledBorder(new EtchedBorder(), "Players"));
        return scrollPane;
    }
 
    private JComponent buildChatPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        JTextArea chatArea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(chatArea, 22, 30);
        chatArea.setEditable(false);
        chatArea.setFont(MONOSPACED);
        JTextField chatField = new JTextField();
        chatField.addActionListener(event -> {
            String text = chatField.getText().trim();
            if (!text.isEmpty()) {
                this.server.handleConsoleInput(text, this.server.createCommandSourceStack());
            }
 
            chatField.setText("");
        });
        chatArea.addFocusListener(new FocusAdapter() {
            {
                Objects.requireNonNull(MinecraftServerGui.this);
            }
 
            @Override
            public void focusGained(FocusEvent arg0) {
            }
        });
        panel.add(scrollPane, "Center");
        panel.add(chatField, "South");
        panel.setBorder(new TitledBorder(new EtchedBorder(), "Log and chat"));
        this.logAppenderThread = new Thread(() -> {
            String line;
            while ((line = LogQueues.getNextLogEvent("ServerGuiConsole")) != null) {
                this.print(chatArea, scrollPane, line);
            }
        });
        this.logAppenderThread.setUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler(LOGGER));
        this.logAppenderThread.setDaemon(true);
        return panel;
    }
 
    public void start() {
        this.logAppenderThread.start();
    }
 
    public void close() {
        if (!this.isClosing.getAndSet(true)) {
            this.runFinalizers();
        }
    }
 
    private void runFinalizers() {
        this.finalizers.forEach(Runnable::run);
    }
 
    public void print(JTextArea console, JScrollPane scrollPane, String line) {
        if (!SwingUtilities.isEventDispatchThread()) {
            SwingUtilities.invokeLater(() -> this.print(console, scrollPane, line));
        } else {
            Document document = console.getDocument();
            JScrollBar scrollBar = scrollPane.getVerticalScrollBar();
            boolean shouldScroll = false;
            if (scrollPane.getViewport().getView() == console) {
                shouldScroll = scrollBar.getValue() + scrollBar.getSize().getHeight() + MONOSPACED.getSize() * 4 > scrollBar.getMaximum();
            }
 
            try {
                document.insertString(document.getLength(), line, null);
            } catch (BadLocationException var8) {
            }
 
            if (shouldScroll) {
                scrollBar.setValue(Integer.MAX_VALUE);
            }
        }
    }
}

引用的其他类