Fixed crashing bug when using more than 1 root node

This commit is contained in:
rtm516 2020-04-13 17:27:30 +01:00
parent b0d39c587f
commit 549fe1d479

View file

@ -25,11 +25,8 @@
package org.geysermc.connector.network.translators.java; package org.geysermc.connector.network.translators.java;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.steveice10.mc.protocol.data.game.command.CommandNode; 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.data.game.command.CommandParser;
import com.github.steveice10.mc.protocol.data.game.command.CommandType;
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.CommandData; import com.nukkitx.protocol.bedrock.data.CommandData;
import com.nukkitx.protocol.bedrock.data.CommandEnumData; import com.nukkitx.protocol.bedrock.data.CommandEnumData;
@ -47,37 +44,30 @@ import java.util.*;
public class JavaServerDeclareCommandsTranslator extends PacketTranslator<ServerDeclareCommandsPacket> { public class JavaServerDeclareCommandsTranslator extends PacketTranslator<ServerDeclareCommandsPacket> {
@Override @Override
public void translate(ServerDeclareCommandsPacket packet, GeyserSession session) { public void translate(ServerDeclareCommandsPacket packet, GeyserSession session) {
List<CommandNode> rootNodes = new ArrayList<>();
List<CommandData> commandData = new ArrayList<>(); List<CommandData> commandData = new ArrayList<>();
Map<Integer, String> commands = new HashMap<>(); Map<Integer, String> commands = new HashMap<>();
Map<Integer, List<CommandNode>> commandArgs = new HashMap<>(); Map<Integer, List<CommandNode>> commandArgs = new HashMap<>();
// Find the root nodes // Get the first node, it should be a root node
for (CommandNode node : packet.getNodes()) { CommandNode rootNode = packet.getNodes()[packet.getFirstNodeIndex()];
if (node.getType() == CommandType.ROOT) {
rootNodes.add(node);
}
}
// Loop through the root nodes to get all commands // Loop through the root nodes to get all commands
for (CommandNode rootNode : rootNodes) { for (int nodeIndex : rootNode.getChildIndices()) {
for (int nodeIndex : rootNode.getChildIndices()) { CommandNode node = packet.getNodes()[nodeIndex];
CommandNode node = packet.getNodes()[nodeIndex];
// Make sure we dont have duplicated commands (happens if there is more than 1 root node) // Make sure we dont have duplicated commands (happens if there is more than 1 root node)
if (commands.containsKey(nodeIndex)) { continue; } if (commands.containsKey(nodeIndex)) { 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
commands.put(nodeIndex, node.getName());
} }
// Insert the command name into the list
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
@ -104,6 +94,8 @@ public class JavaServerDeclareCommandsTranslator extends PacketTranslator<Server
availableCommandsPacket.getCommands().add(data); availableCommandsPacket.getCommands().add(data);
} }
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.getUpstream().sendPacket(availableCommandsPacket); session.getUpstream().sendPacket(availableCommandsPacket);
} }