ChaseServer .java

net.minecraft.server.chase.ChaseServer (class): members: broadcastIntervalMs, serverBindAddress, PlayerPosition, clientSockets, serverSocket, wantsToRun.

net.minecraft.server.chaseclass145 linesserver4.9 KB

API Surface (2)

void start()
void stop()

Fields & Constants (8)

LOGGER
serverBindAddress
serverPort
playerList
broadcastIntervalMs
wantsToRun
serverSocket
clientSockets

String literals (4)

"chase-server-acceptor"
"chase-server-sender"
"Remote control server closed by interrupt"
"Remote control server is now stopped"

Same Package (2)

Code Graph: Dependencies (4)

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

Full Source

ChaseServer.java145 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.chase;
2
3
import com.mojang.logging.LogUtils;
4
import java.io.IOException;
5
import java.io.OutputStream;
6
import java.net.InetAddress;
7
import java.net.ServerSocket;
8
import java.net.Socket;
9
import java.nio.channels.ClosedByInterruptException;
10
import java.nio.charset.StandardCharsets;
11
import java.util.List;
12
import java.util.Locale;
13
import java.util.concurrent.CopyOnWriteArrayList;
14
import java.util.stream.Collectors;
15
import net.minecraft.server.commands.ChaseCommand;
16
import net.minecraft.server.level.ServerPlayer;
17
import net.minecraft.server.players.PlayerList;
18
import net.minecraft.util.Util;
19
import org.apache.commons.io.IOUtils;
20
import org.jspecify.annotations.Nullable;
21
import org.slf4j.Logger;
22
23
public class ChaseServer {
24
	private static final Logger LOGGER = LogUtils.getLogger();
25
	private final String serverBindAddress;
26
	private final int serverPort;
27
	private final PlayerList playerList;
28
	private final int broadcastIntervalMs;
29
	private volatile boolean wantsToRun;
30
	private @Nullable ServerSocket serverSocket;
31
	private final CopyOnWriteArrayList<Socket> clientSockets = new CopyOnWriteArrayList<>();
32
33
	public ChaseServer(final String serverBindAddress, final int serverPort, final PlayerList playerList, final int broadcastIntervalMs) {
34
		this.serverBindAddress = serverBindAddress;
35
		this.serverPort = serverPort;
36
		this.playerList = playerList;
37
		this.broadcastIntervalMs = broadcastIntervalMs;
38
	}
39
40
	public void start() throws IOException {
41
		if (this.serverSocket != null && !this.serverSocket.isClosed()) {
42
			LOGGER.warn("Remote control server was asked to start, but it is already running. Will ignore.");
43
		} else {
44
			this.wantsToRun = true;
45
			this.serverSocket = new ServerSocket(this.serverPort, 50, InetAddress.getByName(this.serverBindAddress));
46
			Thread acceptor = new Thread(this::runAcceptor, "chase-server-acceptor");
47
			acceptor.setDaemon(true);
48
			acceptor.start();
49
			Thread sender = new Thread(this::runSender, "chase-server-sender");
50
			sender.setDaemon(true);
51
			sender.start();
52
		}
53
	}
54
55
	private void runSender() {
56
		ChaseServer.PlayerPosition oldPlayerPosition = null;
57
58
		while (this.wantsToRun) {
59
			if (!this.clientSockets.isEmpty()) {
60
				ChaseServer.PlayerPosition playerPosition = this.getPlayerPosition();
61
				if (playerPosition != null && !playerPosition.equals(oldPlayerPosition)) {
62
					oldPlayerPosition = playerPosition;
63
					byte[] messageBytes = playerPosition.format().getBytes(StandardCharsets.US_ASCII);
64
65
					for (Socket clientSocket : this.clientSockets) {
66
						if (!clientSocket.isClosed()) {
67
							Util.ioPool().execute(() -> {
68
								try {
69
									OutputStream output = clientSocket.getOutputStream();
70
									output.write(messageBytes);
71
									output.flush();
72
								} catch (IOException e) {
73
									LOGGER.info("Remote control client socket got an IO exception and will be closed", e);
74
									IOUtils.closeQuietly(clientSocket);
75
								}
76
							});
77
						}
78
					}
79
				}
80
81
				List<Socket> closed = this.clientSockets.stream().filter(Socket::isClosed).collect(Collectors.toList());
82
				this.clientSockets.removeAll(closed);
83
			}
84
85
			if (this.wantsToRun) {
86
				try {
87
					Thread.sleep(this.broadcastIntervalMs);
88
				} catch (InterruptedException var6) {
89
				}
90
			}
91
		}
92
	}
93
94
	public void stop() {
95
		this.wantsToRun = false;
96
		IOUtils.closeQuietly(this.serverSocket);
97
		this.serverSocket = null;
98
	}
99
100
	private void runAcceptor() {
101
		try {
102
			while (this.wantsToRun) {
103
				if (this.serverSocket != null) {
104
					LOGGER.info("Remote control server is listening for connections on port {}", this.serverPort);
105
					Socket clientSocket = this.serverSocket.accept();
106
					LOGGER.info("Remote control server received client connection on port {}", clientSocket.getPort());
107
					this.clientSockets.add(clientSocket);
108
				}
109
			}
110
		} catch (ClosedByInterruptException e) {
111
			if (this.wantsToRun) {
112
				LOGGER.info("Remote control server closed by interrupt");
113
			}
114
		} catch (IOException e) {
115
			if (this.wantsToRun) {
116
				LOGGER.error("Remote control server closed because of an IO exception", e);
117
			}
118
		} finally {
119
			IOUtils.closeQuietly(this.serverSocket);
120
		}
121
122
		LOGGER.info("Remote control server is now stopped");
123
		this.wantsToRun = false;
124
	}
125
126
	private ChaseServer.@Nullable PlayerPosition getPlayerPosition() {
127
		List<ServerPlayer> players = this.playerList.getPlayers();
128
		if (players.isEmpty()) {
129
			return null;
130
		}
131
132
		ServerPlayer player = players.get(0);
133
		String dimensionName = ChaseCommand.DIMENSION_NAMES.inverse().get(player.level().dimension());
134
		return dimensionName == null
135
			? null
136
			: new ChaseServer.PlayerPosition(dimensionName, player.getX(), player.getY(), player.getZ(), player.getYRot(), player.getXRot());
137
	}
138
139
	private record PlayerPosition(String dimensionName, double x, double y, double z, float yRot, float xRot) {
140
		private String format() {
141
			return String.format(Locale.ROOT, "t %s %.2f %.2f %.2f %.2f %.2f\n", this.dimensionName, this.x, this.y, this.z, this.yRot, this.xRot);
142
		}
143
	}
144
}
145

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