Add a commands.yml file for specifying custom command descriptions

This commit is contained in:
Camotoy 2021-12-26 14:57:17 -05:00
parent b70e2645c8
commit 3061481a1a
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
16 changed files with 123 additions and 165 deletions

View file

@ -31,6 +31,7 @@ import org.geysermc.geyser.dump.BootstrapDumpInfo;
import org.geysermc.geyser.level.GeyserWorldManager;
import org.geysermc.geyser.level.WorldManager;
import org.geysermc.geyser.ping.IGeyserPingPassthrough;
import org.jetbrains.annotations.Contract;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -68,11 +69,14 @@ public interface GeyserBootstrap {
GeyserLogger getGeyserLogger();
/**
* Returns the current CommandManager
* Creates a command manager for {@link GeyserImpl to use}.
*
* @return The current CommandManager
* @return a new CommandManager instance
*/
CommandManager getGeyserCommandManager();
@Contract("_ -> new")
default CommandManager createGeyserCommandManager(GeyserImpl geyser) {
return new CommandManager(geyser);
}
/**
* Returns the current PingPassthrough manager

View file

@ -118,6 +118,8 @@ public class GeyserImpl implements GeyserApi {
private final ScheduledExecutorService scheduledThread;
private final CommandManager commandManager;
private final BedrockServer bedrockServer;
private final PlatformType platformType;
private final GeyserBootstrap bootstrap;
@ -150,6 +152,8 @@ public class GeyserImpl implements GeyserApi {
this.scheduledThread = Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("Geyser Scheduled Thread"));
this.commandManager = bootstrap.createGeyserCommandManager(this);
logger.setDebug(config.isDebugMode());
/* Initialize translators and registries */
@ -480,10 +484,6 @@ public class GeyserImpl implements GeyserApi {
return bootstrap.getGeyserConfig();
}
public CommandManager getCommandManager() {
return bootstrap.getGeyserCommandManager();
}
public WorldManager getWorldManager() {
return bootstrap.getWorldManager();
}

View file

@ -25,20 +25,30 @@
package org.geysermc.geyser.command;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import lombok.Getter;
import org.geysermc.common.PlatformType;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.command.defaults.*;
import org.geysermc.geyser.configuration.CustomCommandsConfiguration;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.text.GeyserLocale;
import java.util.*;
public abstract class CommandManager {
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;
public class CommandManager {
@Getter
private final Map<String, GeyserCommand> commands = new HashMap<>();
@Getter
private final Map<String, String> commandDescriptionOverrides;
private final GeyserImpl geyser;
@ -58,6 +68,32 @@ public abstract class CommandManager {
if (GeyserImpl.getInstance().getPlatformType() == PlatformType.STANDALONE) {
registerCommand(new StopCommand(geyser, "stop", "geyser.commands.stop.desc", "geyser.command.stop"));
}
// 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());
}
}
}
public void registerCommand(GeyserCommand command) {
@ -105,18 +141,15 @@ public abstract class CommandManager {
}
}
/**
* @return a list of all subcommands under {@code /geyser}.
*/
public List<String> getCommandNames() {
return Arrays.asList(geyser.getCommandManager().getCommands().keySet().toArray(new String[0]));
}
/**
* Returns the description of the given command
*
* @param command Command to get the description for
* @return Command description
*/
public abstract String getDescription(String command);
@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, "");
}
}

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
*
* 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:
*
* 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.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.configuration;
import lombok.Getter;
import lombok.ToString;
import java.util.Collections;
import java.util.List;
@Getter
@ToString
public class CustomCommandsConfiguration {
private List<CustomCommandEntry> descriptions = Collections.emptyList();
@Getter
@ToString
public static class CustomCommandEntry {
private String description;
private List<String> commands;
}
}

View file

@ -0,0 +1,15 @@
# Specify custom command descriptions.
# On platforms that have their own command descriptions, any entry here will override what the platform provides.
descriptions: [
{
description: "This is an example command description setup for Bedrock players",
# Each command here will have the above command description applied.
commands: [
"foo", "bar"
]
},
{
description: "The main command for Geyser.",
commands: ["geyser"]
}
]