Massively clean up Cloud -> Bedrock code

This commit is contained in:
Camotoy 2024-11-01 13:13:20 -04:00
parent df8bd9b583
commit dc1674fcbf
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F

View file

@ -67,6 +67,7 @@ import org.incendo.cloud.internal.CommandNode;
import org.incendo.cloud.parser.standard.EnumParser; import org.incendo.cloud.parser.standard.EnumParser;
import org.incendo.cloud.parser.standard.IntegerParser; import org.incendo.cloud.parser.standard.IntegerParser;
import org.incendo.cloud.parser.standard.LiteralParser; import org.incendo.cloud.parser.standard.LiteralParser;
import org.incendo.cloud.parser.standard.StringArrayParser;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -320,7 +321,7 @@ public class CommandRegistry implements EventRegistrar {
} }
public void export(GeyserSession session, List<CommandData> bedrockCommands) { public void export(GeyserSession session, List<CommandData> bedrockCommands) {
cloud.commandTree().rootNode().children().forEach(commandTree -> { cloud.commandTree().rootNodes().forEach(commandTree -> {
var command = commandTree.command(); var command = commandTree.command();
// Command null happens if you register an extension command with custom Cloud parameters... // Command null happens if you register an extension command with custom Cloud parameters...
if (command == null || session.hasPermission(command.commandPermission().permissionString())) { if (command == null || session.hasPermission(command.commandPermission().permissionString())) {
@ -335,8 +336,7 @@ public class CommandRegistry implements EventRegistrar {
List<CommandOverloadData> data = new ArrayList<>(); List<CommandOverloadData> data = new ArrayList<>();
for (var node : commandTree.children()) { for (var node : commandTree.children()) {
List<List<CommandParamData>> params = new ArrayList<>(); List<List<CommandParamData>> params = createParamData(node);
createParamData(node, params);
params.forEach(param -> data.add(new CommandOverloadData(false, param.toArray(CommandParamData[]::new)))); params.forEach(param -> data.add(new CommandOverloadData(false, param.toArray(CommandParamData[]::new))));
} }
@ -348,7 +348,7 @@ public class CommandRegistry implements EventRegistrar {
}); });
} }
private void createParamData(CommandNode<GeyserCommandSource> node, List<List<CommandParamData>> bedrockData) { private List<List<CommandParamData>> createParamData(CommandNode<GeyserCommandSource> node) {
CommandParamData data = new CommandParamData(); CommandParamData data = new CommandParamData();
var component = node.component(); var component = node.component();
data.setName(component.name()); data.setName(component.name());
@ -370,44 +370,25 @@ public class CommandRegistry implements EventRegistrar {
} }
data.setEnumData(new CommandEnumData(component.name().toLowerCase(Locale.ROOT), map, false)); data.setEnumData(new CommandEnumData(component.name().toLowerCase(Locale.ROOT), map, false));
} else if (component.parser() instanceof StringArrayParser<?>) {
data.setType(CommandParam.TEXT);
} else { } else {
data.setType(CommandParam.STRING); data.setType(CommandParam.STRING);
} }
// This, realistically, is not going to be used without extensions using internals and implementing complicated commands. var children = node.children();
// It essentially does the same behavior as JavaCommandsTranslator#isCompatible. if (children.isEmpty()) {
// But, selfishly, I would like to use it, and in the future it's possible extensions can register commands List<CommandParamData> list = new ArrayList<>(); // Must be mutable; parents will be added to list.
// using Cloud, and in that case this becomes relevant!
if (bedrockData.isEmpty()) {
List<CommandParamData> list = new ArrayList<>();
list.add(data); list.add(data);
bedrockData.add(list); return Collections.singletonList(list); // Safe to do; will be consumed in an addAll call.
} else {
int size = bedrockData.size(); // Preserve original list size in case new entries get added.
for (int i = 0; i < size; i++) {
List<CommandParamData> cpdList = bedrockData.get(i);
if (cpdList.size() <= 1) { // No commands or parent will be root.
cpdList.add(data);
} else {
String parentName = node.parent().component().name(); // Should never be null.
if (!cpdList.get(cpdList.size() - 1).getName().equals(parentName)) { // We need to copy the list as this is a new branch.
for (int j = cpdList.size() - 2; j >= 0; j--) {
if (cpdList.get(j).getName().equals(parentName)) {
List<CommandParamData> newList = new ArrayList<>(cpdList.subList(0, j + 1));
newList.add(data);
bedrockData.add(newList);
break;
} }
List<List<CommandParamData>> collectiveData = new ArrayList<>();
// If a node has multiple children, this will need to be represented
// by creating a new list/branch for each and cloning this node down each line.
for (var child : children) {
collectiveData.addAll(createParamData(child));
} }
} else { collectiveData.forEach(list -> list.add(0, data));
cpdList.add(data); return collectiveData;
}
}
}
}
for (var child : node.children()) {
createParamData(child, bedrockData);
}
} }
} }