CommandHistory .java

net.minecraft.client.CommandHistory (class): members: MAX_PERSISTED_COMMAND_HISTORY, PERSISTED_COMMANDS_FILE_NAME, commandsPath, lastCommands, history.

net.minecraft.clientclass61 linesclient1.9 KB

API Surface (2)

void addCommand(final String command)
Collection<String> history()

Fields & Constants (5)

LOGGER
MAX_PERSISTED_COMMAND_HISTORY
PERSISTED_COMMANDS_FILE_NAME
commandsPath
lastCommands

String literals (1)

"command_history.txt"

Code Graph: Dependencies (2)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

CommandHistory.java61 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client;
2
3
import com.mojang.logging.LogUtils;
4
import java.io.BufferedReader;
5
import java.io.BufferedWriter;
6
import java.io.IOException;
7
import java.nio.charset.StandardCharsets;
8
import java.nio.file.Files;
9
import java.nio.file.Path;
10
import java.util.Collection;
11
import net.fabricmc.api.EnvType;
12
import net.fabricmc.api.Environment;
13
import net.minecraft.util.ArrayListDeque;
14
import org.slf4j.Logger;
15
16
@Environment(EnvType.CLIENT)
17
public class CommandHistory {
18
	private static final Logger LOGGER = LogUtils.getLogger();
19
	private static final int MAX_PERSISTED_COMMAND_HISTORY = 50;
20
	private static final String PERSISTED_COMMANDS_FILE_NAME = "command_history.txt";
21
	private final Path commandsPath;
22
	private final ArrayListDeque<String> lastCommands = new ArrayListDeque<>(50);
23
24
	public CommandHistory(final Path gameFolder) {
25
		this.commandsPath = gameFolder.resolve("command_history.txt");
26
		if (Files.exists(this.commandsPath)) {
27
			try (BufferedReader reader = Files.newBufferedReader(this.commandsPath, StandardCharsets.UTF_8)) {
28
				this.lastCommands.addAll(reader.lines().toList());
29
			} catch (Exception exception) {
30
				LOGGER.error("Failed to read {}, command history will be missing", "command_history.txt", exception);
31
			}
32
		}
33
	}
34
35
	public void addCommand(final String command) {
36
		if (!command.equals(this.lastCommands.peekLast())) {
37
			if (this.lastCommands.size() >= 50) {
38
				this.lastCommands.removeFirst();
39
			}
40
41
			this.lastCommands.addLast(command);
42
			this.save();
43
		}
44
	}
45
46
	private void save() {
47
		try (BufferedWriter writer = Files.newBufferedWriter(this.commandsPath, StandardCharsets.UTF_8)) {
48
			for (String command : this.lastCommands) {
49
				writer.write(command);
50
				writer.newLine();
51
			}
52
		} catch (IOException exception) {
53
			LOGGER.error("Failed to write {}, command history will be missing", "command_history.txt", exception);
54
		}
55
	}
56
57
	public Collection<String> history() {
58
		return this.lastCommands;
59
	}
60
}
61

Decompiled Minecraft 26.3 source of CommandHistory (net/minecraft/client/CommandHistory.java) with Mojang official mappings, stored verbatim on the site.