Skip to content

Commit

Permalink
fix: fix custom form empty dropdown crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Dofes committed Oct 12, 2024
1 parent da55b0e commit 994788f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
18 changes: 16 additions & 2 deletions src-server/ll/api/form/CustomForm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ class Dropdown : public CustomFormElement {
}

[[nodiscard]] CustomFormElementResult parseResult(nlohmann::ordered_json const& data) const override {
return mOptions[data.get<int>()];
if (!data.is_number_integer()) {
return std::string{};
}
int index = data.get<int>();
if (index < 0 || index >= static_cast<int>(mOptions.size())) {
return std::string{};
}
return mOptions[index];
}
};

Expand Down Expand Up @@ -220,7 +227,14 @@ class StepSlider : public CustomFormElement {
}

[[nodiscard]] CustomFormElementResult parseResult(nlohmann::ordered_json const& data) const override {
return mSteps[data.get<int>()];
if (!data.is_number_integer()) {
return std::string{};
}
int index = data.get<int>();
if (index < 0 || index >= static_cast<int>(mSteps.size())) {
return std::string{};
}
return mSteps[index];
}
};

Expand Down
4 changes: 3 additions & 1 deletion src-test/server/FormTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ void registerFormTestCommand() {
[](CommandOrigin const& ori, CommandOutput&, TestFormParam const& param) {
switch (param.type) {
case TestFormParam::FormType::custom: {
ll::form::CustomForm form;
ll::form::CustomForm form;
std::vector<std::string> names;
form.setTitle("CustomForm")
.appendLabel("label")
.appendInput("input1", "input")
.appendToggle("toggle", "toggle")
.appendSlider("slider", "slider", 0, 100, 1)
.appendStepSlider("stepSlider", "stepSlider", {"a", "b", "c"})
.appendDropdown("dropdown", "dropdown", {"a", "b", "c"})
.appendDropdown("emptydropdown", "empty dropdown", names)
.sendTo(
*(Player*)ori.getEntity(),
[](Player&, ll::form::CustomFormResult const& data, ll::form::FormCancelReason) {
Expand Down

0 comments on commit 994788f

Please sign in to comment.