| 1 | package net.minecraft.server.chase;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.io.BufferedReader;
|
| 5 | import java.io.IOException;
|
| 6 | import java.io.InputStreamReader;
|
| 7 | import java.io.StringReader;
|
| 8 | import java.net.Socket;
|
| 9 | import java.nio.charset.StandardCharsets;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Locale;
|
| 12 | import java.util.NoSuchElementException;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.Scanner;
|
| 15 | import net.minecraft.commands.CommandSourceStack;
|
| 16 | import net.minecraft.commands.Commands;
|
| 17 | import net.minecraft.resources.ResourceKey;
|
| 18 | import net.minecraft.server.MinecraftServer;
|
| 19 | import net.minecraft.server.commands.ChaseCommand;
|
| 20 | import net.minecraft.server.level.ServerLevel;
|
| 21 | import net.minecraft.server.level.ServerPlayer;
|
| 22 | import net.minecraft.server.permissions.LevelBasedPermissionSet;
|
| 23 | import net.minecraft.world.level.Level;
|
| 24 | import net.minecraft.world.phys.Vec2;
|
| 25 | import net.minecraft.world.phys.Vec3;
|
| 26 | import org.apache.commons.io.IOUtils;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 | import org.slf4j.Logger;
|
| 29 |
|
| 30 | public class ChaseClient {
|
| 31 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 32 | private static final int RECONNECT_INTERVAL_SECONDS = 5;
|
| 33 | private final String serverHost;
|
| 34 | private final int serverPort;
|
| 35 | private final MinecraftServer server;
|
| 36 | private volatile boolean wantsToRun;
|
| 37 | private @Nullable Socket socket;
|
| 38 | private @Nullable Thread thread;
|
| 39 |
|
| 40 | public ChaseClient(final String serverHost, final int serverPort, final MinecraftServer server) {
|
| 41 | this.serverHost = serverHost;
|
| 42 | this.serverPort = serverPort;
|
| 43 | this.server = server;
|
| 44 | }
|
| 45 |
|
| 46 | public void start() {
|
| 47 | if (this.thread != null && this.thread.isAlive()) {
|
| 48 | LOGGER.warn("Remote control client was asked to start, but it is already running. Will ignore.");
|
| 49 | }
|
| 50 |
|
| 51 | this.wantsToRun = true;
|
| 52 | this.thread = new Thread(this::run, "chase-client");
|
| 53 | this.thread.setDaemon(true);
|
| 54 | this.thread.start();
|
| 55 | }
|
| 56 |
|
| 57 | public void stop() {
|
| 58 | this.wantsToRun = false;
|
| 59 | IOUtils.closeQuietly(this.socket);
|
| 60 | this.socket = null;
|
| 61 | this.thread = null;
|
| 62 | }
|
| 63 |
|
| 64 | public void run() {
|
| 65 | String serverAddress = this.serverHost + ":" + this.serverPort;
|
| 66 |
|
| 67 | while (this.wantsToRun) {
|
| 68 | try {
|
| 69 | LOGGER.info("Connecting to remote control server {}", serverAddress);
|
| 70 | this.socket = new Socket(this.serverHost, this.serverPort);
|
| 71 | LOGGER.info("Connected to remote control server! Will continuously execute the command broadcasted by that server.");
|
| 72 |
|
| 73 | try (BufferedReader input = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), StandardCharsets.US_ASCII))) {
|
| 74 | while (this.wantsToRun) {
|
| 75 | String message = input.readLine();
|
| 76 | if (message == null) {
|
| 77 | LOGGER.warn("Lost connection to remote control server {}. Will retry in {}s.", serverAddress, 5);
|
| 78 | break;
|
| 79 | }
|
| 80 |
|
| 81 | this.handleMessage(message);
|
| 82 | }
|
| 83 | } catch (IOException err) {
|
| 84 | LOGGER.warn("Lost connection to remote control server {}. Will retry in {}s.", serverAddress, 5);
|
| 85 | }
|
| 86 | } catch (IOException e) {
|
| 87 | LOGGER.warn("Failed to connect to remote control server {}. Will retry in {}s.", serverAddress, 5);
|
| 88 | }
|
| 89 |
|
| 90 | if (this.wantsToRun) {
|
| 91 | try {
|
| 92 | Thread.sleep(5000L);
|
| 93 | } catch (InterruptedException var5) {
|
| 94 | }
|
| 95 | }
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | private void handleMessage(final String message) {
|
| 100 | try (Scanner scanner = new Scanner(new StringReader(message))) {
|
| 101 | scanner.useLocale(Locale.ROOT);
|
| 102 | String head = scanner.next();
|
| 103 | if ("t".equals(head)) {
|
| 104 | this.handleTeleport(scanner);
|
| 105 | } else {
|
| 106 | LOGGER.warn("Unknown message type '{}'", head);
|
| 107 | }
|
| 108 | } catch (NoSuchElementException e) {
|
| 109 | LOGGER.warn("Could not parse message '{}', ignoring", message);
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | private void handleTeleport(final Scanner scanner) {
|
| 114 | parseTarget(scanner)
|
| 115 | .ifPresent(
|
| 116 | target -> this.executeCommand(
|
| 117 | String.format(
|
| 118 | Locale.ROOT,
|
| 119 | "execute in %s run tp @s %.3f %.3f %.3f %.3f %.3f",
|
| 120 | target.level.identifier(),
|
| 121 | target.pos.x,
|
| 122 | target.pos.y,
|
| 123 | target.pos.z,
|
| 124 | target.rot.y,
|
| 125 | target.rot.x
|
| 126 | )
|
| 127 | )
|
| 128 | );
|
| 129 | }
|
| 130 |
|
| 131 | private static Optional<ChaseClient.TeleportTarget> parseTarget(final Scanner scanner) {
|
| 132 | ResourceKey<Level> levelType = ChaseCommand.DIMENSION_NAMES.get(scanner.next());
|
| 133 | if (levelType == null) {
|
| 134 | return Optional.empty();
|
| 135 | }
|
| 136 |
|
| 137 | float x = scanner.nextFloat();
|
| 138 | float y = scanner.nextFloat();
|
| 139 | float z = scanner.nextFloat();
|
| 140 | float yRot = scanner.nextFloat();
|
| 141 | float xRot = scanner.nextFloat();
|
| 142 | return Optional.of(new ChaseClient.TeleportTarget(levelType, new Vec3(x, y, z), new Vec2(xRot, yRot)));
|
| 143 | }
|
| 144 |
|
| 145 | private void executeCommand(final String command) {
|
| 146 | this.server
|
| 147 | .execute(
|
| 148 | () -> {
|
| 149 | List<ServerPlayer> players = this.server.getPlayerList().getPlayers();
|
| 150 | if (!players.isEmpty()) {
|
| 151 | ServerPlayer player = players.get(0);
|
| 152 | ServerLevel level = this.server.overworld();
|
| 153 | CommandSourceStack commandSourceStack = new CommandSourceStack(
|
| 154 | player.commandSource(), Vec3.atLowerCornerOf(level.getRespawnData().pos()), Vec2.ZERO, level, LevelBasedPermissionSet.OWNER, this.server, player
|
| 155 | );
|
| 156 | Commands commands = this.server.getCommands();
|
| 157 | commands.performPrefixedCommand(commandSourceStack, command);
|
| 158 | }
|
| 159 | }
|
| 160 | );
|
| 161 | }
|
| 162 |
|
| 163 | private record TeleportTarget(ResourceKey<Level> level, Vec3 pos, Vec2 rot) {
|
| 164 | }
|
| 165 | }
|
| 166 |
|