diff --git a/CMakeLists.txt b/CMakeLists.txt index f1d03ea..01c659d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 () @@ -33,6 +37,10 @@ FetchContent_Declare( ) FetchContent_MakeAvailable(fmt) + +# =============== +# Endstone API +# =============== FetchContent_Declare( endstone GIT_REPOSITORY https://github.com/EndstoneMC/endstone.git @@ -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) # =============== diff --git a/include/endstone_cpp_plugin.h b/include/endstone_cpp_plugin.h index dd144a2..ff7001f 100644 --- a/include/endstone_cpp_plugin.h +++ b/include/endstone_cpp_plugin.h @@ -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()->setExecutor(std::make_unique()); + } + + 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 &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) diff --git a/include/fibonacci_command.h b/include/fibonacci_command.h index dd183d2..92bb205 100644 --- a/include/fibonacci_command.h +++ b/include/fibonacci_command.h @@ -17,13 +17,42 @@ #include "endstone/command/command.h" #include "endstone/command/command_executor.h" +#include // 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 "); + } }; class FibonacciCommandExecutor : public endstone::CommandExecutor { public: bool onCommand(const endstone::CommandSender &sender, const endstone::Command &command, - const std::vector &args) override; + const std::vector &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; + } + } }; diff --git a/src/endstone_cpp_plugin.cpp b/src/endstone_cpp_plugin.cpp index 34df807..2d5ccfc 100644 --- a/src/endstone_cpp_plugin.cpp +++ b/src/endstone_cpp_plugin.cpp @@ -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(); - command->setExecutor(std::make_unique()); -} - -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) diff --git a/src/fibonacci_command.cpp b/src/fibonacci_command.cpp deleted file mode 100644 index 445c6c5..0000000 --- a/src/fibonacci_command.cpp +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "fibonacci_command.h" - -#include -#include - -FibonacciCommand::FibonacciCommand() : Command("fibonacci") -{ - setDescription("A simple command that prints out the fibonacci series for n <= 20"); - setAliases("fib"); - setUsages("/fibonacci "); -} - -bool FibonacciCommandExecutor::onCommand(const endstone::CommandSender &sender, const endstone::Command &command, - const std::vector &args) -{ - int n = std::stoi(args[0]); - if (n > 0 && n <= 20) { - int t1 = 1, t2 = 1, next; - std::stringstream 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; - } -}