Skip to content

Commit

Permalink
add entity and block entity
Browse files Browse the repository at this point in the history
  • Loading branch information
ferriarnus committed Dec 30, 2023
1 parent f121105 commit 19eb838
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
package com.example.examplemod.registry;

import com.example.examplemod.data.EnderDataProvider;
import com.example.examplemod.data.EnderItemModelProvider;
import com.example.examplemod.data.EnderTagProvider;
import com.example.examplemod.events.ColorEvents;
import com.example.examplemod.mixin.DeferredRegisterAccessor;
import com.mojang.datafixers.types.Func;
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.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;

public class EnderBlockEntityRegistry extends DeferredRegister<BlockEntityType<?>> {
Expand All @@ -17,20 +31,49 @@ protected EnderBlockEntityRegistry(String namespace) {
}

public <T extends BlockEntity> EnderDeferredBlockEntity<T> registerBlockEntity(String name, BlockEntityType.BlockEntitySupplier<T> sup, Block... blocks) {
DeferredHolder<BlockEntityType<?>, BlockEntityType<T>> holder = this.register(name, () -> BlockEntityType.Builder.of(sup, blocks).build(null));
return EnderDeferredBlockEntity.createBlockEntity(holder);
return this.registerBlockEntity(name, () -> BlockEntityType.Builder.of(sup, blocks).build(null));
}

private <T extends BlockEntity> EnderDeferredBlockEntity<T> registerBlockEntity(String name, Function<ResourceLocation, BlockEntityType<T>> func) {
Objects.requireNonNull(name);
Objects.requireNonNull(func);
final ResourceLocation key = new ResourceLocation(getNamespace(), name);

EnderDeferredBlockEntity<T> ret = createBlockEntityHolder(getRegistryKey(), key);

if (((DeferredRegisterAccessor<BlockEntityType<?>>)this).getEntries().putIfAbsent(ret, () -> func.apply(key)) != null) {
throw new IllegalArgumentException("Duplicate registration " + name);
}

return ret;
}

//TODO steam mess, can this be cleaner?
@SafeVarargs
public final <T extends BlockEntity> EnderDeferredBlockEntity<T> registerBlockEntity(String name, BlockEntityType.BlockEntitySupplier<T> sup,
Supplier<? extends Block>... blocks) {
List<? extends Block> blockList = Arrays.stream(blocks).map(Supplier::get).toList();
DeferredHolder<BlockEntityType<?>, BlockEntityType<T>> holder = this.register(name, () -> BlockEntityType.Builder.of(sup, blockList.toArray(new Block[] {})).build(null));
return EnderDeferredBlockEntity.createBlockEntity(holder);
public final <T extends BlockEntity> EnderDeferredBlockEntity<T> registerBlockEntity(String name, BlockEntityType.BlockEntitySupplier<T> sup, Supplier<? extends Block>... blocks) {
return this.registerBlockEntity(name, () -> BlockEntityType.Builder.of(sup, Arrays.stream(blocks).map(Supplier::get).toList().toArray(new Block[] {})).build(null));
}

public <T extends BlockEntity> EnderDeferredBlockEntity<T> registerBlockEntity(String name, Supplier<BlockEntityType<T>> supplier) {
return this.registerBlockEntity(name, key -> supplier.get());
}

protected <T extends BlockEntity> EnderDeferredBlockEntity<T> createBlockEntityHolder(ResourceKey<? extends Registry<BlockEntityType<?>>> registryKey, ResourceLocation key) {
return new EnderDeferredBlockEntity<>(ResourceKey.create(registryKey, key));
}

public static EnderBlockEntityRegistry create(String modid) {
public static <T extends BlockEntity> EnderBlockEntityRegistry create(String modid) {
return new EnderBlockEntityRegistry(modid);
}

@Override
public void register(IEventBus bus) {
super.register(bus);
this.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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.registries.DeferredHolder;

import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.Set;

public class EnderDeferredBlockEntity<T extends BlockEntity> extends DeferredHolder<BlockEntityType<? extends BlockEntity>, BlockEntityType<T>> {
public class EnderDeferredBlockEntity<T extends BlockEntity> extends DeferredHolder<BlockEntityType<? extends BlockEntity>, BlockEntityType<T>> implements ITagagble<BlockEntityType<?>> {
protected Set<TagKey<BlockEntityType<?>>> BlockEntityTags = new HashSet<>();

/**
* Creates a new DeferredHolder with a ResourceKey.
Expand All @@ -26,12 +31,23 @@ protected EnderDeferredBlockEntity(ResourceKey<BlockEntityType<? extends BlockEn
super(key);
}

public static <T extends BlockEntity> EnderDeferredBlockEntity<T> createBlockEntity(DeferredHolder<BlockEntityType<?>, BlockEntityType<T>> holder) {
return new EnderDeferredBlockEntity<>(holder.getKey());
public static <T extends BlockEntity> EnderDeferredBlockEntity<T> createBlockEntity(ResourceKey<BlockEntityType<? extends BlockEntity>> key) {
return new EnderDeferredBlockEntity<>(key);
}

@Nullable
public T create(BlockPos pos, BlockState state) {
return this.get().create(pos, state);
}

@Override
public Set<TagKey<BlockEntityType<?>>> getTags() {
return this.BlockEntityTags;
}

@SafeVarargs
public final EnderDeferredBlockEntity<T> addBlockEntityTagsTags(TagKey<BlockEntityType<?>>... tags) {
BlockEntityTags.addAll(Set.of(tags));
return this;
}
}
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;
}
}
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));
}
}

0 comments on commit 19eb838

Please sign in to comment.