mirror of https://github.com/GeyserMC/Geyser.git
574 lines
27 KiB
Java
574 lines
27 KiB
Java
/*
|
|
* Copyright (c) 2019-2022 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.translator.protocol.java;
|
|
|
|
import org.geysermc.mcprotocollib.protocol.data.game.command.CommandNode;
|
|
import org.geysermc.mcprotocollib.protocol.data.game.command.CommandParser;
|
|
import org.geysermc.mcprotocollib.protocol.data.game.command.properties.ResourceProperties;
|
|
import org.geysermc.mcprotocollib.protocol.data.game.entity.attribute.AttributeType;
|
|
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundCommandsPacket;
|
|
import it.unimi.dsi.fastutil.Hash;
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
|
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
|
import it.unimi.dsi.fastutil.ints.IntSet;
|
|
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
|
|
import lombok.Getter;
|
|
import lombok.ToString;
|
|
import net.kyori.adventure.text.format.NamedTextColor;
|
|
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
|
import org.cloudburstmc.protocol.bedrock.data.command.*;
|
|
import org.cloudburstmc.protocol.bedrock.packet.AvailableCommandsPacket;
|
|
import org.geysermc.geyser.GeyserImpl;
|
|
import org.geysermc.geyser.api.event.java.ServerDefineCommandsEvent;
|
|
import org.geysermc.geyser.command.CommandRegistry;
|
|
import org.geysermc.geyser.inventory.item.Enchantment;
|
|
import org.geysermc.geyser.registry.BlockRegistries;
|
|
import org.geysermc.geyser.registry.Registries;
|
|
import org.geysermc.geyser.session.GeyserSession;
|
|
import org.geysermc.geyser.translator.protocol.PacketTranslator;
|
|
import org.geysermc.geyser.translator.protocol.Translator;
|
|
import org.geysermc.geyser.util.EntityUtils;
|
|
|
|
import java.util.*;
|
|
|
|
@SuppressWarnings("removal") // We know. This is our doing.
|
|
@Translator(packet = ClientboundCommandsPacket.class)
|
|
public class JavaCommandsTranslator extends PacketTranslator<ClientboundCommandsPacket> {
|
|
|
|
private static final String[] ALL_EFFECT_IDENTIFIERS = EntityUtils.getAllEffectIdentifiers();
|
|
private static final String[] ATTRIBUTES = AttributeType.Builtin.BUILTIN.values().stream().map(AttributeType::getIdentifier).toList().toArray(new String[0]);
|
|
private static final String[] ENUM_BOOLEAN = {"true", "false"};
|
|
private static final String[] VALID_COLORS;
|
|
private static final String[] VALID_SCOREBOARD_SLOTS;
|
|
|
|
private static final Hash.Strategy<BedrockCommandInfo> PARAM_STRATEGY = new Hash.Strategy<>() {
|
|
@Override
|
|
public int hashCode(BedrockCommandInfo o) {
|
|
int paramHash = Arrays.deepHashCode(o.paramData());
|
|
return 31 * paramHash + o.description().hashCode();
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(BedrockCommandInfo a, BedrockCommandInfo b) {
|
|
if (a == b) return true;
|
|
if (a == null || b == null) return false;
|
|
if (!a.description().equals(b.description())) return false;
|
|
if (a.paramData().length != b.paramData().length) return false;
|
|
for (int i = 0; i < a.paramData().length; i++) {
|
|
CommandParamData[] a1 = a.paramData()[i].getOverloads();
|
|
CommandParamData[] b1 = b.paramData()[i].getOverloads();
|
|
if (a1.length != b1.length) return false;
|
|
|
|
for (int j = 0; j < a1.length; j++) {
|
|
if (!a1[j].equals(b1[j])) return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
};
|
|
|
|
static {
|
|
List<String> validColors = new ArrayList<>(NamedTextColor.NAMES.keys());
|
|
validColors.add("reset");
|
|
VALID_COLORS = validColors.toArray(new String[0]);
|
|
|
|
List<String> teamOptions = new ArrayList<>(Arrays.asList("list", "sidebar", "belowName"));
|
|
for (String color : NamedTextColor.NAMES.keys()) {
|
|
teamOptions.add("sidebar.team." + color);
|
|
}
|
|
VALID_SCOREBOARD_SLOTS = teamOptions.toArray(new String[0]);
|
|
}
|
|
|
|
@Override
|
|
public void translate(GeyserSession session, ClientboundCommandsPacket packet) {
|
|
// Don't send command suggestions if they are disabled
|
|
if (!session.getGeyser().getConfig().isCommandSuggestions()) {
|
|
session.getGeyser().getLogger().debug("Not sending translated command suggestions as they are disabled.");
|
|
|
|
// Send an empty packet so Bedrock doesn't override /help with its own, built-in help command.
|
|
AvailableCommandsPacket emptyPacket = new AvailableCommandsPacket();
|
|
session.sendUpstreamPacket(emptyPacket);
|
|
return;
|
|
}
|
|
|
|
CommandRegistry registry = session.getGeyser().commandRegistry();
|
|
CommandNode[] nodes = packet.getNodes();
|
|
List<CommandData> commandData = new ArrayList<>();
|
|
IntSet commandNodes = new IntOpenHashSet();
|
|
Set<String> knownAliases = new HashSet<>();
|
|
Map<BedrockCommandInfo, Set<String>> commands = new Object2ObjectOpenCustomHashMap<>(PARAM_STRATEGY);
|
|
Int2ObjectMap<List<CommandNode>> commandArgs = new Int2ObjectOpenHashMap<>();
|
|
|
|
// Get the first node, it should be a root node
|
|
CommandNode rootNode = nodes[packet.getFirstNodeIndex()];
|
|
|
|
// Loop through the root nodes to get all commands
|
|
for (int nodeIndex : rootNode.getChildIndices()) {
|
|
CommandNode node = nodes[nodeIndex];
|
|
|
|
// Make sure we don't have duplicated commands (happens if there is more than 1 root node)
|
|
if (!commandNodes.add(nodeIndex) || !knownAliases.add(node.getName().toLowerCase(Locale.ROOT))) continue;
|
|
|
|
// Get and update the commandArgs list with the found arguments
|
|
if (node.getChildIndices().length >= 1) {
|
|
for (int childIndex : node.getChildIndices()) {
|
|
commandArgs.computeIfAbsent(nodeIndex, ($) -> new ArrayList<>()).add(nodes[childIndex]);
|
|
}
|
|
}
|
|
|
|
// Get and parse all params
|
|
CommandOverloadData[] params = getParams(session, nodes[nodeIndex], nodes);
|
|
|
|
// Insert the alias name into the command list
|
|
String name = node.getName().toLowerCase(Locale.ROOT);
|
|
String description = registry.description(name, session.locale());
|
|
BedrockCommandInfo info = new BedrockCommandInfo(name, description, params);
|
|
commands.computeIfAbsent(info, $ -> new HashSet<>()).add(name);
|
|
}
|
|
|
|
var eventBus = session.getGeyser().eventBus();
|
|
|
|
var event = new ServerDefineCommandsEvent(session, commands.keySet());
|
|
eventBus.fire(event);
|
|
if (event.isCancelled()) {
|
|
return;
|
|
}
|
|
|
|
var oldEvent = new org.geysermc.geyser.api.event.downstream.ServerDefineCommandsEvent(session, commands.keySet());
|
|
eventBus.fire(oldEvent);
|
|
if (oldEvent.isCancelled()) {
|
|
return;
|
|
}
|
|
|
|
// The command flags, not sure what these do apart from break things
|
|
Set<CommandData.Flag> flags = Set.of();
|
|
|
|
// Loop through all the found commands
|
|
for (Map.Entry<BedrockCommandInfo, Set<String>> entry : commands.entrySet()) {
|
|
String commandName = entry.getValue().iterator().next(); // We know this has a value
|
|
|
|
LinkedHashMap<String, Set<CommandEnumConstraint>> values = new LinkedHashMap<>();
|
|
// Is this right?
|
|
for (String s : entry.getValue()) {
|
|
values.put(s, EnumSet.of(CommandEnumConstraint.ALLOW_ALIASES));
|
|
}
|
|
|
|
// Create a basic alias
|
|
CommandEnumData aliases = new CommandEnumData(commandName + "Aliases", values, false);
|
|
|
|
// Build the completed command and add it to the final list
|
|
CommandData data = new CommandData(commandName, entry.getKey().description(), flags, CommandPermission.ANY, aliases, Collections.emptyList(), entry.getKey().paramData());
|
|
commandData.add(data);
|
|
}
|
|
|
|
// Add our commands to the AvailableCommandsPacket for the bedrock client
|
|
AvailableCommandsPacket availableCommandsPacket = new AvailableCommandsPacket();
|
|
availableCommandsPacket.getCommands().addAll(commandData);
|
|
|
|
session.getGeyser().getLogger().debug("Sending command packet of " + commandData.size() + " commands");
|
|
|
|
// Finally, send the commands to the client
|
|
session.sendUpstreamPacket(availableCommandsPacket);
|
|
}
|
|
|
|
/**
|
|
* Build the command parameter array for the given command
|
|
*
|
|
* @param session the session
|
|
* @param commandNode The command to build the parameters for
|
|
* @param allNodes Every command node
|
|
* @return An array of parameter option arrays
|
|
*/
|
|
private static CommandOverloadData[] getParams(GeyserSession session, CommandNode commandNode, CommandNode[] allNodes) {
|
|
// Check if the command is an alias and redirect it
|
|
if (commandNode.getRedirectIndex().isPresent()) {
|
|
int redirectIndex = commandNode.getRedirectIndex().getAsInt();
|
|
GeyserImpl.getInstance().getLogger().debug("Redirecting command " + commandNode.getName() + " to " + allNodes[redirectIndex].getName());
|
|
commandNode = allNodes[redirectIndex];
|
|
}
|
|
|
|
if (commandNode.getChildIndices().length >= 1) {
|
|
// Create the root param node and build all the children
|
|
ParamInfo rootParam = new ParamInfo(commandNode, null);
|
|
rootParam.buildChildren(new CommandBuilderContext(session), allNodes);
|
|
|
|
List<CommandOverloadData> treeData = rootParam.getTree();
|
|
|
|
return treeData.toArray(new CommandOverloadData[0]);
|
|
}
|
|
|
|
return new CommandOverloadData[0];
|
|
}
|
|
|
|
/**
|
|
* Convert Java edition command types to Bedrock edition
|
|
*
|
|
* @param context the session's command context
|
|
* @param node Command type to convert
|
|
* @return Bedrock parameter data type
|
|
*/
|
|
private static Object mapCommandType(CommandBuilderContext context, CommandNode node) {
|
|
CommandParser parser = node.getParser();
|
|
if (parser == null) {
|
|
return CommandParam.STRING;
|
|
}
|
|
|
|
return switch (parser) {
|
|
case FLOAT, ROTATION, DOUBLE -> CommandParam.FLOAT;
|
|
case INTEGER, LONG -> CommandParam.INT;
|
|
case ENTITY, GAME_PROFILE -> CommandParam.TARGET;
|
|
case BLOCK_POS -> CommandParam.BLOCK_POSITION;
|
|
case COLUMN_POS, VEC3 -> CommandParam.POSITION;
|
|
case MESSAGE -> CommandParam.MESSAGE;
|
|
case NBT_COMPOUND_TAG, NBT_TAG, NBT_PATH -> CommandParam.JSON; //TODO NBT was removed
|
|
case RESOURCE_LOCATION, FUNCTION -> CommandParam.FILE_PATH;
|
|
case BOOL -> ENUM_BOOLEAN;
|
|
case OPERATION -> CommandParam.OPERATOR; // ">=", "==", etc
|
|
case BLOCK_STATE -> context.getBlockStates();
|
|
case ITEM_STACK -> context.getItemNames();
|
|
case COLOR -> VALID_COLORS;
|
|
case SCOREBOARD_SLOT -> VALID_SCOREBOARD_SLOTS;
|
|
case RESOURCE -> handleResource(context, ((ResourceProperties) node.getProperties()).getRegistryKey(), false);
|
|
case RESOURCE_OR_TAG -> handleResource(context, ((ResourceProperties) node.getProperties()).getRegistryKey(), true);
|
|
case DIMENSION -> context.session.getLevels();
|
|
case TEAM -> context.getTeams(); // Note: as of Java 1.19.3, objectives are currently parsed from the server
|
|
default -> CommandParam.STRING;
|
|
};
|
|
}
|
|
|
|
private static Object handleResource(CommandBuilderContext context, String resource, boolean tags) {
|
|
return switch (resource) {
|
|
case "minecraft:attribute" -> ATTRIBUTES;
|
|
case "minecraft:enchantment" -> Enchantment.JavaEnchantment.ALL_JAVA_IDENTIFIERS;
|
|
case "minecraft:entity_type" -> context.getEntityTypes();
|
|
case "minecraft:mob_effect" -> ALL_EFFECT_IDENTIFIERS;
|
|
case "minecraft:worldgen/biome" -> tags ? context.getBiomesWithTags() : context.getBiomes();
|
|
default -> CommandParam.STRING;
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Stores the command description and parameter data for best optimizing the Bedrock commands packet.
|
|
*/
|
|
private record BedrockCommandInfo(String name, String description, CommandOverloadData[] paramData) implements
|
|
org.geysermc.geyser.api.event.downstream.ServerDefineCommandsEvent.CommandInfo,
|
|
ServerDefineCommandsEvent.CommandInfo
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Stores command completions so we don't have to rebuild the same values multiple times.
|
|
*/
|
|
@MonotonicNonNull
|
|
private static class CommandBuilderContext {
|
|
private final GeyserSession session;
|
|
private Object biomesWithTags;
|
|
private Object biomesNoTags;
|
|
private String[] blockStates;
|
|
private String[] entityTypes;
|
|
private String[] itemNames;
|
|
private CommandEnumData teams;
|
|
|
|
CommandBuilderContext(GeyserSession session) {
|
|
this.session = session;
|
|
}
|
|
|
|
private Object getBiomes() {
|
|
if (biomesNoTags != null) {
|
|
return biomesNoTags;
|
|
}
|
|
|
|
String[] identifiers = session.getGeyser().getWorldManager().getBiomeIdentifiers(false);
|
|
return (biomesNoTags = identifiers != null ? identifiers : CommandParam.STRING);
|
|
}
|
|
|
|
private Object getBiomesWithTags() {
|
|
if (biomesWithTags != null) {
|
|
return biomesWithTags;
|
|
}
|
|
|
|
String[] identifiers = session.getGeyser().getWorldManager().getBiomeIdentifiers(true);
|
|
return (biomesWithTags = identifiers != null ? identifiers : CommandParam.STRING);
|
|
}
|
|
|
|
private String[] getBlockStates() {
|
|
if (blockStates != null) {
|
|
return blockStates;
|
|
}
|
|
return (blockStates = BlockRegistries.JAVA_TO_BEDROCK_IDENTIFIERS.get().keySet().toArray(new String[0]));
|
|
}
|
|
|
|
private String[] getEntityTypes() {
|
|
if (entityTypes != null) {
|
|
return entityTypes;
|
|
}
|
|
return (entityTypes = Registries.JAVA_ENTITY_IDENTIFIERS.get().keySet().toArray(new String[0]));
|
|
}
|
|
|
|
public String[] getItemNames() {
|
|
if (itemNames != null) {
|
|
return itemNames;
|
|
}
|
|
return (itemNames = Registries.JAVA_ITEM_IDENTIFIERS.get().keySet().toArray(new String[0]));
|
|
}
|
|
|
|
private CommandEnumData getTeams() {
|
|
if (teams != null) {
|
|
return teams;
|
|
}
|
|
return (teams = new CommandEnumData("Geyser_Teams",
|
|
session.getWorldCache().getScoreboard().getTeamNames(), true
|
|
));
|
|
}
|
|
}
|
|
|
|
@Getter
|
|
@ToString
|
|
private static class ParamInfo {
|
|
private final CommandNode paramNode;
|
|
private final CommandParamData paramData;
|
|
private final List<ParamInfo> children;
|
|
|
|
/**
|
|
* Create a new parameter info object
|
|
*
|
|
* @param paramNode CommandNode the parameter is for
|
|
* @param paramData The existing parameters for the command
|
|
*/
|
|
public ParamInfo(CommandNode paramNode, CommandParamData paramData) {
|
|
this.paramNode = paramNode;
|
|
this.paramData = paramData;
|
|
this.children = new ArrayList<>();
|
|
}
|
|
|
|
/**
|
|
* Build the array of all the child parameters (recursive)
|
|
*
|
|
* @param context the session's command builder context
|
|
* @param allNodes Every command node
|
|
*/
|
|
public void buildChildren(CommandBuilderContext context, CommandNode[] allNodes) {
|
|
for (int paramID : paramNode.getChildIndices()) {
|
|
CommandNode paramNode = allNodes[paramID];
|
|
|
|
if (paramNode == this.paramNode) {
|
|
// Fixes a StackOverflowError when an argument has itself as a child
|
|
continue;
|
|
}
|
|
|
|
if (paramNode.getParser() == null) {
|
|
boolean foundCompatible = false;
|
|
for (int i = 0; i < children.size(); i++) {
|
|
ParamInfo enumParamInfo = children.get(i);
|
|
// Check to make sure all descending nodes of this command are compatible - otherwise, create a new overload
|
|
if (isCompatible(allNodes, enumParamInfo.getParamNode(), paramNode)) {
|
|
foundCompatible = true;
|
|
// TODO: Check this
|
|
// Extend the current list of enum values
|
|
// String[] enumOptions = Arrays.copyOf(enumParamInfo.getParamData().getEnumData().getValues(), enumParamInfo.getParamData().getEnumData().getValues().size() + 1);
|
|
// enumOptions[enumOptions.length - 1] = paramNode.getName();
|
|
|
|
Map<String, Set<CommandEnumConstraint>> values = new LinkedHashMap<>(enumParamInfo.getParamData().getEnumData().getValues());
|
|
values.put(paramNode.getName(), Set.of());
|
|
|
|
// Re-create the command using the updated values
|
|
CommandEnumData enumData = new CommandEnumData(enumParamInfo.getParamData().getEnumData().getName(), values, false);
|
|
CommandParamData commandParamData = new CommandParamData();
|
|
commandParamData.setName(enumParamInfo.getParamData().getName());
|
|
commandParamData.setOptional(this.paramNode.isExecutable());
|
|
commandParamData.setEnumData(enumData);
|
|
|
|
children.set(i, new ParamInfo(enumParamInfo.getParamNode(), commandParamData));
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!foundCompatible) {
|
|
// Create a new subcommand with this exact type
|
|
LinkedHashMap<String, Set<CommandEnumConstraint>> map = new LinkedHashMap<>();
|
|
map.put(paramNode.getName(), Set.of());
|
|
CommandEnumData enumData = new CommandEnumData(paramNode.getName(), map, false);
|
|
|
|
// On setting optional:
|
|
// isExecutable is defined as a node "constitutes a valid command."
|
|
// Therefore, any children of the parameter must simply be optional.
|
|
CommandParamData commandParamData = new CommandParamData();
|
|
commandParamData.setName(paramNode.getName());
|
|
commandParamData.setOptional(this.paramNode.isExecutable());
|
|
commandParamData.setEnumData(enumData);
|
|
|
|
children.add(new ParamInfo(paramNode, commandParamData));
|
|
}
|
|
} else {
|
|
// Put the non-enum param into the list
|
|
Object mappedType = mapCommandType(context, paramNode);
|
|
CommandEnumData enumData = null;
|
|
CommandParam type = null;
|
|
boolean optional = this.paramNode.isExecutable();
|
|
if (mappedType instanceof CommandEnumData) {
|
|
// Likely to specify isSoft, to be possibly updated later.
|
|
enumData = (CommandEnumData) mappedType;
|
|
} else if (mappedType instanceof String[]) {
|
|
LinkedHashMap<String, Set<CommandEnumConstraint>> map = new LinkedHashMap<>();
|
|
for (String s : (String[]) mappedType) {
|
|
map.put(s, Set.of());
|
|
}
|
|
|
|
enumData = new CommandEnumData(getEnumDataName(paramNode).toLowerCase(Locale.ROOT), map, false);
|
|
} else {
|
|
type = (CommandParam) mappedType;
|
|
// Bedrock throws a fit if an optional message comes after a string or target
|
|
// Example vanilla commands: ban-ip, ban, and kick
|
|
if (optional && type == CommandParam.MESSAGE && (paramData.getType() == CommandParam.STRING || paramData.getType() == CommandParam.TARGET)) {
|
|
optional = false;
|
|
}
|
|
}
|
|
// IF enumData != null:
|
|
// In game, this will show up like <paramNode.getName(): enumData.getName()>
|
|
// So if paramNode.getName() == "value" and enumData.getName() == "bool": <value: bool>
|
|
CommandParamData commandParamData = new CommandParamData();
|
|
commandParamData.setName(paramNode.getName());
|
|
commandParamData.setOptional(optional);
|
|
commandParamData.setEnumData(enumData);
|
|
commandParamData.setType(type);
|
|
|
|
children.add(new ParamInfo(paramNode, commandParamData));
|
|
}
|
|
}
|
|
|
|
// Recursively build all child options
|
|
for (ParamInfo child : children) {
|
|
child.buildChildren(context, allNodes);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Mitigates <a href="https://github.com/GeyserMC/Geyser/issues/3411">issue 3411</a>. Not a perfect solution.
|
|
*/
|
|
private static String getEnumDataName(CommandNode node) {
|
|
if (node.getProperties() instanceof ResourceProperties properties) {
|
|
String registryKey = properties.getRegistryKey();
|
|
int identifierSplit = registryKey.indexOf(':');
|
|
if (identifierSplit != -1) {
|
|
return registryKey.substring(identifierSplit);
|
|
}
|
|
return registryKey;
|
|
}
|
|
return node.getParser().name();
|
|
}
|
|
|
|
/**
|
|
* Comparing CommandNode type a and b, determine if they are in the same overload.
|
|
* <p>
|
|
* Take the <code>gamerule</code> command, and let's present three "subcommands" you can perform:
|
|
*
|
|
* <ul>
|
|
* <li><code>gamerule doDaylightCycle true</code></li>
|
|
* <li><code>gamerule announceAdvancements false</code></li>
|
|
* <li><code>gamerule randomTickSpeed 3</code></li>
|
|
* </ul>
|
|
*
|
|
* While all three of them are indeed part of the same command, the command setting randomTickSpeed parses an int,
|
|
* while the others use boolean. In Bedrock, this should be presented as a separate overload to indicate that this
|
|
* does something a little different.
|
|
* <p>
|
|
* Therefore, this function will return <code>true</code> if the first two are compared, as they use the same
|
|
* parsers. If the third is compared with either of the others, this function will return <code>false</code>.
|
|
* <p>
|
|
* Here's an example of how the above would be presented to Bedrock (as of 1.16.200). Notice how the top two <code>CommandParamData</code>
|
|
* classes of each array are identical in type, but the following class is different:
|
|
* <pre>
|
|
* overloads=[
|
|
* [
|
|
* CommandParamData(name=doDaylightCycle, optional=false, enumData=CommandEnumData(name=announceAdvancements, values=[announceAdvancements, doDaylightCycle], isSoft=false), type=STRING, postfix=null, options=[])
|
|
* CommandParamData(name=value, optional=false, enumData=CommandEnumData(name=value, values=[true, false], isSoft=false), type=null, postfix=null, options=[])
|
|
* ]
|
|
* [
|
|
* CommandParamData(name=randomTickSpeed, optional=false, enumData=CommandEnumData(name=randomTickSpeed, values=[randomTickSpeed], isSoft=false), type=STRING, postfix=null, options=[])
|
|
* CommandParamData(name=value, optional=false, enumData=null, type=INT, postfix=null, options=[])
|
|
* ]
|
|
* ]
|
|
* </pre>
|
|
*
|
|
* @return if these two can be merged into one overload.
|
|
*/
|
|
private boolean isCompatible(CommandNode[] allNodes, CommandNode a, CommandNode b) {
|
|
if (a == b) return true;
|
|
if (a.getParser() != b.getParser()) return false;
|
|
if (a.getChildIndices().length != b.getChildIndices().length) return false;
|
|
|
|
for (int i = 0; i < a.getChildIndices().length; i++) {
|
|
boolean hasSimilarity = false;
|
|
CommandNode a1 = allNodes[a.getChildIndices()[i]];
|
|
// Search "b" until we find a child that matches this one
|
|
for (int j = 0; j < b.getChildIndices().length; j++) {
|
|
if (isCompatible(allNodes, a1, allNodes[b.getChildIndices()[j]])) {
|
|
hasSimilarity = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!hasSimilarity) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get the tree of every parameter node (recursive)
|
|
*
|
|
* @return List of parameter options arrays for the command
|
|
*/
|
|
public List<CommandOverloadData> getTree() {
|
|
List<CommandOverloadData> treeParamData = new ArrayList<>();
|
|
|
|
for (ParamInfo child : children) {
|
|
// Get the tree from the child
|
|
List<CommandOverloadData> childTree = child.getTree();
|
|
|
|
// Un-pack the tree append the child node to it and push into the list
|
|
for (CommandOverloadData subChildData : childTree) {
|
|
CommandParamData[] subChild = subChildData.getOverloads();
|
|
CommandParamData[] tmpTree = new CommandParamData[subChild.length + 1];
|
|
tmpTree[0] = child.getParamData();
|
|
System.arraycopy(subChild, 0, tmpTree, 1, subChild.length);
|
|
|
|
treeParamData.add(new CommandOverloadData(false, tmpTree));
|
|
}
|
|
|
|
// If we have no more child parameters just the child
|
|
if (childTree.size() == 0) {
|
|
treeParamData.add(new CommandOverloadData(false, new CommandParamData[] { child.getParamData() }));
|
|
}
|
|
}
|
|
|
|
return treeParamData;
|
|
}
|
|
}
|
|
}
|