-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add specialized deferred register for AttachmentType
- Loading branch information
1 parent
c92fc44
commit 34055f2
Showing
3 changed files
with
178 additions
and
4 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/main/java/net/neoforged/neoforge/registries/deferred/DeferredAttachmentType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
src/main/java/net/neoforged/neoforge/registries/deferred/DeferredAttachmentTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters