Skip to content

Commit

Permalink
feat: add example '/fibonacci' command
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Mar 5, 2024
1 parent 2a3b049 commit 29acf72
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 12 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ FetchContent_MakeAvailable(fmt)
FetchContent_Declare(
endstone
GIT_REPOSITORY https://github.com/EndstoneMC/endstone.git
GIT_TAG v1.20.51
GIT_TAG main # or set to a specific version tag
)
FetchContent_GetProperties(endstone)
if (NOT endstone_POPULATED)
Expand All @@ -47,7 +47,7 @@ endif ()
# ===============
# Build
# ===============
add_library(${PROJECT_NAME} SHARED src/endstone_cpp_plugin.cpp)
add_library(${PROJECT_NAME} SHARED src/endstone_cpp_plugin.cpp src/fibonacci_command.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)
Expand Down
13 changes: 3 additions & 10 deletions include/endstone_cpp_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,16 @@

#include "endstone/plugin/plugin.h"

class EndstoneCppPlugin : public Plugin {
class EndstoneCppPlugin : public endstone::Plugin {
public:
EndstoneCppPlugin() = default;

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

void onLoad() override;

void onEnable() override;

void onDisable() override;
[[nodiscard]] const endstone::PluginDescription &getDescription() const override;

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

ENDSTONE_PLUGIN(EndstoneCppPlugin)
29 changes: 29 additions & 0 deletions include/fibonacci_command.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.

#pragma once

#include "endstone/command/command.h"
#include "endstone/command/command_executor.h"

class FibonacciCommand : public endstone::Command {
public:
FibonacciCommand();
};

class FibonacciCommandExecutor : public endstone::CommandExecutor {
public:
bool onCommand(const endstone::CommandSender &sender, const endstone::Command &command,
const std::vector<std::string> &args) override;
};
10 changes: 10 additions & 0 deletions src/endstone_cpp_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

#include "endstone_cpp_plugin.h"

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

void EndstoneCppPlugin::onLoad()
{
getLogger().info("onLoad is called");
Expand All @@ -22,9 +25,16 @@ void EndstoneCppPlugin::onLoad()
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_;
}
50 changes: 50 additions & 0 deletions src/fibonacci_command.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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 <sstream>
#include <string>

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

bool FibonacciCommandExecutor::onCommand(const endstone::CommandSender &sender, const endstone::Command &command,
const std::vector<std::string> &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;
}
}

0 comments on commit 29acf72

Please sign in to comment.