ChaseCommand.java
net.minecraft.server.commands.ChaseCommand
信息
- 全限定名:net.minecraft.server.commands.ChaseCommand
- 类型:public class
- 包:net.minecraft.server.commands
- 源码路径:src/main/java/net/minecraft/server/commands/ChaseCommand.java
- 起始行号:L20
- 职责:
TODO
字段/常量
-
LOGGER- 类型:
Logger - 修饰符:
private static final - 源码定位:
L21 - 说明:
TODO
- 类型:
-
DEFAULT_CONNECT_HOST- 类型:
String - 修饰符:
private static final - 源码定位:
L22 - 说明:
TODO
- 类型:
-
DEFAULT_BIND_ADDRESS- 类型:
String - 修饰符:
private static final - 源码定位:
L23 - 说明:
TODO
- 类型:
-
DEFAULT_PORT- 类型:
int - 修饰符:
private static final - 源码定位:
L24 - 说明:
TODO
- 类型:
-
BROADCAST_INTERVAL_MS- 类型:
int - 修饰符:
private static final - 源码定位:
L25 - 说明:
TODO
- 类型:
-
DIMENSION_NAMES- 类型:
BiMap<String,ResourceKey<Level>> - 修饰符:
public static final - 源码定位:
L26 - 说明:
TODO
- 类型:
-
chaseServer- 类型:
ChaseServer - 修饰符:
private static - 源码定位:
L27 - 说明:
TODO
- 类型:
-
chaseClient- 类型:
ChaseClient - 修饰符:
private static - 源码定位:
L28 - 说明:
TODO
- 类型:
内部类/嵌套类型
- 无
构造器
- 无
方法
下面的方法块按源码顺序生成。
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) @ L30
- 方法名:register
- 源码定位:L30
- 返回类型:void
- 修饰符:public static
参数:
- dispatcher: CommandDispatcher
说明:
TODO
private static int stop(CommandSourceStack source) @ L65
- 方法名:stop
- 源码定位:L65
- 返回类型:int
- 修饰符:private static
参数:
- source: CommandSourceStack
说明:
TODO
private static boolean alreadyRunning(CommandSourceStack source) @ L81
- 方法名:alreadyRunning
- 源码定位:L81
- 返回类型:boolean
- 修饰符:private static
参数:
- source: CommandSourceStack
说明:
TODO
private static int lead(CommandSourceStack source, String serverBindAddress, int port) @ L93
- 方法名:lead
- 源码定位:L93
- 返回类型:int
- 修饰符:private static
参数:
- source: CommandSourceStack
- serverBindAddress: String
- port: int
说明:
TODO
private static int follow(CommandSourceStack source, String host, int port) @ L114
- 方法名:follow
- 源码定位:L114
- 返回类型:int
- 修饰符:private static
参数:
- source: CommandSourceStack
- host: String
- port: int
说明:
TODO
代码
public class ChaseCommand {
private static final Logger LOGGER = LogUtils.getLogger();
private static final String DEFAULT_CONNECT_HOST = "localhost";
private static final String DEFAULT_BIND_ADDRESS = "0.0.0.0";
private static final int DEFAULT_PORT = 10000;
private static final int BROADCAST_INTERVAL_MS = 100;
public static final BiMap<String, ResourceKey<Level>> DIMENSION_NAMES = ImmutableBiMap.of("o", Level.OVERWORLD, "n", Level.NETHER, "e", Level.END);
private static @Nullable ChaseServer chaseServer;
private static @Nullable ChaseClient chaseClient;
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(
Commands.literal("chase")
.then(
Commands.literal("follow")
.then(
Commands.argument("host", StringArgumentType.string())
.executes(c -> follow(c.getSource(), StringArgumentType.getString(c, "host"), 10000))
.then(
Commands.argument("port", IntegerArgumentType.integer(1, 65535))
.executes(
c -> follow(c.getSource(), StringArgumentType.getString(c, "host"), IntegerArgumentType.getInteger(c, "port"))
)
)
)
.executes(c -> follow(c.getSource(), "localhost", 10000))
)
.then(
Commands.literal("lead")
.then(
Commands.argument("bind_address", StringArgumentType.string())
.executes(c -> lead(c.getSource(), StringArgumentType.getString(c, "bind_address"), 10000))
.then(
Commands.argument("port", IntegerArgumentType.integer(1024, 65535))
.executes(
c -> lead(c.getSource(), StringArgumentType.getString(c, "bind_address"), IntegerArgumentType.getInteger(c, "port"))
)
)
)
.executes(c -> lead(c.getSource(), "0.0.0.0", 10000))
)
.then(Commands.literal("stop").executes(c -> stop(c.getSource())))
);
}
private static int stop(CommandSourceStack source) {
if (chaseClient != null) {
chaseClient.stop();
source.sendSuccess(() -> Component.literal("You have now stopped chasing"), false);
chaseClient = null;
}
if (chaseServer != null) {
chaseServer.stop();
source.sendSuccess(() -> Component.literal("You are no longer being chased"), false);
chaseServer = null;
}
return 0;
}
private static boolean alreadyRunning(CommandSourceStack source) {
if (chaseServer != null) {
source.sendFailure(Component.literal("Chase server is already running. Stop it using /chase stop"));
return true;
} else if (chaseClient != null) {
source.sendFailure(Component.literal("You are already chasing someone. Stop it using /chase stop"));
return true;
} else {
return false;
}
}
private static int lead(CommandSourceStack source, String serverBindAddress, int port) {
if (alreadyRunning(source)) {
return 0;
} else {
chaseServer = new ChaseServer(serverBindAddress, port, source.getServer().getPlayerList(), 100);
try {
chaseServer.start();
source.sendSuccess(
() -> Component.literal("Chase server is now running on port " + port + ". Clients can follow you using /chase follow <ip> <port>"), false
);
} catch (IOException var4) {
LOGGER.error("Failed to start chase server", (Throwable)var4);
source.sendFailure(Component.literal("Failed to start chase server on port " + port));
chaseServer = null;
}
return 0;
}
}
private static int follow(CommandSourceStack source, String host, int port) {
if (alreadyRunning(source)) {
return 0;
} else {
chaseClient = new ChaseClient(host, port, source.getServer());
chaseClient.start();
source.sendSuccess(
() -> Component.literal(
"You are now chasing "
+ host
+ ":"
+ port
+ ". If that server does '/chase lead' then you will automatically go to the same position. Use '/chase stop' to stop chasing."
),
false
);
return 0;
}
}
}引用的其他类
-
- 引用位置:
参数
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
Commands.argument(), Commands.literal()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
Component.literal()
- 引用位置:
-
- 引用位置:
字段
- 引用位置:
-
- 引用位置:
字段/构造调用 - 关联成员:
ChaseClient()
- 引用位置:
-
- 引用位置:
字段/构造调用 - 关联成员:
ChaseServer()
- 引用位置:
-
- 引用位置:
字段
- 引用位置: