Implement support for adding Geyser subcommands

This commit is contained in:
RednedEpic 2022-01-16 15:09:53 -06:00
parent 57345fa102
commit 30303d5f16
90 changed files with 1207 additions and 402 deletions

View file

@ -41,15 +41,15 @@ import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.geysermc.common.PlatformType;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserBootstrap;
import org.geysermc.geyser.command.CommandManager;
import org.geysermc.geyser.command.GeyserCommandManager;
import org.geysermc.geyser.configuration.GeyserConfiguration;
import org.geysermc.geyser.configuration.GeyserJacksonConfiguration;
import org.geysermc.geyser.dump.BootstrapDumpInfo;
import org.geysermc.geyser.ping.GeyserLegacyPingPassthrough;
import org.geysermc.geyser.ping.IGeyserPingPassthrough;
import org.geysermc.geyser.platform.standalone.command.GeyserStandaloneCommandManager;
import org.geysermc.geyser.util.FileUtils;
import org.geysermc.geyser.text.GeyserLocale;
import org.geysermc.geyser.platform.standalone.command.GeyserCommandManager;
import org.geysermc.geyser.platform.standalone.gui.GeyserStandaloneGUI;
import java.io.File;
@ -63,7 +63,7 @@ import java.util.stream.Collectors;
public class GeyserStandaloneBootstrap implements GeyserBootstrap {
private GeyserCommandManager geyserCommandManager;
private GeyserStandaloneCommandManager geyserCommandManager;
private GeyserStandaloneConfiguration geyserConfig;
private GeyserStandaloneLogger geyserLogger;
private IGeyserPingPassthrough geyserPingPassthrough;
@ -215,7 +215,8 @@ public class GeyserStandaloneBootstrap implements GeyserBootstrap {
logger.get().setLevel(geyserConfig.isDebugMode() ? Level.DEBUG : Level.INFO);
geyser = GeyserImpl.start(PlatformType.STANDALONE, this);
geyserCommandManager = new GeyserCommandManager(geyser);
geyserCommandManager = new GeyserStandaloneCommandManager(geyser);
geyserCommandManager.init();
if (gui != null) {
gui.setupInterface(geyserLogger, geyserCommandManager);
@ -260,7 +261,7 @@ public class GeyserStandaloneBootstrap implements GeyserBootstrap {
}
@Override
public CommandManager getGeyserCommandManager() {
public GeyserCommandManager getGeyserCommandManager() {
return geyserCommandManager;
}

View file

@ -31,11 +31,11 @@ import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;
import org.geysermc.geyser.command.CommandSender;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.text.ChatColor;
@Log4j2
public class GeyserStandaloneLogger extends SimpleTerminalConsole implements GeyserLogger, CommandSender {
public class GeyserStandaloneLogger extends SimpleTerminalConsole implements GeyserLogger, GeyserCommandSource {
@Override
protected boolean isRunning() {
@ -44,7 +44,7 @@ public class GeyserStandaloneLogger extends SimpleTerminalConsole implements Gey
@Override
protected void runCommand(String line) {
GeyserImpl.getInstance().getCommandManager().runCommand(this, line);
GeyserImpl.getInstance().commandManager().runCommand(this, line);
}
@Override

View file

@ -26,16 +26,16 @@
package org.geysermc.geyser.platform.standalone.command;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.command.CommandManager;
import org.geysermc.geyser.command.GeyserCommandManager;
public class GeyserCommandManager extends CommandManager {
public class GeyserStandaloneCommandManager extends GeyserCommandManager {
public GeyserCommandManager(GeyserImpl geyser) {
public GeyserStandaloneCommandManager(GeyserImpl geyser) {
super(geyser);
}
@Override
public String getDescription(String command) {
public String description(String command) {
return ""; // this is not sent over the protocol, so we return none
}
}

View file

@ -26,11 +26,12 @@
package org.geysermc.geyser.platform.standalone.gui;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.command.Command;
import org.geysermc.geyser.command.GeyserCommand;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.text.GeyserLocale;
import org.geysermc.geyser.platform.standalone.GeyserStandaloneLogger;
import org.geysermc.geyser.platform.standalone.command.GeyserCommandManager;
import org.geysermc.geyser.platform.standalone.command.GeyserStandaloneCommandManager;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
@ -255,33 +256,34 @@ public class GeyserStandaloneGUI {
* @param geyserStandaloneLogger The current logger
* @param geyserCommandManager The commands manager
*/
public void setupInterface(GeyserStandaloneLogger geyserStandaloneLogger, GeyserCommandManager geyserCommandManager) {
public void setupInterface(GeyserStandaloneLogger geyserStandaloneLogger, GeyserStandaloneCommandManager geyserCommandManager) {
commandsMenu.removeAll();
optionsMenu.removeAll();
for (Map.Entry<String, GeyserCommand> command : geyserCommandManager.getCommands().entrySet()) {
for (Map.Entry<String, Command> entry : geyserCommandManager.getCommands().entrySet()) {
// Remove the offhand command and any alias commands to prevent duplicates in the list
if (!command.getValue().isExecutableOnConsole() || command.getValue().getAliases().contains(command.getKey())) {
if (!entry.getValue().isExecutableOnConsole() || entry.getValue().aliases().contains(entry.getKey())) {
continue;
}
GeyserCommand command = (GeyserCommand) entry.getValue();
// Create the button that runs the command
boolean hasSubCommands = command.getValue().hasSubCommands();
boolean hasSubCommands = !entry.getValue().subCommands().isEmpty();
// Add an extra menu if there are more commands that can be run
JMenuItem commandButton = hasSubCommands ? new JMenu(command.getValue().getName()) : new JMenuItem(command.getValue().getName());
commandButton.getAccessibleContext().setAccessibleDescription(command.getValue().getDescription());
JMenuItem commandButton = hasSubCommands ? new JMenu(entry.getValue().name()) : new JMenuItem(entry.getValue().name());
commandButton.getAccessibleContext().setAccessibleDescription(entry.getValue().description());
if (!hasSubCommands) {
commandButton.addActionListener(e -> command.getValue().execute(null, geyserStandaloneLogger, new String[]{ }));
commandButton.addActionListener(e -> command.execute(null, geyserStandaloneLogger, new String[]{ }));
} else {
// Add a submenu that's the same name as the menu can't be pressed
JMenuItem otherCommandButton = new JMenuItem(command.getValue().getName());
otherCommandButton.getAccessibleContext().setAccessibleDescription(command.getValue().getDescription());
otherCommandButton.addActionListener(e -> command.getValue().execute(null, geyserStandaloneLogger, new String[]{ }));
JMenuItem otherCommandButton = new JMenuItem(entry.getValue().name());
otherCommandButton.getAccessibleContext().setAccessibleDescription(entry.getValue().description());
otherCommandButton.addActionListener(e -> command.execute(null, geyserStandaloneLogger, new String[]{ }));
commandButton.add(otherCommandButton);
// Add a menu option for all possible subcommands
for (String subCommandName : command.getValue().getSubCommands()) {
for (String subCommandName : entry.getValue().subCommands()) {
JMenuItem item = new JMenuItem(subCommandName);
item.addActionListener(e -> command.getValue().execute(null, geyserStandaloneLogger, new String[]{subCommandName}));
item.addActionListener(e -> command.execute(null, geyserStandaloneLogger, new String[]{subCommandName}));
commandButton.add(item);
}
}