StoredUserList .java

net.minecraft.server.players.StoredUserList (class): members: notificationService, getKeyForUser, getUserList, getFile, createEntry, getEntries, file, map.

net.minecraft.server.playersclass160 linesserverextends StoredUserEntry<K>>4.0 KB

API Surface (12)

File getFile()
boolean add(final V infos)
boolean remove(final K user)
boolean remove(final StoredUserEntry<K> infos)
void clear()
String[] getUserList()
boolean isEmpty()
String getKeyForUser(final K user)
boolean contains(final K user)
Collection<V> getEntries()
void save()
void load()

Fields & Constants (5)

LOGGER
GSON
file
map
notificationService

String literals (3)

"Could not save the list after adding a user."
"Could not save the list after removing a user."
"entry"

Code Graph: Dependencies (4)

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

Full Source

StoredUserList.java160 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.players;
2
3
import com.google.common.collect.Lists;
4
import com.google.common.collect.Maps;
5
import com.google.common.io.Files;
6
import com.google.gson.Gson;
7
import com.google.gson.GsonBuilder;
8
import com.google.gson.JsonArray;
9
import com.google.gson.JsonElement;
10
import com.google.gson.JsonObject;
11
import com.mojang.logging.LogUtils;
12
import java.io.BufferedReader;
13
import java.io.BufferedWriter;
14
import java.io.File;
15
import java.io.IOException;
16
import java.nio.charset.StandardCharsets;
17
import java.util.Collection;
18
import java.util.List;
19
import java.util.Map;
20
import java.util.Objects;
21
import net.minecraft.server.notifications.NotificationService;
22
import net.minecraft.util.GsonHelper;
23
import net.minecraft.util.Util;
24
import org.jspecify.annotations.Nullable;
25
import org.slf4j.Logger;
26
27
public abstract class StoredUserList<K, V extends StoredUserEntry<K>> {
28
	private static final Logger LOGGER = LogUtils.getLogger();
29
	private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
30
	private final File file;
31
	private final Map<String, V> map = Maps.newHashMap();
32
	protected final NotificationService notificationService;
33
34
	public StoredUserList(final File file, final NotificationService notificationService) {
35
		this.file = file;
36
		this.notificationService = notificationService;
37
	}
38
39
	public File getFile() {
40
		return this.file;
41
	}
42
43
	public boolean add(final V infos) {
44
		String keyForUser = this.getKeyForUser(infos.getUser());
45
		V previous = this.map.get(keyForUser);
46
		if (infos.equals(previous)) {
47
			return false;
48
		}
49
50
		this.map.put(keyForUser, infos);
51
52
		try {
53
			this.save();
54
		} catch (IOException e) {
55
			LOGGER.warn("Could not save the list after adding a user.", e);
56
		}
57
58
		return true;
59
	}
60
61
	public @Nullable V get(final K user) {
62
		this.removeExpired();
63
		return this.map.get(this.getKeyForUser(user));
64
	}
65
66
	public boolean remove(final K user) {
67
		V removed = this.map.remove(this.getKeyForUser(user));
68
		if (removed == null) {
69
			return false;
70
		}
71
72
		try {
73
			this.save();
74
		} catch (IOException e) {
75
			LOGGER.warn("Could not save the list after removing a user.", e);
76
		}
77
78
		return true;
79
	}
80
81
	public boolean remove(final StoredUserEntry<K> infos) {
82
		return this.remove(Objects.requireNonNull(infos.getUser()));
83
	}
84
85
	public void clear() {
86
		this.map.clear();
87
88
		try {
89
			this.save();
90
		} catch (IOException e) {
91
			LOGGER.warn("Could not save the list after removing a user.", e);
92
		}
93
	}
94
95
	public String[] getUserList() {
96
		return this.map.keySet().toArray(new String[0]);
97
	}
98
99
	public boolean isEmpty() {
100
		return this.map.isEmpty();
101
	}
102
103
	protected String getKeyForUser(final K user) {
104
		return user.toString();
105
	}
106
107
	protected boolean contains(final K user) {
108
		return this.map.containsKey(this.getKeyForUser(user));
109
	}
110
111
	private void removeExpired() {
112
		List<K> toRemove = Lists.newArrayList();
113
114
		for (V entry : this.map.values()) {
115
			if (entry.hasExpired()) {
116
				toRemove.add(entry.getUser());
117
			}
118
		}
119
120
		for (K user : toRemove) {
121
			this.map.remove(this.getKeyForUser(user));
122
		}
123
	}
124
125
	protected abstract StoredUserEntry<K> createEntry(final JsonObject object);
126
127
	public Collection<V> getEntries() {
128
		return this.map.values();
129
	}
130
131
	public void save() throws IOException {
132
		JsonArray result = new JsonArray();
133
		this.map.values().stream().map(entry -> Util.make(new JsonObject(), entry::serialize)).forEach(result::add);
134
135
		try (BufferedWriter writer = Files.newWriter(this.file, StandardCharsets.UTF_8)) {
136
			GSON.toJson(result, GSON.newJsonWriter(writer));
137
		}
138
	}
139
140
	public void load() throws IOException {
141
		if (this.file.exists()) {
142
			try (BufferedReader reader = Files.newReader(this.file, StandardCharsets.UTF_8)) {
143
				this.map.clear();
144
				JsonArray contents = GSON.fromJson(reader, JsonArray.class);
145
				if (contents == null) {
146
					return;
147
				}
148
149
				for (JsonElement element : contents) {
150
					JsonObject object = GsonHelper.convertToJsonObject(element, "entry");
151
					StoredUserEntry<K> entry = this.createEntry(object);
152
					if (entry.getUser() != null) {
153
						this.map.put(this.getKeyForUser(entry.getUser()), (V)entry);
154
					}
155
				}
156
			}
157
		}
158
	}
159
}
160

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