Something went wrong. Try again.
Library for my mods and adding supporter perks
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110/* * 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. * * <p>To quickly create attachment types, 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> * * <h2>Implementation note</h2> * * <p>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 <i>and registers</i> 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 <A> the type of attached data * @return the registered {@link GuitaAttachmentType} instance */ public static <A> GuitaAttachmentType<A> create(Identifier id, Consumer<GuitaAttachmentBuilder<A>> consumer) { return CommonAbstraction.get().createGuitaAttachmentType(id, consumer); }
/** * Creates <i>and registers</i> an attachment. The data will not be persisted or synchronized. * * @param id the identifier of this attachment * @param <A> the type of attached data * @return the registered {@link GuitaAttachmentType} instance */ public static <A> GuitaAttachmentType<A> create(Identifier id) { return create(id, builder -> {}); }
/** * Creates <i>and registers</i> 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 <A> the type of attached data * @return the registered {@link GuitaAttachmentType} instance */ public static <A> GuitaAttachmentType<A> createDefaulted(Identifier id, Supplier<A> initializer) { return create(id, builder -> builder.initializer(initializer)); }
/** * Creates <i>and registers</i> an attachment that will persist across server restarts. * * @param id the identifier of this attachment * @param codec the codec used for (de)serialization * @param <A> the type of attached data * @return the registered {@link GuitaAttachmentType} instance */ public static <A> GuitaAttachmentType<A> createPersistent(Identifier id, Codec<A> codec) { return create(id, builder -> builder.persistent(codec)); }}