| 1 | package net.minecraft.util.debug;
|
| 2 |
|
| 3 | import java.util.Objects;
|
| 4 | import java.util.Optional;
|
| 5 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 6 | import net.minecraft.core.registries.Registries;
|
| 7 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 8 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 9 | import net.minecraft.network.codec.StreamCodec;
|
| 10 | import net.minecraft.util.Util;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | public class DebugSubscription<T> {
|
| 14 | public static final int DOES_NOT_EXPIRE = 0;
|
| 15 | private final @Nullable StreamCodec<? super RegistryFriendlyByteBuf, T> valueStreamCodec;
|
| 16 | private final int expireAfterTicks;
|
| 17 |
|
| 18 | public DebugSubscription(final @Nullable StreamCodec<? super RegistryFriendlyByteBuf, T> valueStreamCodec, final int expireAfterTicks) {
|
| 19 | this.valueStreamCodec = valueStreamCodec;
|
| 20 | this.expireAfterTicks = expireAfterTicks;
|
| 21 | }
|
| 22 |
|
| 23 | public DebugSubscription(final @Nullable StreamCodec<? super RegistryFriendlyByteBuf, T> valueStreamCodec) {
|
| 24 | this(valueStreamCodec, 0);
|
| 25 | }
|
| 26 |
|
| 27 | public DebugSubscription.Update<T> packUpdate(final @Nullable T value) {
|
| 28 | return new DebugSubscription.Update<>(this, Optional.ofNullable(value));
|
| 29 | }
|
| 30 |
|
| 31 | public DebugSubscription.Update<T> emptyUpdate() {
|
| 32 | return new DebugSubscription.Update<>(this, Optional.empty());
|
| 33 | }
|
| 34 |
|
| 35 | public DebugSubscription.Event<T> packEvent(final T value) {
|
| 36 | return new DebugSubscription.Event<>(this, value);
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public String toString() {
|
| 41 | return Util.getRegisteredName(BuiltInRegistries.DEBUG_SUBSCRIPTION, this);
|
| 42 | }
|
| 43 |
|
| 44 | public @Nullable StreamCodec<? super RegistryFriendlyByteBuf, T> valueStreamCodec() {
|
| 45 | return this.valueStreamCodec;
|
| 46 | }
|
| 47 |
|
| 48 | public int expireAfterTicks() {
|
| 49 | return this.expireAfterTicks;
|
| 50 | }
|
| 51 |
|
| 52 | public record Event<T>(DebugSubscription<T> subscription, T value) {
|
| 53 | public static final StreamCodec<RegistryFriendlyByteBuf, DebugSubscription.Event<?>> STREAM_CODEC = ByteBufCodecs.registry(Registries.DEBUG_SUBSCRIPTION)
|
| 54 | .dispatch(DebugSubscription.Event::subscription, DebugSubscription.Event::streamCodec);
|
| 55 |
|
| 56 | private static <T> StreamCodec<? super RegistryFriendlyByteBuf, DebugSubscription.Event<T>> streamCodec(final DebugSubscription<T> subscription) {
|
| 57 | return Objects.requireNonNull(subscription.valueStreamCodec)
|
| 58 | .map(value -> new DebugSubscription.Event<>(subscription, (T)value), DebugSubscription.Event::value);
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | public record Update<T>(DebugSubscription<T> subscription, Optional<T> value) {
|
| 63 | public static final StreamCodec<RegistryFriendlyByteBuf, DebugSubscription.Update<?>> STREAM_CODEC = ByteBufCodecs.registry(Registries.DEBUG_SUBSCRIPTION)
|
| 64 | .dispatch(DebugSubscription.Update::subscription, DebugSubscription.Update::streamCodec);
|
| 65 |
|
| 66 | private static <T> StreamCodec<? super RegistryFriendlyByteBuf, DebugSubscription.Update<T>> streamCodec(final DebugSubscription<T> subscription) {
|
| 67 | return ByteBufCodecs.optional(Objects.requireNonNull(subscription.valueStreamCodec))
|
| 68 | .map(value -> new DebugSubscription.Update<>(subscription, (Optional<T>)value), DebugSubscription.Update::value);
|
| 69 | }
|
| 70 | }
|
| 71 | }
|
| 72 |
|