Makes it possible to load a controller without generating a GUI, for better testing Makes sure not to try and parse empty profile lines Saves controller settings in a more readable and appendable format Adds code for using the correct java version for the occasion Adds a new function for writing to files
274 lines
13 KiB
Java
274 lines
13 KiB
Java
package net.knarcraft.minecraftserverlauncher.userinterface;
|
|
|
|
import net.knarcraft.minecraftserverlauncher.profile.Collection;
|
|
import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.util.ArrayList;
|
|
|
|
/**
|
|
* This class takes care of all server controls on the server control tab
|
|
*/
|
|
public class ServerControlTab implements ActionListener {
|
|
|
|
private JComboBox<String> targetServerCombo;
|
|
private JComboBox<String> targetPlayerCombo;
|
|
private JButton kickButton;
|
|
private JButton banButton;
|
|
private JButton opButton;
|
|
private JButton deopButton;
|
|
private JButton customCommandButton;
|
|
private JButton saveServerButton;
|
|
private JButton reloadButton;
|
|
private JButton showConsolesButton;
|
|
private JTextField customCommandTextField;
|
|
|
|
private final ServerLauncherController controller = ServerLauncherController.getInstance();
|
|
private final ArrayList<String> globalPlayers;
|
|
|
|
/**
|
|
* Instantiates a new server control tab
|
|
*
|
|
* @param mainFrame <p>The main frame of the GUI</p>
|
|
* @param controlServers <p>The JPanel to attach the server controls to</p>
|
|
*/
|
|
public ServerControlTab(JFrame mainFrame, JPanel controlServers) {
|
|
this.globalPlayers = new ArrayList<>();
|
|
initialize(mainFrame, controlServers);
|
|
}
|
|
|
|
/**
|
|
* Initializes GUI elements for the server control tab
|
|
*
|
|
* @param mainFrame <p>The main frame of the GUI</p>
|
|
* @param controlServers <p>The JPanel to attach the server controls to</p>
|
|
*/
|
|
private void initialize(JFrame mainFrame, JPanel controlServers) {
|
|
SpringLayout springLayout = new SpringLayout();
|
|
controlServers.setLayout(springLayout);
|
|
targetServerCombo = new JComboBox<>();
|
|
springLayout.putConstraint(SpringLayout.NORTH, targetServerCombo, 10, SpringLayout.NORTH, controlServers);
|
|
controlServers.add(targetServerCombo);
|
|
targetServerCombo.addActionListener(this);
|
|
|
|
targetPlayerCombo = new JComboBox<>();
|
|
springLayout.putConstraint(SpringLayout.NORTH, targetPlayerCombo, 6, SpringLayout.SOUTH, targetServerCombo);
|
|
targetPlayerCombo.setEditable(true);
|
|
controlServers.add(targetPlayerCombo);
|
|
|
|
kickButton = new JButton("Kick");
|
|
springLayout.putConstraint(SpringLayout.NORTH, kickButton, 10, SpringLayout.NORTH, controlServers);
|
|
springLayout.putConstraint(SpringLayout.WEST, kickButton, 6, SpringLayout.EAST, targetServerCombo);
|
|
springLayout.putConstraint(SpringLayout.EAST, kickButton, 104, SpringLayout.WEST, kickButton);
|
|
springLayout.putConstraint(SpringLayout.SOUTH, targetServerCombo, 0, SpringLayout.SOUTH, kickButton);
|
|
controlServers.add(kickButton);
|
|
kickButton.addActionListener(this);
|
|
|
|
banButton = new JButton("Ban");
|
|
springLayout.putConstraint(SpringLayout.NORTH, banButton, 6, SpringLayout.SOUTH, kickButton);
|
|
springLayout.putConstraint(SpringLayout.WEST, banButton, 6, SpringLayout.EAST, targetPlayerCombo);
|
|
springLayout.putConstraint(SpringLayout.EAST, banButton, 104, SpringLayout.WEST, banButton);
|
|
springLayout.putConstraint(SpringLayout.SOUTH, targetPlayerCombo, 0, SpringLayout.SOUTH, banButton);
|
|
controlServers.add(banButton);
|
|
banButton.addActionListener(this);
|
|
|
|
opButton = new JButton("OP");
|
|
springLayout.putConstraint(SpringLayout.NORTH, opButton, 10, SpringLayout.NORTH, controlServers);
|
|
springLayout.putConstraint(SpringLayout.WEST, opButton, 6, SpringLayout.EAST, kickButton);
|
|
springLayout.putConstraint(SpringLayout.EAST, opButton, -10, SpringLayout.EAST, controlServers);
|
|
controlServers.add(opButton);
|
|
opButton.addActionListener(this);
|
|
|
|
deopButton = new JButton("DEOP");
|
|
springLayout.putConstraint(SpringLayout.WEST, deopButton, 6, SpringLayout.EAST, banButton);
|
|
springLayout.putConstraint(SpringLayout.NORTH, deopButton, 5, SpringLayout.SOUTH, opButton);
|
|
springLayout.putConstraint(SpringLayout.EAST, deopButton, -10, SpringLayout.EAST, controlServers);
|
|
controlServers.add(deopButton);
|
|
deopButton.addActionListener(this);
|
|
|
|
JLabel lblTargetServer = new JLabel("Target server");
|
|
springLayout.putConstraint(SpringLayout.WEST, targetServerCombo, 6, SpringLayout.EAST, lblTargetServer);
|
|
springLayout.putConstraint(SpringLayout.EAST, targetServerCombo, 121, SpringLayout.EAST, lblTargetServer);
|
|
springLayout.putConstraint(SpringLayout.NORTH, lblTargetServer, 10, SpringLayout.NORTH, controlServers);
|
|
springLayout.putConstraint(SpringLayout.SOUTH, lblTargetServer, 0, SpringLayout.SOUTH, targetServerCombo);
|
|
springLayout.putConstraint(SpringLayout.WEST, lblTargetServer, 10, SpringLayout.WEST, controlServers);
|
|
controlServers.add(lblTargetServer);
|
|
|
|
JLabel lblTargetPlayer = new JLabel("Target player");
|
|
springLayout.putConstraint(SpringLayout.WEST, targetPlayerCombo, 7, SpringLayout.EAST, lblTargetPlayer);
|
|
springLayout.putConstraint(SpringLayout.EAST, targetPlayerCombo, 122, SpringLayout.EAST, lblTargetPlayer);
|
|
springLayout.putConstraint(SpringLayout.NORTH, lblTargetPlayer, 6, SpringLayout.SOUTH, lblTargetServer);
|
|
springLayout.putConstraint(SpringLayout.SOUTH, lblTargetPlayer, 0, SpringLayout.SOUTH, targetPlayerCombo);
|
|
springLayout.putConstraint(SpringLayout.WEST, lblTargetPlayer, 0, SpringLayout.WEST, lblTargetServer);
|
|
controlServers.add(lblTargetPlayer);
|
|
|
|
customCommandButton = new JButton("Custom command");
|
|
springLayout.putConstraint(SpringLayout.WEST, customCommandButton, 250, SpringLayout.WEST, controlServers);
|
|
springLayout.putConstraint(SpringLayout.EAST, customCommandButton, 0, SpringLayout.EAST, opButton);
|
|
controlServers.add(customCommandButton);
|
|
customCommandButton.addActionListener(this);
|
|
mainFrame.getRootPane().setDefaultButton(customCommandButton);
|
|
|
|
customCommandTextField = new JTextField();
|
|
springLayout.putConstraint(SpringLayout.WEST, customCommandTextField, 10, SpringLayout.WEST, controlServers);
|
|
springLayout.putConstraint(SpringLayout.EAST, customCommandTextField, -6, SpringLayout.WEST, customCommandButton);
|
|
springLayout.putConstraint(SpringLayout.NORTH, customCommandButton, 0, SpringLayout.NORTH, customCommandTextField);
|
|
springLayout.putConstraint(SpringLayout.SOUTH, customCommandTextField, 0, SpringLayout.SOUTH, customCommandButton);
|
|
controlServers.add(customCommandTextField);
|
|
customCommandTextField.setColumns(10);
|
|
|
|
saveServerButton = new JButton("Save server");
|
|
springLayout.putConstraint(SpringLayout.NORTH, customCommandTextField, 6, SpringLayout.SOUTH, saveServerButton);
|
|
springLayout.putConstraint(SpringLayout.NORTH, saveServerButton, 6, SpringLayout.SOUTH, banButton);
|
|
springLayout.putConstraint(SpringLayout.WEST, saveServerButton, 0, SpringLayout.WEST, kickButton);
|
|
springLayout.putConstraint(SpringLayout.EAST, saveServerButton, 104, SpringLayout.WEST, kickButton);
|
|
springLayout.putConstraint(SpringLayout.EAST, saveServerButton, 104, SpringLayout.WEST, kickButton);
|
|
controlServers.add(saveServerButton);
|
|
saveServerButton.addActionListener(this);
|
|
|
|
reloadButton = new JButton("Reload");
|
|
springLayout.putConstraint(SpringLayout.NORTH, reloadButton, 6, SpringLayout.SOUTH, deopButton);
|
|
springLayout.putConstraint(SpringLayout.WEST, reloadButton, 0, SpringLayout.WEST, deopButton);
|
|
springLayout.putConstraint(SpringLayout.EAST, reloadButton, 0, SpringLayout.EAST, opButton);
|
|
controlServers.add(reloadButton);
|
|
reloadButton.addActionListener(this);
|
|
|
|
showConsolesButton = new JButton("View server consoles");
|
|
springLayout.putConstraint(SpringLayout.NORTH, showConsolesButton, 0, SpringLayout.NORTH, saveServerButton);
|
|
springLayout.putConstraint(SpringLayout.WEST, showConsolesButton, 0, SpringLayout.WEST, lblTargetServer);
|
|
springLayout.putConstraint(SpringLayout.EAST, showConsolesButton, 0, SpringLayout.EAST, targetServerCombo);
|
|
controlServers.add(showConsolesButton);
|
|
showConsolesButton.addActionListener(this);
|
|
}
|
|
|
|
/**
|
|
* Updates available servers according to existing collections
|
|
*/
|
|
public void update() {
|
|
this.targetServerCombo.removeAllItems();
|
|
this.targetServerCombo.addItem("All");
|
|
for (Collection collection : ServerLauncherController.getInstance().getCurrentProfile().getCollections()) {
|
|
this.targetServerCombo.addItem(collection.getName());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void actionPerformed(ActionEvent actionEvent) {
|
|
//Registers actions on the server control tab
|
|
handleServerControlTabButtons(actionEvent.getSource());
|
|
}
|
|
|
|
/**
|
|
* Handles buttons and combos on the server control tab
|
|
*
|
|
* @param actionSource <p>The object being interacted with</p>
|
|
*/
|
|
private void handleServerControlTabButtons(Object actionSource) {
|
|
String selectedServerValue = null;
|
|
String selectedPlayerValue = null;
|
|
Object selectedServer = targetServerCombo.getSelectedItem();
|
|
if (selectedServer != null) {
|
|
selectedServerValue = selectedServer.toString();
|
|
}
|
|
Object selectedPlayer = targetPlayerCombo.getSelectedItem();
|
|
if (selectedPlayer != null) {
|
|
selectedPlayerValue = selectedPlayer.toString();
|
|
}
|
|
//Register actions on all commands executed on a specific player
|
|
handlePlayerCommands(actionSource, selectedServerValue, selectedPlayerValue);
|
|
//Registers actions on all commands executed on a specific server
|
|
handleServerCommands(actionSource, selectedServerValue);
|
|
|
|
if (actionSource == showConsolesButton) {
|
|
ServerConsoles.setAsVisible();
|
|
} else if (actionSource == targetServerCombo) {
|
|
updatePlayers();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Updates the list of players currently online on the selected server
|
|
*/
|
|
private void updatePlayers() {
|
|
String selectedServerValue;
|
|
Object selectedServer = targetServerCombo.getSelectedItem();
|
|
if (selectedServer != null) {
|
|
targetPlayerCombo.removeAllItems();
|
|
selectedServerValue = selectedServer.toString();
|
|
if (selectedServerValue.equals("All")) {
|
|
for (String player : this.globalPlayers) {
|
|
targetPlayerCombo.addItem(player);
|
|
}
|
|
} else {
|
|
for (String player : controller.getCurrentProfile().getCollection(selectedServerValue).getServer().getPlayers()) {
|
|
targetPlayerCombo.addItem(player);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Adds a player to the global player list, and updates the players combo
|
|
*
|
|
* @param name <p>The name of the player to add</p>
|
|
*/
|
|
public void addPlayer(String name) {
|
|
this.globalPlayers.add(name);
|
|
this.updatePlayers();
|
|
}
|
|
|
|
/**
|
|
* Removes a player from the global list of players
|
|
*
|
|
* @param name <p>The name of the player to remove</p>
|
|
*/
|
|
public void removePlayer(String name) {
|
|
globalPlayers.removeIf(playerName -> playerName.equals(name));
|
|
this.updatePlayers();
|
|
}
|
|
|
|
/**
|
|
* Handles command buttons acting on a specific server
|
|
*
|
|
* @param actionSource <p>The object being interacted with</p>
|
|
* @param selectedServerValue <p>The server currently selected</p>
|
|
*/
|
|
private void handleServerCommands(Object actionSource, String selectedServerValue) {
|
|
if (selectedServerValue == null) {
|
|
return;
|
|
}
|
|
if (actionSource == customCommandButton) {
|
|
controller.getCurrentProfile().sendCommand(selectedServerValue, customCommandTextField.getText());
|
|
customCommandTextField.setText("");
|
|
} else if (actionSource == saveServerButton) {
|
|
controller.getCurrentProfile().sendCommand(selectedServerValue, "save-all");
|
|
} else if (actionSource == reloadButton) {
|
|
controller.getCurrentProfile().sendCommand(selectedServerValue, "reload");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handles command buttons which act on a player
|
|
*
|
|
* @param actionSource <p>The clicked object</p>
|
|
* @param selectedServerValue <p>The server currently selected</p>
|
|
* @param selectedPlayerValue <p>The player currently selected</p>
|
|
*/
|
|
private void handlePlayerCommands(Object actionSource, String selectedServerValue, String selectedPlayerValue) {
|
|
if (selectedServerValue == null || selectedPlayerValue == null) {
|
|
return;
|
|
}
|
|
if (actionSource == kickButton) {
|
|
controller.getCurrentProfile().sendCommand(selectedServerValue, "kick " + selectedPlayerValue);
|
|
} else if (actionSource == banButton) {
|
|
controller.getCurrentProfile().sendCommand(selectedServerValue, "ban " + selectedPlayerValue);
|
|
} else if (actionSource == opButton) {
|
|
controller.getCurrentProfile().sendCommand(selectedServerValue, "op " + selectedPlayerValue);
|
|
} else if (actionSource == deopButton) {
|
|
controller.getCurrentProfile().sendCommand(selectedServerValue, "deop " + selectedPlayerValue);
|
|
}
|
|
}
|
|
}
|