-
-
Notifications
You must be signed in to change notification settings - Fork 347
/
0042-Make-the-GUI-better.patch
1205 lines (1197 loc) · 42.8 KB
/
0042-Make-the-GUI-better.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From 566ac634b6c6db3e101070eaef5a7e851ef4b303 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <Blake.Galbreath@GMail.com>
Date: Thu, 16 Jan 2020 14:59:16 -0600
Subject: [PATCH] Make the GUI better
---
.../net/minecraft/server/DedicatedServer.java | 9 +-
.../net/minecraft/server/MinecraftServer.java | 1 +
.../java/net/pl3x/purpur/gui/ServerGUI.java | 123 +++++++++++++++
.../purpur/gui/console/JColorTextPane.java | 81 ++++++++++
.../purpur/gui/console/JConsolePanel.java | 128 ++++++++++++++++
.../gui/info/DetailsListSelectionModel.java | 21 +++
.../net/pl3x/purpur/gui/info/JInfoPanel.java | 42 +++++
.../net/pl3x/purpur/gui/info/RAMDetails.java | 59 +++++++
.../purpur/gui/info/graph/GraphColor.java | 44 ++++++
.../pl3x/purpur/gui/info/graph/GraphData.java | 47 ++++++
.../pl3x/purpur/gui/info/graph/RAMGraph.java | 144 ++++++++++++++++++
.../purpur/gui/playerlist/JPlayerList.java | 57 +++++++
.../playerlist/PlayerListCellRenderer.java | 22 +++
.../gui/playerlist/PlayerListModel.java | 47 ++++++
.../playerlist/PlayerListMouseAdapter.java | 32 ++++
.../net/pl3x/purpur/gui/util/GUIColor.java | 54 +++++++
.../purpur/util/HighlightErrorConverter.java | 86 +++++++++++
src/main/resources/log4j2.xml | 13 +-
18 files changed, 1005 insertions(+), 5 deletions(-)
create mode 100644 src/main/java/net/pl3x/purpur/gui/ServerGUI.java
create mode 100644 src/main/java/net/pl3x/purpur/gui/console/JColorTextPane.java
create mode 100644 src/main/java/net/pl3x/purpur/gui/console/JConsolePanel.java
create mode 100644 src/main/java/net/pl3x/purpur/gui/info/DetailsListSelectionModel.java
create mode 100644 src/main/java/net/pl3x/purpur/gui/info/JInfoPanel.java
create mode 100644 src/main/java/net/pl3x/purpur/gui/info/RAMDetails.java
create mode 100644 src/main/java/net/pl3x/purpur/gui/info/graph/GraphColor.java
create mode 100644 src/main/java/net/pl3x/purpur/gui/info/graph/GraphData.java
create mode 100644 src/main/java/net/pl3x/purpur/gui/info/graph/RAMGraph.java
create mode 100644 src/main/java/net/pl3x/purpur/gui/playerlist/JPlayerList.java
create mode 100644 src/main/java/net/pl3x/purpur/gui/playerlist/PlayerListCellRenderer.java
create mode 100644 src/main/java/net/pl3x/purpur/gui/playerlist/PlayerListModel.java
create mode 100644 src/main/java/net/pl3x/purpur/gui/playerlist/PlayerListMouseAdapter.java
create mode 100644 src/main/java/net/pl3x/purpur/gui/util/GUIColor.java
create mode 100644 src/main/java/net/pl3x/purpur/util/HighlightErrorConverter.java
diff --git a/src/main/java/net/minecraft/server/DedicatedServer.java b/src/main/java/net/minecraft/server/DedicatedServer.java
index cf5abd393..73d2b32c5 100644
--- a/src/main/java/net/minecraft/server/DedicatedServer.java
+++ b/src/main/java/net/minecraft/server/DedicatedServer.java
@@ -8,6 +8,8 @@ import com.mojang.authlib.GameProfileRepository;
import com.mojang.authlib.minecraft.MinecraftSessionService;
import com.mojang.authlib.yggdrasil.YggdrasilAuthenticationService;
import com.mojang.datafixers.DataFixer;
+
+import java.awt.GraphicsEnvironment;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
@@ -51,7 +53,7 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer
public DedicatedServerSettings propertyManager;
private EnumGamemode o;
@Nullable
- private ServerGUI p;
+ private net.pl3x.purpur.gui.ServerGUI p; // Purpur
// CraftBukkit start - Signature changed
public DedicatedServer(joptsimple.OptionSet options, DedicatedServerSettings dedicatedserversettings, DataFixer datafixer, YggdrasilAuthenticationService yggdrasilauthenticationservice, MinecraftSessionService minecraftsessionservice, GameProfileRepository gameprofilerepository, UserCache usercache, WorldLoadListenerFactory worldloadlistenerfactory, String s) {
@@ -87,6 +89,7 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer
return;
}
// Paper start - Use TerminalConsoleAppender
+ if (DedicatedServer.this.p == null || System.console() != null) // Purpur - has no GUI or has console (did not double click)
new com.destroystokyo.paper.console.PaperConsole(DedicatedServer.this).start();
/*
jline.console.ConsoleReader bufferedreader = reader;
@@ -420,7 +423,7 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer
@Override
public void exit() {
if (this.p != null) {
- this.p.b();
+ this.p.close(); // Purpur
}
if (this.remoteControlListener != null) {
@@ -519,7 +522,7 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer
public void bc() {
if (this.p == null) {
- this.p = ServerGUI.a(this);
+ this.p = net.pl3x.purpur.gui.ServerGUI.create(this); // Purpur
}
}
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 9c96994f2..6b6439493 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -1362,6 +1362,7 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
return true;
}
+ public void addTickable(Runnable tickable) { b(tickable); } // Purpur - OBFHELPER
public void b(Runnable runnable) {
this.tickables.add(runnable);
}
diff --git a/src/main/java/net/pl3x/purpur/gui/ServerGUI.java b/src/main/java/net/pl3x/purpur/gui/ServerGUI.java
new file mode 100644
index 000000000..973b5efef
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/gui/ServerGUI.java
@@ -0,0 +1,123 @@
+package net.pl3x.purpur.gui;
+
+import com.google.common.collect.Lists;
+import net.minecraft.server.DedicatedServer;
+import net.pl3x.purpur.gui.console.JConsolePanel;
+import net.pl3x.purpur.gui.info.JInfoPanel;
+import net.pl3x.purpur.gui.playerlist.JPlayerList;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import javax.swing.JComponent;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.ScrollPaneConstants;
+import javax.swing.UIManager;
+import javax.swing.WindowConstants;
+import javax.swing.border.EtchedBorder;
+import javax.swing.border.TitledBorder;
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.util.Collection;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+public class ServerGUI extends JComponent {
+ private static final Logger LOGGER = LogManager.getLogger();
+
+ private final DedicatedServer server;
+
+ private final Collection<Runnable> finalizers = Lists.newArrayList();
+ private final AtomicBoolean isClosing = new AtomicBoolean();
+
+ private JConsolePanel consolePanel;
+
+ public static ServerGUI create(final DedicatedServer dedicatedserver) {
+ try {
+ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
+ } catch (Exception ignore) {
+ }
+
+ JFrame window = new JFrame("Purpur Minecraft Server");
+ ServerGUI serverGUI = new ServerGUI(dedicatedserver);
+
+ window.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
+ window.add(serverGUI);
+ window.pack();
+ window.setLocationRelativeTo(null);
+ window.setVisible(true);
+
+ window.addWindowListener(new WindowAdapter() {
+ @Override
+ public void windowClosing(WindowEvent windowevent) {
+ if (!serverGUI.isClosing.getAndSet(true)) {
+ window.setTitle("Purpur Minecraft Server - Shutting Down!");
+ dedicatedserver.safeShutdown(true);
+ serverGUI.runFinalizers();
+ }
+
+ }
+ });
+
+ serverGUI.addFinalizer(window::dispose);
+ serverGUI.start();
+
+ return serverGUI;
+ }
+
+ private ServerGUI(DedicatedServer dedicatedserver) {
+ this.server = dedicatedserver;
+
+ setPreferredSize(new Dimension(854, 480));
+ setLayout(new BorderLayout());
+
+ consolePanel = new JConsolePanel(server);
+
+ try {
+ add(consolePanel, "Center");
+ add(buildInfoPanel(), "West");
+ } catch (Exception exception) {
+ LOGGER.error("Couldn't build server GUI", exception);
+ }
+ }
+
+ private void addFinalizer(Runnable runnable) {
+ finalizers.add(runnable);
+ }
+
+ private JComponent buildInfoPanel() {
+ JPanel jpanel = new JPanel(new BorderLayout());
+
+ JInfoPanel serverInfo = new JInfoPanel(server);
+ finalizers.add(serverInfo::stop);
+
+ jpanel.add(serverInfo, "North");
+ jpanel.add(buildPlayerPanel(), "Center");
+
+ jpanel.setBorder(new TitledBorder(new EtchedBorder(), "Stats"));
+ return jpanel;
+ }
+
+ private JComponent buildPlayerPanel() {
+ JScrollPane jscrollpane = new JScrollPane(new JPlayerList(server), ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
+ jscrollpane.setBorder(new TitledBorder(new EtchedBorder(), "Players"));
+ return jscrollpane;
+ }
+
+ public void start() {
+ consolePanel.start();
+ }
+
+ public void close() {
+ if (!isClosing.getAndSet(true)) {
+ runFinalizers();
+ }
+
+ }
+
+ private void runFinalizers() {
+ finalizers.forEach(Runnable::run);
+ }
+}
diff --git a/src/main/java/net/pl3x/purpur/gui/console/JColorTextPane.java b/src/main/java/net/pl3x/purpur/gui/console/JColorTextPane.java
new file mode 100644
index 000000000..55feec811
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/gui/console/JColorTextPane.java
@@ -0,0 +1,81 @@
+package net.pl3x.purpur.gui.console;
+
+import net.md_5.bungee.api.ChatColor;
+import net.md_5.bungee.api.chat.BaseComponent;
+import net.md_5.bungee.api.chat.TextComponent;
+import net.pl3x.purpur.gui.util.GUIColor;
+
+import javax.swing.JTextPane;
+import javax.swing.Timer;
+import javax.swing.text.AttributeSet;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.SimpleAttributeSet;
+import javax.swing.text.StyleConstants;
+import javax.swing.text.StyleContext;
+import java.util.HashSet;
+import java.util.Set;
+
+public class JColorTextPane extends JTextPane {
+ private static final GUIColor DEFAULT_COLOR = GUIColor.BLACK;
+
+ public void append(String str) {
+ BaseComponent[] components = TextComponent.fromLegacyText(DEFAULT_COLOR.getCode() + str, ChatColor.BLACK);
+ for (BaseComponent component : components) {
+ String text = component.toPlainText();
+ if (text == null || text.isEmpty()) {
+ continue;
+ }
+
+ GUIColor guiColor = GUIColor.getColor(component.getColor());
+
+ StyleContext context = StyleContext.getDefaultStyleContext();
+ AttributeSet attr = context.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, guiColor.getColor());
+ attr = context.addAttribute(attr, StyleConstants.CharacterConstants.Bold, component.isBold() || guiColor != DEFAULT_COLOR);
+ attr = context.addAttribute(attr, StyleConstants.CharacterConstants.Italic, component.isItalic());
+ attr = context.addAttribute(attr, StyleConstants.CharacterConstants.Underline, component.isUnderlined());
+ attr = context.addAttribute(attr, StyleConstants.CharacterConstants.StrikeThrough, component.isStrikethrough());
+ //attr = context.addAttribute(attr, StyleConstants.CharacterConstants.Blink, component.isObfuscated()); // no such thing as Blink, sadly
+
+ try {
+ int pos = getDocument().getLength();
+ getDocument().insertString(pos, text, attr);
+
+ if (component.isObfuscated()) {
+ // dirty hack to blink some text
+ Blink blink = new Blink(pos, text.length(), attr, context.addAttribute(attr, StyleConstants.Foreground, getBackground()));
+ BLINKS.add(blink);
+ }
+ } catch (BadLocationException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private static final Set<Blink> BLINKS = new HashSet<>();
+ private static boolean SYNC_BLINK;
+
+ static {
+ Timer timer = new Timer(500, e -> {
+ SYNC_BLINK = !SYNC_BLINK;
+ BLINKS.forEach(Blink::blink);
+ });
+ timer.start();
+ }
+
+ public class Blink {
+ private final int start, length;
+ private final AttributeSet attr1;
+ private final AttributeSet attr2;
+
+ private Blink(int start, int length, AttributeSet attr1, AttributeSet attr2) {
+ this.start = start;
+ this.length = length;
+ this.attr1 = attr1;
+ this.attr2 = attr2;
+ }
+
+ private void blink() {
+ getStyledDocument().setCharacterAttributes(start, length, SYNC_BLINK ? attr1 : attr2, true);
+ }
+ }
+}
diff --git a/src/main/java/net/pl3x/purpur/gui/console/JConsolePanel.java b/src/main/java/net/pl3x/purpur/gui/console/JConsolePanel.java
new file mode 100644
index 000000000..fb017a070
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/gui/console/JConsolePanel.java
@@ -0,0 +1,128 @@
+package net.pl3x.purpur.gui.console;
+
+import com.mojang.util.QueueLogAppender;
+import net.minecraft.server.DedicatedServer;
+import net.minecraft.server.DefaultUncaughtExceptionHandler;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import javax.swing.AbstractAction;
+import javax.swing.JPanel;
+import javax.swing.JScrollBar;
+import javax.swing.JScrollPane;
+import javax.swing.JTextField;
+import javax.swing.KeyStroke;
+import javax.swing.ScrollPaneConstants;
+import javax.swing.SwingUtilities;
+import javax.swing.border.EtchedBorder;
+import javax.swing.border.TitledBorder;
+import java.awt.BorderLayout;
+import java.awt.Font;
+import java.awt.event.ActionEvent;
+import java.util.LinkedList;
+
+public class JConsolePanel extends JPanel {
+ private static final Font MONOSPACED = new Font("Monospaced", Font.PLAIN, 12);
+ private static final Logger LOGGER = LogManager.getLogger();
+
+ private final CommandHistory history = new CommandHistory();
+ private String currentCommand = "";
+ private int historyIndex = 0;
+
+ private Thread logAppenderThread;
+
+ public JConsolePanel(DedicatedServer server) {
+ super(new BorderLayout());
+
+ JColorTextPane console = new JColorTextPane();
+ console.setEditable(false);
+ console.setFont(MONOSPACED);
+
+ JTextField jtextfield = new JTextField();
+ jtextfield.addActionListener((actionevent) -> {
+ String msg = jtextfield.getText().trim();
+ if (!msg.isEmpty()) {
+ server.issueCommand(msg, server.getServerCommandListener());
+ history.add(msg);
+ historyIndex = -1;
+ }
+ jtextfield.setText("");
+ });
+ jtextfield.getInputMap().put(KeyStroke.getKeyStroke("UP"), "up");
+ jtextfield.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "down");
+ jtextfield.getActionMap().put("up", new AbstractAction() {
+ @Override
+ public void actionPerformed(ActionEvent actionEvent) {
+ if (historyIndex < 0) {
+ currentCommand = jtextfield.getText();
+ }
+ if (historyIndex < history.size() - 1) {
+ jtextfield.setText(history.get(++historyIndex));
+ }
+ }
+ });
+ jtextfield.getActionMap().put("down", new AbstractAction() {
+ @Override
+ public void actionPerformed(ActionEvent actionEvent) {
+ if (historyIndex >= 0) {
+ if (historyIndex == 0) {
+ --historyIndex;
+ jtextfield.setText(currentCommand);
+ } else {
+ --historyIndex;
+ jtextfield.setText(history.get(historyIndex));
+ }
+ }
+ }
+ });
+
+ JScrollPane jscrollpane = new JScrollPane(console, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
+
+ add(jscrollpane, "Center");
+ add(jtextfield, "South");
+ setBorder(new TitledBorder(new EtchedBorder(), "Console"));
+
+ logAppenderThread = new Thread(() -> {
+ String msg;
+ while ((msg = QueueLogAppender.getNextLogEvent("ServerGuiConsole")) != null) {
+ this.print(console, jscrollpane, msg);
+ }
+
+ });
+ logAppenderThread.setUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler(LOGGER));
+ logAppenderThread.setDaemon(true);
+ }
+
+ public void start() {
+ logAppenderThread.start();
+ }
+
+ private void print(JColorTextPane console, JScrollPane jscrollpane, String str) {
+ if (!SwingUtilities.isEventDispatchThread()) {
+ SwingUtilities.invokeLater(() -> print(console, jscrollpane, str));
+ } else {
+ JScrollBar jscrollbar = jscrollpane.getVerticalScrollBar();
+ boolean scrollToBottom = false;
+
+ if (jscrollpane.getViewport().getView() == console) {
+ scrollToBottom = (double) jscrollbar.getValue() + jscrollbar.getSize().getHeight() + (double) (MONOSPACED.getSize() * 4) > (double) jscrollbar.getMaximum();
+ }
+
+ console.append(str);
+
+ if (scrollToBottom) {
+ jscrollbar.setValue(Integer.MAX_VALUE);
+ }
+ }
+ }
+
+ public static class CommandHistory extends LinkedList<String> {
+ @Override
+ public boolean add(String command) {
+ if (size() > 1000) {
+ remove();
+ }
+ return super.offerFirst(command);
+ }
+ }
+}
diff --git a/src/main/java/net/pl3x/purpur/gui/info/DetailsListSelectionModel.java b/src/main/java/net/pl3x/purpur/gui/info/DetailsListSelectionModel.java
new file mode 100644
index 000000000..4e0b80fdd
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/gui/info/DetailsListSelectionModel.java
@@ -0,0 +1,21 @@
+package net.pl3x.purpur.gui.info;
+
+import javax.swing.DefaultListSelectionModel;
+
+public class DetailsListSelectionModel extends DefaultListSelectionModel {
+ @Override
+ public void setAnchorSelectionIndex(final int anchorIndex) {
+ }
+
+ @Override
+ public void setLeadAnchorNotificationEnabled(final boolean flag) {
+ }
+
+ @Override
+ public void setLeadSelectionIndex(final int leadIndex) {
+ }
+
+ @Override
+ public void setSelectionInterval(final int index0, final int index1) {
+ }
+}
diff --git a/src/main/java/net/pl3x/purpur/gui/info/JInfoPanel.java b/src/main/java/net/pl3x/purpur/gui/info/JInfoPanel.java
new file mode 100644
index 000000000..c4903c7db
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/gui/info/JInfoPanel.java
@@ -0,0 +1,42 @@
+package net.pl3x.purpur.gui.info;
+
+import net.minecraft.server.MinecraftServer;
+import net.pl3x.purpur.gui.info.graph.RAMGraph;
+
+import javax.swing.JPanel;
+import javax.swing.Timer;
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+
+public class JInfoPanel extends JPanel {
+ private final Timer timer;
+ private final RAMGraph ramGraph;
+
+ public JInfoPanel(MinecraftServer server) {
+ super(new BorderLayout());
+
+ setOpaque(false);
+
+ ramGraph = new RAMGraph();
+ RAMDetails ramDetails = new RAMDetails(server);
+
+ add(ramGraph, "North");
+ add(ramDetails, "Center");
+
+ timer = new Timer(500, (event) -> {
+ ramGraph.update();
+ ramDetails.update();
+ });
+ timer.start();
+ }
+
+ @Override
+ public Dimension getPreferredSize() {
+ return new Dimension(350, 200);
+ }
+
+ public void stop() {
+ timer.stop();
+ ramGraph.stop();
+ }
+}
diff --git a/src/main/java/net/pl3x/purpur/gui/info/RAMDetails.java b/src/main/java/net/pl3x/purpur/gui/info/RAMDetails.java
new file mode 100644
index 000000000..1e049d575
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/gui/info/RAMDetails.java
@@ -0,0 +1,59 @@
+package net.pl3x.purpur.gui.info;
+
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.SystemUtils;
+import net.pl3x.purpur.gui.info.graph.GraphData;
+import net.pl3x.purpur.gui.info.graph.RAMGraph;
+import org.bukkit.Bukkit;
+
+import javax.swing.DefaultListCellRenderer;
+import javax.swing.JList;
+import javax.swing.border.EmptyBorder;
+import java.awt.Dimension;
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.util.Locale;
+import java.util.Vector;
+
+public class RAMDetails extends JList<String> {
+ private static final DecimalFormat DECIMAL_FORMAT = SystemUtils.a(new DecimalFormat("########0.000"), (format)
+ -> format.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ROOT)));
+
+ private final MinecraftServer server;
+
+ public RAMDetails(MinecraftServer server) {
+ this.server = server;
+
+ setBorder(new EmptyBorder(0, 10, 0, 0));
+ setFixedCellHeight(20);
+ setOpaque(false);
+
+ DefaultListCellRenderer renderer = new DefaultListCellRenderer();
+ renderer.setOpaque(false);
+ setCellRenderer(renderer);
+
+ setSelectionModel(new DetailsListSelectionModel());
+ }
+
+ @Override
+ public Dimension getPreferredSize() {
+ return new Dimension(350, 100);
+ }
+
+ public void update() {
+ GraphData data = RAMGraph.DATA.peekLast();
+ Vector<String> vector = new Vector<>();
+ vector.add("Memory use: " + (data.getUsedMem() / 1024L / 1024L) + " mb (" + (data.getFree() * 100L / data.getMax()) + "% free)");
+ vector.add("Heap: " + (data.getTotal() / 1024L / 1024L) + " / " + (data.getMax() / 1024L / 1024L) + " mb");
+ vector.add("Avg tick: " + DECIMAL_FORMAT.format(server.tickTimes5s.getAverage()) + " ms");
+ setListData(vector);
+ }
+
+ private double getAverage(long[] values) {
+ long total = 0L;
+ for (long value : values) {
+ total += value;
+ }
+ return (double) total / (double) values.length;
+ }
+}
diff --git a/src/main/java/net/pl3x/purpur/gui/info/graph/GraphColor.java b/src/main/java/net/pl3x/purpur/gui/info/graph/GraphColor.java
new file mode 100644
index 000000000..2fdb9bdd0
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/gui/info/graph/GraphColor.java
@@ -0,0 +1,44 @@
+package net.pl3x.purpur.gui.info.graph;
+
+import java.awt.Color;
+
+public class GraphColor {
+ private static final Color[] colorLine = new Color[101];
+ private static final Color[] colorFill = new Color[101];
+
+ static {
+ for (int i = 0; i < 101; i++) {
+ Color color = createColor(i);
+ colorLine[i] = new Color(color.getRed() / 2, color.getGreen() / 2, color.getBlue() / 2, 255);
+ colorFill[i] = new Color(colorLine[i].getRed(), colorLine[i].getGreen(), colorLine[i].getBlue(), 125);
+ }
+ }
+
+ public static Color getLineColor(int percent) {
+ return colorLine[percent];
+ }
+
+ public static Color getFillColor(int percent) {
+ return colorFill[percent];
+ }
+
+ private static Color createColor(int percent) {
+ if (percent <= 50) {
+ return new Color(0X00FF00);
+ }
+
+ int value = 510 - (int) (Math.min(Math.max(0, ((percent - 50) / 50F)), 1) * 510);
+
+ int red, green;
+ if (value < 255) {
+ red = 255;
+ green = (int) (Math.sqrt(value) * 16);
+ } else {
+ green = 255;
+ value = value - 255;
+ red = 255 - (value * value / 255);
+ }
+
+ return new Color(red, green, 0);
+ }
+}
diff --git a/src/main/java/net/pl3x/purpur/gui/info/graph/GraphData.java b/src/main/java/net/pl3x/purpur/gui/info/graph/GraphData.java
new file mode 100644
index 000000000..85babad9c
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/gui/info/graph/GraphData.java
@@ -0,0 +1,47 @@
+package net.pl3x.purpur.gui.info.graph;
+
+import java.awt.Color;
+
+public class GraphData {
+ private long total;
+ private long free;
+ private long max;
+ private long usedMem;
+ private int usedPercent;
+
+ public GraphData(long total, long free, long max) {
+ this.total = total;
+ this.free = free;
+ this.max = max;
+ this.usedMem = total - free;
+ this.usedPercent = usedMem == 0 ? 0 : (int) (usedMem * 100L / max);
+ }
+
+ public long getTotal() {
+ return total;
+ }
+
+ public long getFree() {
+ return free;
+ }
+
+ public long getMax() {
+ return max;
+ }
+
+ public long getUsedMem() {
+ return usedMem;
+ }
+
+ public int getUsedPercent() {
+ return usedPercent;
+ }
+
+ public Color getFillColor() {
+ return GraphColor.getFillColor(usedPercent);
+ }
+
+ public Color getLineColor() {
+ return GraphColor.getLineColor(usedPercent);
+ }
+}
diff --git a/src/main/java/net/pl3x/purpur/gui/info/graph/RAMGraph.java b/src/main/java/net/pl3x/purpur/gui/info/graph/RAMGraph.java
new file mode 100644
index 000000000..0c0c73bee
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/gui/info/graph/RAMGraph.java
@@ -0,0 +1,144 @@
+package net.pl3x.purpur.gui.info.graph;
+
+import javax.swing.JComponent;
+import javax.swing.SwingUtilities;
+import javax.swing.Timer;
+import javax.swing.ToolTipManager;
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Graphics;
+import java.awt.MouseInfo;
+import java.awt.Point;
+import java.awt.PointerInfo;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.LinkedList;
+import java.util.concurrent.TimeUnit;
+
+public class RAMGraph extends JComponent {
+ public static final LinkedList<GraphData> DATA = new LinkedList<GraphData>() {
+ @Override
+ public boolean add(GraphData data) {
+ if (size() >= 348) {
+ remove();
+ }
+ return super.add(data);
+ }
+ };
+
+ static {
+ GraphData empty = new GraphData(0, 0, 0);
+ for (int i = 0; i < 350; i++) {
+ DATA.add(empty);
+ }
+ }
+
+ private final Timer timer;
+ private final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");
+
+ private int currentTick;
+
+ public RAMGraph() {
+ ToolTipManager.sharedInstance().setInitialDelay(0);
+
+ addMouseListener(new MouseAdapter() {
+ final int defaultDismissTimeout = ToolTipManager.sharedInstance().getDismissDelay();
+ final int dismissDelayMinutes = (int) TimeUnit.MINUTES.toMillis(10);
+
+ @Override
+ public void mouseEntered(MouseEvent me) {
+ ToolTipManager.sharedInstance().setDismissDelay(dismissDelayMinutes);
+ }
+
+ @Override
+ public void mouseExited(MouseEvent me) {
+ ToolTipManager.sharedInstance().setDismissDelay(defaultDismissTimeout);
+ }
+ });
+
+ timer = new Timer(50, (event) -> repaint());
+ timer.start();
+ }
+
+ @Override
+ public Dimension getPreferredSize() {
+ return new Dimension(350, 110);
+ }
+
+ public void update() {
+ Runtime jvm = Runtime.getRuntime();
+ DATA.add(new GraphData(jvm.totalMemory(), jvm.freeMemory(), jvm.maxMemory()));
+
+ PointerInfo pointerInfo = MouseInfo.getPointerInfo();
+ if (pointerInfo != null) {
+ Point point = pointerInfo.getLocation();
+ if (point != null) {
+ Point loc = new Point(point);
+ SwingUtilities.convertPointFromScreen(loc, this);
+ if (this.contains(loc)) {
+ ToolTipManager.sharedInstance().mouseMoved(
+ new MouseEvent(this, -1, System.currentTimeMillis(), 0, loc.x, loc.y,
+ point.x, point.y, 0, false, 0));
+ }
+ }
+ }
+
+ currentTick++;
+ }
+
+ @Override
+ public void paint(Graphics graphics) {
+ graphics.setColor(new Color(0xFFFFFFFF));
+ graphics.fillRect(0, 0, 350, 100);
+
+ graphics.setColor(new Color(0x888888));
+ graphics.drawLine(1, 25, 348, 25);
+ graphics.drawLine(1, 50, 348, 50);
+ graphics.drawLine(1, 75, 348, 75);
+
+ int i = 0;
+ for (GraphData data : DATA) {
+ i++;
+ if ((i + currentTick) % 120 == 0) {
+ graphics.setColor(new Color(0x888888));
+ graphics.drawLine(i, 1, i, 99);
+ }
+ int used = data.getUsedPercent();
+ if (used > 0) {
+ Color color = data.getLineColor();
+ graphics.setColor(data.getFillColor());
+ graphics.fillRect(i, 100 - used, 1, used);
+ graphics.setColor(color);
+ graphics.fillRect(i, 100 - used, 1, 1);
+ }
+ }
+
+ graphics.setColor(new Color(0xFF000000));
+ graphics.drawRect(0, 0, 348, 100);
+
+ Point m = getMousePosition();
+ if (m != null && m.x > 0 && m.x < 348 && m.y > 0 && m.y < 100) {
+ GraphData data = DATA.get(m.x);
+ int used = data.getUsedPercent();
+ graphics.setColor(new Color(0x000000));
+ graphics.drawLine(m.x, 1, m.x, 99);
+ graphics.drawOval(m.x - 2, 100 - used - 2, 5, 5);
+ graphics.setColor(data.getLineColor());
+ graphics.fillOval(m.x - 2, 100 - used - 2, 5, 5);
+ setToolTipText(String.format("<html><body>Used: %s mb (%s%%)<br/>%s</body></html>",
+ Math.round(data.getUsedMem() / 1024F / 1024F),
+ used, getTime(m.x)));
+ }
+ }
+
+ public String getTime(int halfSeconds) {
+ int millis = (348 - halfSeconds) / 2 * 1000;
+ return TIME_FORMAT.format(new Date((System.currentTimeMillis() - millis)));
+ }
+
+ public void stop() {
+ timer.stop();
+ }
+}
diff --git a/src/main/java/net/pl3x/purpur/gui/playerlist/JPlayerList.java b/src/main/java/net/pl3x/purpur/gui/playerlist/JPlayerList.java
new file mode 100644
index 000000000..17e75a207
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/gui/playerlist/JPlayerList.java
@@ -0,0 +1,57 @@
+package net.pl3x.purpur.gui.playerlist;
+
+import net.minecraft.server.EntityPlayer;
+import net.minecraft.server.MinecraftServer;
+
+import javax.swing.JList;
+import javax.swing.JMenuItem;
+import javax.swing.JPopupMenu;
+import javax.swing.ListSelectionModel;
+import java.awt.Dimension;
+
+public class JPlayerList extends JList<EntityPlayer> {
+ private final MinecraftServer server;
+ private final PlayerListModel model;
+ private final JPopupMenu popupMenu;
+ private int currentSelection = -1;
+ private int tickCount;
+
+ public JPlayerList(MinecraftServer server) {
+ this.server = server;
+ server.addTickable(this::tick);
+
+ setModel(model = new PlayerListModel(this));
+ setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+ addListSelectionListener(event -> currentSelection = event.getFirstIndex());
+ addMouseListener(new PlayerListMouseAdapter(this));
+ setCellRenderer(new PlayerListCellRenderer());
+
+ popupMenu = new JPopupMenu();
+ popupMenu.add(new JMenuItem("Details"));
+ popupMenu.add(new JPopupMenu.Separator());
+ popupMenu.add(new JMenuItem("Kick"));
+ popupMenu.add(new JMenuItem("Ban"));
+ popupMenu.add(new JPopupMenu.Separator());
+ popupMenu.add(new JMenuItem("Op"));
+ }
+
+ @Override
+ public Dimension getPreferredSize() {
+ Dimension superPref = super.getPreferredSize();
+ return new Dimension(330, superPref.height);
+ }
+
+ public void tick() {
+ if (tickCount++ % 20 == 0) {
+ model.update(server.getPlayerList().getPlayers());
+ }
+ }
+
+ public int getCurrentSelection() {
+ return currentSelection;
+ }
+
+ public JPopupMenu getPopupMenu() {
+ return popupMenu;
+ }
+}
diff --git a/src/main/java/net/pl3x/purpur/gui/playerlist/PlayerListCellRenderer.java b/src/main/java/net/pl3x/purpur/gui/playerlist/PlayerListCellRenderer.java
new file mode 100644
index 000000000..2f02e9adc
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/gui/playerlist/PlayerListCellRenderer.java
@@ -0,0 +1,22 @@
+package net.pl3x.purpur.gui.playerlist;
+
+import com.mojang.authlib.GameProfile;
+import net.minecraft.server.EntityPlayer;
+
+import javax.swing.DefaultListCellRenderer;
+import javax.swing.JList;
+import java.awt.Component;
+
+public class PlayerListCellRenderer extends DefaultListCellRenderer {
+ @Override
+ public Component getListCellRendererComponent(JList<?> list, Object player, int index, boolean isSelected, boolean cellHasFocus) {
+ super.getListCellRendererComponent(list, player, index, isSelected, cellHasFocus);
+ if (player instanceof EntityPlayer) {
+ GameProfile profile = ((EntityPlayer) player).getProfile();
+ setText(profile.getName());
+ setToolTipText(String.format("<html><body>%s<br/>%s</body></html>",
+ profile.getName(), profile.getId()));
+ }
+ return this;
+ }
+}
diff --git a/src/main/java/net/pl3x/purpur/gui/playerlist/PlayerListModel.java b/src/main/java/net/pl3x/purpur/gui/playerlist/PlayerListModel.java
new file mode 100644
index 000000000..edbf45416
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/gui/playerlist/PlayerListModel.java
@@ -0,0 +1,47 @@
+package net.pl3x.purpur.gui.playerlist;
+
+import net.minecraft.server.EntityPlayer;
+
+import javax.swing.DefaultListModel;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+public class PlayerListModel extends DefaultListModel<EntityPlayer> {
+ private final Set<EntityPlayer> datas = new HashSet<>();
+ private final JPlayerList parent;
+
+ public PlayerListModel(JPlayerList parent) {
+ this.parent = parent;
+ }
+
+ public void update(Collection<EntityPlayer> players) {
+ boolean hadFocus = parent.hasFocus();
+
+ players.forEach(this::add);
+
+ Set<EntityPlayer> result = new HashSet<>(datas);
+ result.removeIf(players::contains);
+ result.forEach(this::remove);
+
+ if (parent.getCurrentSelection() >= 0) {
+ parent.setSelectedIndex(parent.getCurrentSelection());
+ }
+
+ if (hadFocus) {
+ parent.grabFocus();
+ }
+ }
+
+ public void add(EntityPlayer player) {
+ if (datas.add(player)) {
+ addElement(player);
+ }
+ }
+
+ public void remove(EntityPlayer player) {
+ if (datas.remove(player)) {
+ removeElement(player);
+ }
+ }
+}
diff --git a/src/main/java/net/pl3x/purpur/gui/playerlist/PlayerListMouseAdapter.java b/src/main/java/net/pl3x/purpur/gui/playerlist/PlayerListMouseAdapter.java
new file mode 100644
index 000000000..13a5eb3ad
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/gui/playerlist/PlayerListMouseAdapter.java
@@ -0,0 +1,32 @@
+package net.pl3x.purpur.gui.playerlist;
+
+import javax.swing.SwingUtilities;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+