Skip to content

Commit

Permalink
feat: add type caster for endstone::Result
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Sep 29, 2024
1 parent dde4d6f commit 9dc8d9e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
44 changes: 41 additions & 3 deletions include/endstone/detail/pybind_type_caster.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <pybind11/pybind11.h>

#include "endstone/util/result.h"
#include "endstone/util/uuid.h"

namespace pybind11::detail {
template <>
struct type_caster<endstone::UUID> {
class type_caster<endstone::UUID> {
public:
PYBIND11_TYPE_CASTER(endstone::UUID, const_name("uuid.UUID"));

// Python -> C++
bool load(handle src, bool)
{
Expand Down Expand Up @@ -82,5 +83,42 @@ struct type_caster<endstone::UUID> {

return {res};
}

PYBIND11_TYPE_CASTER(endstone::UUID, const_name("uuid.UUID"));
};

template <typename Value>
class type_caster<endstone::Result<Value>> {
public:
using value_conv = make_caster<Value>;

template <typename T>
static handle cast(T &&src, return_value_policy policy, handle parent)
{
if (!src) {
throw std::runtime_error(std::string(src.error().getMessage()));
}
if (!std::is_lvalue_reference<T>::value) {
policy = return_value_policy_override<Value>::policy(policy);
}
return value_conv::cast(*std::forward<T>(src), policy, parent);
}

PYBIND11_TYPE_CASTER(endstone::Result<Value>, value_conv::name);
};

template <>
class type_caster<endstone::Result<void>> {
public:
template <typename T>
static handle cast(T &&src, return_value_policy policy, handle parent)
{
if (!src) {
throw std::runtime_error(std::string(src.error().getMessage()));
}
return none().release();
}
PYBIND11_TYPE_CASTER(endstone::Result<void>, const_name("None"));
};

} // namespace pybind11::detail
10 changes: 5 additions & 5 deletions python/src/endstone/_internal/endstone_python.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1374,15 +1374,15 @@ class Objective:
Gets the criteria this objective tracks
"""
@property
def display_name(self) -> str | None:
def display_name(self) -> str:
"""
Gets or sets the name displayed to players for this objective
"""
@display_name.setter
def display_name(self, arg1: str) -> None:
...
@property
def display_slot(self) -> DisplaySlot | None:
def display_slot(self) -> DisplaySlot:
"""
Gets the display slot this objective is displayed at
"""
Expand All @@ -1392,12 +1392,12 @@ class Objective:
Gets if the objective's scores can be modified directly by a plugin
"""
@property
def name(self) -> str | None:
def name(self) -> str:
"""
Gets the name of this Objective
"""
@property
def render_type(self) -> RenderType | None:
def render_type(self) -> RenderType:
"""
Gets and sets the manner in which this objective will be rendered.
"""
Expand All @@ -1410,7 +1410,7 @@ class Objective:
Gets the scoreboard to which this objective is attached
"""
@property
def sort_order(self) -> ObjectiveSortOrder | None:
def sort_order(self) -> ObjectiveSortOrder:
"""
Gets and sets the sort order for this objective
"""
Expand Down
4 changes: 2 additions & 2 deletions src/endstone_core/scoreboard/objective.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Result<DisplaySlot> EndstoneObjective::getDisplaySlot() const
if (result) {
return result.value();
}
return nonstd::make_unexpected(make_error("Object is not displayer"));
return nonstd::make_unexpected(make_error("Object is not displayed."));
})
.or_else([](const auto &err) -> Result<DisplaySlot> { return nonstd::make_unexpected(err); });
}
Expand All @@ -117,7 +117,7 @@ Result<ObjectiveSortOrder> EndstoneObjective::getSortOrder() const
if (result) {
return result.value();
}
return nonstd::make_unexpected(make_error("Object is not displayer"));
return nonstd::make_unexpected(make_error("Object is not displayed."));
})
.or_else([](const auto &err) -> Result<ObjectiveSortOrder> { return nonstd::make_unexpected(err); });
}
Expand Down
1 change: 1 addition & 0 deletions src/endstone_python/scoreboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

// must be included after pybind11
#include "endstone/actor/actor.h"
#include "endstone/detail/pybind_type_caster.h"
#include "endstone/player.h"
#include "endstone/scoreboard/criteria.h"
#include "endstone/scoreboard/display_slot.h"
Expand Down

0 comments on commit 9dc8d9e

Please sign in to comment.