From 23bf01fc271266a57429e7b56bacddf9765e7344 Mon Sep 17 00:00:00 2001 From: KKNecmi Date: Sun, 8 Mar 2026 02:26:28 +0300 Subject: [PATCH] ChatCommands Testing --- Minecraft.Client/PlayerConnection.cpp | 74 ++++++++++++++++++++++++-- Minecraft.World/CommandDispatcher.cpp | 4 +- Minecraft.World/GameModeCommand.cpp | 75 ++++++++++++++++----------- 3 files changed, 117 insertions(+), 36 deletions(-) diff --git a/Minecraft.Client/PlayerConnection.cpp b/Minecraft.Client/PlayerConnection.cpp index 9404a5d68..c7fc41abf 100644 --- a/Minecraft.Client/PlayerConnection.cpp +++ b/Minecraft.Client/PlayerConnection.cpp @@ -633,10 +633,76 @@ void PlayerConnection::handleChat(shared_ptr packet) void PlayerConnection::handleCommand(const wstring& message) { - // 4J - TODO -#if 0 - server.getCommandDispatcher().performCommand(player, message); -#endif + wstring commandStr = message; + stripWhitespaceForHtml(commandStr, true); + + vector args; + if (message.find(' ') >= 0) + { + args = stringSplit(commandStr, ' '); + commandStr = args[0]; + args.erase(args.begin()); + } + + if (commandStr == L"/kill") + { + server->getCommandDispatcher()->performCommand(player, eGameCommand_Kill, byteArray()); + } + else if (commandStr == L"/gamemode") + { + ByteArrayOutputStream baos(2); + DataOutputStream dos(&baos); + + if (args.size() == 0) + { + player->sendMessage(L"Incorrect command usage!"); + return; + } + + dos.writeUTF(args[0]); + + if (args.size() >= 2) + { + // Player was specified in the command arguments + shared_ptr serverPlayer = server->getPlayers()->getPlayer(args[1]); + string playerName = wstringtochararray(args[1]); + if (!serverPlayer) + { + // Player not found + player->sendMessage(L"Player '" + args[1] + L"' not found!"); + return; + } + + PlayerUID uid = serverPlayer->getXuid(); + dos.writePlayerUID(uid); + } + else + { + // Set own gamemode + dos.writePlayerUID(player->getXuid()); + } + + server->getCommandDispatcher()->performCommand(player, eGameCommand_GameMode, baos.toByteArray()); + } + else if (commandStr == L"/teleport" || commandStr == L"/tp") + { + if (args.size() == 0) + { + player->sendMessage(L"Incorrect command usage!"); + return; + } + + if (args.size() >= 4) + { + app.DebugPrintf("size much"); + } + + app.DebugPrintf("Teleported"); + } + else + { + player->sendMessage(L"Unknown command!"); + } } void PlayerConnection::handleAnimate(shared_ptr packet) diff --git a/Minecraft.World/CommandDispatcher.cpp b/Minecraft.World/CommandDispatcher.cpp index 45b3f5fe6..69444c01d 100644 --- a/Minecraft.World/CommandDispatcher.cpp +++ b/Minecraft.World/CommandDispatcher.cpp @@ -15,9 +15,7 @@ int CommandDispatcher::performCommand(shared_ptr sender, EGameCom } else { -#ifndef _CONTENT_PACKAGE - sender->sendMessage(L"\u00A7cYou do not have permission to use this command."); -#endif + sender->sendMessage(L"You do not have permission to use this command."); } } else diff --git a/Minecraft.World/GameModeCommand.cpp b/Minecraft.World/GameModeCommand.cpp index f8c1ffb6f..8a1be50ab 100644 --- a/Minecraft.World/GameModeCommand.cpp +++ b/Minecraft.World/GameModeCommand.cpp @@ -1,5 +1,7 @@ #include "stdafx.h" #include "net.minecraft.commands.h" +#include "../Minecraft.Client/ServerPlayer.h" +#include "LevelSettings.h" #include "GameModeCommand.h" EGameCommand GameModeCommand::getId() @@ -12,39 +14,54 @@ int GameModeCommand::getPermissionLevel() return LEVEL_GAMEMASTERS; } -void GameModeCommand::execute(shared_ptr source, byteArray commandData) -{ - //if (args.length > 0) { - // GameType newMode = getModeForString(source, args[0]); - // Player player = args.length >= 2 ? convertToPlayer(source, args[1]) : convertSourceToPlayer(source); + void GameModeCommand::execute(shared_ptr source, byteArray commandData) + { - // player.setGameMode(newMode); - // player.fallDistance = 0; // reset falldistance so flying people do not die :P + ByteArrayInputStream bais(commandData); + DataInputStream dis(&bais); + + wstring gamemodeStr = dis.readUTF(); + PlayerUID uid = dis.readPlayerUID(); - // ChatMessageComponent mode = ChatMessageComponent.forTranslation("gameMode." + newMode.getName()); + GameType *gamemode = getModeForString(source, gamemodeStr); + shared_ptr player = getPlayer(uid); + + if (gamemode != NULL) + { + player->setGameMode(gamemode); + player->fallDistance = 0; + + //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"); -} + if (player != source) + { + logAdminAction(source, ChatPacket::e_ChatCustom, L"Set game mode of " + player->getName() + L" to " + gamemode->getName(), gamemode->getId(), player->getAName()); + //logAdminAction(source, AdminLogCommand::LOGTYPE_DONT_SHOW_TO_SELF, "commands.gamemode.success.other", player->getAName(), mode); + } + else + { + logAdminAction(source, ChatPacket::e_ChatCustom, L"Set own game mode to " + gamemode->getName(), gamemode->getId(), player->getAName()); + //logAdminAction(source, AdminLogCommand::LOGTYPE_DONT_SHOW_TO_SELF, "commands.gamemode.success.self", mode); + } + } + } GameType *GameModeCommand::getModeForString(shared_ptr source, const wstring &name) { - return NULL; - //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)); - //} + if (equalsIgnoreCase(name, GameType::SURVIVAL->getName()) || equalsIgnoreCase(name, L"s")) + { + return GameType::SURVIVAL; + } + else if (equalsIgnoreCase(name, GameType::CREATIVE->getName()) || equalsIgnoreCase(name, L"c")) + { + return GameType::CREATIVE; + } + else if (equalsIgnoreCase(name, GameType::ADVENTURE->getName()) || equalsIgnoreCase(name, L"a")) + { + return GameType::ADVENTURE; + } + else + { + return NULL; + } } \ No newline at end of file