Uncomment JavaDeclareCommandsTranslator and update

This commit is contained in:
DoctorMacc 2020-06-24 12:16:30 -04:00
parent 2b874b4f24
commit b34dc05c1d

View file

@ -25,281 +25,297 @@
package org.geysermc.connector.network.translators.java; package org.geysermc.connector.network.translators.java;
import com.github.steveice10.mc.protocol.data.game.command.CommandNode;
import com.github.steveice10.mc.protocol.data.game.command.CommandParser;
import com.github.steveice10.mc.protocol.packet.ingame.server.ServerDeclareCommandsPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.ServerDeclareCommandsPacket;
import com.nukkitx.protocol.bedrock.data.command.CommandData;
import com.nukkitx.protocol.bedrock.data.command.CommandEnumData;
import com.nukkitx.protocol.bedrock.data.command.CommandParamData;
import com.nukkitx.protocol.bedrock.data.command.CommandParamType;
import com.nukkitx.protocol.bedrock.packet.AvailableCommandsPacket;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import lombok.Getter;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator; import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.Translator; import org.geysermc.connector.network.translators.Translator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@Translator(packet = ServerDeclareCommandsPacket.class) @Translator(packet = ServerDeclareCommandsPacket.class)
public class JavaDeclareCommandsTranslator extends PacketTranslator<ServerDeclareCommandsPacket> { public class JavaDeclareCommandsTranslator extends PacketTranslator<ServerDeclareCommandsPacket> {
@Override @Override
public void translate(ServerDeclareCommandsPacket packet, GeyserSession session) { public void translate(ServerDeclareCommandsPacket packet, GeyserSession session) {
// Don't send command suggestions if they are disabled // Don't send command suggestions if they are disabled
// if (!session.getConnector().getConfig().isCommandSuggestions()) { if (!session.getConnector().getConfig().isCommandSuggestions()) {
// session.getConnector().getLogger().debug("Not sending command suggestions as they are disabled."); session.getConnector().getLogger().debug("Not sending command suggestions as they are disabled.");
// return; return;
// } }
// List<CommandData> commandData = new ArrayList<>(); List<CommandData> commandData = new ArrayList<>();
// Int2ObjectMap<String> commands = new Int2ObjectOpenHashMap<>(); Int2ObjectMap<String> commands = new Int2ObjectOpenHashMap<>();
// Int2ObjectMap<List<CommandNode>> commandArgs = new Int2ObjectOpenHashMap<>(); Int2ObjectMap<List<CommandNode>> commandArgs = new Int2ObjectOpenHashMap<>();
//
// // Get the first node, it should be a root node // Get the first node, it should be a root node
// CommandNode rootNode = packet.getNodes()[packet.getFirstNodeIndex()]; CommandNode rootNode = packet.getNodes()[packet.getFirstNodeIndex()];
//
// // Loop through the root nodes to get all commands // Loop through the root nodes to get all commands
// for (int nodeIndex : rootNode.getChildIndices()) { for (int nodeIndex : rootNode.getChildIndices()) {
// CommandNode node = packet.getNodes()[nodeIndex]; CommandNode node = packet.getNodes()[nodeIndex];
//
// // Make sure we don't have duplicated commands (happens if there is more than 1 root node) // Make sure we don't have duplicated commands (happens if there is more than 1 root node)
// if (commands.containsKey(nodeIndex)) { continue; } if (commands.containsKey(nodeIndex)) { continue; }
// if (commands.containsValue(node.getName())) { continue; } if (commands.containsValue(node.getName())) { continue; }
//
// // Get and update the commandArgs list with the found arguments // Get and update the commandArgs list with the found arguments
// if (node.getChildIndices().length >= 1) { if (node.getChildIndices().length >= 1) {
// for (int childIndex : node.getChildIndices()) { for (int childIndex : node.getChildIndices()) {
// commandArgs.putIfAbsent(nodeIndex, new ArrayList<>()); commandArgs.putIfAbsent(nodeIndex, new ArrayList<>());
// commandArgs.get(nodeIndex).add(packet.getNodes()[childIndex]); commandArgs.get(nodeIndex).add(packet.getNodes()[childIndex]);
// } }
// } }
//
// // Insert the command name into the list // Insert the command name into the list
// commands.put(nodeIndex, node.getName()); commands.put(nodeIndex, node.getName());
// } }
//
// // The command flags, not sure what these do apart from break things // The command flags, not sure what these do apart from break things
// List<CommandData.Flag> flags = new ArrayList<>(); List<CommandData.Flag> flags = new ArrayList<>();
//
// // Loop through all the found commands // Loop through all the found commands
// for (int commandID : commands.keySet()) { for (int commandID : commands.keySet()) {
// String commandName = commands.get(commandID); String commandName = commands.get(commandID);
//
// // Create a basic alias // Create a basic alias
// CommandEnumData aliases = new CommandEnumData( commandName + "Aliases", new String[] { commandName.toLowerCase() }, false); CommandEnumData aliases = new CommandEnumData( commandName + "Aliases", new String[] { commandName.toLowerCase() }, false);
//
// // Get and parse all params // Get and parse all params
// CommandParamData[][] params = getParams(packet.getNodes()[commandID], packet.getNodes()); CommandParamData[][] params = getParams(packet.getNodes()[commandID], packet.getNodes());
//
// // Build the completed command and add it to the final list // Build the completed command and add it to the final list
// CommandData data = new CommandData(commandName, session.getConnector().getCommandManager().getDescription(commandName), flags, (byte) 0, aliases, params); CommandData data = new CommandData(commandName, session.getConnector().getCommandManager().getDescription(commandName), flags, (byte) 0, aliases, params);
// commandData.add(data); commandData.add(data);
// } }
//
// // Add our commands to the AvailableCommandsPacket for the bedrock client // Add our commands to the AvailableCommandsPacket for the bedrock client
// AvailableCommandsPacket availableCommandsPacket = new AvailableCommandsPacket(); AvailableCommandsPacket availableCommandsPacket = new AvailableCommandsPacket();
// for (CommandData data : commandData) { for (CommandData data : commandData) {
// availableCommandsPacket.getCommands().add(data); availableCommandsPacket.getCommands().add(data);
// } }
//
// GeyserConnector.getInstance().getLogger().debug("Sending command packet of " + commandData.size() + " commands"); GeyserConnector.getInstance().getLogger().debug("Sending command packet of " + commandData.size() + " commands");
//
// // Finally, send the commands to the client // Finally, send the commands to the client
// session.sendUpstreamPacket(availableCommandsPacket); session.sendUpstreamPacket(availableCommandsPacket);
// } }
//
// /** /**
// * Build the command parameter array for the given command * Build the command parameter array for the given command
// * *
// * @param commandNode The command to build the parameters for * @param commandNode The command to build the parameters for
// * @param allNodes Every command node * @param allNodes Every command node
// * *
// * @return An array of parameter option arrays * @return An array of parameter option arrays
// */ */
// private CommandParamData[][] getParams(CommandNode commandNode, CommandNode[] allNodes) { private CommandParamData[][] getParams(CommandNode commandNode, CommandNode[] allNodes) {
// // Check if the command is an alias and redirect it // Check if the command is an alias and redirect it
// if (commandNode.getRedirectIndex() != -1) { if (commandNode.getRedirectIndex() != -1) {
// GeyserConnector.getInstance().getLogger().debug("Redirecting command " + commandNode.getName() + " to " + allNodes[commandNode.getRedirectIndex()].getName()); GeyserConnector.getInstance().getLogger().debug("Redirecting command " + commandNode.getName() + " to " + allNodes[commandNode.getRedirectIndex()].getName());
// commandNode = allNodes[commandNode.getRedirectIndex()]; commandNode = allNodes[commandNode.getRedirectIndex()];
// } }
//
// if (commandNode.getChildIndices().length >= 1) { if (commandNode.getChildIndices().length >= 1) {
// // Create the root param node and build all the children // Create the root param node and build all the children
// ParamInfo rootParam = new ParamInfo(commandNode, null); ParamInfo rootParam = new ParamInfo(commandNode, null);
// rootParam.buildChildren(allNodes); rootParam.buildChildren(allNodes);
//
// List<CommandParamData[]> treeData = rootParam.getTree(); List<CommandParamData[]> treeData = rootParam.getTree();
// CommandParamData[][] params = new CommandParamData[treeData.size()][]; CommandParamData[][] params = new CommandParamData[treeData.size()][];
//
// // Fill the nested params array // Fill the nested params array
// int i = 0; int i = 0;
// for (CommandParamData[] tree : treeData) { for (CommandParamData[] tree : treeData) {
// params[i] = tree; params[i] = tree;
// i++; i++;
// } }
//
// return params; return params;
// } }
//
// return new CommandParamData[0][0]; return new CommandParamData[0][0];
// } }
//
// /** /**
// * Convert Java edition command types to Bedrock edition * Convert Java edition command types to Bedrock edition
// * *
// * @param parser Command type to convert * @param parser Command type to convert
// * *
// * @return Bedrock parameter data type * @return Bedrock parameter data type
// */ */
// private CommandParamData.Type mapCommandType(CommandParser parser) { private CommandParamType mapCommandType(CommandParser parser) {
// if (parser == null) { return CommandParamData.Type.STRING; } //TODO: this if (parser == null) { return CommandParamType.STRING; }
//
// switch (parser) { switch (parser) {
// case FLOAT: case FLOAT:
// return CommandParamData.Type.FLOAT; return CommandParamType.FLOAT;
//
// case INTEGER: case INTEGER:
// return CommandParamData.Type.INT; return CommandParamType.INT;
//
// case ENTITY: case ENTITY:
// case GAME_PROFILE: case GAME_PROFILE:
// return CommandParamData.Type.TARGET; return CommandParamType.TARGET;
//
// case BLOCK_POS: case BLOCK_POS:
// return CommandParamData.Type.BLOCK_POSITION; return CommandParamType.BLOCK_POSITION;
//
// case COLUMN_POS: case COLUMN_POS:
// case VEC3: case VEC3:
// return CommandParamData.Type.POSITION; return CommandParamType.POSITION;
//
// case MESSAGE: case MESSAGE:
// return CommandParamData.Type.MESSAGE; return CommandParamType.MESSAGE;
//
// case NBT: case NBT:
// case NBT_COMPOUND_TAG: case NBT_COMPOUND_TAG:
// case NBT_TAG: case NBT_TAG:
// case NBT_PATH: case NBT_PATH:
// return CommandParamData.Type.JSON; return CommandParamType.JSON;
//
// case RESOURCE_LOCATION: case RESOURCE_LOCATION:
// return CommandParamData.Type.FILE_PATH; return CommandParamType.FILE_PATH;
//
// case INT_RANGE: case INT_RANGE:
// return CommandParamData.Type.INT_RANGE; return CommandParamType.INT_RANGE;
//
// case BOOL: case BOOL:
// case DOUBLE: case DOUBLE:
// case STRING: case STRING:
// case VEC2: case VEC2:
// case BLOCK_STATE: case BLOCK_STATE:
// case BLOCK_PREDICATE: case BLOCK_PREDICATE:
// case ITEM_STACK: case ITEM_STACK:
// case ITEM_PREDICATE: case ITEM_PREDICATE:
// case COLOR: case COLOR:
// case COMPONENT: case COMPONENT:
// case OBJECTIVE: case OBJECTIVE:
// case OBJECTIVE_CRITERIA: case OBJECTIVE_CRITERIA:
// case OPERATION: // Possibly OPERATOR case OPERATION: // Possibly OPERATOR
// case PARTICLE: case PARTICLE:
// case ROTATION: case ROTATION:
// case SCOREBOARD_SLOT: case SCOREBOARD_SLOT:
// case SCORE_HOLDER: case SCORE_HOLDER:
// case SWIZZLE: case SWIZZLE:
// case TEAM: case TEAM:
// case ITEM_SLOT: case ITEM_SLOT:
// case MOB_EFFECT: case MOB_EFFECT:
// case FUNCTION: case FUNCTION:
// case ENTITY_ANCHOR: case ENTITY_ANCHOR:
// case RANGE: case RANGE:
// case FLOAT_RANGE: case FLOAT_RANGE:
// case ITEM_ENCHANTMENT: case ITEM_ENCHANTMENT:
// case ENTITY_SUMMON: case ENTITY_SUMMON:
// case DIMENSION: case DIMENSION:
// case TIME: case TIME:
// default: default:
// return CommandParamData.Type.STRING; return CommandParamType.STRING;
// } }
// } }
//
// @Getter @Getter
// private class ParamInfo { private class ParamInfo {
// private CommandNode paramNode; private CommandNode paramNode;
// private CommandParamData paramData; private CommandParamData paramData;
// private List<ParamInfo> children; private List<ParamInfo> children;
//
// /** /**
// * Create a new parameter info object * Create a new parameter info object
// * *
// * @param paramNode CommandNode the parameter is for * @param paramNode CommandNode the parameter is for
// * @param paramData The existing parameters for the command * @param paramData The existing parameters for the command
// */ */
// public ParamInfo(CommandNode paramNode, CommandParamData paramData) { public ParamInfo(CommandNode paramNode, CommandParamData paramData) {
// this.paramNode = paramNode; this.paramNode = paramNode;
// this.paramData = paramData; this.paramData = paramData;
// this.children = new ArrayList<>(); this.children = new ArrayList<>();
// } }
//
// /** /**
// * Build the array of all the child parameters (recursive) * Build the array of all the child parameters (recursive)
// * *
// * @param allNodes Every command node * @param allNodes Every command node
// */ */
// public void buildChildren(CommandNode[] allNodes) { public void buildChildren(CommandNode[] allNodes) {
// int enumIndex = -1; int enumIndex = -1;
//
// for (int paramID : paramNode.getChildIndices()) { for (int paramID : paramNode.getChildIndices()) {
// CommandNode paramNode = allNodes[paramID]; CommandNode paramNode = allNodes[paramID];
//
// if (paramNode.getParser() == null) { if (paramNode.getParser() == null) {
// if (enumIndex == -1) { if (enumIndex == -1) {
// enumIndex = children.size(); enumIndex = children.size();
//
// // Create the new enum command // Create the new enum command
// CommandEnumData enumData = new CommandEnumData(paramNode.getName(), new String[] { paramNode.getName() }, false); CommandEnumData enumData = new CommandEnumData(paramNode.getName(), new String[] { paramNode.getName() }, false);
// children.add(new ParamInfo(paramNode, new CommandParamData(paramNode.getName(), false, enumData, mapCommandType(paramNode.getParser()), null, Collections.emptyList()))); children.add(new ParamInfo(paramNode, new CommandParamData(paramNode.getName(), false, enumData, mapCommandType(paramNode.getParser()), null, Collections.emptyList())));
// } else { } else {
// // Get the existing enum // Get the existing enum
// ParamInfo enumParamInfo = children.get(enumIndex); ParamInfo enumParamInfo = children.get(enumIndex);
//
// // Extend the current list of enum values // Extend the current list of enum values
// String[] enumOptions = Arrays.copyOf(enumParamInfo.getParamData().getEnumData().getValues(), enumParamInfo.getParamData().getEnumData().getValues().length + 1); String[] enumOptions = Arrays.copyOf(enumParamInfo.getParamData().getEnumData().getValues(), enumParamInfo.getParamData().getEnumData().getValues().length + 1);
// enumOptions[enumOptions.length - 1] = paramNode.getName(); enumOptions[enumOptions.length - 1] = paramNode.getName();
//
// // Re-create the command using the updated values // Re-create the command using the updated values
// CommandEnumData enumData = new CommandEnumData(enumParamInfo.getParamData().getEnumData().getName(), enumOptions, false); CommandEnumData enumData = new CommandEnumData(enumParamInfo.getParamData().getEnumData().getName(), enumOptions, false);
// children.set(enumIndex, new ParamInfo(enumParamInfo.getParamNode(), new CommandParamData(enumParamInfo.getParamData().getName(), false, enumData, enumParamInfo.getParamData().getType(), null, Collections.emptyList()))); children.set(enumIndex, new ParamInfo(enumParamInfo.getParamNode(), new CommandParamData(enumParamInfo.getParamData().getName(), false, enumData, enumParamInfo.getParamData().getType(), null, Collections.emptyList())));
// } }
// }else{ }else{
// // Put the non-enum param into the list // Put the non-enum param into the list
// children.add(new ParamInfo(paramNode, new CommandParamData(paramNode.getName(), false, null, mapCommandType(paramNode.getParser()), null, Collections.emptyList()))); children.add(new ParamInfo(paramNode, new CommandParamData(paramNode.getName(), false, null, mapCommandType(paramNode.getParser()), null, Collections.emptyList())));
// } }
// } }
//
// // Recursively build all child options // Recursively build all child options
// for (ParamInfo child : children) { for (ParamInfo child : children) {
// child.buildChildren(allNodes); child.buildChildren(allNodes);
// } }
// } }
//
// /** /**
// * Get the tree of every parameter node (recursive) * Get the tree of every parameter node (recursive)
// * *
// * @return List of parameter options arrays for the command * @return List of parameter options arrays for the command
// */ */
// public List<CommandParamData[]> getTree() { public List<CommandParamData[]> getTree() {
// List<CommandParamData[]> treeParamData = new ArrayList<>(); List<CommandParamData[]> treeParamData = new ArrayList<>();
//
// for (ParamInfo child : children) { for (ParamInfo child : children) {
// // Get the tree from the child // Get the tree from the child
// List<CommandParamData[]> childTree = child.getTree(); List<CommandParamData[]> childTree = child.getTree();
//
// // Un-pack the tree append the child node to it and push into the list // Un-pack the tree append the child node to it and push into the list
// for (CommandParamData[] subchild : childTree) { for (CommandParamData[] subchild : childTree) {
// CommandParamData[] tmpTree = new ArrayList<CommandParamData>() { CommandParamData[] tmpTree = new ArrayList<CommandParamData>() {
// { {
// add(child.getParamData()); add(child.getParamData());
// addAll(Arrays.asList(subchild)); addAll(Arrays.asList(subchild));
// } }
// }.toArray(new CommandParamData[0]); }.toArray(new CommandParamData[0]);
//
// treeParamData.add(tmpTree); treeParamData.add(tmpTree);
// } }
//
// // If we have no more child parameters just the child // If we have no more child parameters just the child
// if (childTree.size() == 0) { if (childTree.size() == 0) {
// treeParamData.add(new CommandParamData[] { child.getParamData() }); treeParamData.add(new CommandParamData[] { child.getParamData() });
// } }
// } }
//
// return treeParamData; return treeParamData;
// } }
} }
} }