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 959c299ac3..e325bfaa99 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 @@ -16,7 +16,7 @@ package net.fabricmc.fabric.api.attachment.v1; -import java.util.Objects; +import java.util.function.Consumer; import java.util.function.Supplier; import com.mojang.serialization.Codec; @@ -30,19 +30,35 @@ * Class used to create and register {@link AttachmentType}s. To quickly create {@link AttachmentType}s, use one of the various * {@code createXXX} methods: * * - *

For finer control over the attachment type and its properties, use {@link AttachmentRegistry#builder()} to - * get a {@link Builder} instance.

+ *

For finer control over the attachment type and its properties, use {@link #create(Identifier, Consumer)} to + * get and configure a {@link Builder} instance.

*/ @ApiStatus.Experimental public final class AttachmentRegistry { private AttachmentRegistry() { } + /** + * Creates and registers an attachment, configuring the builder used underneath. + * + * @param id the identifier of this attachment + * @param the type of attached data + * @return the registered {@link AttachmentType} instance + */ + public static AttachmentType create(Identifier id, Consumer> consumer) { + AttachmentRegistry.Builder builder = AttachmentRegistryImpl.builder(); + + consumer.accept(builder); + + return builder.buildAndRegister(id); + } + /** * Creates and registers an attachment. The data will not be persisted. * @@ -51,9 +67,7 @@ private AttachmentRegistry() { * @return the registered {@link AttachmentType} instance */ public static AttachmentType create(Identifier id) { - Objects.requireNonNull(id, "identifier cannot be null"); - - return AttachmentRegistry.builder().buildAndRegister(id); + return create(id, builder -> { }); } /** @@ -66,12 +80,7 @@ public static AttachmentType create(Identifier id) { * @return the registered {@link AttachmentType} instance */ public static AttachmentType createDefaulted(Identifier id, Supplier initializer) { - Objects.requireNonNull(id, "identifier cannot be null"); - Objects.requireNonNull(initializer, "initializer cannot be null"); - - return AttachmentRegistry.builder() - .initializer(initializer) - .buildAndRegister(id); + return create(id, builder -> builder.initializer(initializer)); } /** @@ -83,18 +92,17 @@ public static AttachmentType createDefaulted(Identifier id, Supplier i * @return the registered {@link AttachmentType} instance */ public static AttachmentType createPersistent(Identifier id, Codec codec) { - Objects.requireNonNull(id, "identifier cannot be null"); - Objects.requireNonNull(codec, "codec cannot be null"); - - return AttachmentRegistry.builder().persistent(codec).buildAndRegister(id); + return create(id, builder -> builder.persistent(codec)); } /** - * Creates a {@link Builder}, that gives finer control over the attachment's properties. + * Creates a {@link Builder}, that gives finer control over the attachment's properties. Calling this method + * directly is not recommended, as it requires explicit type parameters. {@link #create} should be used instead. * * @param the type of the attached data * @return a {@link Builder} instance */ + @Deprecated public static Builder builder() { return AttachmentRegistryImpl.builder(); } 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 d2be65d7c1..f1ce874659 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 @@ -39,7 +39,7 @@ public static void register(Identifier id, AttachmentType attachmentType) AttachmentType existing = attachmentRegistry.put(id, attachmentType); if (existing != null) { - LOGGER.warn("Encountered duplicate type registration for id " + id); + LOGGER.warn("Encountered duplicate type registration for id {}", id); } } @@ -83,6 +83,8 @@ public AttachmentRegistry.Builder initializer(Supplier initializer) { @Override public AttachmentType buildAndRegister(Identifier id) { + Objects.requireNonNull(id, "identifier cannot be null"); + var attachment = new AttachmentTypeImpl<>(id, defaultInitializer, persistenceCodec, copyOnDeath); register(id, attachment); return attachment; diff --git a/fabric-data-attachment-api-v1/src/test/java/net/fabricmc/fabric/test/attachment/CommonAttachmentTests.java b/fabric-data-attachment-api-v1/src/test/java/net/fabricmc/fabric/test/attachment/CommonAttachmentTests.java index f0e0ddaa1c..789f5cec64 100644 --- a/fabric-data-attachment-api-v1/src/test/java/net/fabricmc/fabric/test/attachment/CommonAttachmentTests.java +++ b/fabric-data-attachment-api-v1/src/test/java/net/fabricmc/fabric/test/attachment/CommonAttachmentTests.java @@ -70,6 +70,12 @@ public class CommonAttachmentTests { Codec.INT ); + private static final AttachmentType WHEEL = AttachmentRegistry.create(Identifier.of(AttachmentTestMod.MOD_ID, "wheel_info"), + attachment -> attachment + .initializer(() -> new WheelInfo(100, 5432, 37)) + .persistent(WheelInfo.CODEC) + ); + @BeforeAll static void beforeAll() { SharedConstants.createGameVersion(); @@ -176,9 +182,8 @@ void testEntityCopy() { AttachmentType notCopiedOnRespawn = AttachmentRegistry.create( Identifier.of(MOD_ID, "not_copied_on_respawn") ); - AttachmentType copiedOnRespawn = AttachmentRegistry.builder() - .copyOnDeath() - .buildAndRegister(Identifier.of(MOD_ID, "copied_on_respawn")); + AttachmentType copiedOnRespawn = AttachmentRegistry.create(Identifier.of(MOD_ID, "copied_on_respawn"), + AttachmentRegistry.Builder::copyOnDeath); Entity original = mock(Entity.class, CALLS_REAL_METHODS); original.setAttached(notCopiedOnRespawn, true); diff --git a/fabric-data-attachment-api-v1/src/test/java/net/fabricmc/fabric/test/attachment/WheelInfo.java b/fabric-data-attachment-api-v1/src/test/java/net/fabricmc/fabric/test/attachment/WheelInfo.java new file mode 100644 index 0000000000..2ce0a54f32 --- /dev/null +++ b/fabric-data-attachment-api-v1/src/test/java/net/fabricmc/fabric/test/attachment/WheelInfo.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.test.attachment; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; + +import net.minecraft.util.dynamic.Codecs; + +public record WheelInfo(float wheelDiameter, float tireDiameter, float tireThickness) { + public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( + Codecs.POSITIVE_FLOAT.fieldOf("wheelDiameter").forGetter(WheelInfo::wheelDiameter), + Codecs.POSITIVE_FLOAT.fieldOf("tireDiameter").forGetter(WheelInfo::tireDiameter), + Codecs.POSITIVE_FLOAT.fieldOf("tireThickness").forGetter(WheelInfo::tireThickness) + ).apply(instance, WheelInfo::new)); +} diff --git a/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/gametest/AttachmentCopyTests.java b/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/gametest/AttachmentCopyTests.java index fa39158ef3..5e2529965a 100644 --- a/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/gametest/AttachmentCopyTests.java +++ b/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/gametest/AttachmentCopyTests.java @@ -41,9 +41,10 @@ public class AttachmentCopyTests implements FabricGameTest { public static AttachmentType DUMMY = AttachmentRegistry.create( Identifier.of(AttachmentTestMod.MOD_ID, "dummy") ); - public static AttachmentType COPY_ON_DEATH = AttachmentRegistry.builder() - .copyOnDeath() - .buildAndRegister(Identifier.of(AttachmentTestMod.MOD_ID, "copy_test")); + public static AttachmentType COPY_ON_DEATH = AttachmentRegistry.create( + Identifier.of(AttachmentTestMod.MOD_ID, "copy_test"), + AttachmentRegistry.Builder::copyOnDeath + ); @GameTest(templateName = FabricGameTest.EMPTY_STRUCTURE) public void testCrossWorldTeleport(TestContext context) {