Path: blob/1.21.x/src/main/java/net/minecraftforge/network/payload/PayloadFlow.java
6513 views
/*1* Copyright (c) Forge Development LLC and contributors2* SPDX-License-Identifier: LGPL-2.1-only3*/45package net.minecraftforge.network.payload;67import java.util.function.BiConsumer;8import net.minecraft.network.FriendlyByteBuf;9import net.minecraft.network.codec.StreamCodec;10import net.minecraft.network.protocol.common.custom.CustomPacketPayload;11import net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type;12import net.minecraftforge.event.network.CustomPayloadEvent;13import net.minecraftforge.event.network.CustomPayloadEvent.Context;14import net.minecraftforge.network.ChannelBuildable;1516public interface PayloadFlow<BUF extends FriendlyByteBuf, BASE extends CustomPacketPayload> extends PayloadProtocol<BUF, BASE>, ChannelBuildable<CustomPacketPayload> {17/**18* Adds a packet to this channel that has it's protocol validated whenever sent or received.19* <p>20* The handler is called on the network thread, and so should not interact with most game state by default.21* {@link CustomPayloadEvent.Context#enqueueWork(Runnable)} can be used to handle the message on the main server or22* client thread.23*/24<MSG extends BASE> PayloadFlow<BUF, BASE> add(Type<MSG> type, StreamCodec<BUF, MSG> codec, BiConsumer<MSG, Context> handler);2526/**27* Adds a packet to this channel that has it's protocol validated whenever sent or received.28* <p>29* Unlike {@link #add(Class,StreamCodec,BiConsumer)}, the consumer is called on the main thread, and so can30* interact with most game state by default.31*/32default <MSG extends BASE> PayloadFlow<BUF, BASE> addMain(Type<MSG> type, StreamCodec<BUF, MSG> codec, BiConsumer<MSG, Context> handler) {33return add(type, codec, (msg, ctx) -> {34ctx.enqueueWork(() -> handler.accept(msg, ctx));35ctx.setPacketHandled(true);36});37}38}394041