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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// Minect is library that allows a program to connect to a running Minecraft instance without
// requiring any Minecraft mods.
//
// © Copyright (C) 2021-2023 Adrodoc <adrodoc55@googlemail.com> & skess42 <skagaros@gmail.com>
//
// This file is part of Minect.
//
// Minect is free software: you can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// Minect is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along with Minect.
// If not, see <http://www.gnu.org/licenses/>.
//! Functions for generating Minecraft commands that produce [LogEvent](crate::log::LogEvent)s.
//!
//! [LogEvents](crate::log::LogEvent) are only produced when the output of a command is written to
//! Minecraft's log file. For this to happen a number of preconditions have to be met:
//! 1. The command has to be executed by a player, command block or command block minecart. The
//! output of a command executed by a `mcfunction` is never logged.
//! 2. The gamerule `logAdminCommands` has to be `true`. If the command block is executed by a
//! command block or command block minecart then the gamerule `commandBlockOutput` also has to be
//! `true`.
//!
//! # Set Gamerules appropriately for Logging
//!
//! It is typically not a good idea to enable the gamerule `commandBlockOutput` for longer than
//! neccessary. The reason for this is that the output of commands is also written to the chat when
//! the gamerule `sendCommandFeedback` is enabled. This will likely annoy players as it makes the
//! chat unusable and causes it to take up a big part of the screen, even when only a single command
//! logs it's output every game tick. So whenever `commandBlockOutput` is enabled,
//! `sendCommandFeedback` should be disabled. But `sendCommandFeedback` should not be disabled for
//! longer than neccessary, because without it players will not get any output from commands they
//! execute.
//!
//! Ideally whenever the output of one or more commands should be logged, the three gamerules should
//! first be set to enable logging without spamming the chat and after the commands are executed,
//! the gamerules should be reset to their previous values to preserve the world configuration. This
//! can be done with [enable_logging_command] and [reset_logging_command].
//!
//! # Logging Command Output from Minecraft Function Files
//!
//! Minect offers two ways to work around the limitation of `mcfunction` files. To log the output of
//! a command from a `mcfunction` file you can either use [logged_block_commands] or a
//! [logged_cart_command].
//!
//! # Common Commands with useful Output
//!
//! Minect offers a few functions to generate commands commonly used to retrieve information from
//! Minecraft. Their output can then be parsed into predefined structs:
//! * [summon_named_entity_command] -> [SummonNamedEntityOutput]
//! * [add_tag_command] -> [AddTagOutput]
//! * [query_scoreboard_command] -> [QueryScoreboardOutput]
use crate;
use ;
/// Generates a Minecraft command that ensures [LogEvent](crate::log::LogEvent)s are created for all
/// commands until a [reset_logging_command] is executed. These two commands are executed
/// automatically by [execute_commands](crate::MinecraftConnection::execute_commands) if
/// [enable_logging_automatically](crate::MinecraftConnectionBuilder::enable_logging_automatically)
/// is `true` (which is the default).
///
/// This command sets the following three gamerules:
/// 1. `logAdminCommands`: This must be `true` for Minecraft to write the output of commands to the
/// log file.
/// 2. `commandBlockOutput`: This must be `true` for command blocks and command block minecarts to
/// broadcast the output of their commands.
/// 3. `sendCommandFeedback`: This is set to `false` to prevent the output to to also be written to
/// the chat which would likely annoy players.
///
/// This changes the logging configuration of the world in such a way that a player does not get any
/// output from any command (including commands the player executes). So the original values of the
/// gamerules are stored and can be restored by executing a [reset_logging_command].
///
/// After executing multiple [enable_logging_command]s, the same number of [reset_logging_command]s
/// has to be executed to reset logging.
/// Generates a Minecraft command that restores the logging gamerules to their values before the
/// last [enable_logging_command] was executed.
/// Generates two Minecraft commands that cause the given command to be executed from a command
/// block. This can be used to log the output of a command when running in a `mcfunction`.
///
/// The two commands are also available individually through [prepare_logged_block_command] and
/// [logged_block_command]. To work properly each [logged_block_command] has to be preceded by a
/// single [prepare_logged_block_command], otherwise it may overwrite a previous command or not be
/// executed at all.
///
/// There are two variants of this function that also define the name of the command block:
/// [named_logged_block_commands] and [json_named_logged_block_commands]. They can be used to allow
/// easy filtering of [LogEvent](crate::log::LogEvent)s with
/// [MinecraftConnection::add_named_listener](crate::MinecraftConnection::add_named_listener) or
/// [LogObserver::add_named_listener](crate::log::LogObserver::add_named_listener).
///
/// When the command block executes, the gamerules will be set appropriately for logging. So there
/// is no need to execute an [enable_logging_command] and a [reset_logging_command].
///
/// # Example
///
/// ```no_run
/// # use minect::*;
/// # use minect::command::*;
/// let mut commands = Vec::new();
/// commands.push("say querying scoreboard ...".to_string());
/// commands.extend(logged_block_commands(query_scoreboard_command("@p", "my_scoreboard")));
/// let my_function = commands.join("\n");
///
/// // Generate datapack containing my_function ...
///
/// // Call my_function (could also be done in Minecraft)
/// # let mut connection = MinecraftConnection::builder("", "").build();
/// connection.execute_commands([Command::new("function my_namespace:my_function")])?;
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// # Timing
///
/// The command block executes delayed, but it is guaranteed to execute within the same gametick as
/// the `mcfunction` in the following cases:
/// * The `mcfunction` is executed by a `function` command passed to
/// [execute_commands](crate::MinecraftConnection::execute_commands).
/// * The `mcfunction` is executed by a `function` command passed to [logged_block_commands].
/// * The `mcfunction` is executed by a `schedule` command.
///
/// Otherwise the command block may execute in the next game tick. Examples include, but are not
/// limited to:
/// * The `mcfunction` is executed by the function tag `#minecraft:tick`.
/// * The `mcfunction` is executed by a custom command block.
/// The same as [logged_block_commands], but also defines the name of the command block to allow
/// easy filtering of [LogEvent](crate::log::LogEvent)s with
/// [MinecraftConnection::add_named_listener](crate::MinecraftConnection::add_named_listener) or
/// [LogObserver::add_named_listener](crate::log::LogObserver::add_named_listener).
/// The same as [named_logged_block_commands], but the name of the command block is given as a JSON
/// text component.
/// Generates a Minecraft command that prepares the next [logged_block_command],
/// [named_logged_block_command] or [json_named_logged_block_command].
const EXECUTE_AT_CURSOR: &str = "execute at @e[type=area_effect_cloud,tag=minect_cursor] run";
/// See [logged_block_commands]. Must be preceded by a [prepare_logged_block_command].
/// See [named_logged_block_commands]. Must be preceded by a [prepare_logged_block_command].
/// See [json_named_logged_block_commands]. Must be preceded by a [prepare_logged_block_command].
/// Generates a Minecraft command that causes the given command to be executed from a command block
/// minecart. This can be used to log the output of a command when running in a `mcfunction`.
///
/// There are two variants of this function that also define the name of the command block:
/// [named_logged_cart_command] and [json_named_logged_cart_command]. They can be used to allow easy
/// filtering of [LogEvent](crate::log::LogEvent)s with
/// [MinecraftConnection::add_named_listener](crate::MinecraftConnection::add_named_listener) or
/// [LogObserver::add_named_listener](crate::log::LogObserver::add_named_listener).
///
/// To ensure [LogEvent](crate::log::LogEvent)s are created, the first logged command should be an
/// [enable_logging_command] and the last one should be a [reset_logging_command]:
/// ```no_run
/// # use minect::*;
/// # use minect::command::*;
/// let my_function = [
/// logged_cart_command(enable_logging_command()),
/// logged_cart_command(query_scoreboard_command("@p", "my_scoreboard")),
/// logged_cart_command(reset_logging_command()),
/// ].join("\n");
///
/// // Generate datapack containing my_function ...
///
/// // Call my_function (could also be done in Minecraft)
/// # let mut connection = MinecraftConnection::builder("", "").build();
/// connection.execute_commands([Command::new("function my_namespace:my_function")])?;
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// # Timing
///
/// Command block minecarts always execute with a 4 tick delay, so it is generally better to use
/// [logged_block_commands].
/// The same as [logged_cart_command], but also defines the name of the command block minecart to
/// allow easy filtering of [LogEvent](crate::log::LogEvent)s with
/// [MinecraftConnection::add_named_listener](crate::MinecraftConnection::add_named_listener) or
/// [LogObserver::add_named_listener](crate::log::LogObserver::add_named_listener).
/// The same as [named_logged_cart_command], but the name of the command block minecart is given as
/// a JSON text component.
/// Generates a Minecraft command that summons an area effect cloud with the given `name`.
///
/// The resulting [LogEvent::output](crate::log::LogEvent::output) can be parsed into a
/// [SummonNamedEntityOutput].
///
/// `name` is interpreted as a string, not a JSON text component.
///
/// By using a unique `name` this command can be used inside an `execute if` command to check if
/// some condition is true in Minecraft. A good way to generate a unique `name` is to use a UUID.
///
/// When using [logged_cart_command]s, [add_tag_command] is usually a better alternative in terms of
/// performance, because it avoids the overhead of summoning a new entity.
/// The output of a [summon_named_entity_command]. This can be parsed from a
/// [LogEvent::output](crate::log::LogEvent::output).
///
/// The output has the following format:
/// ```none
/// Summoned new <name>
/// ```
///
/// For example:
/// ```none
/// Summoned new my_name
/// ```
/// Generates a Minecraft command that adds the given `tag` to the given `entity`.
///
/// The resulting [LogEvent::output](crate::log::LogEvent::output) can be parsed into an
/// [AddTagOutput].
///
/// `entity` can be any selector or name.
///
/// For a [logged_cart_command] that only uses this tag as a means to know when/if the command is
/// executed (for example inside an `execute if` command) it can be useful to add a tag to the `@s`
/// entity. This saves the trouble of removing the tag again, because the command block minecart is
/// killed after the command is executed. Otherwise the tag will likely need to be removed, because
/// adding a tag twice to the same entity fails, thus preventing further
/// [LogEvent](crate::log::LogEvent)s.
/// The output of an [add_tag_command]. This can be parsed from a
/// [LogEvent::output](crate::log::LogEvent::output).
///
/// The output has the following format:
/// ```none
/// Added tag '<tag>' to <entity>
/// ```
///
/// For example:
/// ```none
/// Added tag 'my_tag' to my_entity
/// ```
/// Generates a Minecraft command that queries the score of `entity` in `scoreboard`.
///
/// The resulting [LogEvent::output](crate::log::LogEvent::output) can be parsed into a
/// [QueryScoreboardOutput].
///
/// `entity` can be any selector or name.
/// The output of a [query_scoreboard_command]. This can be parsed from
/// [LogEvent::output](crate::log::LogEvent::output).
///
/// The output has the following format:
/// ```none
/// Added 0 to [<scoreboard>] for <entity> (now <score>)
/// ```
///
/// For example:
/// ```none
/// Added 0 to [my_scoreboard] for my_entity (now 42)
/// ```