Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttachmentType Registration Enhancements #4109

Open
wants to merge 12 commits into
base: 1.21.1
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:
* <ul>
* <li>{@link #create(Identifier, Consumer)}: attachments can be further configured by the supplied consumer.</li>
* <li>{@link #create(Identifier)}: attachments will be neither persistent nor auto-initialized.</li>
* <li>{@link #createDefaulted(Identifier, Supplier)}: attachments will be auto-initialized, but not persistent.</li>
* <li>{@link #createPersistent(Identifier, Codec)}: attachments will be persistent, but not auto-initialized.</li>
* </ul>
*
* <p>For finer control over the attachment type and its properties, use {@link AttachmentRegistry#builder()} to
* get a {@link Builder} instance.</p>
* <p>For finer control over the attachment type and its properties, use {@link #create(Identifier, Consumer)} to
* get and configure a {@link Builder} instance.</p>
*/
@ApiStatus.Experimental
public final class AttachmentRegistry {
private AttachmentRegistry() {
}

/**
* Creates <i>and registers</i> an attachment, configuring the builder used underneath.
*
* @param id the identifier of this attachment
* @param <A> the type of attached data
* @return the registered {@link AttachmentType} instance
*/
public static <A> AttachmentType<A> create(Identifier id, Consumer<Builder<A>> consumer) {
forgetmenot13579 marked this conversation as resolved.
Show resolved Hide resolved
AttachmentRegistry.Builder<A> builder = AttachmentRegistryImpl.builder();

consumer.accept(builder);

return builder.buildAndRegister(id);
}

/**
* Creates <i>and registers</i> an attachment. The data will not be persisted.
*
Expand All @@ -51,9 +67,7 @@ private AttachmentRegistry() {
* @return the registered {@link AttachmentType} instance
*/
public static <A> AttachmentType<A> create(Identifier id) {
Objects.requireNonNull(id, "identifier cannot be null");

return AttachmentRegistry.<A>builder().buildAndRegister(id);
return create(id, builder -> { });
}

/**
Expand All @@ -66,12 +80,7 @@ public static <A> AttachmentType<A> create(Identifier id) {
* @return the registered {@link AttachmentType} instance
*/
public static <A> AttachmentType<A> createDefaulted(Identifier id, Supplier<A> initializer) {
Objects.requireNonNull(id, "identifier cannot be null");
Objects.requireNonNull(initializer, "initializer cannot be null");

return AttachmentRegistry.<A>builder()
.initializer(initializer)
.buildAndRegister(id);
return create(id, builder -> builder.initializer(initializer));
}

/**
Expand All @@ -83,18 +92,17 @@ public static <A> AttachmentType<A> createDefaulted(Identifier id, Supplier<A> i
* @return the registered {@link AttachmentType} instance
*/
public static <A> AttachmentType<A> createPersistent(Identifier id, Codec<A> codec) {
Objects.requireNonNull(id, "identifier cannot be null");
Objects.requireNonNull(codec, "codec cannot be null");

return AttachmentRegistry.<A>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 <A> the type of the attached data
* @return a {@link Builder} instance
*/
@Deprecated
public static <A> Builder<A> builder() {
return AttachmentRegistryImpl.builder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static <A> void register(Identifier id, AttachmentType<A> 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);
}
}

Expand Down Expand Up @@ -83,6 +83,8 @@ public AttachmentRegistry.Builder<A> initializer(Supplier<A> initializer) {

@Override
public AttachmentType<A> buildAndRegister(Identifier id) {
Objects.requireNonNull(id, "identifier cannot be null");

var attachment = new AttachmentTypeImpl<>(id, defaultInitializer, persistenceCodec, copyOnDeath);
register(id, attachment);
return attachment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public class CommonAttachmentTests {
Codec.INT
);

private static final AttachmentType<WheelInfo> 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();
Expand Down Expand Up @@ -176,9 +182,8 @@ void testEntityCopy() {
AttachmentType<Boolean> notCopiedOnRespawn = AttachmentRegistry.create(
Identifier.of(MOD_ID, "not_copied_on_respawn")
);
AttachmentType<Boolean> copiedOnRespawn = AttachmentRegistry.<Boolean>builder()
.copyOnDeath()
.buildAndRegister(Identifier.of(MOD_ID, "copied_on_respawn"));
AttachmentType<Boolean> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<WheelInfo> 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));
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ public class AttachmentCopyTests implements FabricGameTest {
public static AttachmentType<IntSupplier> DUMMY = AttachmentRegistry.create(
Identifier.of(AttachmentTestMod.MOD_ID, "dummy")
);
public static AttachmentType<IntSupplier> COPY_ON_DEATH = AttachmentRegistry.<IntSupplier>builder()
.copyOnDeath()
.buildAndRegister(Identifier.of(AttachmentTestMod.MOD_ID, "copy_test"));
public static AttachmentType<IntSupplier> 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) {
Expand Down