Interface FabricPacket
Implementations should have fields of values sent over the network.
For example, a packet consisting of two integers should have two int
fields with appropriate name. This is written to the buffer in write(net.minecraft.network.PacketByteBuf)
.
The packet should have two constructors: one that creates a packet on the sender,
which initializes the fields to be written, and one that takes a PacketByteBuf
and reads the packet.
For each packet class, a corresponding PacketType
instance should be created.
The type should be stored in a static final
field, and getType()
should
return that type.
Example of a packet:
public record BoomPacket(boolean fire) implements FabricPacket {
public static final PacketType<BoomPacket> TYPE = PacketType.create(new Identifier("example:boom"), BoomPacket::new);
public BoomPacket(PacketByteBuf buf) {
this(buf.readBoolean());
}
@Override
public void write(PacketByteBuf buf) {
buf.writeBoolean(this.fire);
}
@Override
public PacketType<?> getType() {
return TYPE;
}
}
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionPacketType
<?> getType()
Returns the packet type of this packet.void
write
(PacketByteBuf buf) Writes the contents of this packet to the buffer.
-
Method Details
-
write
Writes the contents of this packet to the buffer.- Parameters:
buf
- the output buffer
-
getType
PacketType<?> getType()Returns the packet type of this packet.Implementations should store the packet type instance in a
static final
field and return that here, instead of creating a new instance.- Returns:
- the type of this packet
-