2019-07-08 17:55:14 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 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
|
|
|
|
*/
|
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
package org.geysermc.geyser.command;
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2021-12-26 19:57:17 +00:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
|
|
|
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
2020-03-17 16:43:09 +00:00
|
|
|
import lombok.Getter;
|
2021-10-13 17:09:19 +00:00
|
|
|
import org.geysermc.common.PlatformType;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.GeyserImpl;
|
|
|
|
import org.geysermc.geyser.command.defaults.*;
|
2021-12-26 19:57:17 +00:00
|
|
|
import org.geysermc.geyser.configuration.CustomCommandsConfiguration;
|
2021-11-22 19:52:26 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.text.GeyserLocale;
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2021-12-26 19:57:17 +00:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2021-12-26 19:57:17 +00:00
|
|
|
public class CommandManager {
|
2020-03-17 16:43:09 +00:00
|
|
|
@Getter
|
2021-11-29 15:31:31 +00:00
|
|
|
private final Map<String, GeyserCommand> commands = new HashMap<>();
|
2021-12-26 19:57:17 +00:00
|
|
|
@Getter
|
|
|
|
private final Map<String, String> commandDescriptionOverrides;
|
2020-03-17 16:43:09 +00:00
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
private final GeyserImpl geyser;
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
public CommandManager(GeyserImpl geyser) {
|
|
|
|
this.geyser = geyser;
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
registerCommand(new HelpCommand(geyser, "help", "geyser.commands.help.desc", "geyser.command.help"));
|
|
|
|
registerCommand(new ListCommand(geyser, "list", "geyser.commands.list.desc", "geyser.command.list"));
|
|
|
|
registerCommand(new ReloadCommand(geyser, "reload", "geyser.commands.reload.desc", "geyser.command.reload"));
|
|
|
|
registerCommand(new OffhandCommand(geyser, "offhand", "geyser.commands.offhand.desc", "geyser.command.offhand"));
|
|
|
|
registerCommand(new DumpCommand(geyser, "dump", "geyser.commands.dump.desc", "geyser.command.dump"));
|
|
|
|
registerCommand(new VersionCommand(geyser, "version", "geyser.commands.version.desc", "geyser.command.version"));
|
|
|
|
registerCommand(new SettingsCommand(geyser, "settings", "geyser.commands.settings.desc", "geyser.command.settings"));
|
|
|
|
registerCommand(new StatisticsCommand(geyser, "statistics", "geyser.commands.statistics.desc", "geyser.command.statistics"));
|
2021-06-05 22:53:58 +00:00
|
|
|
registerCommand(new AdvancementsCommand("advancements", "geyser.commands.advancements.desc", "geyser.command.advancements"));
|
2021-11-26 15:49:28 +00:00
|
|
|
registerCommand(new AdvancedTooltipsCommand("tooltips", "geyser.commands.advancedtooltips.desc", "geyser.command.tooltips"));
|
2021-11-20 21:34:30 +00:00
|
|
|
if (GeyserImpl.getInstance().getPlatformType() == PlatformType.STANDALONE) {
|
|
|
|
registerCommand(new StopCommand(geyser, "stop", "geyser.commands.stop.desc", "geyser.command.stop"));
|
2021-10-13 17:09:19 +00:00
|
|
|
}
|
2021-12-26 19:57:17 +00:00
|
|
|
|
|
|
|
// Read or create command overrides
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
|
|
|
|
|
|
|
|
File commandsFile = geyser.getBootstrap().getConfigFolder().resolve("commands.yml").toFile();
|
|
|
|
if (!commandsFile.exists()) {
|
|
|
|
try (InputStream stream = geyser.getBootstrap().getResource("commands.yml")) {
|
|
|
|
Files.copy(stream, commandsFile.toPath());
|
|
|
|
} catch (IOException e) {
|
|
|
|
geyser.getLogger().error("Unable to load commands.yml template from jar!", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CustomCommandsConfiguration commandsConfiguration;
|
|
|
|
try {
|
|
|
|
commandsConfiguration = objectMapper.readValue(commandsFile, CustomCommandsConfiguration.class);
|
|
|
|
} catch (IOException e) {
|
|
|
|
geyser.getLogger().error("Unable to load commands configuration!", e);
|
|
|
|
commandsConfiguration = new CustomCommandsConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.commandDescriptionOverrides = new Object2ObjectOpenHashMap<>();
|
|
|
|
for (CustomCommandsConfiguration.CustomCommandEntry entry : commandsConfiguration.getDescriptions()) {
|
|
|
|
for (String name : entry.getCommands()) {
|
|
|
|
commandDescriptionOverrides.put(name, entry.getDescription());
|
|
|
|
}
|
|
|
|
}
|
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);
|
2021-11-20 23:29:46 +00:00
|
|
|
geyser.getLogger().debug(GeyserLocale.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) {
|
2021-11-20 23:29:46 +00:00
|
|
|
geyser.getLogger().error(GeyserLocale.getLocaleStringLog("geyser.commands.invalid"));
|
2019-07-08 17:55:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-22 19:52:26 +00:00
|
|
|
if (sender instanceof GeyserSession) {
|
|
|
|
cmd.execute((GeyserSession) sender, sender, args);
|
2021-01-17 03:18:13 +00:00
|
|
|
} else {
|
|
|
|
if (!cmd.isBedrockOnly()) {
|
|
|
|
cmd.execute(null, sender, args);
|
|
|
|
} else {
|
2021-11-20 23:29:46 +00:00
|
|
|
geyser.getLogger().error(GeyserLocale.getLocaleStringLog("geyser.bootstrap.command.bedrock_only"));
|
2021-01-17 03:18:13 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-08 17:55:14 +00:00
|
|
|
}
|
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
|
|
|
|
*/
|
2021-12-26 19:57:17 +00:00
|
|
|
@Nonnull
|
|
|
|
public String getDescription(String command) {
|
|
|
|
// Most platforms don't have an option to get the description, so we rely on our overrides instead
|
|
|
|
return commandDescriptionOverrides.getOrDefault(command, "");
|
|
|
|
}
|
2019-07-08 17:55:14 +00:00
|
|
|
}
|