| 1 | package net.minecraft.server;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import java.net.URI;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.function.IntFunction;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 | import net.minecraft.network.chat.ComponentSerialization;
|
| 11 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 12 | import net.minecraft.network.codec.StreamCodec;
|
| 13 | import net.minecraft.util.ByIdMap;
|
| 14 |
|
| 15 | public record ServerLinks(List<ServerLinks.Entry> entries) {
|
| 16 | public static final ServerLinks EMPTY = new ServerLinks(List.of());
|
| 17 | public static final StreamCodec<ByteBuf, Either<ServerLinks.KnownLinkType, Component>> TYPE_STREAM_CODEC = ByteBufCodecs.either(
|
| 18 | ServerLinks.KnownLinkType.STREAM_CODEC, ComponentSerialization.TRUSTED_CONTEXT_FREE_STREAM_CODEC
|
| 19 | );
|
| 20 | public static final StreamCodec<ByteBuf, List<ServerLinks.UntrustedEntry>> UNTRUSTED_LINKS_STREAM_CODEC = ServerLinks.UntrustedEntry.STREAM_CODEC
|
| 21 | .apply(ByteBufCodecs.list());
|
| 22 |
|
| 23 | public boolean isEmpty() {
|
| 24 | return this.entries.isEmpty();
|
| 25 | }
|
| 26 |
|
| 27 | public Optional<ServerLinks.Entry> findKnownType(final ServerLinks.KnownLinkType type) {
|
| 28 | return this.entries.stream().filter(e -> e.type.map(l -> l == type, r -> false)).findFirst();
|
| 29 | }
|
| 30 |
|
| 31 | public List<ServerLinks.UntrustedEntry> untrust() {
|
| 32 | return this.entries.stream().map(e -> new ServerLinks.UntrustedEntry(e.type, e.link.toString())).toList();
|
| 33 | }
|
| 34 |
|
| 35 | public record Entry(Either<ServerLinks.KnownLinkType, Component> type, URI link) {
|
| 36 | public static ServerLinks.Entry knownType(final ServerLinks.KnownLinkType type, final URI link) {
|
| 37 | return new ServerLinks.Entry(Either.left(type), link);
|
| 38 | }
|
| 39 |
|
| 40 | public static ServerLinks.Entry custom(final Component displayName, final URI link) {
|
| 41 | return new ServerLinks.Entry(Either.right(displayName), link);
|
| 42 | }
|
| 43 |
|
| 44 | public Component displayName() {
|
| 45 | return this.type.map(ServerLinks.KnownLinkType::displayName, r -> (Component)r);
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | public enum KnownLinkType {
|
| 50 | BUG_REPORT(0, "report_bug"),
|
| 51 | COMMUNITY_GUIDELINES(1, "community_guidelines"),
|
| 52 | SUPPORT(2, "support"),
|
| 53 | STATUS(3, "status"),
|
| 54 | FEEDBACK(4, "feedback"),
|
| 55 | COMMUNITY(5, "community"),
|
| 56 | WEBSITE(6, "website"),
|
| 57 | FORUMS(7, "forums"),
|
| 58 | NEWS(8, "news"),
|
| 59 | ANNOUNCEMENTS(9, "announcements");
|
| 60 |
|
| 61 | private static final IntFunction<ServerLinks.KnownLinkType> BY_ID = ByIdMap.continuous(e -> e.id, values(), ByIdMap.OutOfBoundsStrategy.ZERO);
|
| 62 | public static final StreamCodec<ByteBuf, ServerLinks.KnownLinkType> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, e -> e.id);
|
| 63 | private final int id;
|
| 64 | private final String name;
|
| 65 |
|
| 66 | KnownLinkType(final int id, final String name) {
|
| 67 | this.id = id;
|
| 68 | this.name = name;
|
| 69 | }
|
| 70 |
|
| 71 | private Component displayName() {
|
| 72 | return Component.translatable("known_server_link." + this.name);
|
| 73 | }
|
| 74 |
|
| 75 | public ServerLinks.Entry create(final URI link) {
|
| 76 | return ServerLinks.Entry.knownType(this, link);
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | public record UntrustedEntry(Either<ServerLinks.KnownLinkType, Component> type, String link) {
|
| 81 | public static final StreamCodec<ByteBuf, ServerLinks.UntrustedEntry> STREAM_CODEC = StreamCodec.composite(
|
| 82 | ServerLinks.TYPE_STREAM_CODEC,
|
| 83 | ServerLinks.UntrustedEntry::type,
|
| 84 | ByteBufCodecs.STRING_UTF8,
|
| 85 | ServerLinks.UntrustedEntry::link,
|
| 86 | ServerLinks.UntrustedEntry::new
|
| 87 | );
|
| 88 | }
|
| 89 | }
|
| 90 |
|