aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Server/Console/commands/give/CliCommandGive.cpp
blob: 20c0949721f96149ee05f6e12eb493f7c79c3f53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "stdafx.h"

#include "CliCommandGive.h"

#include "..\..\ServerCliEngine.h"
#include "..\..\ServerCliParser.h"
#include "..\CommandParsing.h"
#include "..\..\..\..\Minecraft.World\GameCommandPacket.h"
#include "..\..\..\..\Minecraft.World\GiveItemCommand.h"
#include "..\..\..\..\Minecraft.World\net.minecraft.world.entity.player.h"
#include "..\..\..\..\Minecraft.Client\ServerPlayer.h"

namespace ServerRuntime
{
	namespace
	{
		constexpr const char *kGiveUsage = "give <player> <itemId> [amount] [aux]";
	}

	const char *CliCommandGive::Name() const
	{
		return "give";
	}

	const char *CliCommandGive::Usage() const
	{
		return kGiveUsage;
	}

	const char *CliCommandGive::Description() const
	{
		return "Give an item via Minecraft.World command dispatcher.";
	}

	bool CliCommandGive::Execute(const ServerCliParsedLine &line, ServerCliEngine *engine)
	{
		if (line.tokens.size() < 3 || line.tokens.size() > 5)
		{
			engine->LogWarn(std::string("Usage: ") + kGiveUsage);
			return false;
		}

		std::shared_ptr<ServerPlayer> target = engine->FindPlayerByNameUtf8(line.tokens[1]);
		if (target == nullptr)
		{
			engine->LogWarn("Unknown player: " + line.tokens[1]);
			return false;
		}

		int itemId = 0;
		int amount = 1;
		int aux = 0;
		if (!CommandParsing::TryParseInt(line.tokens[2], &itemId))
		{
			engine->LogWarn("Invalid item id: " + line.tokens[2]);
			return false;
		}
		if (itemId <= 0)
		{
			engine->LogWarn("Item id must be greater than 0.");
			return false;
		}
		if (line.tokens.size() >= 4 && !CommandParsing::TryParseInt(line.tokens[3], &amount))
		{
			engine->LogWarn("Invalid amount: " + line.tokens[3]);
			return false;
		}
		if (line.tokens.size() >= 5 && !CommandParsing::TryParseInt(line.tokens[4], &aux))
		{
			engine->LogWarn("Invalid aux value: " + line.tokens[4]);
			return false;
		}
		if (amount <= 0)
		{
			engine->LogWarn("Amount must be greater than 0.");
			return false;
		}

		std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(target);
		if (player == nullptr)
		{
			engine->LogWarn("Cannot resolve target player entity.");
			return false;
		}

		std::shared_ptr<GameCommandPacket> packet = GiveItemCommand::preparePacket(player, itemId, amount, aux);
		if (packet == nullptr)
		{
			engine->LogError("Failed to build give command packet.");
			return false;
		}

		return engine->DispatchWorldCommand(packet->command, packet->data);
	}

	void CliCommandGive::Complete(const ServerCliCompletionContext &context, const ServerCliEngine *engine, std::vector<std::string> *out) const
	{
		if (context.currentTokenIndex == 1)
		{
			engine->SuggestPlayers(context.prefix, context.linePrefix, out);
		}
	}
}