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 @@ -43,6 +43,21 @@ 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
var builder = AttachmentRegistry.<A>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 +66,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 +79,9 @@ 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 -> {
forgetmenot13579 marked this conversation as resolved.
Show resolved Hide resolved
builder.initializer(initializer);
});
}

/**
Expand All @@ -83,10 +93,9 @@ 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 -> {
forgetmenot13579 marked this conversation as resolved.
Show resolved Hide resolved
builder.persistent(codec);
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import java.util.function.Supplier;

import com.mojang.serialization.Codec;

import net.fabricmc.loader.api.FabricLoader;

import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -39,7 +42,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 +86,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 @@ -33,6 +33,7 @@
import java.util.function.UnaryOperator;

import com.mojang.serialization.Codec;

forgetmenot13579 marked this conversation as resolved.
Show resolved Hide resolved
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;

import net.minecraft.util.Identifier;
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));

public static final AttachmentType<WheelInfo> ATTACHMENT = AttachmentRegistry.create(Identifier.of(AttachmentTestMod.MOD_ID, "wheel_info"),
attachment -> attachment.initializer(() -> new WheelInfo(100, 5432, 37))
.persistent(WheelInfo.CODEC)
);
forgetmenot13579 marked this conversation as resolved.
Show resolved Hide resolved
}