| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.google.common.collect.BiMap;
|
| 4 | import com.google.common.collect.ImmutableBiMap;
|
| 5 | import com.mojang.brigadier.CommandDispatcher;
|
| 6 | import com.mojang.brigadier.arguments.IntegerArgumentType;
|
| 7 | import com.mojang.brigadier.arguments.StringArgumentType;
|
| 8 | import com.mojang.logging.LogUtils;
|
| 9 | import java.io.IOException;
|
| 10 | import net.minecraft.commands.CommandSourceStack;
|
| 11 | import net.minecraft.commands.Commands;
|
| 12 | import net.minecraft.network.chat.Component;
|
| 13 | import net.minecraft.resources.ResourceKey;
|
| 14 | import net.minecraft.server.chase.ChaseClient;
|
| 15 | import net.minecraft.server.chase.ChaseServer;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 | import org.slf4j.Logger;
|
| 19 |
|
| 20 | public class ChaseCommand {
|
| 21 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 22 | private static final String DEFAULT_CONNECT_HOST = "localhost";
|
| 23 | private static final String DEFAULT_BIND_ADDRESS = "0.0.0.0";
|
| 24 | private static final int DEFAULT_PORT = 10000;
|
| 25 | private static final int BROADCAST_INTERVAL_MS = 100;
|
| 26 | public static final BiMap<String, ResourceKey<Level>> DIMENSION_NAMES = ImmutableBiMap.of("o", Level.OVERWORLD, "n", Level.NETHER, "e", Level.END);
|
| 27 | private static @Nullable ChaseServer chaseServer;
|
| 28 | private static @Nullable ChaseClient chaseClient;
|
| 29 |
|
| 30 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 31 | dispatcher.register(
|
| 32 | Commands.literal("chase")
|
| 33 | .then(
|
| 34 | Commands.literal("follow")
|
| 35 | .then(
|
| 36 | Commands.argument("host", StringArgumentType.string())
|
| 37 | .executes(c -> follow(c.getSource(), StringArgumentType.getString(c, "host"), 10000))
|
| 38 | .then(
|
| 39 | Commands.argument("port", IntegerArgumentType.integer(1, 65535))
|
| 40 | .executes(c -> follow(c.getSource(), StringArgumentType.getString(c, "host"), IntegerArgumentType.getInteger(c, "port")))
|
| 41 | )
|
| 42 | )
|
| 43 | .executes(c -> follow(c.getSource(), "localhost", 10000))
|
| 44 | )
|
| 45 | .then(
|
| 46 | Commands.literal("lead")
|
| 47 | .then(
|
| 48 | Commands.argument("bind_address", StringArgumentType.string())
|
| 49 | .executes(c -> lead(c.getSource(), StringArgumentType.getString(c, "bind_address"), 10000))
|
| 50 | .then(
|
| 51 | Commands.argument("port", IntegerArgumentType.integer(1024, 65535))
|
| 52 | .executes(c -> lead(c.getSource(), StringArgumentType.getString(c, "bind_address"), IntegerArgumentType.getInteger(c, "port")))
|
| 53 | )
|
| 54 | )
|
| 55 | .executes(c -> lead(c.getSource(), "0.0.0.0", 10000))
|
| 56 | )
|
| 57 | .then(Commands.literal("stop").executes(c -> stop(c.getSource())))
|
| 58 | );
|
| 59 | }
|
| 60 |
|
| 61 | private static int stop(final CommandSourceStack source) {
|
| 62 | if (chaseClient != null) {
|
| 63 | chaseClient.stop();
|
| 64 | source.sendSuccess(() -> Component.literal("You have now stopped chasing"), false);
|
| 65 | chaseClient = null;
|
| 66 | }
|
| 67 |
|
| 68 | if (chaseServer != null) {
|
| 69 | chaseServer.stop();
|
| 70 | source.sendSuccess(() -> Component.literal("You are no longer being chased"), false);
|
| 71 | chaseServer = null;
|
| 72 | }
|
| 73 |
|
| 74 | return 0;
|
| 75 | }
|
| 76 |
|
| 77 | private static boolean alreadyRunning(final CommandSourceStack source) {
|
| 78 | if (chaseServer != null) {
|
| 79 | source.sendFailure(Component.literal("Chase server is already running. Stop it using /chase stop"));
|
| 80 | return true;
|
| 81 | } else if (chaseClient != null) {
|
| 82 | source.sendFailure(Component.literal("You are already chasing someone. Stop it using /chase stop"));
|
| 83 | return true;
|
| 84 | } else {
|
| 85 | return false;
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | private static int lead(final CommandSourceStack source, final String serverBindAddress, final int port) {
|
| 90 | if (alreadyRunning(source)) {
|
| 91 | return 0;
|
| 92 | }
|
| 93 |
|
| 94 | chaseServer = new ChaseServer(serverBindAddress, port, source.getServer().getPlayerList(), 100);
|
| 95 |
|
| 96 | try {
|
| 97 | chaseServer.start();
|
| 98 | source.sendSuccess(
|
| 99 | () -> Component.literal("Chase server is now running on port " + port + ". Clients can follow you using /chase follow <ip> <port>"), false
|
| 100 | );
|
| 101 | } catch (IOException e) {
|
| 102 | LOGGER.error("Failed to start chase server", e);
|
| 103 | source.sendFailure(Component.literal("Failed to start chase server on port " + port));
|
| 104 | chaseServer = null;
|
| 105 | }
|
| 106 |
|
| 107 | return 0;
|
| 108 | }
|
| 109 |
|
| 110 | private static int follow(final CommandSourceStack source, final String host, final int port) {
|
| 111 | if (alreadyRunning(source)) {
|
| 112 | return 0;
|
| 113 | }
|
| 114 |
|
| 115 | chaseClient = new ChaseClient(host, port, source.getServer());
|
| 116 | chaseClient.start();
|
| 117 | source.sendSuccess(
|
| 118 | () -> Component.literal(
|
| 119 | "You are now chasing "
|
| 120 | + host
|
| 121 | + ":"
|
| 122 | + port
|
| 123 | + ". If that server does '/chase lead' then you will automatically go to the same position. Use '/chase stop' to stop chasing."
|
| 124 | ),
|
| 125 | false
|
| 126 | );
|
| 127 | return 0;
|
| 128 | }
|
| 129 | }
|
| 130 |
|