| 1 | package net.minecraft.client.multiplayer;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import java.nio.file.Files;
|
| 6 | import java.nio.file.Path;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Objects;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.client.Minecraft;
|
| 12 | import net.minecraft.nbt.CompoundTag;
|
| 13 | import net.minecraft.nbt.ListTag;
|
| 14 | import net.minecraft.nbt.NbtIo;
|
| 15 | import net.minecraft.util.Util;
|
| 16 | import net.minecraft.util.thread.ConsecutiveExecutor;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 | import org.slf4j.Logger;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class ServerList {
|
| 22 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 23 | private static final ConsecutiveExecutor IO_EXECUTOR = new ConsecutiveExecutor(Util.backgroundExecutor(), "server-list-io");
|
| 24 | private static final int MAX_HIDDEN_SERVERS = 16;
|
| 25 | private final Minecraft minecraft;
|
| 26 | private final List<ServerData> serverList = Lists.newArrayList();
|
| 27 | private final List<ServerData> hiddenServerList = Lists.newArrayList();
|
| 28 |
|
| 29 | public ServerList(final Minecraft minecraft) {
|
| 30 | this.minecraft = minecraft;
|
| 31 | }
|
| 32 |
|
| 33 | public void load() {
|
| 34 | try {
|
| 35 | this.serverList.clear();
|
| 36 | this.hiddenServerList.clear();
|
| 37 | CompoundTag tag = NbtIo.read(this.minecraft.gameDirectory.toPath().resolve("servers.dat"));
|
| 38 | if (tag == null) {
|
| 39 | return;
|
| 40 | }
|
| 41 |
|
| 42 | tag.getListOrEmpty("servers").compoundStream().forEach(serverTag -> {
|
| 43 | ServerData serverData = ServerData.read(serverTag);
|
| 44 | if (serverTag.getBooleanOr("hidden", false)) {
|
| 45 | this.hiddenServerList.add(serverData);
|
| 46 | } else {
|
| 47 | this.serverList.add(serverData);
|
| 48 | }
|
| 49 | });
|
| 50 | } catch (Exception e) {
|
| 51 | LOGGER.error("Couldn't load server list", e);
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | public void save() {
|
| 56 | try {
|
| 57 | ListTag serverTags = new ListTag();
|
| 58 |
|
| 59 | for (ServerData server : this.serverList) {
|
| 60 | CompoundTag serverTag = server.write();
|
| 61 | serverTag.putBoolean("hidden", false);
|
| 62 | serverTags.add(serverTag);
|
| 63 | }
|
| 64 |
|
| 65 | for (ServerData server : this.hiddenServerList) {
|
| 66 | CompoundTag serverTag = server.write();
|
| 67 | serverTag.putBoolean("hidden", true);
|
| 68 | serverTags.add(serverTag);
|
| 69 | }
|
| 70 |
|
| 71 | CompoundTag tag = new CompoundTag();
|
| 72 | tag.put("servers", serverTags);
|
| 73 | Path gameDirectoryPath = this.minecraft.gameDirectory.toPath();
|
| 74 | Path newFile = Files.createTempFile(gameDirectoryPath, "servers", ".dat");
|
| 75 | NbtIo.write(tag, newFile);
|
| 76 | Path oldFile = gameDirectoryPath.resolve("servers.dat_old");
|
| 77 | Path currentFile = gameDirectoryPath.resolve("servers.dat");
|
| 78 | Util.safeReplaceFile(currentFile, newFile, oldFile);
|
| 79 | } catch (Exception e) {
|
| 80 | LOGGER.error("Couldn't save server list", e);
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | public ServerData get(final int index) {
|
| 85 | return this.serverList.get(index);
|
| 86 | }
|
| 87 |
|
| 88 | public @Nullable ServerData get(final String ip) {
|
| 89 | for (ServerData serverData : this.serverList) {
|
| 90 | if (serverData.ip.equals(ip)) {
|
| 91 | return serverData;
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | for (ServerData serverData : this.hiddenServerList) {
|
| 96 | if (serverData.ip.equals(ip)) {
|
| 97 | return serverData;
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | return null;
|
| 102 | }
|
| 103 |
|
| 104 | public @Nullable ServerData unhide(final String ip) {
|
| 105 | for (int i = 0; i < this.hiddenServerList.size(); i++) {
|
| 106 | ServerData serverData = this.hiddenServerList.get(i);
|
| 107 | if (serverData.ip.equals(ip)) {
|
| 108 | this.hiddenServerList.remove(i);
|
| 109 | this.serverList.add(serverData);
|
| 110 | return serverData;
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | return null;
|
| 115 | }
|
| 116 |
|
| 117 | public void remove(final ServerData thing) {
|
| 118 | if (!this.serverList.remove(thing)) {
|
| 119 | this.hiddenServerList.remove(thing);
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | public void add(final ServerData server, final boolean hidden) {
|
| 124 | if (hidden) {
|
| 125 | this.hiddenServerList.add(0, server);
|
| 126 |
|
| 127 | while (this.hiddenServerList.size() > 16) {
|
| 128 | this.hiddenServerList.remove(this.hiddenServerList.size() - 1);
|
| 129 | }
|
| 130 | } else {
|
| 131 | this.serverList.add(server);
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | public int size() {
|
| 136 | return this.serverList.size();
|
| 137 | }
|
| 138 |
|
| 139 | public void swap(final int a, final int b) {
|
| 140 | ServerData swap = this.get(a);
|
| 141 | this.serverList.set(a, this.get(b));
|
| 142 | this.serverList.set(b, swap);
|
| 143 | this.save();
|
| 144 | }
|
| 145 |
|
| 146 | public void replace(final int id, final ServerData data) {
|
| 147 | this.serverList.set(id, data);
|
| 148 | }
|
| 149 |
|
| 150 | private static boolean set(final ServerData data, final List<ServerData> list) {
|
| 151 | for (int i = 0; i < list.size(); i++) {
|
| 152 | ServerData target = list.get(i);
|
| 153 | if (Objects.equals(target.name, data.name) && target.ip.equals(data.ip)) {
|
| 154 | list.set(i, data);
|
| 155 | return true;
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | return false;
|
| 160 | }
|
| 161 |
|
| 162 | public static void saveSingleServer(final ServerData data) {
|
| 163 | IO_EXECUTOR.schedule(() -> {
|
| 164 | ServerList list = new ServerList(Minecraft.getInstance());
|
| 165 | list.load();
|
| 166 | if (!set(data, list.serverList)) {
|
| 167 | set(data, list.hiddenServerList);
|
| 168 | }
|
| 169 |
|
| 170 | list.save();
|
| 171 | });
|
| 172 | }
|
| 173 | }
|
| 174 |
|