PublishCommand .java

net.minecraft.server.commands.PublishCommand (class): members: ERROR_ALREADY_PUBLISHED_LAN, getSuccessMessage, ERROR_FAILED, register. strings: "publish".

net.minecraft.server.commandsclass58 linesserver2.5 KB

API Surface (2)

void register(final CommandDispatcher<CommandSourceStack> dispatcher)
MutableComponent getSuccessMessage(final int port)

Fields & Constants (2)

ERROR_FAILED
ERROR_ALREADY_PUBLISHED_LAN

String literals (6)

"commands.publish.failed"
"commands.publish.alreadyPublished.lan"
"publish"
"allowCommands"
"port"
"commands.publish.started.lan"

Full Source

PublishCommand.java58 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.mojang.brigadier.CommandDispatcher;
4
import com.mojang.brigadier.arguments.BoolArgumentType;
5
import com.mojang.brigadier.arguments.IntegerArgumentType;
6
import com.mojang.brigadier.exceptions.CommandSyntaxException;
7
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
8
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
9
import net.minecraft.commands.CommandSourceStack;
10
import net.minecraft.commands.Commands;
11
import net.minecraft.network.chat.Component;
12
import net.minecraft.network.chat.ComponentUtils;
13
import net.minecraft.network.chat.MutableComponent;
14
import net.minecraft.server.MinecraftServer;
15
import net.minecraft.util.HttpUtil;
16
17
public class PublishCommand {
18
	private static final SimpleCommandExceptionType ERROR_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.publish.failed"));
19
	private static final DynamicCommandExceptionType ERROR_ALREADY_PUBLISHED_LAN = new DynamicCommandExceptionType(
20
		port -> Component.translatableEscape("commands.publish.alreadyPublished.lan", port)
21
	);
22
23
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
24
		dispatcher.register(
25
			Commands.literal("publish")
26
				.requires(Commands.hasPermission(Commands.LEVEL_OWNERS))
27
				.executes(c -> publish(c.getSource(), HttpUtil.getAvailablePort(), false))
28
				.then(
29
					Commands.argument("allowCommands", BoolArgumentType.bool())
30
						.executes(c -> publish(c.getSource(), HttpUtil.getAvailablePort(), BoolArgumentType.getBool(c, "allowCommands")))
31
						.then(
32
							Commands.argument("port", IntegerArgumentType.integer(0, 65535))
33
								.executes(c -> publish(c.getSource(), IntegerArgumentType.getInteger(c, "port"), BoolArgumentType.getBool(c, "allowCommands")))
34
						)
35
				)
36
		);
37
	}
38
39
	private static int publish(final CommandSourceStack source, final int port, final boolean allowCommands) throws CommandSyntaxException {
40
		MinecraftServer server = source.getServer();
41
		if (server.isPublished() && server.getPort() > -1) {
42
			throw ERROR_ALREADY_PUBLISHED_LAN.create(server.getPort());
43
		}
44
45
		if (!server.publishServer(MinecraftServer.MultiplayerScope.LAN, allowCommands, port)) {
46
			throw ERROR_FAILED.create();
47
		}
48
49
		source.sendSuccess(() -> getSuccessMessage(port), true);
50
		return port;
51
	}
52
53
	public static MutableComponent getSuccessMessage(final int port) {
54
		Component portText = ComponentUtils.copyOnClickText(String.valueOf(port));
55
		return Component.translatable("commands.publish.started.lan", portText);
56
	}
57
}
58

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