Skip to content

Commit

Permalink
refactor: update more methods to return Result type
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Sep 30, 2024
1 parent e492667 commit 22e3a74
Show file tree
Hide file tree
Showing 31 changed files with 157 additions and 133 deletions.
3 changes: 2 additions & 1 deletion include/endstone/boss/boss_bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "endstone/boss/bar_color.h"
#include "endstone/boss/bar_flag.h"
#include "endstone/boss/bar_style.h"
#include "endstone/util/result.h"

namespace endstone {

Expand Down Expand Up @@ -97,7 +98,7 @@ class BossBar {
* @brief Sets the progress of the bar. Values should be between 0.0 (empty) and 1.0 (full)
* @param progress the progress of the bar
*/
virtual void setProgress(float progress) = 0;
virtual Result<void> setProgress(float progress) = 0;

/**
* @brief Return if the boss bar is displayed to attached players
Expand Down
6 changes: 3 additions & 3 deletions include/endstone/detail/actor/actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class EndstoneActor : public Actor {
[[nodiscard]] bool isPermissionSet(const Permission &perm) const override;
[[nodiscard]] bool hasPermission(std::string name) const override;
[[nodiscard]] bool hasPermission(const Permission &perm) const override;
PermissionAttachment *addAttachment(Plugin &plugin, const std::string &name, bool value) override;
PermissionAttachment *addAttachment(Plugin &plugin) override;
bool removeAttachment(PermissionAttachment &attachment) override;
Result<PermissionAttachment *> addAttachment(Plugin &plugin, const std::string &name, bool value) override;
Result<PermissionAttachment *> addAttachment(Plugin &plugin) override;
Result<void> removeAttachment(PermissionAttachment &attachment) override;
void recalculatePermissions() override;
[[nodiscard]] std::unordered_set<PermissionAttachmentInfo *> getEffectivePermissions() const override;
[[nodiscard]] bool isOp() const override;
Expand Down
6 changes: 3 additions & 3 deletions include/endstone/detail/actor/mob.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class EndstoneMob : public EndstoneActor, public Mob {
[[nodiscard]] bool isPermissionSet(const Permission &perm) const override;
[[nodiscard]] bool hasPermission(std::string name) const override;
[[nodiscard]] bool hasPermission(const Permission &perm) const override;
PermissionAttachment *addAttachment(Plugin &plugin, const std::string &name, bool value) override;
PermissionAttachment *addAttachment(Plugin &plugin) override;
bool removeAttachment(PermissionAttachment &attachment) override;
Result<PermissionAttachment *> addAttachment(Plugin &plugin, const std::string &name, bool value) override;
Result<PermissionAttachment *> addAttachment(Plugin &plugin) override;
Result<void> removeAttachment(PermissionAttachment &attachment) override;
void recalculatePermissions() override;
[[nodiscard]] std::unordered_set<PermissionAttachmentInfo *> getEffectivePermissions() const override;
[[nodiscard]] bool isOp() const override;
Expand Down
2 changes: 1 addition & 1 deletion include/endstone/detail/boss/boss_bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class EndstoneBossBar : public BossBar {
void addFlag(BarFlag flag) override;
void removeFlag(BarFlag flag) override;
[[nodiscard]] float getProgress() const override;
void setProgress(float progress) override;
Result<void> setProgress(float progress) override;
[[nodiscard]] bool isVisible() const override;
void setVisible(bool visible) override;
void addPlayer(Player &player) override;
Expand Down
6 changes: 3 additions & 3 deletions include/endstone/detail/command/console_command_sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class EndstoneConsoleCommandSender : public ServerCommandSender, public ConsoleC
[[nodiscard]] bool isPermissionSet(const Permission &perm) const override;
[[nodiscard]] bool hasPermission(std::string name) const override;
[[nodiscard]] bool hasPermission(const Permission &perm) const override;
PermissionAttachment *addAttachment(Plugin &plugin, const std::string &name, bool value) override;
PermissionAttachment *addAttachment(Plugin &plugin) override;
bool removeAttachment(PermissionAttachment &attachment) override;
Result<PermissionAttachment *> addAttachment(Plugin &plugin, const std::string &name, bool value) override;
Result<PermissionAttachment *> addAttachment(Plugin &plugin) override;
Result<void> removeAttachment(PermissionAttachment &attachment) override;
void recalculatePermissions() override;
[[nodiscard]] std::unordered_set<PermissionAttachmentInfo *> getEffectivePermissions() const override;
};
Expand Down
6 changes: 3 additions & 3 deletions include/endstone/detail/command/proxied_command_sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class EndstoneProxiedCommandSender : public ProxiedCommandSender {
[[nodiscard]] bool isPermissionSet(const Permission &perm) const override;
[[nodiscard]] bool hasPermission(std::string name) const override;
[[nodiscard]] bool hasPermission(const Permission &perm) const override;
PermissionAttachment *addAttachment(Plugin &plugin, const std::string &name, bool value) override;
PermissionAttachment *addAttachment(Plugin &plugin) override;
bool removeAttachment(PermissionAttachment &attachment) override;
Result<PermissionAttachment *> addAttachment(Plugin &plugin, const std::string &name, bool value) override;
Result<PermissionAttachment *> addAttachment(Plugin &plugin) override;
Result<void> removeAttachment(PermissionAttachment &attachment) override;
void recalculatePermissions() override;
[[nodiscard]] std::unordered_set<PermissionAttachmentInfo *> getEffectivePermissions() const override;
[[nodiscard]] CommandSender *asCommandSender() const override;
Expand Down
6 changes: 3 additions & 3 deletions include/endstone/detail/command/server_command_sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class ServerCommandSender : public CommandSender {
[[nodiscard]] bool isPermissionSet(const Permission &perm) const override;
[[nodiscard]] bool hasPermission(std::string name) const override;
[[nodiscard]] bool hasPermission(const Permission &perm) const override;
PermissionAttachment *addAttachment(Plugin &plugin, const std::string &name, bool value) override;
PermissionAttachment *addAttachment(Plugin &plugin) override;
bool removeAttachment(PermissionAttachment &attachment) override;
Result<PermissionAttachment *> addAttachment(Plugin &plugin, const std::string &name, bool value) override;
Result<PermissionAttachment *> addAttachment(Plugin &plugin) override;
Result<void> removeAttachment(PermissionAttachment &attachment) override;
void recalculatePermissions() override;
[[nodiscard]] std::unordered_set<PermissionAttachmentInfo *> getEffectivePermissions() const override;

Expand Down
8 changes: 5 additions & 3 deletions include/endstone/detail/permissions/permissible_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <unordered_map>
#include <vector>

#include <nonstd/expected.hpp>

#include "endstone/permissions/permissible.h"
#include "endstone/permissions/permission_attachment.h"
#include "endstone/permissions/permission_attachment_info.h"
Expand All @@ -38,9 +40,9 @@ class PermissibleBase : public Permissible {
[[nodiscard]] bool isPermissionSet(const Permission &perm) const override;
[[nodiscard]] bool hasPermission(std::string name) const override;
[[nodiscard]] bool hasPermission(const Permission &perm) const override;
PermissionAttachment *addAttachment(Plugin &plugin, const std::string &name, bool value) override;
PermissionAttachment *addAttachment(Plugin &plugin) override;
bool removeAttachment(PermissionAttachment &attachment) override;
Result<PermissionAttachment *> addAttachment(Plugin &plugin, const std::string &name, bool value) override;
Result<PermissionAttachment *> addAttachment(Plugin &plugin) override;
Result<void> removeAttachment(PermissionAttachment &attachment) override;
void recalculatePermissions() override;
[[nodiscard]] std::unordered_set<PermissionAttachmentInfo *> getEffectivePermissions() const override;
[[nodiscard]] CommandSender *asCommandSender() const override;
Expand Down
12 changes: 6 additions & 6 deletions include/endstone/detail/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class EndstonePlayer : public EndstoneMob, public Player {
[[nodiscard]] bool isPermissionSet(const Permission &perm) const override;
[[nodiscard]] bool hasPermission(std::string name) const override;
[[nodiscard]] bool hasPermission(const Permission &perm) const override;
PermissionAttachment *addAttachment(Plugin &plugin, const std::string &name, bool value) override;
PermissionAttachment *addAttachment(Plugin &plugin) override;
bool removeAttachment(PermissionAttachment &attachment) override;
Result<PermissionAttachment *> addAttachment(Plugin &plugin, const std::string &name, bool value) override;
Result<PermissionAttachment *> addAttachment(Plugin &plugin) override;
Result<void> removeAttachment(PermissionAttachment &attachment) override;
void recalculatePermissions() override;
[[nodiscard]] std::unordered_set<PermissionAttachmentInfo *> getEffectivePermissions() const override;
[[nodiscard]] bool isOp() const override;
Expand Down Expand Up @@ -83,14 +83,14 @@ class EndstonePlayer : public EndstoneMob, public Player {
void giveExp(int amount) override;
void giveExpLevels(int amount) override;
[[nodiscard]] float getExpProgress() const override;
void setExpProgress(float progress) override;
Result<void> setExpProgress(float progress) override;
[[nodiscard]] int getExpLevel() const override;
void setExpLevel(int level) override;
Result<void> setExpLevel(int level) override;
[[nodiscard]] int getTotalExp() const override;
[[nodiscard]] bool getAllowFlight() const override;
void setAllowFlight(bool flight) override;
[[nodiscard]] bool isFlying() const override;
void setFlying(bool value) override;
Result<void> setFlying(bool value) override;
[[nodiscard]] float getFlySpeed() const override;
void setFlySpeed(float value) const override;
[[nodiscard]] float getWalkSpeed() const override;
Expand Down
6 changes: 3 additions & 3 deletions include/endstone/detail/plugin/plugin_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class EndstonePluginManager : public PluginManager {

/** Event system */
void callEvent(Event &event) override;
void registerEvent(std::string event, std::function<void(Event &)> executor, EventPriority priority, Plugin &plugin,
bool ignore_cancelled) override;
Result<void> registerEvent(std::string event, std::function<void(Event &)> executor, EventPriority priority,
Plugin &plugin, bool ignore_cancelled) override;

/** Permission system */
[[nodiscard]] Permission *getPermission(std::string name) const override;
Expand All @@ -66,7 +66,7 @@ class EndstonePluginManager : public PluginManager {

private:
friend class EndstoneServer;
void initPlugin(Plugin &plugin, PluginLoader &loader, const std::filesystem::path& base_folder);
void initPlugin(Plugin &plugin, PluginLoader &loader, const std::filesystem::path &base_folder);
void calculatePermissionDefault(Permission &perm);
void dirtyPermissibles(bool op) const;
Server &server_;
Expand Down
7 changes: 4 additions & 3 deletions include/endstone/detail/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class EndstoneServer : public Server {

[[nodiscard]] std::vector<Player *> getOnlinePlayers() const override;
[[nodiscard]] int getMaxPlayers() const override;
void setMaxPlayers(int max_players) override;
Result<void> setMaxPlayers(int max_players) override;
[[nodiscard]] Player *getPlayer(endstone::UUID id) const override;
[[nodiscard]] Player *getPlayer(std::string name) const override;
[[nodiscard]] Player *getPlayer(const ::NetworkIdentifier &network_id, SubClientId sub_id) const;
Expand Down Expand Up @@ -93,8 +93,9 @@ class EndstoneServer : public Server {
BarStyle style) const override;
[[nodiscard]] std::unique_ptr<BossBar> createBossBar(std::string title, BarColor color, BarStyle style,
std::vector<BarFlag> flags) const override;
[[nodiscard]] std::shared_ptr<BlockData> createBlockData(std::string type) const override;
[[nodiscard]] std::shared_ptr<BlockData> createBlockData(std::string type, BlockStates block_states) const override;
[[nodiscard]] Result<std::shared_ptr<BlockData>> createBlockData(std::string type) const override;
[[nodiscard]] Result<std::shared_ptr<BlockData>> createBlockData(std::string type,
BlockStates block_states) const override;

[[nodiscard]] EndstoneScoreboard &getPlayerBoard(const EndstonePlayer &player) const;
void setPlayerBoard(EndstonePlayer &player, Scoreboard &scoreboard);
Expand Down
8 changes: 4 additions & 4 deletions include/endstone/permissions/permissible.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <unordered_set>

#include "endstone/permissions/permission_attachment_info.h"
#include "endstone/util/error.h"
#include "endstone/util/result.h"

namespace endstone {

Expand Down Expand Up @@ -90,23 +90,23 @@ class Permissible {
* @param value Value of the permission
* @return The PermissionAttachment that was just created
*/
virtual PermissionAttachment *addAttachment(Plugin &plugin, const std::string &name, bool value) = 0;
virtual Result<PermissionAttachment *> addAttachment(Plugin &plugin, const std::string &name, bool value) = 0;

/**
* Adds a new empty PermissionAttachment to this object
*
* @param plugin Plugin responsible for this attachment, may not be null or disabled
* @return The PermissionAttachment that was just created
*/
virtual PermissionAttachment *addAttachment(Plugin &plugin) = 0;
virtual Result<PermissionAttachment *> addAttachment(Plugin &plugin) = 0;

/**
* Removes the given PermissionAttachment from this object
*
* @param attachment Attachment to remove
* @return true if the specified attachment was removed successfully, false when it isn't part of this object
*/
virtual bool removeAttachment(PermissionAttachment &attachment) = 0;
virtual Result<void> removeAttachment(PermissionAttachment &attachment) = 0;

/**
* Recalculates the permissions for this object, if the attachments have changed values.
Expand Down
2 changes: 1 addition & 1 deletion include/endstone/permissions/permission_attachment.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class PermissionAttachment {
*/
bool remove()
{
return permissible_.removeAttachment(*this);
return permissible_.removeAttachment(*this).has_value();
}

private:
Expand Down
6 changes: 3 additions & 3 deletions include/endstone/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class Player : public Mob {
*
* @param progress New experience progress
*/
virtual void setExpProgress(float progress) = 0;
virtual Result<void> setExpProgress(float progress) = 0;

/**
* @brief Gets the players current experience level
Expand All @@ -147,7 +147,7 @@ class Player : public Mob {
*
* @param level New experience level
*/
virtual void setExpLevel(int level) = 0;
virtual Result<void> setExpLevel(int level) = 0;

/**
* @brief Gets the players total experience points.
Expand Down Expand Up @@ -185,7 +185,7 @@ class Player : public Mob {
*
* @param value True to fly.
*/
virtual void setFlying(bool value) = 0;
virtual Result<void> setFlying(bool value) = 0;

/**
* @brief Gets the current allowed speed that a client can fly.
Expand Down
12 changes: 10 additions & 2 deletions include/endstone/plugin/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,26 @@ class Plugin : public CommandExecutor {
void registerEvent(void (T::*func)(EventType &), T &instance, EventPriority priority = EventPriority::Normal,
bool ignore_cancelled = false)
{
getServer().getPluginManager().registerEvent(
auto result = getServer().getPluginManager().registerEvent(
EventType::NAME, [func, &instance](Event &e) { (instance.*func)(static_cast<EventType &>(e)); }, priority,
*this, ignore_cancelled);
if (!result) {
server_->getLogger().error(result.error().getMessage());
server_->getLogger().error(result.error().getStackTrace());
}
}

template <typename EventType>
void registerEvent(std::function<void(EventType &)> func, EventPriority priority = EventPriority::Normal,
bool ignore_cancelled = false)
{
getServer().getPluginManager().registerEvent(
auto result = getServer().getPluginManager().registerEvent(
EventType::NAME, [func](Event &e) { func(static_cast<EventType &>(e)); }, priority, *this,
ignore_cancelled);
if (!result) {
server_->getLogger().error(result.error().getMessage());
server_->getLogger().error(result.error().getStackTrace());
}
}

protected:
Expand Down
4 changes: 2 additions & 2 deletions include/endstone/plugin/plugin_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ class PluginManager {
* @param ignore_cancelled Do not call executor if event was already
* cancelled
*/
virtual void registerEvent(std::string event, std::function<void(Event &)> executor, EventPriority priority,
Plugin &plugin, bool ignore_cancelled) = 0;
virtual Result<void> registerEvent(std::string event, std::function<void(Event &)> executor, EventPriority priority,
Plugin &plugin, bool ignore_cancelled) = 0;

/**
* Gets a Permission from its fully qualified name
Expand Down
9 changes: 5 additions & 4 deletions include/endstone/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "endstone/logger.h"
#include "endstone/player.h"
#include "endstone/scoreboard/scoreboard.h"
#include "endstone/util/result.h"
#include "endstone/util/uuid.h"

namespace endstone {
Expand Down Expand Up @@ -138,7 +139,7 @@ class Server {
*
* @param max_players The maximum amount of concurrent players
*/
virtual void setMaxPlayers(int max_players) = 0;
virtual Result<void> setMaxPlayers(int max_players) = 0;

/**
* @brief Gets the player with the given UUID.
Expand Down Expand Up @@ -296,7 +297,7 @@ class Server {
* @param type the block type
* @return new data instance
*/
[[nodiscard]] virtual std::shared_ptr<BlockData> createBlockData(std::string type) const = 0;
[[nodiscard]] virtual Result<std::shared_ptr<BlockData>> createBlockData(std::string type) const = 0;

/**
* @brief Creates a new BlockData instance for the specified block type, with all properties initialized to
Expand All @@ -306,8 +307,8 @@ class Server {
* @param block_states block states, for example {"old_leaf_type":"birch", "persistent_bit":true}
* @return new data instance
*/
[[nodiscard]] virtual std::shared_ptr<BlockData> createBlockData(std::string type,
BlockStates block_states) const = 0;
[[nodiscard]] virtual Result<std::shared_ptr<BlockData>> createBlockData(std::string type,
BlockStates block_states) const = 0;

/**
* @brief Gets the start time of the server.
Expand Down
6 changes: 3 additions & 3 deletions src/endstone_core/actor/actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ bool EndstoneActor::hasPermission(const Permission &perm) const
return getPermissibleBase().hasPermission(perm);
}

PermissionAttachment *EndstoneActor::addAttachment(Plugin &plugin, const std::string &name, bool value)
Result<PermissionAttachment *> EndstoneActor::addAttachment(Plugin &plugin, const std::string &name, bool value)
{
return getPermissibleBase().addAttachment(plugin, name, value);
}

PermissionAttachment *EndstoneActor::addAttachment(Plugin &plugin)
Result<PermissionAttachment *> EndstoneActor::addAttachment(Plugin &plugin)
{
return getPermissibleBase().addAttachment(plugin);
}

bool EndstoneActor::removeAttachment(PermissionAttachment &attachment)
Result<void> EndstoneActor::removeAttachment(PermissionAttachment &attachment)
{
return getPermissibleBase().removeAttachment(attachment);
}
Expand Down
6 changes: 3 additions & 3 deletions src/endstone_core/actor/mob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ bool EndstoneMob::hasPermission(const Permission &perm) const
return EndstoneActor::hasPermission(perm);
}

PermissionAttachment *EndstoneMob::addAttachment(Plugin &plugin, const std::string &name, bool value)
Result<PermissionAttachment *> EndstoneMob::addAttachment(Plugin &plugin, const std::string &name, bool value)
{
return EndstoneActor::addAttachment(plugin, name, value);
}

PermissionAttachment *EndstoneMob::addAttachment(Plugin &plugin)
Result<PermissionAttachment *> EndstoneMob::addAttachment(Plugin &plugin)
{
return EndstoneActor::addAttachment(plugin);
}

bool EndstoneMob::removeAttachment(PermissionAttachment &attachment)
Result<void> EndstoneMob::removeAttachment(PermissionAttachment &attachment)
{
return EndstoneActor::removeAttachment(attachment);
}
Expand Down
8 changes: 6 additions & 2 deletions src/endstone_core/block/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ void EndstoneBlock::setType(std::string type, bool apply_physics)
{
using detail::EndstoneServer;
if (checkState()) {
auto &server = entt::locator<EndstoneServer>::value();
setData(server.createBlockData(type), apply_physics);
const auto &server = entt::locator<EndstoneServer>::value();
server.createBlockData(type).and_then([&](auto &block_data) -> Result<void> {
// TODO: propagate the error from createBlockData
setData(block_data, apply_physics);
return {};
});
}
}

Expand Down
Loading

0 comments on commit 22e3a74

Please sign in to comment.