-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathJavaExample.java
More file actions
80 lines (65 loc) · 2.75 KB
/
JavaExample.java
File metadata and controls
80 lines (65 loc) · 2.75 KB
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
package rlbotexample;
import rlbot.manager.BotManager;
import rlbotexample.util.PortReader;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.Set;
import java.util.stream.Collectors;
/**
* See JavaAgent.py for usage instructions.
*
* Look inside SampleBot.java for the actual bot logic!
*/
public class JavaExample {
private static final int DEFAULT_PORT = 17357;
public static void main(String[] args) {
BotManager botManager = new BotManager();
int port = PortReader.readPortFromArgs(args).orElseGet(() -> {
System.out.println("Could not read port from args, using default!");
return DEFAULT_PORT;
});
SamplePythonInterface pythonInterface = new SamplePythonInterface(port, botManager);
new Thread(pythonInterface::start).start();
displayWindow(botManager, port);
}
private static void displayWindow(BotManager botManager, int port) {
JFrame frame = new JFrame("Java Bot");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
BorderLayout borderLayout = new BorderLayout();
panel.setLayout(borderLayout);
JPanel dataPanel = new JPanel();
dataPanel.setLayout(new BoxLayout(dataPanel, BoxLayout.Y_AXIS));
dataPanel.setBorder(new EmptyBorder(0, 10, 0, 0));
dataPanel.add(new JLabel("Listening on port " + port), BorderLayout.CENTER);
dataPanel.add(new JLabel("I'm the thing controlling the Java bot, keep me open :)"), BorderLayout.CENTER);
JLabel botsRunning = new JLabel("Bots running: ");
dataPanel.add(botsRunning, BorderLayout.CENTER);
panel.add(dataPanel, BorderLayout.CENTER);
frame.add(panel);
URL url = JavaExample.class.getClassLoader().getResource("icon.png");
Image image = Toolkit.getDefaultToolkit().createImage(url);
panel.add(new JLabel(new ImageIcon(image)), BorderLayout.WEST);
frame.setIconImage(image);
frame.pack();
frame.setVisible(true);
ActionListener myListener = e -> {
Set<Integer> runningBotIndices = botManager.getRunningBotIndices();
String botsStr;
if (runningBotIndices.isEmpty()) {
botsStr = "None";
} else {
botsStr = runningBotIndices.stream()
.sorted()
.map(i -> "#" + i)
.collect(Collectors.joining(", "));
}
botsRunning.setText("Bots indices running: " + botsStr);
};
new Timer(1000, myListener).start();
}
}