aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Server/Console/commands/time/CliCommandTime.cpp
blob: d274993ccd4e7f2c95230e75d6c5366cdf51e1b3 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "stdafx.h"

#include "CliCommandTime.h"

#include "..\..\ServerCliEngine.h"
#include "..\..\ServerCliParser.h"
#include "..\..\..\Common\StringUtils.h"
#include "..\..\..\..\Minecraft.World\GameCommandPacket.h"
#include "..\..\..\..\Minecraft.World\TimeCommand.h"

namespace ServerRuntime
{
	namespace
	{
		constexpr const char *kTimeUsage = "time <day|night|set day|set night>";

		static bool TryResolveNightFlag(const std::vector<std::string> &tokens, bool *outNight)
		{
			if (outNight == nullptr)
			{
				return false;
			}

			std::string value;
			if (tokens.size() == 2)
			{
				value = StringUtils::ToLowerAscii(tokens[1]);
			}
			else if (tokens.size() == 3 && StringUtils::ToLowerAscii(tokens[1]) == "set")
			{
				value = StringUtils::ToLowerAscii(tokens[2]);
			}
			else
			{
				return false;
			}

			if (value == "day")
			{
				*outNight = false;
				return true;
			}
			if (value == "night")
			{
				*outNight = true;
				return true;
			}

			return false;
		}

		static void SuggestLiteral(const char *candidate, const ServerCliCompletionContext &context, std::vector<std::string> *out)
		{
			if (candidate == nullptr || out == nullptr)
			{
				return;
			}

			const std::string text(candidate);
			if (StringUtils::StartsWithIgnoreCase(text, context.prefix))
			{
				out->push_back(context.linePrefix + text);
			}
		}
	}

	const char *CliCommandTime::Name() const
	{
		return "time";
	}

	const char *CliCommandTime::Usage() const
	{
		return kTimeUsage;
	}

	const char *CliCommandTime::Description() const
	{
		return "Set day or night via Minecraft.World command dispatcher.";
	}

	bool CliCommandTime::Execute(const ServerCliParsedLine &line, ServerCliEngine *engine)
	{
		bool night = false;
		if (!TryResolveNightFlag(line.tokens, &night))
		{
			engine->LogWarn(std::string("Usage: ") + kTimeUsage);
			return false;
		}

		std::shared_ptr<GameCommandPacket> packet = TimeCommand::preparePacket(night);
		if (packet == nullptr)
		{
			engine->LogError("Failed to build time command packet.");
			return false;
		}

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

	void CliCommandTime::Complete(const ServerCliCompletionContext &context, const ServerCliEngine *engine, std::vector<std::string> *out) const
	{
		(void)engine;
		if (context.currentTokenIndex == 1)
		{
			SuggestLiteral("day", context, out);
			SuggestLiteral("night", context, out);
			SuggestLiteral("set", context, out);
		}
		else if (context.currentTokenIndex == 2 &&
			context.parsed.tokens.size() >= 2 &&
			StringUtils::ToLowerAscii(context.parsed.tokens[1]) == "set")
		{
			SuggestLiteral("day", context, out);
			SuggestLiteral("night", context, out);
		}
	}
}