feat(Minecraft.Client): client-side commands (#52)

This commit is contained in:
/home/neo
2026-05-02 18:58:37 +03:00
committed by GitHub
parent a2f8569670
commit abacd76024
3 changed files with 166 additions and 37 deletions

View File

@@ -38,6 +38,17 @@
// 4J Added
#include "../Minecraft.World/net.minecraft.world.item.crafting.h"
#include "Options.h"
//neo: Command Includes
#include "TeleportCommand.h"
#include "../Minecraft.World/GiveItemCommand.h"
#include "../Minecraft.World/TimeCommand.h"
#include "../Minecraft.World/KillCommand.h"
#include "../Minecraft.World/GameModeCommand.h"
#include "../Minecraft.World/ToggleDownfallCommand.h"
#include <sstream>
#if defined(_WINDOWS64) && defined(MINECRAFT_SERVER_BUILD)
#include "../Minecraft.Server/ServerLogManager.h"
#include "../Minecraft.Server/Access/Access.h"
@@ -1026,10 +1037,127 @@ void PlayerConnection::handleCommand(const wstring& message)
if (FourKitBridge::HandlePlayerCommand(player->entityId, commandLine))
return;
#endif
// 4J - TODO
#if 0
server.getCommandDispatcher().performCommand(player, message);
#endif
wstringstream ss(message.substr(1));
wstring cmd;
ss >> cmd;
if (cmd == L"tp" || cmd == L"teleport")
{
wstring arg1, arg2;
ss >> arg1 >> arg2;
shared_ptr<ServerPlayer> target;
shared_ptr<ServerPlayer> destination;
if (arg1.empty())
{
warn(L"Usage: /tp [player] <target_player>");
return;
}
if (arg2.empty())
{
target = player;
destination = server->getPlayers()->getPlayer(arg1);
}
else
{
target = server->getPlayers()->getPlayer(arg1);
destination = server->getPlayers()->getPlayer(arg2);
}
if (target && destination)
{
shared_ptr<GameCommandPacket> packet = TeleportCommand::preparePacket(target->getXuid(), destination->getXuid());
server->getCommandDispatcher()->performCommand(player, eGameCommand_Teleport, packet->data);
}
else
{
warn(L"Player not found.");
}
}
else if (cmd == L"time")
{
wstring action;
ss >> action;
if (action == L"set")
{
wstring timeVal;
ss >> timeVal;
bool night = (timeVal == L"night");
shared_ptr<GameCommandPacket> packet = TimeCommand::preparePacket(night);
server->getCommandDispatcher()->performCommand(player, eGameCommand_Time, packet->data);
}
else
{
warn(L"Usage: /time set <day|night>");
}
}
else if (cmd == L"kill")
{
server->getCommandDispatcher()->performCommand(player, eGameCommand_Kill, byteArray());
}
else if (cmd == L"toggledownfall")
{
shared_ptr<GameCommandPacket> packet = ToggleDownfallCommand::preparePacket();
server->getCommandDispatcher()->performCommand(player, eGameCommand_ToggleDownfall, packet->data);
} else if (cmd == L"gamemode") {
wstring modeStr, targetName;
ss >> modeStr >> targetName;
if (modeStr.empty()) {
warn(L"Usage: /gamemode <mode> [player]");
return;
}
int mode = -1;
if (modeStr == L"0" || modeStr == L"s" || modeStr == L"survival")
mode = 0;
else if (modeStr == L"1" || modeStr == L"c" || modeStr == L"creative")
mode = 1;
else if (modeStr == L"2" || modeStr == L"a" || modeStr == L"adventure")
mode = 2;
else {
warn(L"Unknown game mode: " + modeStr);
return;
}
shared_ptr<ServerPlayer> target;
if (targetName.empty()) {
target = player;
} else {
target = server->getPlayers()->getPlayer(targetName);
if (!target) {
warn(L"Player not found: " + targetName);
return;
}
}
shared_ptr<GameCommandPacket> packet = GameModeCommand::preparePacket(target, mode);
server->getCommandDispatcher()->performCommand(player, eGameCommand_GameMode, packet->data);
} else if (cmd == L"give") {
wstring targetName, itemStr, amountStr, auxStr;
ss >> targetName >> itemStr >> amountStr >> auxStr;
if (targetName.empty() || itemStr.empty()) {
warn(L"Usage: /give <player> <item_id> [amount] [data]");
return;
}
shared_ptr<ServerPlayer> target = server->getPlayers()->getPlayer(targetName);
if (!target) {
warn(L"Player not found: " + targetName);
return;
}
int item, amount = 1, aux = 0;
try {
item = std::stoi(itemStr);
if (!amountStr.empty()) amount = std::stoi(amountStr);
if (!auxStr.empty()) aux = std::stoi(auxStr);
} catch (...) {
warn(L"Invalid item ID or amount");
return;
}
shared_ptr<GameCommandPacket> packet = GiveItemCommand::preparePacket(target, item, amount, aux);
server->getCommandDispatcher()->performCommand(player, eGameCommand_Give, packet->data);
}
}
void PlayerConnection::handleAnimate(shared_ptr<AnimatePacket> packet)
@@ -1127,14 +1255,12 @@ int PlayerConnection::countDelayedPackets()
void PlayerConnection::info(const wstring& string)
{
// 4J-PB - removed, since it needs to be localised in the language the client is in
//send( shared_ptr<ChatPacket>( new ChatPacket(L"<22>7" + string) ) );
send( shared_ptr<ChatPacket>( new ChatPacket(L"§7" + string) ) );
}
void PlayerConnection::warn(const wstring& string)
{
// 4J-PB - removed, since it needs to be localised in the language the client is in
//send( shared_ptr<ChatPacket>( new ChatPacket(L"<22>9" + string) ) );
send( shared_ptr<ChatPacket>( new ChatPacket(L"§c" + string) ) );
}
wstring PlayerConnection::getConsoleName()

View File

@@ -1,6 +1,11 @@
#include "stdafx.h"
#include "net.minecraft.commands.h"
#include "GameModeCommand.h"
#include "../Minecraft.Client/MinecraftServer.h"
#include "../Minecraft.Client/ServerPlayer.h"
#include "../Minecraft.Client/PlayerList.h"
#include "LevelSettings.h"
#include "net.minecraft.network.packet.h"
EGameCommand GameModeCommand::getId()
{
@@ -14,37 +19,34 @@ int GameModeCommand::getPermissionLevel()
void GameModeCommand::execute(shared_ptr<CommandSender> source, byteArray commandData)
{
//if (args.length > 0) {
// GameType newMode = getModeForString(source, args[0]);
// Player player = args.length >= 2 ? convertToPlayer(source, args[1]) : convertSourceToPlayer(source);
ByteArrayInputStream bais(commandData);
DataInputStream dis(&bais);
PlayerUID uid = dis.readPlayerUID();
int modeId = dis.readInt();
shared_ptr<ServerPlayer> player = MinecraftServer::getInstance()->getPlayers()->getPlayer(uid);
if (player != nullptr)
{
GameType *newMode = GameType::byId(modeId);
if (newMode != nullptr)
{
player->setGameMode(newMode);
player->resetLastActionTime();
source->sendMessage(L"Set " + player->getName() + L"'s game mode to " + newMode->getName());
}
}
}
// player.setGameMode(newMode);
// player.fallDistance = 0; // reset falldistance so flying people do not die :P
// ChatMessageComponent mode = ChatMessageComponent.forTranslation("gameMode." + newMode.getName());
// if (player != source) {
// logAdminAction(source, AdminLogCommand.LOGTYPE_DONT_SHOW_TO_SELF, "commands.gamemode.success.other", player.getAName(), mode);
// } else {
// logAdminAction(source, AdminLogCommand.LOGTYPE_DONT_SHOW_TO_SELF, "commands.gamemode.success.self", mode);
// }
// return;
//}
//throw new UsageException("commands.gamemode.usage");
shared_ptr<GameCommandPacket> GameModeCommand::preparePacket(shared_ptr<Player> player, int gameMode)
{
if (player == nullptr) return nullptr;
ByteArrayOutputStream baos;
DataOutputStream dos(&baos);
dos.writePlayerUID(player->getXuid());
dos.writeInt(gameMode);
return std::make_shared<GameCommandPacket>(eGameCommand_GameMode, baos.toByteArray());
}
GameType *GameModeCommand::getModeForString(shared_ptr<CommandSender> source, const wstring &name)
{
return nullptr;
//if (name.equalsIgnoreCase(GameType.SURVIVAL.getName()) || name.equalsIgnoreCase("s")) {
// return GameType.SURVIVAL;
//} else if (name.equalsIgnoreCase(GameType.CREATIVE.getName()) || name.equalsIgnoreCase("c")) {
// return GameType.CREATIVE;
//} else if (name.equalsIgnoreCase(GameType.ADVENTURE.getName()) || name.equalsIgnoreCase("a")) {
// return GameType.ADVENTURE;
//} else {
// return LevelSettings.validateGameType(convertArgToInt(source, name, 0, GameType.values().length - 2));
//}
return GameType::byName(name);
}

View File

@@ -1,8 +1,8 @@
#pragma once
#include "Command.h"
class GameType;
class GameCommandPacket;
class GameModeCommand : public Command
{
@@ -10,6 +10,7 @@ public:
virtual EGameCommand getId();
int getPermissionLevel();
virtual void execute(shared_ptr<CommandSender> source, byteArray commandData);
static shared_ptr<GameCommandPacket> preparePacket(shared_ptr<Player> player, int gameMode);
protected:
GameType *getModeForString(shared_ptr<CommandSender> source, const wstring &name);