Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
minecraftforge
GitHub Repository: minecraftforge/minecraftforge
Path: blob/1.21.x/src/main/java/net/minecraftforge/network/payload/PayloadFlow.java
6513 views
1
/*
2
* Copyright (c) Forge Development LLC and contributors
3
* SPDX-License-Identifier: LGPL-2.1-only
4
*/
5
6
package net.minecraftforge.network.payload;
7
8
import java.util.function.BiConsumer;
9
import net.minecraft.network.FriendlyByteBuf;
10
import net.minecraft.network.codec.StreamCodec;
11
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
12
import net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type;
13
import net.minecraftforge.event.network.CustomPayloadEvent;
14
import net.minecraftforge.event.network.CustomPayloadEvent.Context;
15
import net.minecraftforge.network.ChannelBuildable;
16
17
public interface PayloadFlow<BUF extends FriendlyByteBuf, BASE extends CustomPacketPayload> extends PayloadProtocol<BUF, BASE>, ChannelBuildable<CustomPacketPayload> {
18
/**
19
* Adds a packet to this channel that has it's protocol validated whenever sent or received.
20
* <p>
21
* The handler is called on the network thread, and so should not interact with most game state by default.
22
* {@link CustomPayloadEvent.Context#enqueueWork(Runnable)} can be used to handle the message on the main server or
23
* client thread.
24
*/
25
<MSG extends BASE> PayloadFlow<BUF, BASE> add(Type<MSG> type, StreamCodec<BUF, MSG> codec, BiConsumer<MSG, Context> handler);
26
27
/**
28
* Adds a packet to this channel that has it's protocol validated whenever sent or received.
29
* <p>
30
* Unlike {@link #add(Class,StreamCodec,BiConsumer)}, the consumer is called on the main thread, and so can
31
* interact with most game state by default.
32
*/
33
default <MSG extends BASE> PayloadFlow<BUF, BASE> addMain(Type<MSG> type, StreamCodec<BUF, MSG> codec, BiConsumer<MSG, Context> handler) {
34
return add(type, codec, (msg, ctx) -> {
35
ctx.enqueueWork(() -> handler.accept(msg, ctx));
36
ctx.setPacketHandled(true);
37
});
38
}
39
}
40
41