forked from Kolyah35/minecraft-pe-0.6.1
FEAT: Server operators
This commit is contained in:
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 Player;
|
||||
|
||||
class Command {
|
||||
public:
|
||||
const std::string& getName() { return m_name; }
|
||||
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;
|
||||
|
||||
protected:
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
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()) {
|
||||
auto cmds = mc.commandManager().getListAllCommands();
|
||||
|
||||
|
||||
@@ -4,6 +4,6 @@ class CommandHelp : public Command {
|
||||
public:
|
||||
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);
|
||||
};
|
||||
@@ -9,7 +9,11 @@
|
||||
|
||||
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()) {
|
||||
return printHelp(mc);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,6 @@ class CommandKick : public Command {
|
||||
public:
|
||||
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);
|
||||
};
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "commands/Command.hpp"
|
||||
#include "commands/CommandHelp.hpp"
|
||||
#include "commands/CommandKick.hpp"
|
||||
#include "commands/CommandOp.hpp"
|
||||
#include "network/packet/ChatPacket.h"
|
||||
#include "network/RakNetInstance.h"
|
||||
#include "world/level/Level.h"
|
||||
@@ -19,6 +20,7 @@ CommandManager::CommandManager() {
|
||||
void CommandManager::registerAllCommands() {
|
||||
m_commands.push_back(new CommandHelp());
|
||||
m_commands.push_back(new CommandKick());
|
||||
m_commands.push_back(new CommandOp());
|
||||
}
|
||||
|
||||
std::vector<std::string> CommandManager::getListAllCommands() {
|
||||
@@ -31,7 +33,7 @@ std::vector<std::string> CommandManager::getListAllCommands() {
|
||||
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::string cmd;
|
||||
|
||||
@@ -51,7 +53,7 @@ void CommandManager::execute(Minecraft& mc, const std::string& input) {
|
||||
while (ss >> tok) args.push_back(tok);
|
||||
|
||||
if (!mc.level->isClientSide || (*it)->getFlags() & CommandFlags::COMMAND_FLAG_SINGLEPLAYER_ONLY) {
|
||||
(*it)->execute(mc, args);
|
||||
(*it)->execute(mc, player, args);
|
||||
} else {
|
||||
ChatPacket packet("/" + input);
|
||||
mc.raknetInstance->send(packet);
|
||||
|
||||
@@ -10,7 +10,7 @@ public:
|
||||
|
||||
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);
|
||||
|
||||
|
||||
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);
|
||||
};
|
||||
Reference in New Issue
Block a user