2019-07-08 17:55:14 +00:00
|
|
|
/*
|
2020-01-09 03:05:42 +00:00
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
2019-07-08 17:55:14 +00:00
|
|
|
*
|
2019-07-11 21:30:35 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2019-07-08 17:55:14 +00:00
|
|
|
*
|
2019-07-11 21:30:35 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2019-07-08 17:55:14 +00:00
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.geysermc.connector.command;
|
|
|
|
|
2020-03-17 16:43:09 +00:00
|
|
|
import lombok.Getter;
|
2020-04-29 20:01:53 +00:00
|
|
|
|
2019-07-08 17:55:14 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2020-04-25 04:03:34 +00:00
|
|
|
import org.geysermc.connector.command.defaults.*;
|
2020-07-05 23:35:51 +00:00
|
|
|
import org.geysermc.connector.utils.LanguageUtils;
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2020-11-18 23:18:36 +00:00
|
|
|
import java.util.*;
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2020-04-29 20:01:53 +00:00
|
|
|
public abstract class CommandManager {
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2020-03-17 16:43:09 +00:00
|
|
|
@Getter
|
|
|
|
private final Map<String, GeyserCommand> commands = Collections.synchronizedMap(new HashMap<>());
|
|
|
|
|
2020-10-29 22:30:52 +00:00
|
|
|
private final GeyserConnector connector;
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2020-04-19 23:08:14 +00:00
|
|
|
public CommandManager(GeyserConnector connector) {
|
2019-07-08 17:55:14 +00:00
|
|
|
this.connector = connector;
|
|
|
|
|
2020-10-29 22:30:52 +00:00
|
|
|
registerCommand(new HelpCommand(connector, "help", "geyser.commands.help.desc", "geyser.command.help"));
|
|
|
|
registerCommand(new ListCommand(connector, "list", "geyser.commands.list.desc", "geyser.command.list"));
|
|
|
|
registerCommand(new ReloadCommand(connector, "reload", "geyser.commands.reload.desc", "geyser.command.reload"));
|
|
|
|
registerCommand(new StopCommand(connector, "stop", "geyser.commands.stop.desc", "geyser.command.stop"));
|
|
|
|
registerCommand(new OffhandCommand(connector, "offhand", "geyser.commands.offhand.desc", "geyser.command.offhand"));
|
|
|
|
registerCommand(new DumpCommand(connector, "dump", "geyser.commands.dump.desc", "geyser.command.dump"));
|
|
|
|
registerCommand(new VersionCommand(connector, "version", "geyser.commands.version.desc", "geyser.command.version"));
|
2020-12-12 07:45:54 +00:00
|
|
|
registerCommand(new SettingsCommand(connector, "settings", "geyser.commands.settings.desc", "geyser.command.settings"));
|
2020-10-29 22:30:52 +00:00
|
|
|
registerCommand(new StatisticsCommand(connector, "statistics", "geyser.commands.statistics.desc", "geyser.command.statistics"));
|
2019-07-08 17:55:14 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 17:35:48 +00:00
|
|
|
public void registerCommand(GeyserCommand command) {
|
2020-03-17 16:43:09 +00:00
|
|
|
commands.put(command.getName(), command);
|
2020-07-05 23:35:51 +00:00
|
|
|
connector.getLogger().debug(LanguageUtils.getLocaleStringLog("geyser.commands.registered", command.getName()));
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2019-12-21 17:35:48 +00:00
|
|
|
if (command.getAliases().isEmpty())
|
2019-08-10 00:15:06 +00:00
|
|
|
return;
|
|
|
|
|
2019-07-08 17:55:14 +00:00
|
|
|
for (String alias : command.getAliases())
|
2020-03-17 16:43:09 +00:00
|
|
|
commands.put(alias, command);
|
2019-07-08 17:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void runCommand(CommandSender sender, String command) {
|
2020-03-05 02:35:23 +00:00
|
|
|
if (!command.startsWith("geyser "))
|
2019-12-21 17:35:48 +00:00
|
|
|
return;
|
|
|
|
|
2020-03-17 16:43:09 +00:00
|
|
|
command = command.trim().replace("geyser ", "");
|
2019-12-21 17:35:48 +00:00
|
|
|
String label;
|
|
|
|
String[] args;
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2019-12-21 17:35:48 +00:00
|
|
|
if (!command.contains(" ")) {
|
|
|
|
label = command.toLowerCase();
|
2019-07-08 17:55:14 +00:00
|
|
|
args = new String[0];
|
|
|
|
} else {
|
2019-12-21 17:35:48 +00:00
|
|
|
label = command.substring(0, command.indexOf(" ")).toLowerCase();
|
2020-06-15 10:20:38 +00:00
|
|
|
String argLine = command.substring(command.indexOf(" ") + 1);
|
2019-07-08 17:55:14 +00:00
|
|
|
args = argLine.contains(" ") ? argLine.split(" ") : new String[] { argLine };
|
|
|
|
}
|
|
|
|
|
2020-03-17 16:43:09 +00:00
|
|
|
GeyserCommand cmd = commands.get(label);
|
2019-07-08 17:55:14 +00:00
|
|
|
if (cmd == null) {
|
2020-07-05 23:35:51 +00:00
|
|
|
connector.getLogger().error(LanguageUtils.getLocaleStringLog("geyser.commands.invalid"));
|
2019-07-08 17:55:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.execute(sender, args);
|
|
|
|
}
|
2020-04-29 20:01:53 +00:00
|
|
|
|
2020-11-18 23:18:36 +00:00
|
|
|
/**
|
|
|
|
* @return a list of all subcommands under {@code /geyser}.
|
|
|
|
*/
|
|
|
|
public List<String> getCommandNames() {
|
|
|
|
return Arrays.asList(connector.getCommandManager().getCommands().keySet().toArray(new String[0]));
|
|
|
|
}
|
|
|
|
|
2020-04-29 20:01:53 +00:00
|
|
|
/**
|
|
|
|
* Returns the description of the given command
|
|
|
|
*
|
|
|
|
* @param command Command to get the description for
|
|
|
|
* @return Command description
|
|
|
|
*/
|
|
|
|
public abstract String getDescription(String command);
|
2019-07-08 17:55:14 +00:00
|
|
|
}
|