Skip to content

Commit

Permalink
refactor: project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Mar 6, 2024
1 parent 29acf72 commit cc9320d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 88 deletions.
23 changes: 17 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ add_compile_definitions(_ITERATOR_DEBUG_LEVEL=0)
# ===============
# Compiler Check
# ===============
if (WIN32 AND NOT MSVC)
message(FATAL_ERROR "MSVC is required on Windows")
if (WIN32)
if (NOT MSVC)
message(FATAL_ERROR "MSVC is required on Windows.")
endif ()
elseif (UNIX)
if (NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
message(FATAL_ERROR "Clang is required on Linux")
message(FATAL_ERROR "Clang is required on Linux.")
endif ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
else ()
message(FATAL_ERROR "${CMAKE_SYSTEM_NAME} (${CMAKE_SYSTEM_PROCESSOR}) is not supported")
endif ()


Expand All @@ -33,6 +37,10 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(fmt)


# ===============
# Endstone API
# ===============
FetchContent_Declare(
endstone
GIT_REPOSITORY https://github.com/EndstoneMC/endstone.git
Expand All @@ -42,15 +50,18 @@ FetchContent_GetProperties(endstone)
if (NOT endstone_POPULATED)
FetchContent_Populate(endstone)
endif ()
add_library(endstone_headers INTERFACE)
add_library(endstone::headers ALIAS endstone_headers)
target_include_directories(endstone_headers INTERFACE ${endstone_SOURCE_DIR}/include)
target_link_libraries(endstone_headers INTERFACE fmt::fmt)


# ===============
# Build
# ===============
add_library(${PROJECT_NAME} SHARED src/endstone_cpp_plugin.cpp src/fibonacci_command.cpp)
add_library(${PROJECT_NAME} SHARED src/endstone_cpp_plugin.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC include)
target_include_directories(${PROJECT_NAME} PRIVATE ${endstone_SOURCE_DIR}/include)
target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt)
target_link_libraries(${PROJECT_NAME} PRIVATE endstone::headers)


# ===============
Expand Down
35 changes: 29 additions & 6 deletions include/endstone_cpp_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,41 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "endstone/command/plugin_command.h"
#include "endstone/plugin/plugin.h"
#include "fibonacci_command.h"

class EndstoneCppPlugin : public endstone::Plugin {
public:
EndstoneCppPlugin() = default;
void onLoad() override;
void onEnable() override;
void onDisable() override;
[[nodiscard]] const endstone::PluginDescription &getDescription() const override;
void onLoad() override
{
getLogger().info("onLoad is called");
}

void onEnable() override
{
getLogger().info("onEnable is called");
registerCommand<FibonacciCommand>()->setExecutor(std::make_unique<FibonacciCommandExecutor>());
}

void onDisable() override
{
getLogger().info("onDisable is called");
}

[[nodiscard]] const endstone::PluginDescription &getDescription() const override
{
return description_;
}

bool onCommand(const endstone::CommandSender &sender, const endstone::Command &command,
const std::vector<std::string> &args) override
{
// You can also handle commands here instead of setting an executor in onEnable if you prefer
return false;
}

private:
endstone::PluginDescription description_{"EndstoneCppPlugin", "0.1.0"};
};

ENDSTONE_PLUGIN(EndstoneCppPlugin)
33 changes: 31 additions & 2 deletions include/fibonacci_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,42 @@
#include "endstone/command/command.h"
#include "endstone/command/command_executor.h"

#include <sstream> // for std::ostringstream

class FibonacciCommand : public endstone::Command {
public:
FibonacciCommand();
FibonacciCommand() : Command("fibonacci")
{
setDescription("A simple command that prints out the fibonacci series for n <= 20");
setAliases("fib");
setUsages("/fibonacci <n: int>");
}
};

class FibonacciCommandExecutor : public endstone::CommandExecutor {
public:
bool onCommand(const endstone::CommandSender &sender, const endstone::Command &command,
const std::vector<std::string> &args) override;
const std::vector<std::string> &args) override
{
int n = std::stoi(args[0]);
if (n > 0 && n <= 20) {
int t1 = 1, t2 = 1, next;
std::ostringstream ss;
for (int i = 1; i <= n; ++i) {
if (i > 1) {
ss << ", ";
}
ss << t1;
next = t1 + t2;
t1 = t2;
t2 = next;
}
sender.sendMessage("Fibonacci Series (n = {}): {}", n, ss.str());
return true;
}
else {
sender.sendErrorMessage("'n' must be greater than 0 and less than or equal to 20.");
return false;
}
}
};
26 changes: 2 additions & 24 deletions src/endstone_cpp_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,5 @@

#include "endstone_cpp_plugin.h"

#include "endstone/command/plugin_command.h"
#include "fibonacci_command.h"

void EndstoneCppPlugin::onLoad()
{
getLogger().info("onLoad is called");
}

void EndstoneCppPlugin::onEnable()
{
getLogger().info("onEnable is called");
endstone::PluginCommand *command = registerCommand<FibonacciCommand>();
command->setExecutor(std::make_unique<FibonacciCommandExecutor>());
}

void EndstoneCppPlugin::onDisable()
{
getLogger().info("onDisable is called");
}

const endstone::PluginDescription &EndstoneCppPlugin::getDescription() const
{
return description_;
}
// The ENDSTONE_PLUGIN macro defines the entry point for the plugin.
ENDSTONE_PLUGIN(EndstoneCppPlugin)
50 changes: 0 additions & 50 deletions src/fibonacci_command.cpp

This file was deleted.

0 comments on commit cc9320d

Please sign in to comment.