|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.nukkitx.protocol.bedrock.data.command.CommandData; |
|
import com.nukkitx.protocol.bedrock.data.command.CommandEnumData; |
|
import com.nukkitx.protocol.bedrock.data.command.CommandParam; |
|
import com.nukkitx.protocol.bedrock.data.command.CommandParamData; |
|
import com.nukkitx.protocol.bedrock.packet.AvailableCommandsPacket; |
|
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.geysermc.connector.GeyserConnector; |
|
import org.geysermc.connector.entity.type.EntityType; |
|
import org.geysermc.connector.network.session.GeyserSession; |
|
import org.geysermc.connector.network.translators.PacketTranslator; |
|
import org.geysermc.connector.network.translators.Translator; |
|
import org.geysermc.connector.network.translators.item.Enchantment; |
|
import org.geysermc.connector.registry.BlockRegistries; |
|
|
|
import java.util.*; |
|
|
|
@Translator(packet = ServerDeclareCommandsPacket.class) |
|
public class JavaDeclareCommandsTranslator extends PacketTranslator<ServerDeclareCommandsPacket> { |
|
|
|
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<CommandParamData[][]> PARAM_STRATEGY = new Hash.Strategy<CommandParamData[][]>() { |
|
@Override |
|
public int hashCode(CommandParamData[][] o) { |
|
return Arrays.deepHashCode(o); |
|
} |
|
|
|
@Override |
|
public boolean equals(CommandParamData[][] a, CommandParamData[][] b) { |
|
if (a == b) return true; |
|
if (a == null || b == null) return false; |
|
if (a.length != b.length) return false; |
|
for (int i = 0; i < a.length; i++) { |
|
CommandParamData[] a1 = a[i]; |
|
CommandParamData[] b1 = b[i]; |
|
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, ServerDeclareCommandsPacket packet) { |
|
|
|
if (!session.getConnector().getConfig().isCommandSuggestions()) { |
|
session.getConnector().getLogger().debug("Not sending translated command suggestions as they are disabled."); |
|
|
|
|
|
AvailableCommandsPacket emptyPacket = new AvailableCommandsPacket(); |
|
session.sendUpstreamPacket(emptyPacket); |
|
return; |
|
} |
|
|
|
CommandNode[] nodes = packet.getNodes(); |
|
List<CommandData> commandData = new ArrayList<>(); |
|
IntSet commandNodes = new IntOpenHashSet(); |
|
Set<String> knownAliases = new HashSet<>(); |
|
Map<CommandParamData[][], Set<String>> commands = new Object2ObjectOpenCustomHashMap<>(PARAM_STRATEGY); |
|
Int2ObjectMap<List<CommandNode>> commandArgs = new Int2ObjectOpenHashMap<>(); |
|
|
|
|
|
CommandNode rootNode = nodes[packet.getFirstNodeIndex()]; |
|
|
|
|
|
for (int nodeIndex : rootNode.getChildIndices()) { |
|
CommandNode node = nodes[nodeIndex]; |
|
|
|
|
|
if (!commandNodes.add(nodeIndex) || !knownAliases.add(node.getName().toLowerCase())) continue; |
|
|
|
|
|
if (node.getChildIndices().length >= 1) { |
|
for (int childIndex : node.getChildIndices()) { |
|
commandArgs.computeIfAbsent(nodeIndex, ArrayList::new).add(nodes[childIndex]); |
|
} |
|
} |
|
|
|
|
|
CommandParamData[][] params = getParams(session, nodes[nodeIndex], nodes); |
|
|
|
|
|
commands.computeIfAbsent(params, index -> new HashSet<>()).add(node.getName().toLowerCase()); |
|
} |
|
|
|
|
|
List<CommandData.Flag> flags = Collections.emptyList(); |
|
|
|
|
|
|
|
for (Map.Entry<CommandParamData[][], Set<String>> entry : commands.entrySet()) { |
|
String commandName = entry.getValue().iterator().next(); |
|
|
|
|
|
CommandEnumData aliases = new CommandEnumData(commandName + "Aliases", entry.getValue().toArray(new String[0]), false); |
|
|
|
|
|
CommandData data = new CommandData(commandName, session.getConnector().getCommandManager().getDescription(commandName), flags, (byte) 0, aliases, entry.getKey()); |
|
commandData.add(data); |
|
} |
|
|
|
|
|
AvailableCommandsPacket availableCommandsPacket = new AvailableCommandsPacket(); |
|
availableCommandsPacket.getCommands().addAll(commandData); |
|
|
|
session.getConnector().getLogger().debug("Sending command packet of " + commandData.size() + " commands"); |
|
|
|
|
|
session.sendUpstreamPacket(availableCommandsPacket); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static CommandParamData[][] getParams(GeyserSession session, CommandNode commandNode, CommandNode[] allNodes) { |
|
|
|
if (commandNode.getRedirectIndex() != -1) { |
|
GeyserConnector.getInstance().getLogger().debug("Redirecting command " + commandNode.getName() + " to " + allNodes[commandNode.getRedirectIndex()].getName()); |
|
commandNode = allNodes[commandNode.getRedirectIndex()]; |
|
} |
|
|
|
if (commandNode.getChildIndices().length >= 1) { |
|
|
|
ParamInfo rootParam = new ParamInfo(commandNode, null); |
|
rootParam.buildChildren(session, allNodes); |
|
|
|
List<CommandParamData[]> treeData = rootParam.getTree(); |
|
|
|
return treeData.toArray(new CommandParamData[0][]); |
|
} |
|
|
|
return new CommandParamData[0][0]; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static Object mapCommandType(GeyserSession session, CommandParser parser) { |
|
if (parser == null) { |
|
return CommandParam.STRING; |
|
} |
|
|
|
switch (parser) { |
|
case FLOAT: |
|
case ROTATION: |
|
case DOUBLE: |
|
return CommandParam.FLOAT; |
|
|
|
case INTEGER: |
|
case LONG: |
|
return CommandParam.INT; |
|
|
|
case ENTITY: |
|
case GAME_PROFILE: |
|
return CommandParam.TARGET; |
|
|
|
case BLOCK_POS: |
|
return CommandParam.BLOCK_POSITION; |
|
|
|
case COLUMN_POS: |
|
case VEC3: |
|
return CommandParam.POSITION; |
|
|
|
case MESSAGE: |
|
return CommandParam.MESSAGE; |
|
|
|
case NBT: |
|
case NBT_COMPOUND_TAG: |
|
case NBT_TAG: |
|
case NBT_PATH: |
|
return CommandParam.JSON; |
|
|
|
case RESOURCE_LOCATION: |
|
case FUNCTION: |
|
return CommandParam.FILE_PATH; |
|
|
|
case BOOL: |
|
return ENUM_BOOLEAN; |
|
|
|
case OPERATION: |
|
return CommandParam.OPERATOR; |
|
|
|
case BLOCK_STATE: |
|
return BlockRegistries.JAVA_TO_BEDROCK_IDENTIFIERS.get().keySet().toArray(new String[0]); |
|
|
|
case ITEM_STACK: |
|
return session.getItemMappings().getItemNames(); |
|
|
|
case ITEM_ENCHANTMENT: |
|
return Enchantment.JavaEnchantment.ALL_JAVA_IDENTIFIERS; |
|
|
|
case ENTITY_SUMMON: |
|
return EntityType.ALL_JAVA_IDENTIFIERS; |
|
|
|
case COLOR: |
|
return VALID_COLORS; |
|
|
|
case SCOREBOARD_SLOT: |
|
return VALID_SCOREBOARD_SLOTS; |
|
|
|
default: |
|
return CommandParam.STRING; |
|
} |
|
} |
|
|
|
@Getter |
|
@ToString |
|
private static class ParamInfo { |
|
private final CommandNode paramNode; |
|
private final CommandParamData paramData; |
|
private final List<ParamInfo> children; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ParamInfo(CommandNode paramNode, CommandParamData paramData) { |
|
this.paramNode = paramNode; |
|
this.paramData = paramData; |
|
this.children = new ArrayList<>(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void buildChildren(GeyserSession session, CommandNode[] allNodes) { |
|
for (int paramID : paramNode.getChildIndices()) { |
|
CommandNode paramNode = allNodes[paramID]; |
|
|
|
if (paramNode == this.paramNode) { |
|
|
|
continue; |
|
} |
|
|
|
if (paramNode.getParser() == null) { |
|
boolean foundCompatible = false; |
|
for (int i = 0; i < children.size(); i++) { |
|
ParamInfo enumParamInfo = children.get(i); |
|
|
|
if (isCompatible(allNodes, enumParamInfo.getParamNode(), paramNode)) { |
|
foundCompatible = true; |
|
|
|
String[] enumOptions = Arrays.copyOf(enumParamInfo.getParamData().getEnumData().getValues(), enumParamInfo.getParamData().getEnumData().getValues().length + 1); |
|
enumOptions[enumOptions.length - 1] = paramNode.getName(); |
|
|
|
|
|
CommandEnumData enumData = new CommandEnumData(enumParamInfo.getParamData().getEnumData().getName(), enumOptions, false); |
|
children.set(i, new ParamInfo(enumParamInfo.getParamNode(), new CommandParamData(enumParamInfo.getParamData().getName(), this.paramNode.isExecutable(), enumData, null, null, Collections.emptyList()))); |
|
break; |
|
} |
|
} |
|
|
|
if (!foundCompatible) { |
|
|
|
CommandEnumData enumData = new CommandEnumData(paramNode.getName(), new String[]{paramNode.getName()}, false); |
|
|
|
|
|
|
|
|
|
children.add(new ParamInfo(paramNode, new CommandParamData(paramNode.getName(), this.paramNode.isExecutable(), enumData, null, null, Collections.emptyList()))); |
|
} |
|
} else { |
|
|
|
Object mappedType = mapCommandType(session, paramNode.getParser()); |
|
CommandEnumData enumData = null; |
|
CommandParam type = null; |
|
if (mappedType instanceof String[]) { |
|
enumData = new CommandEnumData(paramNode.getParser().name().toLowerCase(), (String[]) mappedType, false); |
|
} else { |
|
type = (CommandParam) mappedType; |
|
} |
|
|
|
|
|
|
|
children.add(new ParamInfo(paramNode, new CommandParamData(paramNode.getName(), this.paramNode.isExecutable(), enumData, type, null, Collections.emptyList()))); |
|
} |
|
} |
|
|
|
|
|
for (ParamInfo child : children) { |
|
child.buildChildren(session, allNodes); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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]]; |
|
|
|
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; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<CommandParamData[]> getTree() { |
|
List<CommandParamData[]> treeParamData = new ArrayList<>(); |
|
|
|
for (ParamInfo child : children) { |
|
|
|
List<CommandParamData[]> childTree = child.getTree(); |
|
|
|
|
|
for (CommandParamData[] subChild : childTree) { |
|
CommandParamData[] tmpTree = new CommandParamData[subChild.length + 1]; |
|
tmpTree[0] = child.getParamData(); |
|
System.arraycopy(subChild, 0, tmpTree, 1, subChild.length); |
|
|
|
treeParamData.add(tmpTree); |
|
} |
|
|
|
|
|
if (childTree.size() == 0) { |
|
treeParamData.add(new CommandParamData[] { child.getParamData() }); |
|
} |
|
} |
|
|
|
return treeParamData; |
|
} |
|
} |
|
} |
|
|