Skip to content

Commit

Permalink
Add specialized deferred register for AttachmentType
Browse files Browse the repository at this point in the history
  • Loading branch information
ApexModder committed Sep 5, 2024
1 parent c92fc44 commit 34055f2
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.registries.deferred;

import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.neoforged.neoforge.attachment.AttachmentType;
import net.neoforged.neoforge.registries.NeoForgeRegistries;

/**
* Special {@link DeferredHolder} for {@link AttachmentType AttachmentTypes}.
*
* @param <TData> The specific data type.
*/
public class DeferredAttachmentType<TData> extends DeferredHolder<AttachmentType<?>, AttachmentType<TData>> {
protected DeferredAttachmentType(ResourceKey<AttachmentType<?>> key) {
super(key);
}

/**
* Creates a new {@link DeferredHolder} targeting the specified {@link AttachmentType}.
*
* @param <TData> The type of the target {@link AttachmentType}.
* @param registryKey The resource key of the target {@link AttachmentType}.
*/
public static <TData extends BlockEntity> DeferredAttachmentType<TData> createAttachmentType(ResourceKey<AttachmentType<?>> registryKey) {
return new DeferredAttachmentType<>(registryKey);
}

/**
* Creates a new {@link DeferredHolder} targeting the {@link AttachmentType} with the specified name.
*
* @param <TData> The type of the target {@link AttachmentType}.
* @param registryName The name of the target {@link AttachmentType}.
*/
public static <TData extends BlockEntity> DeferredAttachmentType<TData> createAttachmentType(ResourceLocation registryName) {
return createAttachmentType(ResourceKey.create(NeoForgeRegistries.Keys.ATTACHMENT_TYPES, registryName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.registries.deferred;

import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import net.minecraft.core.Registry;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.attachment.AttachmentType;
import net.neoforged.neoforge.attachment.IAttachmentHolder;
import net.neoforged.neoforge.common.util.INBTSerializable;
import net.neoforged.neoforge.registries.NeoForgeRegistries;

/**
* Specialized DeferredRegister for {@link AttachmentType AttachmentTypes} that uses the specialized {@link DeferredAttachmentType} as the return type for {@link #register}.
*/
public class DeferredAttachmentTypes extends DeferredRegister<AttachmentType<?>> {
protected DeferredAttachmentTypes(String namespace) {
super(NeoForgeRegistries.Keys.ATTACHMENT_TYPES, namespace);
}

@Override
protected <TAttachmentType extends AttachmentType<?>> DeferredHolder<AttachmentType<?>, TAttachmentType> createHolder(ResourceKey<? extends Registry<AttachmentType<?>>> registryType, ResourceLocation registryName) {
return (DeferredHolder<AttachmentType<?>, TAttachmentType>) DeferredAttachmentType.createAttachmentType(ResourceKey.create(registryType, registryName));
}

/**
* Adds a new attachment type to the list of entries to be registered and returns a {@link DeferredAttachmentType} that will be populated with the created entry automatically.
*
* @param identifier The new entry's identifier. It will automatically have the {@linkplain #getNamespace() namespace} prefixed.
* @param factory A factory for the new entry. The factory should not cache the created entry.
* @param builderAction Action to be invoked with the builder during registration.
* @return A {@link DeferredAttachmentType} that will track updates from the registry for this entry.
*/
public <TData> DeferredAttachmentType<TData> registerAttachmentType(String identifier, Function<IAttachmentHolder, TData> factory, UnaryOperator<AttachmentType.Builder<TData>> builderAction) {
return (DeferredAttachmentType<TData>) register(identifier, () -> builderAction.apply(AttachmentType.builder(factory)).build());
}

/**
* Adds a new attachment type to the list of entries to be registered and returns a {@link DeferredAttachmentType} that will be populated with the created entry automatically.
*
* @param identifier The new entry's identifier. It will automatically have the {@linkplain #getNamespace() namespace} prefixed.
* @param factory A factory for the new entry. The factory should not cache the created entry.
* @return A {@link DeferredAttachmentType} that will track updates from the registry for this entry.
*/
public <TData> DeferredAttachmentType<TData> registerAttachmentType(String identifier, Function<IAttachmentHolder, TData> factory) {
return registerAttachmentType(identifier, factory, UnaryOperator.identity());
}

/**
* Adds a new attachment type to the list of entries to be registered and returns a {@link DeferredAttachmentType} that will be populated with the created entry automatically.
*
* @param identifier The new entry's identifier. It will automatically have the {@linkplain #getNamespace() namespace} prefixed.
* @param factory A factory for the new entry. The factory should not cache the created entry.
* @param builderAction Action to be invoked with the builder during registration.
* @return A {@link DeferredAttachmentType} that will track updates from the registry for this entry.
*/
public <TData> DeferredAttachmentType<TData> registerAttachmentType(String identifier, Supplier<TData> factory, UnaryOperator<AttachmentType.Builder<TData>> builderAction) {
return registerAttachmentType(identifier, holder -> factory.get(), builderAction);
}

/**
* Adds a new attachment type to the list of entries to be registered and returns a {@link DeferredAttachmentType} that will be populated with the created entry automatically.
*
* @param identifier The new entry's identifier. It will automatically have the {@linkplain #getNamespace() namespace} prefixed.
* @param factory A factory for the new entry. The factory should not cache the created entry.
* @return A {@link DeferredAttachmentType} that will track updates from the registry for this entry.
*/
public <TData> DeferredAttachmentType<TData> registerAttachmentType(String identifier, Supplier<TData> factory) {
return registerAttachmentType(identifier, factory, UnaryOperator.identity());
}

/**
* Adds a new serializable attachment type to the list of entries to be registered and returns a {@link DeferredAttachmentType} that will be populated with the created entry automatically.
*
* @param identifier The new entry's identifier. It will automatically have the {@linkplain #getNamespace() namespace} prefixed.
* @param factory A factory for the new entry. The factory should not cache the created entry.
* @param builderAction Action to be invoked with the builder during registration.
* @return A {@link DeferredAttachmentType} that will track updates from the registry for this entry.
*/
public <TData extends INBTSerializable<TTag>, TTag extends Tag> DeferredAttachmentType<TData> registerSerializableAttachmentType(String identifier, Function<IAttachmentHolder, TData> factory, UnaryOperator<AttachmentType.Builder<TData>> builderAction) {
return (DeferredAttachmentType<TData>) register(identifier, () -> builderAction.apply(AttachmentType.serializable(factory)).build());
}

/**
* Adds a new serializable attachment type to the list of entries to be registered and returns a {@link DeferredAttachmentType} that will be populated with the created entry automatically.
*
* @param identifier The new entry's identifier. It will automatically have the {@linkplain #getNamespace() namespace} prefixed.
* @param factory A factory for the new entry. The factory should not cache the created entry.
* @return A {@link DeferredAttachmentType} that will track updates from the registry for this entry.
*/
public <TData extends INBTSerializable<TTag>, TTag extends Tag> DeferredAttachmentType<TData> registerSerializableAttachmentType(String identifier, Function<IAttachmentHolder, TData> factory) {
return registerSerializableAttachmentType(identifier, factory, UnaryOperator.identity());
}

/**
* Adds a new serializable attachment type to the list of entries to be registered and returns a {@link DeferredAttachmentType} that will be populated with the created entry automatically.
*
* @param identifier The new entry's identifier. It will automatically have the {@linkplain #getNamespace() namespace} prefixed.
* @param factory A factory for the new entry. The factory should not cache the created entry.
* @param builderAction Action to be invoked with the builder during registration.
* @return A {@link DeferredAttachmentType} that will track updates from the registry for this entry.
*/
public <TData extends INBTSerializable<TTag>, TTag extends Tag> DeferredAttachmentType<TData> registerSerializableAttachmentType(String identifier, Supplier<TData> factory, UnaryOperator<AttachmentType.Builder<TData>> builderAction) {
return registerSerializableAttachmentType(identifier, holder -> factory.get(), builderAction);
}

/**
* Adds a new serializable attachment type to the list of entries to be registered and returns a {@link DeferredAttachmentType} that will be populated with the created entry automatically.
*
* @param identifier The new entry's identifier. It will automatically have the {@linkplain #getNamespace() namespace} prefixed.
* @param factory A factory for the new entry. The factory should not cache the created entry.
* @return A {@link DeferredAttachmentType} that will track updates from the registry for this entry.
*/
public <TData extends INBTSerializable<TTag>, TTag extends Tag> DeferredAttachmentType<TData> registerSerializableAttachmentType(String identifier, Supplier<TData> factory) {
return registerSerializableAttachmentType(identifier, factory, UnaryOperator.identity());
}

/**
* Factory for a specialized DeferredRegister for {@link AttachmentType AttachmentTypes}.
*
* @param namespace The namespace for all objects registered to this DeferredRegister
*/
public static DeferredAttachmentTypes createAttachmentTypes(String namespace) {
return new DeferredAttachmentTypes(namespace);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import net.neoforged.neoforge.attachment.AttachmentType;
import net.neoforged.neoforge.registries.NeoForgeRegistries;
import net.neoforged.neoforge.registries.deferred.DeferredRegister;

public class DeferredAttachmentTypes extends DeferredRegister<AttachmentType<?>> {
public class DeferredAttachmentTypes extends net.neoforged.neoforge.registries.deferred.DeferredAttachmentTypes {
public DeferredAttachmentTypes(String namespace) {
super(NeoForgeRegistries.Keys.ATTACHMENT_TYPES, namespace);
super(namespace);
}

public <T> AttachmentType<T> registerSimpleAttachment(String name, Supplier<T> defaultValue) {
Expand Down

0 comments on commit 34055f2

Please sign in to comment.