|
| 1 | +// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "endstone/core/command/defaults/restart_command.h" |
| 16 | + |
| 17 | +#include <chrono> |
| 18 | +#include <filesystem> |
| 19 | +#include <fstream> |
| 20 | + |
| 21 | +#include <entt/entt.hpp> |
| 22 | + |
| 23 | +#include "endstone/color_format.h" |
| 24 | +#include "endstone/core/server.h" |
| 25 | + |
| 26 | +namespace endstone::core { |
| 27 | + |
| 28 | +RestartCommand::RestartCommand() : EndstoneCommand("restart") |
| 29 | +{ |
| 30 | + setDescription("Restarts the server."); |
| 31 | + setUsages("/restart"); |
| 32 | + setPermissions("endstone.command.restart"); |
| 33 | +} |
| 34 | + |
| 35 | +bool RestartCommand::execute(CommandSender &sender, const std::vector<std::string> &args) const |
| 36 | +{ |
| 37 | + if (!testPermission(sender)) { |
| 38 | + return true; |
| 39 | + } |
| 40 | + |
| 41 | + auto &server = entt::locator<EndstoneServer>::value(); |
| 42 | + server.broadcast(ColorFormat::Yellow + "Server is restarting...", Server::BroadcastChannelAdmin); |
| 43 | + |
| 44 | + // Write a restart marker file with the current timestamp |
| 45 | + auto marker_path = std::filesystem::current_path() / ".endstone_restart"; |
| 46 | + std::ofstream marker(marker_path); |
| 47 | + if (marker.is_open()) { |
| 48 | + auto now = std::chrono::system_clock::now(); |
| 49 | + auto epoch = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count(); |
| 50 | + marker << epoch; |
| 51 | + } |
| 52 | + |
| 53 | + server.shutdown(); |
| 54 | + return true; |
| 55 | +} |
| 56 | + |
| 57 | +} // namespace endstone::core |
0 commit comments