| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.arguments.StringArgumentType;
|
| 5 | import java.nio.charset.StandardCharsets;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.UUID;
|
| 8 | import net.minecraft.commands.CommandSourceStack;
|
| 9 | import net.minecraft.commands.Commands;
|
| 10 | import net.minecraft.commands.arguments.UuidArgument;
|
| 11 | import net.minecraft.network.protocol.Packet;
|
| 12 | import net.minecraft.network.protocol.common.ClientboundResourcePackPopPacket;
|
| 13 | import net.minecraft.network.protocol.common.ClientboundResourcePackPushPacket;
|
| 14 |
|
| 15 | public class ServerPackCommand {
|
| 16 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 17 | dispatcher.register(
|
| 18 | Commands.literal("serverpack")
|
| 19 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 20 | .then(
|
| 21 | Commands.literal("push")
|
| 22 | .then(
|
| 23 | Commands.argument("url", StringArgumentType.string())
|
| 24 | .then(
|
| 25 | Commands.argument("uuid", UuidArgument.uuid())
|
| 26 | .then(
|
| 27 | Commands.argument("hash", StringArgumentType.word())
|
| 28 | .executes(
|
| 29 | c -> pushPack(
|
| 30 | c.getSource(),
|
| 31 | StringArgumentType.getString(c, "url"),
|
| 32 | Optional.of(UuidArgument.getUuid(c, "uuid")),
|
| 33 | Optional.of(StringArgumentType.getString(c, "hash"))
|
| 34 | )
|
| 35 | )
|
| 36 | )
|
| 37 | .executes(c -> pushPack(c.getSource(), StringArgumentType.getString(c, "url"), Optional.of(UuidArgument.getUuid(c, "uuid")), Optional.empty()))
|
| 38 | )
|
| 39 | .executes(c -> pushPack(c.getSource(), StringArgumentType.getString(c, "url"), Optional.empty(), Optional.empty()))
|
| 40 | )
|
| 41 | )
|
| 42 | .then(Commands.literal("pop").then(Commands.argument("uuid", UuidArgument.uuid()).executes(c -> popPack(c.getSource(), UuidArgument.getUuid(c, "uuid")))))
|
| 43 | );
|
| 44 | }
|
| 45 |
|
| 46 | private static void sendToAllConnections(final CommandSourceStack source, final Packet<?> packet) {
|
| 47 | source.getServer().getConnection().getConnections().forEach(connection -> connection.send(packet));
|
| 48 | }
|
| 49 |
|
| 50 | private static int pushPack(final CommandSourceStack source, final String url, final Optional<UUID> maybeId, final Optional<String> maybeHash) {
|
| 51 | UUID id = maybeId.orElseGet(() -> UUID.nameUUIDFromBytes(url.getBytes(StandardCharsets.UTF_8)));
|
| 52 | String hash = maybeHash.orElse("");
|
| 53 | ClientboundResourcePackPushPacket packet = new ClientboundResourcePackPushPacket(id, url, hash, false, null);
|
| 54 | sendToAllConnections(source, packet);
|
| 55 | return 0;
|
| 56 | }
|
| 57 |
|
| 58 | private static int popPack(final CommandSourceStack source, final UUID uuid) {
|
| 59 | ClientboundResourcePackPopPacket packet = new ClientboundResourcePackPopPacket(Optional.of(uuid));
|
| 60 | sendToAllConnections(source, packet);
|
| 61 | return 0;
|
| 62 | }
|
| 63 | }
|
| 64 |
|