mirror of
https://gitea.sffempire.ru/Kolyah35/minecraft-pe-0.6.1.git
synced 2026-03-31 04:23:31 +00:00
FEAT: Server operators
This commit is contained in:
@@ -277,6 +277,8 @@ void Minecraft::setLevel(Level* level, const std::string& message /* ="" */, Loc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
this->level = level;
|
this->level = level;
|
||||||
|
// So uhhh
|
||||||
|
level->ops.emplace(options.getStringValue(OPTIONS_USERNAME));
|
||||||
_hasSignaledGeneratingLevelFinished = false;
|
_hasSignaledGeneratingLevelFinished = false;
|
||||||
#ifdef STANDALONE_SERVER
|
#ifdef STANDALONE_SERVER
|
||||||
const bool threadedLevelCreation = false;
|
const bool threadedLevelCreation = false;
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ void ConsoleScreen::execute()
|
|||||||
if (_input[0] == '/') {
|
if (_input[0] == '/') {
|
||||||
// Command
|
// Command
|
||||||
_input = Util::stringTrim(_input.substr(1));
|
_input = Util::stringTrim(_input.substr(1));
|
||||||
minecraft->commandManager().execute(*minecraft, _input);
|
minecraft->commandManager().execute(*minecraft, *minecraft->player, _input);
|
||||||
} else {
|
} else {
|
||||||
// @ai @rewrite
|
// @ai @rewrite
|
||||||
if (minecraft->netCallback && minecraft->raknetInstance->isServer()) {
|
if (minecraft->netCallback && minecraft->raknetInstance->isServer()) {
|
||||||
|
|||||||
7
src/commands/Command.cpp
Normal file
7
src/commands/Command.cpp
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#include "Command.hpp"
|
||||||
|
#include "world/level/Level.h"
|
||||||
|
#include <client/Minecraft.h>
|
||||||
|
|
||||||
|
bool Command::isPlayerOp(Minecraft& mc, Player& player) {
|
||||||
|
return mc.level->ops.find(player.name) != mc.level->ops.end();
|
||||||
|
}
|
||||||
@@ -8,13 +8,16 @@ enum CommandFlags {
|
|||||||
};
|
};
|
||||||
|
|
||||||
class Minecraft;
|
class Minecraft;
|
||||||
|
class Player;
|
||||||
|
|
||||||
class Command {
|
class Command {
|
||||||
public:
|
public:
|
||||||
const std::string& getName() { return m_name; }
|
const std::string& getName() { return m_name; }
|
||||||
const CommandFlags getFlags() { return m_flags; }
|
const CommandFlags getFlags() { return m_flags; }
|
||||||
|
|
||||||
virtual void execute(Minecraft& mc, const std::vector<std::string>& args) = 0;
|
bool isPlayerOp(Minecraft& mc, Player& player);
|
||||||
|
|
||||||
|
virtual void execute(Minecraft& mc, Player& player, const std::vector<std::string>& args) = 0;
|
||||||
virtual void printHelp(Minecraft& mc) = 0;
|
virtual void printHelp(Minecraft& mc) = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
CommandHelp::CommandHelp() : Command("help") {}
|
CommandHelp::CommandHelp() : Command("help") {}
|
||||||
|
|
||||||
void CommandHelp::execute(Minecraft& mc, const std::vector<std::string>& args) {
|
void CommandHelp::execute(Minecraft& mc, Player& player, const std::vector<std::string>& args) {
|
||||||
if (args.empty()) {
|
if (args.empty()) {
|
||||||
auto cmds = mc.commandManager().getListAllCommands();
|
auto cmds = mc.commandManager().getListAllCommands();
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,6 @@ class CommandHelp : public Command {
|
|||||||
public:
|
public:
|
||||||
CommandHelp();
|
CommandHelp();
|
||||||
|
|
||||||
void execute(Minecraft& mc, const std::vector<std::string>& args);
|
void execute(Minecraft& mc, Player& player, const std::vector<std::string>& args);
|
||||||
void printHelp(Minecraft& mc);
|
void printHelp(Minecraft& mc);
|
||||||
};
|
};
|
||||||
@@ -9,7 +9,11 @@
|
|||||||
|
|
||||||
CommandKick::CommandKick() : Command("kick") {}
|
CommandKick::CommandKick() : Command("kick") {}
|
||||||
|
|
||||||
void CommandKick::execute(Minecraft& mc, const std::vector<std::string>& args) {
|
void CommandKick::execute(Minecraft& mc, Player& player, const std::vector<std::string>& args) {
|
||||||
|
if (!isPlayerOp(mc, player)) {
|
||||||
|
return mc.addMessage("You aren't enough priveleged to run this command");
|
||||||
|
}
|
||||||
|
|
||||||
if (args.empty()) {
|
if (args.empty()) {
|
||||||
return printHelp(mc);
|
return printHelp(mc);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,6 @@ class CommandKick : public Command {
|
|||||||
public:
|
public:
|
||||||
CommandKick();
|
CommandKick();
|
||||||
|
|
||||||
void execute(Minecraft& mc, const std::vector<std::string>& args);
|
void execute(Minecraft& mc, Player& player, const std::vector<std::string>& args);
|
||||||
void printHelp(Minecraft& mc);
|
void printHelp(Minecraft& mc);
|
||||||
};
|
};
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "commands/Command.hpp"
|
#include "commands/Command.hpp"
|
||||||
#include "commands/CommandHelp.hpp"
|
#include "commands/CommandHelp.hpp"
|
||||||
#include "commands/CommandKick.hpp"
|
#include "commands/CommandKick.hpp"
|
||||||
|
#include "commands/CommandOp.hpp"
|
||||||
#include "network/packet/ChatPacket.h"
|
#include "network/packet/ChatPacket.h"
|
||||||
#include "network/RakNetInstance.h"
|
#include "network/RakNetInstance.h"
|
||||||
#include "world/level/Level.h"
|
#include "world/level/Level.h"
|
||||||
@@ -19,6 +20,7 @@ CommandManager::CommandManager() {
|
|||||||
void CommandManager::registerAllCommands() {
|
void CommandManager::registerAllCommands() {
|
||||||
m_commands.push_back(new CommandHelp());
|
m_commands.push_back(new CommandHelp());
|
||||||
m_commands.push_back(new CommandKick());
|
m_commands.push_back(new CommandKick());
|
||||||
|
m_commands.push_back(new CommandOp());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> CommandManager::getListAllCommands() {
|
std::vector<std::string> CommandManager::getListAllCommands() {
|
||||||
@@ -31,7 +33,7 @@ std::vector<std::string> CommandManager::getListAllCommands() {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CommandManager::execute(Minecraft& mc, const std::string& input) {
|
void CommandManager::execute(Minecraft& mc, Player& player, const std::string& input) {
|
||||||
std::istringstream ss(input);
|
std::istringstream ss(input);
|
||||||
std::string cmd;
|
std::string cmd;
|
||||||
|
|
||||||
@@ -51,7 +53,7 @@ void CommandManager::execute(Minecraft& mc, const std::string& input) {
|
|||||||
while (ss >> tok) args.push_back(tok);
|
while (ss >> tok) args.push_back(tok);
|
||||||
|
|
||||||
if (!mc.level->isClientSide || (*it)->getFlags() & CommandFlags::COMMAND_FLAG_SINGLEPLAYER_ONLY) {
|
if (!mc.level->isClientSide || (*it)->getFlags() & CommandFlags::COMMAND_FLAG_SINGLEPLAYER_ONLY) {
|
||||||
(*it)->execute(mc, args);
|
(*it)->execute(mc, player, args);
|
||||||
} else {
|
} else {
|
||||||
ChatPacket packet("/" + input);
|
ChatPacket packet("/" + input);
|
||||||
mc.raknetInstance->send(packet);
|
mc.raknetInstance->send(packet);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ public:
|
|||||||
|
|
||||||
std::vector<std::string> getListAllCommands();
|
std::vector<std::string> getListAllCommands();
|
||||||
|
|
||||||
void execute(Minecraft& mc, const std::string& input);
|
void execute(Minecraft& mc, Player& player, const std::string& input);
|
||||||
|
|
||||||
Command* getCommand(const std::string& name);
|
Command* getCommand(const std::string& name);
|
||||||
|
|
||||||
|
|||||||
35
src/commands/CommandOp.cpp
Normal file
35
src/commands/CommandOp.cpp
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#include "CommandOp.hpp"
|
||||||
|
#include "commands/Command.hpp"
|
||||||
|
#include "network/RakNetInstance.h"
|
||||||
|
#include "raknet/RakPeer.h"
|
||||||
|
#include "world/level/Level.h"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <client/Minecraft.h>
|
||||||
|
|
||||||
|
|
||||||
|
CommandOp::CommandOp() : Command("op") {}
|
||||||
|
|
||||||
|
void CommandOp::execute(Minecraft& mc, Player& player, const std::vector<std::string>& args) {
|
||||||
|
if (!isPlayerOp(mc, player)) {
|
||||||
|
return mc.addMessage("You aren't enough priveleged to run this command");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.empty()) {
|
||||||
|
return printHelp(mc);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto it = std::find_if(mc.level->players.begin(), mc.level->players.end(), [args] (auto& it) -> bool {
|
||||||
|
return it->name == args[0];
|
||||||
|
});
|
||||||
|
|
||||||
|
if (mc.level->ops.find(args[0]) != mc.level->ops.end()) {
|
||||||
|
return mc.addMessage("op: player " + args[0] + " already opped");
|
||||||
|
}
|
||||||
|
|
||||||
|
mc.level->ops.emplace((*it)->name);
|
||||||
|
mc.addMessage("op: successfully opped player " + args[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CommandOp::printHelp(Minecraft& mc) {
|
||||||
|
mc.addMessage("Usage: /op <player>");
|
||||||
|
}
|
||||||
9
src/commands/CommandOp.hpp
Normal file
9
src/commands/CommandOp.hpp
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#include "Command.hpp"
|
||||||
|
|
||||||
|
class CommandOp : public Command {
|
||||||
|
public:
|
||||||
|
CommandOp();
|
||||||
|
|
||||||
|
void execute(Minecraft& mc, Player& player, const std::vector<std::string>& args);
|
||||||
|
void printHelp(Minecraft& mc);
|
||||||
|
};
|
||||||
@@ -307,6 +307,9 @@ public:
|
|||||||
Dimension* dimension;
|
Dimension* dimension;
|
||||||
IRakNetInstance* raknetInstance;
|
IRakNetInstance* raknetInstance;
|
||||||
Random random;
|
Random random;
|
||||||
|
|
||||||
|
std::set<std::string> ops;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool isFindingSpawn;
|
bool isFindingSpawn;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user