Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ public interface Builder<A> {
*/
AttachmentRegistry.Builder<A> syncWith(StreamCodec<? super RegistryFriendlyByteBuf, A> packetCodec, AttachmentSyncPredicate syncPredicate);

/**
* Declares that this attachment type may be automatically synchronized with some clients, as determined by {@code syncPredicate}.
*
* <p>The max size limit should be increased with care, as syncing large amounts of data may result in network lag and excessive bandwidth usage.
*
* @param packetCodec the codec used to serialize the attachment data over the network
* @param syncPredicate an {@link AttachmentSyncPredicate} determining with which clients to synchronize data
* @param maxSyncBytes the max number of data bytes that can be synced, defaults to 1 MiB
* @return the builder
*/
AttachmentRegistry.Builder<A> syncWith(StreamCodec<? super RegistryFriendlyByteBuf, A> packetCodec, AttachmentSyncPredicate syncPredicate, int maxSyncBytes);

/**
* Builds and registers the {@link AttachmentType}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
import net.fabricmc.fabric.api.attachment.v1.AttachmentSyncPredicate;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
import net.fabricmc.fabric.impl.attachment.sync.AttachmentChange;
import net.fabricmc.fabric.impl.attachment.sync.AttachmentSync;

public final class AttachmentRegistryImpl {
Expand Down Expand Up @@ -84,6 +85,7 @@ public static class BuilderImpl<A> implements AttachmentRegistry.Builder<A> {
@Nullable
private AttachmentSyncPredicate syncPredicate = null;
private boolean copyOnDeath = false;
private int maxSyncBytes = 1024 * 1024; // 1 MiB

@Override
public AttachmentRegistry.Builder<A> persistent(Codec<A> codec) {
Expand All @@ -107,7 +109,7 @@ public AttachmentRegistry.Builder<A> initializer(Supplier<A> initializer) {
return this;
}

@Deprecated
@Override
public AttachmentRegistry.Builder<A> syncWith(StreamCodec<? super RegistryFriendlyByteBuf, A> packetCodec, AttachmentSyncPredicate syncPredicate) {
Objects.requireNonNull(packetCodec, "packet codec cannot be null");
Objects.requireNonNull(syncPredicate, "sync predicate cannot be null");
Expand All @@ -117,6 +119,20 @@ public AttachmentRegistry.Builder<A> syncWith(StreamCodec<? super RegistryFriend
return this;
}

@Override
public AttachmentRegistry.Builder<A> syncWith(StreamCodec<? super RegistryFriendlyByteBuf, A> packetCodec, AttachmentSyncPredicate syncPredicate, int maxSyncBytes) {
syncWith(packetCodec, syncPredicate);

if (maxSyncBytes > AttachmentChange.MAX_DATA_SIZE_IN_BYTES) {
throw new IllegalArgumentException("max sync bytes cannot be greater than " + AttachmentChange.MAX_DATA_SIZE_IN_BYTES);
} else if (maxSyncBytes <= 0) {
throw new IllegalArgumentException("max sync bytes must be positive");
}

this.maxSyncBytes = maxSyncBytes;
return this;
}

@Override
public AttachmentType<A> buildAndRegister(Identifier id) {
Objects.requireNonNull(id, "identifier cannot be null");
Expand All @@ -136,6 +152,7 @@ public AttachmentType<A> buildAndRegister(Identifier id) {
persistenceCodec,
packetCodec,
syncPredicate,
maxSyncBytes,
copyOnDeath
);
register(id, attachment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public record AttachmentTypeImpl<A>(
@Nullable Codec<A> persistenceCodec,
@Nullable StreamCodec<? super RegistryFriendlyByteBuf, A> packetCodec,
@Nullable AttachmentSyncPredicate syncPredicate,
int maxSyncBytes,
boolean copyOnDeath
) implements AttachmentType<A> {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import net.fabricmc.fabric.impl.attachment.AttachmentRegistryImpl;
import net.fabricmc.fabric.impl.attachment.AttachmentTypeImpl;
import net.fabricmc.fabric.impl.attachment.sync.s2c.AttachmentSyncPayloadS2C;
import net.fabricmc.fabric.mixin.attachment.ServerboundCustomPayloadPacketAccessor;
import net.fabricmc.fabric.mixin.attachment.VarIntAccessor;
import net.fabricmc.fabric.mixin.networking.accessor.ServerCommonPacketListenerImplAccessor;

Expand All @@ -60,7 +59,7 @@ public record AttachmentChange(AttachmentTargetInfo<?> targetInfo, AttachmentTyp
AttachmentChange::new
);
private static final int MAX_PADDING_SIZE_IN_BYTES = AttachmentTargetInfo.MAX_SIZE_IN_BYTES + AttachmentSync.MAX_IDENTIFIER_SIZE;
private static final int MAX_DATA_SIZE_IN_BYTES = ServerboundCustomPayloadPacketAccessor.getMaxPayloadSize() - MAX_PADDING_SIZE_IN_BYTES;
public static final int MAX_DATA_SIZE_IN_BYTES = AttachmentSync.MAX_ATTACHMENT_SYNC_PAYLOAD_SIZE - MAX_PADDING_SIZE_IN_BYTES;

@SuppressWarnings("unchecked")
public static AttachmentChange create(AttachmentTargetInfo<?> targetInfo, AttachmentType<?> type, @Nullable Object value, RegistryAccess dynamicRegistryManager) {
Expand All @@ -77,13 +76,15 @@ public static AttachmentChange create(AttachmentTargetInfo<?> targetInfo, Attach
buf.writeBoolean(false);
}

byte[] encoded = buf.array();
byte[] encoded = new byte[buf.readableBytes()]; // buf.array() will return the backing array directly, which may contain unused space
buf.readBytes(encoded);
int maxSyncBytes = ((AttachmentTypeImpl<?>) type).maxSyncBytes();

if (encoded.length > MAX_DATA_SIZE_IN_BYTES) {
throw new IllegalArgumentException("Data for attachment '%s' was too big (%d bytes, over maximum %d)".formatted(
if (encoded.length > maxSyncBytes) {
throw new IllegalArgumentException("Data for attachment '%s' was too big (%d bytes, over maximum %d). This limit can be configured during attachment registration.".formatted(
type.identifier(),
encoded.length,
MAX_DATA_SIZE_IN_BYTES
maxSyncBytes
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

public class AttachmentSync implements ModInitializer {
public static final int MAX_IDENTIFIER_SIZE = 256;
public static final int MAX_ATTACHMENT_SYNC_PAYLOAD_SIZE = Integer.MAX_VALUE;

public static AcceptedAttachmentsPayloadC2S createResponsePayload() {
return new AcceptedAttachmentsPayloadC2S(AttachmentRegistryImpl.getSyncableAttachments());
Expand Down Expand Up @@ -103,7 +104,7 @@ public void onInitialize() {
});

// Play
PayloadTypeRegistry.playS2C().register(AttachmentSyncPayloadS2C.ID, AttachmentSyncPayloadS2C.CODEC);
PayloadTypeRegistry.playS2C().registerLarge(AttachmentSyncPayloadS2C.ID, AttachmentSyncPayloadS2C.CODEC, MAX_ATTACHMENT_SYNC_PAYLOAD_SIZE);

ServerPlayerEvents.JOIN.register((player) -> {
List<AttachmentChange> changes = new ArrayList<>();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"PlayerChunkSenderMixin",
"ChunkAccessMixin",
"ConnectionMixin",
"ServerboundCustomPayloadPacketAccessor",
"EntityMixin",
"SerializableChunkDataMixin",
"ServerLevelMixin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package net.fabricmc.fabric.test.attachment;

import java.util.stream.LongStream;

import com.mojang.serialization.Codec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -28,6 +30,7 @@
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
Expand Down Expand Up @@ -95,6 +98,13 @@ public class AttachmentTestMod implements ModInitializer {
.persistent(ExtraCodecs.NON_NEGATIVE_INT)
.syncWith(ByteBufCodecs.INT, AttachmentSyncPredicate.targetOnly())
);
public static final long[] LARGE_DATA = LongStream.generate(RandomSource.create(16554)::nextLong).limit((10 * 1024 * 1024) / 8).toArray();
public static final AttachmentType<long[]> SYNCED_LARGE = AttachmentRegistry.create(
Identifier.fromNamespaceAndPath(MOD_ID, "synced_large"),
builder -> builder
.initializer(() -> LARGE_DATA)
.syncWith(ByteBufCodecs.LONG_ARRAY, AttachmentSyncPredicate.all(), 10 * 1024 * 1024 + 4) // 10 MiB + int length
);

@Override
public void onInitialize() {
Expand All @@ -115,6 +125,14 @@ public void onInitialize() {
player.displayClientMessage(Component.literal("Attached"), false);
return InteractionResult.SUCCESS;
}
} else if (player.getItemInHand(hand).getItem() == Items.GOLDEN_CARROT) {
BlockEntity blockEntity = world.getBlockEntity(hitResult.getBlockPos());

if (blockEntity != null) {
blockEntity.getAttachedOrCreate(SYNCED_LARGE);
player.displayClientMessage(Component.literal("Attached LARGE"), false);
return InteractionResult.SUCCESS;
}
}

return InteractionResult.PASS;
Expand Down
Loading