diff --git a/Minecraft.Client/PlayerConnection.cpp b/Minecraft.Client/PlayerConnection.cpp index f5bbd87e..b298ea30 100644 --- a/Minecraft.Client/PlayerConnection.cpp +++ b/Minecraft.Client/PlayerConnection.cpp @@ -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 + #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 target; + shared_ptr destination; + if (arg1.empty()) + { + warn(L"Usage: /tp [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 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 packet = TimeCommand::preparePacket(night); + server->getCommandDispatcher()->performCommand(player, eGameCommand_Time, packet->data); + } + else + { + warn(L"Usage: /time set "); + } + } + else if (cmd == L"kill") + { + server->getCommandDispatcher()->performCommand(player, eGameCommand_Kill, byteArray()); + } + else if (cmd == L"toggledownfall") + { + shared_ptr 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 [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 target; + if (targetName.empty()) { + target = player; + } else { + target = server->getPlayers()->getPlayer(targetName); + if (!target) { + warn(L"Player not found: " + targetName); + return; + } + } + + shared_ptr 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 [amount] [data]"); + return; + } + + shared_ptr 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 packet = GiveItemCommand::preparePacket(target, item, amount, aux); + server->getCommandDispatcher()->performCommand(player, eGameCommand_Give, packet->data); + } } void PlayerConnection::handleAnimate(shared_ptr 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( new ChatPacket(L"�7" + string) ) ); + send( shared_ptr( 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( new ChatPacket(L"�9" + string) ) ); + send( shared_ptr( new ChatPacket(L"§c" + string) ) ); } wstring PlayerConnection::getConsoleName() diff --git a/Minecraft.World/GameModeCommand.cpp b/Minecraft.World/GameModeCommand.cpp index 6e4cd858..ba38f070 100644 --- a/Minecraft.World/GameModeCommand.cpp +++ b/Minecraft.World/GameModeCommand.cpp @@ -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 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 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 GameModeCommand::preparePacket(shared_ptr player, int gameMode) +{ + if (player == nullptr) return nullptr; + ByteArrayOutputStream baos; + DataOutputStream dos(&baos); + dos.writePlayerUID(player->getXuid()); + dos.writeInt(gameMode); + return std::make_shared(eGameCommand_GameMode, baos.toByteArray()); } GameType *GameModeCommand::getModeForString(shared_ptr 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); } \ No newline at end of file diff --git a/Minecraft.World/GameModeCommand.h b/Minecraft.World/GameModeCommand.h index 302be9f5..70dd10af 100644 --- a/Minecraft.World/GameModeCommand.h +++ b/Minecraft.World/GameModeCommand.h @@ -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 source, byteArray commandData); + static shared_ptr preparePacket(shared_ptr player, int gameMode); protected: GameType *getModeForString(shared_ptr source, const wstring &name);