/* * Copyright 2026 macuguita * * Licensed under the EUPL, Version 1.2 or – as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * * https://joinup.ec.europa.eu/software/page/eupl * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. */ package com.macuguita.lib.api.attachment; import java.util.function.Consumer; import java.util.function.Supplier; import net.minecraft.resources.Identifier; import com.mojang.serialization.Codec; import com.macuguita.lib.impl.platform.CommonAbstraction; /** * Class used to create and register {@link GuitaAttachmentType}s, mirroring Fabric's data * attachment API. * *

To quickly create attachment types, use one of the various {@code createXXX} methods: * *

* *

Implementation note

* *

On Fabric this delegates to Fabric API's {@code AttachmentRegistry}, which auto-registers the * type. On NeoForge attachment types must be registered to a registry, so this uses a * {@code DeferredRegister} attached to macu Lib's own mod event bus. Because the bus is resolved * independently of the active mod container, attachment types may be created from a {@code yumi:init} * initializer on NeoForge (including Fabric mods loaded through Sinytra Connector), just like on * Fabric. * * @see GuitaAttachmentType * @see GuitaAttachmentTarget */ public final class GuitaAttachmentRegistry { private GuitaAttachmentRegistry() {} /** * Creates and registers an attachment using a {@linkplain GuitaAttachmentBuilder builder}. * * @param id the identifier of this attachment * @param consumer a lambda that configures a {@link GuitaAttachmentBuilder} for this attachment * type * @param the type of attached data * @return the registered {@link GuitaAttachmentType} instance */ public static GuitaAttachmentType create(Identifier id, Consumer> consumer) { return CommonAbstraction.get().createGuitaAttachmentType(id, consumer); } /** * Creates and registers an attachment. The data will not be persisted or synchronized. * * @param id the identifier of this attachment * @param the type of attached data * @return the registered {@link GuitaAttachmentType} instance */ public static GuitaAttachmentType create(Identifier id) { return create(id, builder -> {}); } /** * Creates and registers an attachment that will be automatically initialized with a * default value when an attachment does not exist on a given target, using * {@link GuitaAttachmentTarget#getAttachedOrCreate(GuitaAttachmentType)}. * * @param id the identifier of this attachment * @param initializer the initializer used to provide a default value * @param the type of attached data * @return the registered {@link GuitaAttachmentType} instance */ public static GuitaAttachmentType createDefaulted(Identifier id, Supplier initializer) { return create(id, builder -> builder.initializer(initializer)); } /** * Creates and registers an attachment that will persist across server restarts. * * @param id the identifier of this attachment * @param codec the codec used for (de)serialization * @param the type of attached data * @return the registered {@link GuitaAttachmentType} instance */ public static GuitaAttachmentType createPersistent(Identifier id, Codec codec) { return create(id, builder -> builder.persistent(codec)); } }