generated from neoforged/MDK
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f121105
commit 19eb838
Showing
4 changed files
with
178 additions
and
11 deletions.
There are no files selected for viewing
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
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
43 changes: 43 additions & 0 deletions
43
src/main/java/com/example/examplemod/registry/EnderDeferredEntity.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 @@ | ||
package com.example.examplemod.registry; | ||
|
||
import com.example.examplemod.data.EnderDataProvider; | ||
import net.minecraft.resources.ResourceKey; | ||
import net.minecraft.tags.TagKey; | ||
import net.minecraft.world.entity.Entity; | ||
import net.minecraft.world.entity.EntityType; | ||
import net.neoforged.neoforge.registries.DeferredHolder; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.function.Supplier; | ||
|
||
public class EnderDeferredEntity<T extends Entity> extends DeferredHolder<EntityType<? extends Entity>, EntityType<T>> implements ITagagble<EntityType<?>>{ | ||
private final Set<TagKey<EntityType<?>>> entityTags = new HashSet<>(); | ||
private final Supplier<String> supplier = () -> get().getDescriptionId(); | ||
|
||
protected EnderDeferredEntity(ResourceKey<EntityType<? extends Entity>> key) { | ||
super(key); | ||
EnderDataProvider.getInstance(getId().getNamespace()).addTranslation(supplier, StringUtils.capitalize(getId().getPath().replace('_', ' '))); | ||
} | ||
|
||
@Override | ||
public Set<TagKey<EntityType<?>>> getTags() { | ||
return null; | ||
} | ||
|
||
public static <T extends Entity> EnderDeferredEntity<T> createBlockEntity(ResourceKey<EntityType<? extends Entity>> key) { | ||
return new EnderDeferredEntity<>(key); | ||
} | ||
|
||
@SafeVarargs | ||
public final EnderDeferredEntity<T> addEntityTags(TagKey<EntityType<?>>... tags) { | ||
this.entityTags.addAll(Set.of(tags)); | ||
return this; | ||
} | ||
|
||
public EnderDeferredEntity<T> setTranslation(String translation) { | ||
EnderDataProvider.getInstance(getId().getNamespace()).addTranslation(supplier, translation); | ||
return this; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/example/examplemod/registry/EnderEntityRegistry.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,65 @@ | ||
package com.example.examplemod.registry; | ||
|
||
import com.example.examplemod.data.EnderDataProvider; | ||
import com.example.examplemod.data.EnderTagProvider; | ||
import com.example.examplemod.mixin.DeferredRegisterAccessor; | ||
import net.minecraft.core.Registry; | ||
import net.minecraft.core.registries.BuiltInRegistries; | ||
import net.minecraft.resources.ResourceKey; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.entity.Entity; | ||
import net.minecraft.world.entity.EntityType; | ||
import net.minecraft.world.entity.MobCategory; | ||
import net.neoforged.bus.api.IEventBus; | ||
import net.neoforged.neoforge.registries.DeferredRegister; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
public class EnderEntityRegistry extends DeferredRegister<EntityType<?>> { | ||
protected EnderEntityRegistry(String namespace) { | ||
super(BuiltInRegistries.ENTITY_TYPE.key(), namespace); | ||
} | ||
|
||
private <T extends Entity> EnderDeferredEntity<T> registerEntity(String name, Function<ResourceLocation, EntityType<T>> func) { | ||
Objects.requireNonNull(name); | ||
Objects.requireNonNull(func); | ||
final ResourceLocation key = new ResourceLocation(getNamespace(), name); | ||
|
||
EnderDeferredEntity<T> ret = createEntityHolder(getRegistryKey(), key); | ||
|
||
if (((DeferredRegisterAccessor<EntityType<?>>)this).getEntries().putIfAbsent(ret, () -> func.apply(key)) != null) { | ||
throw new IllegalArgumentException("Duplicate registration " + name); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
public <T extends Entity> EnderDeferredEntity<T> registerEntity(String name, EntityType.EntityFactory<T> sup, MobCategory category) { | ||
return this.registerEntity(name, () -> EntityType.Builder.of(sup, category).build(name)); | ||
} | ||
|
||
public <T extends Entity> EnderDeferredEntity<T> registerEntity(String name, Supplier<EntityType<T>> supplier) { | ||
return this.registerEntity(name, key -> supplier.get()); | ||
} | ||
|
||
protected <T extends Entity> EnderDeferredEntity<T> createEntityHolder(ResourceKey<? extends Registry<EntityType<?>>> registryKey, ResourceLocation key) { | ||
return new EnderDeferredEntity<>(ResourceKey.create(registryKey, key)); | ||
} | ||
|
||
public static EnderEntityRegistry create(String modid) { | ||
return new EnderEntityRegistry(modid); | ||
} | ||
|
||
@Override | ||
public void register(IEventBus bus) { | ||
super.register(bus); | ||
onGatherData(); | ||
} | ||
|
||
private void onGatherData() { | ||
EnderDataProvider provider = EnderDataProvider.getInstance(getNamespace()); | ||
provider.addServerSubProvider((packOutput, existingFileHelper, lookup) -> new EnderTagProvider<>(packOutput, this.getRegistryKey(), b -> b.builtInRegistryHolder().key(), lookup, getNamespace(), existingFileHelper, this)); | ||
} | ||
} |