From 7c4ba732dd44303d73726e2d7b1d9fe8ece87be6 Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Thu, 14 Nov 2024 11:02:00 +0000 Subject: [PATCH 1/6] Provide a RegistryByteBuf for attachment syncing --- .../api/attachment/v1/AttachmentRegistry.java | 11 +++++- .../api/attachment/v1/AttachmentType.java | 2 +- .../attachment/AttachmentRegistryImpl.java | 7 ++-- .../impl/attachment/AttachmentTargetImpl.java | 3 ++ .../impl/attachment/AttachmentTypeImpl.java | 4 +-- .../attachment/sync/AttachmentChange.java | 35 +++++++++++++------ .../attachment/AttachmentTargetsMixin.java | 4 +-- .../mixin/attachment/BlockEntityMixin.java | 6 ++++ .../fabric/mixin/attachment/ChunkMixin.java | 7 ++++ .../fabric/mixin/attachment/EntityMixin.java | 6 ++++ .../mixin/attachment/ServerWorldMixin.java | 5 +++ .../mixin/attachment/WorldChunkMixin.java | 6 ++++ .../fabric/mixin/attachment/WorldMixin.java | 9 +++++ .../attachment/WrapperProtoChunkMixin.java | 6 ++++ .../test/attachment/AttachmentTestMod.java | 8 ++--- 15 files changed, 96 insertions(+), 23 deletions(-) diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentRegistry.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentRegistry.java index f549342529..afbf091adb 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentRegistry.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentRegistry.java @@ -23,6 +23,7 @@ import org.jetbrains.annotations.ApiStatus; import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.RegistryByteBuf; import net.minecraft.network.codec.PacketCodec; import net.minecraft.util.Identifier; @@ -150,6 +151,14 @@ public interface Builder { */ Builder initializer(Supplier initializer); + /** + * @deprecated use {@link #syncedWith(PacketCodec, AttachmentSyncPredicate)} instead + */ + @Deprecated + default AttachmentRegistry.Builder syncWith(PacketCodec packetCodec, AttachmentSyncPredicate syncPredicate) { + return syncedWith(packetCodec.cast(), syncPredicate); + } + /** * Declares that this attachment type may be automatically synchronized with some clients, as determined by {@code syncPredicate}. * @@ -157,7 +166,7 @@ public interface Builder { * @param syncPredicate an {@link AttachmentSyncPredicate} determining with which clients to synchronize data * @return the builder */ - AttachmentRegistry.Builder syncWith(PacketCodec packetCodec, AttachmentSyncPredicate syncPredicate); + AttachmentRegistry.Builder syncedWith(PacketCodec packetCodec, AttachmentSyncPredicate syncPredicate); /** * Builds and registers the {@link AttachmentType}. diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentType.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentType.java index e189d65ceb..58559fe796 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentType.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentType.java @@ -56,7 +56,7 @@ * @param type of the attached data. It is encouraged for this to be an immutable type. * @see AttachmentRegistry * @see AttachmentRegistry.Builder#persistent(Codec) - * @see AttachmentRegistry.Builder#syncWith(PacketCodec, AttachmentSyncPredicate) + * @see AttachmentRegistry.Builder#syncedWith(PacketCodec, AttachmentSyncPredicate) */ @ApiStatus.NonExtendable @ApiStatus.Experimental diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentRegistryImpl.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentRegistryImpl.java index ae60764783..a0babd3dab 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentRegistryImpl.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentRegistryImpl.java @@ -29,7 +29,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.RegistryByteBuf; import net.minecraft.network.codec.PacketCodec; import net.minecraft.util.Identifier; @@ -80,7 +80,7 @@ public static class BuilderImpl implements AttachmentRegistry.Builder { @Nullable private Codec persistenceCodec = null; @Nullable - private PacketCodec packetCodec = null; + private PacketCodec packetCodec = null; @Nullable private AttachmentSyncPredicate syncPredicate = null; private boolean copyOnDeath = false; @@ -107,7 +107,8 @@ public AttachmentRegistry.Builder initializer(Supplier initializer) { return this; } - public AttachmentRegistry.Builder syncWith(PacketCodec packetCodec, AttachmentSyncPredicate syncPredicate) { + @Deprecated + public AttachmentRegistry.Builder syncedWith(PacketCodec packetCodec, AttachmentSyncPredicate syncPredicate) { Objects.requireNonNull(packetCodec, "packet codec cannot be null"); Objects.requireNonNull(syncPredicate, "sync predicate cannot be null"); diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentTargetImpl.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentTargetImpl.java index 0794002fa2..0b445cbe86 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentTargetImpl.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentTargetImpl.java @@ -22,6 +22,7 @@ import org.jetbrains.annotations.Nullable; import net.minecraft.nbt.NbtCompound; +import net.minecraft.registry.DynamicRegistryManager; import net.minecraft.registry.RegistryWrapper; import net.minecraft.server.network.ServerPlayerEntity; @@ -93,4 +94,6 @@ default void fabric_markChanged(AttachmentType type) { default boolean fabric_shouldTryToSync() { throw new UnsupportedOperationException("Implemented via mixin"); } + + DynamicRegistryManager fabric_getDynamicRegistryManager(); } diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentTypeImpl.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentTypeImpl.java index a316442e27..22101f90c5 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentTypeImpl.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentTypeImpl.java @@ -21,7 +21,7 @@ import com.mojang.serialization.Codec; import org.jetbrains.annotations.Nullable; -import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.RegistryByteBuf; import net.minecraft.network.codec.PacketCodec; import net.minecraft.util.Identifier; @@ -32,7 +32,7 @@ public record AttachmentTypeImpl( Identifier identifier, @Nullable Supplier initializer, @Nullable Codec persistenceCodec, - @Nullable PacketCodec packetCodec, + @Nullable PacketCodec packetCodec, @Nullable AttachmentSyncPredicate syncPredicate, boolean copyOnDeath ) implements AttachmentType { diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/sync/AttachmentChange.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/sync/AttachmentChange.java index ca4eab840b..3008af0263 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/sync/AttachmentChange.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/sync/AttachmentChange.java @@ -20,15 +20,16 @@ import java.util.Comparator; import java.util.List; import java.util.Objects; -import java.util.Optional; import java.util.Set; import io.netty.buffer.Unpooled; import org.jetbrains.annotations.Nullable; import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.RegistryByteBuf; import net.minecraft.network.codec.PacketCodec; import net.minecraft.network.codec.PacketCodecs; +import net.minecraft.registry.DynamicRegistryManager; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Identifier; import net.minecraft.world.World; @@ -57,12 +58,20 @@ public record AttachmentChange(AttachmentTargetInfo targetInfo, AttachmentTyp private static final int MAX_DATA_SIZE_IN_BYTES = CustomPayloadS2CPacketAccessor.getMaxPayloadSize() - MAX_PADDING_SIZE_IN_BYTES; @SuppressWarnings("unchecked") - public static AttachmentChange create(AttachmentTargetInfo targetInfo, AttachmentType type, @Nullable Object value) { - PacketCodec codec = (PacketCodec) ((AttachmentTypeImpl) type).packetCodec(); + public static AttachmentChange create(AttachmentTargetInfo targetInfo, AttachmentType type, @Nullable Object value, DynamicRegistryManager dynamicRegistryManager) { + PacketCodec codec = (PacketCodec) ((AttachmentTypeImpl) type).packetCodec(); Objects.requireNonNull(codec, "attachment packet codec cannot be null"); + Objects.requireNonNull(dynamicRegistryManager, "dynamic registry manager cannot be null"); + + RegistryByteBuf buf = new RegistryByteBuf(PacketByteBufs.create(), dynamicRegistryManager); + + if (value != null) { + buf.writeBoolean(true); + codec.encode(buf, value); + } else { + buf.writeBoolean(false); + } - PacketByteBuf buf = PacketByteBufs.create(); - buf.writeOptional(Optional.ofNullable(value), codec); byte[] encoded = buf.array(); if (encoded.length > MAX_DATA_SIZE_IN_BYTES) { @@ -109,15 +118,21 @@ public static void partitionAndSendPackets(List changes, Serve @SuppressWarnings("unchecked") @Nullable - public Object decodeValue() { - PacketCodec codec = (PacketCodec) ((AttachmentTypeImpl) type).packetCodec(); + public Object decodeValue(DynamicRegistryManager dynamicRegistryManager) { + PacketCodec codec = (PacketCodec) ((AttachmentTypeImpl) type).packetCodec(); Objects.requireNonNull(codec, "codec was null"); + Objects.requireNonNull(dynamicRegistryManager, "dynamic registry manager cannot be null"); + + RegistryByteBuf buf = new RegistryByteBuf(Unpooled.copiedBuffer(data), dynamicRegistryManager); + + if (!buf.readBoolean()) { + return null; + } - PacketByteBuf buf = new PacketByteBuf(Unpooled.copiedBuffer(data)); - return buf.readOptional(codec).orElse(null); + return codec.decode(buf); } public void apply(World world) { - targetInfo.getTarget(world).setAttached((AttachmentType) type, decodeValue()); + targetInfo.getTarget(world).setAttached((AttachmentType) type, decodeValue(world.getRegistryManager())); } } diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/AttachmentTargetsMixin.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/AttachmentTargetsMixin.java index 2ff585c7ef..bdf2edda75 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/AttachmentTargetsMixin.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/AttachmentTargetsMixin.java @@ -61,7 +61,7 @@ public T setAttached(AttachmentType type, @Nullable T value) { this.fabric_markChanged(type); if (this.fabric_shouldTryToSync() && type.isSynced()) { - AttachmentChange change = AttachmentChange.create(fabric_getSyncTargetInfo(), type, value); + AttachmentChange change = AttachmentChange.create(fabric_getSyncTargetInfo(), type, value, fabric_getDynamicRegistryManager()); acknowledgeSyncedEntry(type, change); this.fabric_syncChange(type, new AttachmentSyncPayloadS2C(List.of(change))); } @@ -118,7 +118,7 @@ public boolean fabric_hasPersistentAttachments() { @Unique private void acknowledgeSynced(AttachmentType type, Object value) { - acknowledgeSyncedEntry(type, AttachmentChange.create(fabric_getSyncTargetInfo(), type, value)); + acknowledgeSyncedEntry(type, AttachmentChange.create(fabric_getSyncTargetInfo(), type, value, fabric_getDynamicRegistryManager())); } @Unique diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/BlockEntityMixin.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/BlockEntityMixin.java index 4155442394..4409821079 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/BlockEntityMixin.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/BlockEntityMixin.java @@ -27,6 +27,7 @@ import net.minecraft.block.entity.BlockEntity; import net.minecraft.nbt.NbtCompound; +import net.minecraft.registry.DynamicRegistryManager; import net.minecraft.registry.RegistryWrapper; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -95,4 +96,9 @@ public boolean fabric_shouldTryToSync() { // Persistent attachments are read at a time with no world return !this.hasWorld() || !this.world.isClient(); } + + @Override + public DynamicRegistryManager fabric_getDynamicRegistryManager() { + return this.world.getRegistryManager(); + } } diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/ChunkMixin.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/ChunkMixin.java index e819d8be53..20c5eaa280 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/ChunkMixin.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/ChunkMixin.java @@ -20,6 +20,7 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; +import net.minecraft.registry.DynamicRegistryManager; import net.minecraft.util.math.ChunkPos; import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.ChunkStatus; @@ -67,4 +68,10 @@ public boolean fabric_shouldTryToSync() { // ProtoChunk or EmptyChunk return false; } + + @Override + public DynamicRegistryManager fabric_getDynamicRegistryManager() { + // Should never happen as this is only used for sync + throw new UnsupportedOperationException("Chunk does not have a DynamicRegistryManager."); + } } diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/EntityMixin.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/EntityMixin.java index 1bfffcccf4..c5c21499c6 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/EntityMixin.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/EntityMixin.java @@ -25,6 +25,7 @@ import net.minecraft.entity.Entity; import net.minecraft.nbt.NbtCompound; +import net.minecraft.registry.DynamicRegistryManager; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.world.World; @@ -89,4 +90,9 @@ public void fabric_syncChange(AttachmentType type, AttachmentSyncPayloadS2C p public boolean fabric_shouldTryToSync() { return !this.getWorld().isClient(); } + + @Override + public DynamicRegistryManager fabric_getDynamicRegistryManager() { + return this.getWorld().getRegistryManager(); + } } diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/ServerWorldMixin.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/ServerWorldMixin.java index 84c6849c87..0caf25cc97 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/ServerWorldMixin.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/ServerWorldMixin.java @@ -89,4 +89,9 @@ public void fabric_syncChange(AttachmentType type, AttachmentSyncPayloadS2C p public AttachmentTargetInfo fabric_getSyncTargetInfo() { return AttachmentTargetInfo.WorldTarget.INSTANCE; } + + @Override + public DynamicRegistryManager fabric_getDynamicRegistryManager() { + return getRegistryManager(); + } } diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/WorldChunkMixin.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/WorldChunkMixin.java index 4fae99a96b..e7d774e61a 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/WorldChunkMixin.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/WorldChunkMixin.java @@ -27,6 +27,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import net.minecraft.block.entity.BlockEntity; +import net.minecraft.registry.DynamicRegistryManager; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.math.BlockPos; @@ -83,4 +84,9 @@ public void fabric_syncChange(AttachmentType type, AttachmentSyncPayloadS2C p public boolean fabric_shouldTryToSync() { return !this.world.isClient(); } + + @Override + public DynamicRegistryManager fabric_getDynamicRegistryManager() { + return world.getRegistryManager(); + } } diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/WorldMixin.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/WorldMixin.java index bc8a89c690..1c983fbffc 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/WorldMixin.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/WorldMixin.java @@ -19,6 +19,7 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; +import net.minecraft.registry.DynamicRegistryManager; import net.minecraft.world.World; import net.fabricmc.fabric.impl.attachment.AttachmentTargetImpl; @@ -28,8 +29,16 @@ abstract class WorldMixin implements AttachmentTargetImpl { @Shadow public abstract boolean isClient(); + @Shadow + public abstract DynamicRegistryManager getRegistryManager(); + @Override public boolean fabric_shouldTryToSync() { return !this.isClient(); } + + @Override + public DynamicRegistryManager fabric_getDynamicRegistryManager() { + return getRegistryManager(); + } } diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/WrapperProtoChunkMixin.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/WrapperProtoChunkMixin.java index 870dd55210..4fce194e5a 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/WrapperProtoChunkMixin.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/mixin/attachment/WrapperProtoChunkMixin.java @@ -25,6 +25,7 @@ import org.spongepowered.asm.mixin.Shadow; import net.minecraft.nbt.NbtCompound; +import net.minecraft.registry.DynamicRegistryManager; import net.minecraft.registry.RegistryWrapper; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.world.chunk.WorldChunk; @@ -103,4 +104,9 @@ public void fabric_syncChange(AttachmentType type, AttachmentSyncPayloadS2C p public void fabric_markChanged(AttachmentType type) { ((AttachmentTargetImpl) wrapped).fabric_markChanged(type); } + + @Override + public DynamicRegistryManager fabric_getDynamicRegistryManager() { + return ((AttachmentTargetImpl) wrapped).fabric_getDynamicRegistryManager(); + } } diff --git a/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java b/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java index fa46f3cf28..134f9d2e10 100644 --- a/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java +++ b/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java @@ -80,28 +80,28 @@ public class AttachmentTestMod implements ModInitializer { builder -> builder .initializer(() -> false) .persistent(Codec.BOOL) - .syncWith(PacketCodecs.BOOLEAN.cast(), AttachmentSyncPredicate.all()) + .syncedWith(PacketCodecs.BOOLEAN.cast(), AttachmentSyncPredicate.all()) ); public static final AttachmentType SYNCED_WITH_TARGET = AttachmentRegistry.create( Identifier.of(MOD_ID, "synced_target"), builder -> builder .initializer(() -> false) .persistent(Codec.BOOL) - .syncWith(PacketCodecs.BOOLEAN.cast(), AttachmentSyncPredicate.targetOnly()) + .syncedWith(PacketCodecs.BOOLEAN.cast(), AttachmentSyncPredicate.targetOnly()) ); public static final AttachmentType SYNCED_EXCEPT_TARGET = AttachmentRegistry.create( Identifier.of(MOD_ID, "synced_except_target"), builder -> builder .initializer(() -> false) .persistent(Codec.BOOL) - .syncWith(PacketCodecs.BOOLEAN.cast(), AttachmentSyncPredicate.allButTarget()) + .syncedWith(PacketCodecs.BOOLEAN.cast(), AttachmentSyncPredicate.allButTarget()) ); public static final AttachmentType SYNCED_CREATIVE_ONLY = AttachmentRegistry.create( Identifier.of(MOD_ID, "synced_custom"), builder -> builder .initializer(() -> false) .persistent(Codec.BOOL) - .syncWith(PacketCodecs.BOOLEAN.cast(), (target, player) -> player.isCreative()) + .syncedWith(PacketCodecs.BOOLEAN.cast(), (target, player) -> player.isCreative()) ); public static final SimpleCommandExceptionType TARGET_NOT_FOUND = new SimpleCommandExceptionType(Text.literal("Target not found")); From e291806f332b90788e4db117ad66340b4b058300 Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Thu, 14 Nov 2024 11:06:21 +0000 Subject: [PATCH 2/6] None breaking? --- .../fabric/api/attachment/v1/AttachmentRegistry.java | 11 +---------- .../impl/attachment/AttachmentRegistryImpl.java | 4 ++-- .../fabric/impl/attachment/AttachmentTypeImpl.java | 2 +- .../fabric/test/attachment/AttachmentTestMod.java | 8 ++++---- 4 files changed, 8 insertions(+), 17 deletions(-) diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentRegistry.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentRegistry.java index afbf091adb..2a9b9d96f3 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentRegistry.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentRegistry.java @@ -22,7 +22,6 @@ import com.mojang.serialization.Codec; import org.jetbrains.annotations.ApiStatus; -import net.minecraft.network.PacketByteBuf; import net.minecraft.network.RegistryByteBuf; import net.minecraft.network.codec.PacketCodec; import net.minecraft.util.Identifier; @@ -151,14 +150,6 @@ public interface Builder { */ Builder initializer(Supplier initializer); - /** - * @deprecated use {@link #syncedWith(PacketCodec, AttachmentSyncPredicate)} instead - */ - @Deprecated - default AttachmentRegistry.Builder syncWith(PacketCodec packetCodec, AttachmentSyncPredicate syncPredicate) { - return syncedWith(packetCodec.cast(), syncPredicate); - } - /** * Declares that this attachment type may be automatically synchronized with some clients, as determined by {@code syncPredicate}. * @@ -166,7 +157,7 @@ default AttachmentRegistry.Builder syncWith(PacketCodec pac * @param syncPredicate an {@link AttachmentSyncPredicate} determining with which clients to synchronize data * @return the builder */ - AttachmentRegistry.Builder syncedWith(PacketCodec packetCodec, AttachmentSyncPredicate syncPredicate); + AttachmentRegistry.Builder syncWith(PacketCodec packetCodec, AttachmentSyncPredicate syncPredicate); /** * Builds and registers the {@link AttachmentType}. diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentRegistryImpl.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentRegistryImpl.java index a0babd3dab..305c24105a 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentRegistryImpl.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentRegistryImpl.java @@ -80,7 +80,7 @@ public static class BuilderImpl implements AttachmentRegistry.Builder { @Nullable private Codec persistenceCodec = null; @Nullable - private PacketCodec packetCodec = null; + private PacketCodec packetCodec = null; @Nullable private AttachmentSyncPredicate syncPredicate = null; private boolean copyOnDeath = false; @@ -108,7 +108,7 @@ public AttachmentRegistry.Builder initializer(Supplier initializer) { } @Deprecated - public AttachmentRegistry.Builder syncedWith(PacketCodec packetCodec, AttachmentSyncPredicate syncPredicate) { + public AttachmentRegistry.Builder syncWith(PacketCodec packetCodec, AttachmentSyncPredicate syncPredicate) { Objects.requireNonNull(packetCodec, "packet codec cannot be null"); Objects.requireNonNull(syncPredicate, "sync predicate cannot be null"); diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentTypeImpl.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentTypeImpl.java index 22101f90c5..7d44bd1663 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentTypeImpl.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/AttachmentTypeImpl.java @@ -32,7 +32,7 @@ public record AttachmentTypeImpl( Identifier identifier, @Nullable Supplier initializer, @Nullable Codec persistenceCodec, - @Nullable PacketCodec packetCodec, + @Nullable PacketCodec packetCodec, @Nullable AttachmentSyncPredicate syncPredicate, boolean copyOnDeath ) implements AttachmentType { diff --git a/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java b/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java index 134f9d2e10..fa46f3cf28 100644 --- a/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java +++ b/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java @@ -80,28 +80,28 @@ public class AttachmentTestMod implements ModInitializer { builder -> builder .initializer(() -> false) .persistent(Codec.BOOL) - .syncedWith(PacketCodecs.BOOLEAN.cast(), AttachmentSyncPredicate.all()) + .syncWith(PacketCodecs.BOOLEAN.cast(), AttachmentSyncPredicate.all()) ); public static final AttachmentType SYNCED_WITH_TARGET = AttachmentRegistry.create( Identifier.of(MOD_ID, "synced_target"), builder -> builder .initializer(() -> false) .persistent(Codec.BOOL) - .syncedWith(PacketCodecs.BOOLEAN.cast(), AttachmentSyncPredicate.targetOnly()) + .syncWith(PacketCodecs.BOOLEAN.cast(), AttachmentSyncPredicate.targetOnly()) ); public static final AttachmentType SYNCED_EXCEPT_TARGET = AttachmentRegistry.create( Identifier.of(MOD_ID, "synced_except_target"), builder -> builder .initializer(() -> false) .persistent(Codec.BOOL) - .syncedWith(PacketCodecs.BOOLEAN.cast(), AttachmentSyncPredicate.allButTarget()) + .syncWith(PacketCodecs.BOOLEAN.cast(), AttachmentSyncPredicate.allButTarget()) ); public static final AttachmentType SYNCED_CREATIVE_ONLY = AttachmentRegistry.create( Identifier.of(MOD_ID, "synced_custom"), builder -> builder .initializer(() -> false) .persistent(Codec.BOOL) - .syncedWith(PacketCodecs.BOOLEAN.cast(), (target, player) -> player.isCreative()) + .syncWith(PacketCodecs.BOOLEAN.cast(), (target, player) -> player.isCreative()) ); public static final SimpleCommandExceptionType TARGET_NOT_FOUND = new SimpleCommandExceptionType(Text.literal("Target not found")); From 6e2b760cccc38ac9948c75950d2313be2663a21a Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Thu, 14 Nov 2024 11:09:50 +0000 Subject: [PATCH 3/6] Slight improvement --- .../fabric/impl/attachment/sync/AttachmentChange.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/sync/AttachmentChange.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/sync/AttachmentChange.java index 3008af0263..990659fc16 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/sync/AttachmentChange.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/impl/attachment/sync/AttachmentChange.java @@ -59,7 +59,7 @@ public record AttachmentChange(AttachmentTargetInfo targetInfo, AttachmentTyp @SuppressWarnings("unchecked") public static AttachmentChange create(AttachmentTargetInfo targetInfo, AttachmentType type, @Nullable Object value, DynamicRegistryManager dynamicRegistryManager) { - PacketCodec codec = (PacketCodec) ((AttachmentTypeImpl) type).packetCodec(); + PacketCodec codec = (PacketCodec) ((AttachmentTypeImpl) type).packetCodec(); Objects.requireNonNull(codec, "attachment packet codec cannot be null"); Objects.requireNonNull(dynamicRegistryManager, "dynamic registry manager cannot be null"); @@ -119,7 +119,7 @@ public static void partitionAndSendPackets(List changes, Serve @SuppressWarnings("unchecked") @Nullable public Object decodeValue(DynamicRegistryManager dynamicRegistryManager) { - PacketCodec codec = (PacketCodec) ((AttachmentTypeImpl) type).packetCodec(); + PacketCodec codec = (PacketCodec) ((AttachmentTypeImpl) type).packetCodec(); Objects.requireNonNull(codec, "codec was null"); Objects.requireNonNull(dynamicRegistryManager, "dynamic registry manager cannot be null"); From 54bde30c0330da42997b0a6d771002dc62cb5e7b Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Thu, 14 Nov 2024 11:19:51 +0000 Subject: [PATCH 4/6] Test syncing an item stack --- .../test/attachment/AttachmentTestMod.java | 25 ++++++++++++++++--- .../client/AttachmentTestModClient.java | 6 +++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java b/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java index fa46f3cf28..a6859b4a90 100644 --- a/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java +++ b/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java @@ -34,12 +34,15 @@ import net.minecraft.command.argument.BlockPosArgumentType; import net.minecraft.command.argument.ColumnPosArgumentType; import net.minecraft.command.argument.EntityArgumentType; +import net.minecraft.entity.EquipmentSlot; +import net.minecraft.item.ItemStack; import net.minecraft.network.codec.PacketCodecs; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; import net.minecraft.registry.RegistryKey; import net.minecraft.registry.RegistryKeys; import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; import net.minecraft.text.Text; import net.minecraft.util.Identifier; @@ -63,6 +66,7 @@ import net.fabricmc.fabric.api.biome.v1.BiomeSelectors; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerChunkEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; public class AttachmentTestMod implements ModInitializer { @@ -80,28 +84,35 @@ public class AttachmentTestMod implements ModInitializer { builder -> builder .initializer(() -> false) .persistent(Codec.BOOL) - .syncWith(PacketCodecs.BOOLEAN.cast(), AttachmentSyncPredicate.all()) + .syncWith(PacketCodecs.BOOLEAN, AttachmentSyncPredicate.all()) ); public static final AttachmentType SYNCED_WITH_TARGET = AttachmentRegistry.create( Identifier.of(MOD_ID, "synced_target"), builder -> builder .initializer(() -> false) .persistent(Codec.BOOL) - .syncWith(PacketCodecs.BOOLEAN.cast(), AttachmentSyncPredicate.targetOnly()) + .syncWith(PacketCodecs.BOOLEAN, AttachmentSyncPredicate.targetOnly()) ); public static final AttachmentType SYNCED_EXCEPT_TARGET = AttachmentRegistry.create( Identifier.of(MOD_ID, "synced_except_target"), builder -> builder .initializer(() -> false) .persistent(Codec.BOOL) - .syncWith(PacketCodecs.BOOLEAN.cast(), AttachmentSyncPredicate.allButTarget()) + .syncWith(PacketCodecs.BOOLEAN, AttachmentSyncPredicate.allButTarget()) ); public static final AttachmentType SYNCED_CREATIVE_ONLY = AttachmentRegistry.create( Identifier.of(MOD_ID, "synced_custom"), builder -> builder .initializer(() -> false) .persistent(Codec.BOOL) - .syncWith(PacketCodecs.BOOLEAN.cast(), (target, player) -> player.isCreative()) + .syncWith(PacketCodecs.BOOLEAN, (target, player) -> player.isCreative()) + ); + public static final AttachmentType SYNCED_ITEM = AttachmentRegistry.create( + Identifier.of(MOD_ID, "synced_item"), + builder -> builder + .initializer(() -> ItemStack.EMPTY) + .persistent(ItemStack.CODEC) + .syncWith(ItemStack.OPTIONAL_PACKET_CODEC, AttachmentSyncPredicate.all()) ); public static final SimpleCommandExceptionType TARGET_NOT_FOUND = new SimpleCommandExceptionType(Text.literal("Target not found")); @@ -196,6 +207,12 @@ public void onInitialize() { .then(buildCommandForKind("others_only", "all but self", SYNCED_EXCEPT_TARGET)) .then(buildCommandForKind("creative_only", "creative players only", SYNCED_CREATIVE_ONLY)) )); + + ServerEntityEvents.EQUIPMENT_CHANGE.register((livingEntity, equipmentSlot, previousStack, currentStack) -> { + if (equipmentSlot == EquipmentSlot.HEAD && livingEntity instanceof ServerPlayerEntity player) { + player.setAttached(SYNCED_ITEM, currentStack); + } + }); } private static LiteralArgumentBuilder buildCommandForKind(String id, String syncedWith, AttachmentType type) { diff --git a/fabric-data-attachment-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/attachment/client/AttachmentTestModClient.java b/fabric-data-attachment-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/attachment/client/AttachmentTestModClient.java index 3f8e7068ea..1d9f9c33ce 100644 --- a/fabric-data-attachment-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/attachment/client/AttachmentTestModClient.java +++ b/fabric-data-attachment-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/attachment/client/AttachmentTestModClient.java @@ -37,6 +37,7 @@ import net.minecraft.command.argument.UuidArgumentType; import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; import net.minecraft.text.Text; import net.minecraft.util.Colors; import net.minecraft.util.math.BlockPos; @@ -99,6 +100,11 @@ private static void displayClientAttachmentInfo( Text.literal("Synced-with-creative attachment: %s".formatted(attCustom)) .withColor(attCustom ? target instanceof PlayerEntity p && p.isCreative() ? Colors.GREEN : Colors.RED : Colors.WHITE) ); + ItemStack stack = target.getAttachedOrCreate(AttachmentTestMod.SYNCED_ITEM); + context.getSource().sendFeedback( + Text.literal("Synced-item attachment: %s".formatted(stack)) + .withColor(attOther ? target != MinecraftClient.getInstance().player ? Colors.GREEN : Colors.RED : Colors.WHITE) + ); } @Override From ac3f9a6e98202cc0fb2028cfea21fa416148644f Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Thu, 14 Nov 2024 11:23:41 +0000 Subject: [PATCH 5/6] Jdoc fix --- .../net/fabricmc/fabric/api/attachment/v1/AttachmentType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentType.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentType.java index 58559fe796..af86280fef 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentType.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentType.java @@ -56,7 +56,7 @@ * @param type of the attached data. It is encouraged for this to be an immutable type. * @see AttachmentRegistry * @see AttachmentRegistry.Builder#persistent(Codec) - * @see AttachmentRegistry.Builder#syncedWith(PacketCodec, AttachmentSyncPredicate) + * @see AttachmentRegistry.Builder#syncWith(PacketCodec, AttachmentSyncPredicate) */ @ApiStatus.NonExtendable @ApiStatus.Experimental From 770ca3a7597f993bd094f1dbe94d594c0edd4ace Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Thu, 14 Nov 2024 11:24:27 +0000 Subject: [PATCH 6/6] Jdoc fix --- .../net/fabricmc/fabric/api/attachment/v1/AttachmentType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentType.java b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentType.java index af86280fef..e189d65ceb 100644 --- a/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentType.java +++ b/fabric-data-attachment-api-v1/src/main/java/net/fabricmc/fabric/api/attachment/v1/AttachmentType.java @@ -56,7 +56,7 @@ * @param type of the attached data. It is encouraged for this to be an immutable type. * @see AttachmentRegistry * @see AttachmentRegistry.Builder#persistent(Codec) - * @see AttachmentRegistry.Builder#syncWith(PacketCodec, AttachmentSyncPredicate) + * @see AttachmentRegistry.Builder#syncWith(PacketCodec, AttachmentSyncPredicate) */ @ApiStatus.NonExtendable @ApiStatus.Experimental