Skip to content

Commit

Permalink
start making it a single instanced setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ferriarnus committed Feb 26, 2024
1 parent a1843da commit e7dcd85
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 52 deletions.
5 changes: 3 additions & 2 deletions src/generated/resources/assets/examplemod/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"block.examplemod.example_block": "Test Example Block",
"block.examplemod.example_fluid": "Example fluid",
"fluid_type.examplemod.example_fluid": "Example fluid",
"block.examplemod.example_fluid": "Example Fluid",
"fluid_type.examplemod.example_fluid": "Example Fluid",
"item.examplemod.example_fluid_bucket": "Example Fluid Bucket",
"item.examplemod.example_item": "Test Example Item",
"itemGroup.examplemod.example_tab": "Example"
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@
import net.neoforged.neoforge.registries.DeferredHolder;

import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.function.BiConsumer;

public class RegiliteBlockLootProvider extends BlockLootSubProvider {

private final BlockRegistry registry;
private final List<DeferredHolder<Block, ? extends Block>> registered;

public RegiliteBlockLootProvider(Set<Item> explosionResistant, BlockRegistry registry) {
public RegiliteBlockLootProvider(Set<Item> explosionResistant, List<DeferredHolder<Block, ? extends Block>> registered) {
super(explosionResistant, FeatureFlags.REGISTRY.allFlags());
this.registry = registry;
this.registered = registered;
}

@Override
protected void generate() {
for (DeferredHolder<Block, ? extends Block> block : registry.getEntries()) {
for (DeferredHolder<Block, ? extends Block> block : registered) {
if (block instanceof RegiliteBlock) {
if (block.get().getLootTable() == BuiltInLootTables.EMPTY) {
continue;
Expand All @@ -48,7 +49,7 @@ public void generate(BiConsumer<ResourceLocation, LootTable.Builder> p_249322_)
this.generate();
Set<ResourceLocation> set = new HashSet<>();

for(DeferredHolder<Block, ? extends Block> block : registry.getEntries()) {
for(DeferredHolder<Block, ? extends Block> block : registered) {
if (block.get().isEnabled(this.enabledFeatures)) {
ResourceLocation resourcelocation = block.get().getLootTable();
if (resourcelocation != BuiltInLootTables.EMPTY && set.add(resourcelocation)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@
import net.neoforged.neoforge.common.data.ExistingFileHelper;
import net.neoforged.neoforge.registries.DeferredHolder;

import java.util.List;
import java.util.function.BiConsumer;

public class RegiliteBlockStateProvider extends BlockStateProvider {
private final BlockRegistry registry;
private final List<DeferredHolder<Block, ? extends Block>> registered;

public RegiliteBlockStateProvider(PackOutput output, String modid, ExistingFileHelper exFileHelper, BlockRegistry registry) {
public RegiliteBlockStateProvider(PackOutput output, String modid, ExistingFileHelper exFileHelper, List<DeferredHolder<Block, ? extends Block>> registered) {
super(output, modid, exFileHelper);
this.registry = registry;
this.registered = registered;
}

@Override
protected void registerStatesAndModels() {
for (DeferredHolder<Block, ? extends Block> block : registry.getEntries()) {
for (DeferredHolder<Block, ? extends Block> block : registered) {
if (block instanceof RegiliteBlock) {
BiConsumer<BlockStateProvider, DataGenContext<Block, Block>> blockstate = ((RegiliteBlock<Block>) block).getBlockStateProvider();
if (blockstate != null) {
Expand Down
35 changes: 33 additions & 2 deletions src/main/java/com/enderio/regilite/data/RegiliteDataProvider.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
package com.enderio.regilite.data;

import com.enderio.regilite.registry.BlockEntityRegistry;
import com.enderio.regilite.registry.BlockRegistry;
import com.enderio.regilite.registry.EntityRegistry;
import com.enderio.regilite.registry.FluidRegister;
import com.enderio.regilite.registry.ItemRegistry;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataProvider;
import net.minecraft.data.PackOutput;
import net.minecraft.data.loot.LootTableProvider;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EntityType;
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.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.common.data.ExistingFileHelper;
import net.neoforged.neoforge.data.event.GatherDataEvent;
import org.apache.commons.lang3.function.TriFunction;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

Expand Down Expand Up @@ -86,14 +102,29 @@ public String getName() {
}

void onGatherData(GatherDataEvent event) {
PackOutput packOutput = event.getGenerator().getPackOutput();
ExistingFileHelper existingFileHelper = event.getExistingFileHelper();
CompletableFuture<HolderLookup.Provider> lookupProvider = event.getLookupProvider();

if (event.includeServer()) {
for (TriFunction<PackOutput, ExistingFileHelper, CompletableFuture<HolderLookup.Provider>, DataProvider> function : this.serverSubProviderConsumers) {
this.subProviders.add(function.apply(event.getGenerator().getPackOutput(), event.getExistingFileHelper(), event.getLookupProvider()));
this.subProviders.add(function.apply(packOutput, existingFileHelper, lookupProvider));
}
}
RegiliteLangProvider enUs = new RegiliteLangProvider(event.getGenerator().getPackOutput(), this.modid, "en_us");
RegiliteLangProvider enUs = new RegiliteLangProvider(packOutput, this.modid, "en_us");
enUs.add(this.langEntries);
this.subProviders.add(enUs);

this.subProviders.add(new RegiliteTagProvider<>(packOutput, Registries.BLOCK, b -> b.builtInRegistryHolder().key(), lookupProvider, modid, existingFileHelper, BlockRegistry.getRegistered()));
this.subProviders.add(new RegiliteTagProvider<>(packOutput, Registries.ITEM, b -> b.builtInRegistryHolder().key(), lookupProvider, modid, existingFileHelper, ItemRegistry.getRegistered()));
this.subProviders.add(new RegiliteTagProvider.FluidTagProvider(packOutput, Registries.FLUID, b -> b.builtInRegistryHolder().key(), lookupProvider, modid, existingFileHelper, FluidRegister.getRegistered()));
this.subProviders.add(new RegiliteTagProvider<>(packOutput, Registries.BLOCK_ENTITY_TYPE, b -> b.builtInRegistryHolder().key(), lookupProvider, modid, existingFileHelper, BlockEntityRegistry.getRegistered()));
this.subProviders.add(new RegiliteTagProvider<>(packOutput, Registries.ENTITY_TYPE, b -> b.builtInRegistryHolder().key(), lookupProvider, modid, existingFileHelper, EntityRegistry.getRegistered()));

this.subProviders.add(new RegiliteBlockStateProvider(packOutput, modid, existingFileHelper, BlockRegistry.getRegistered()));
this.subProviders.add(new LootTableProvider(packOutput, Collections.emptySet(), List.of(new LootTableProvider.SubProviderEntry(() -> new RegiliteBlockLootProvider(Set.of(), BlockRegistry.getRegistered()), LootContextParamSets.BLOCK))));

this.subProviders.add(new RegiliteItemModelProvider(packOutput, modid, existingFileHelper, ItemRegistry.getRegistered()));
event.getGenerator().addProvider(true, this);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.enderio.regilite.data;

import com.enderio.regilite.holder.RegiliteBlock;
import com.enderio.regilite.holder.RegiliteItem;
import com.enderio.regilite.registry.ItemRegistry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -17,20 +15,20 @@
import net.neoforged.neoforge.internal.versions.neoforge.NeoForgeVersion;
import net.neoforged.neoforge.registries.DeferredHolder;

import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;

public class RegiliteItemModelProvider extends ItemModelProvider {
private final ItemRegistry registry;
private final List<DeferredHolder<Item, ? extends Item>> registered;

public RegiliteItemModelProvider(PackOutput output, String modid, ExistingFileHelper existingFileHelper, ItemRegistry itemRegistry) {
public RegiliteItemModelProvider(PackOutput output, String modid, ExistingFileHelper existingFileHelper, List<DeferredHolder<Item, ? extends Item>> registered) {
super(output, modid, existingFileHelper);
this.registry = itemRegistry;
this.registered = registered;
}

@Override
protected void registerModels() {
for (DeferredHolder<Item, ? extends Item> item : registry.getEntries()) {
for (DeferredHolder<Item, ? extends Item> item : registered) {
if (item instanceof RegiliteItem) {
var modelProvider = ((RegiliteItem<Item>) item).getModelProvider();
if (modelProvider != null) {
Expand Down
35 changes: 31 additions & 4 deletions src/main/java/com/enderio/regilite/data/RegiliteTagProvider.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
package com.enderio.regilite.data;

import com.enderio.regilite.holder.RegiliteFluid;
import com.enderio.regilite.registry.ITagagble;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.Registry;
import net.minecraft.data.PackOutput;
import net.minecraft.data.tags.IntrinsicHolderTagsProvider;
import net.minecraft.resources.ResourceKey;
import net.minecraft.tags.TagKey;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.common.data.ExistingFileHelper;
import net.neoforged.neoforge.fluids.FluidType;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

public class RegiliteTagProvider<T> extends IntrinsicHolderTagsProvider<T> {
private final DeferredRegister<T> registry;
private final List<DeferredHolder<T, ? extends T>> registered;

public RegiliteTagProvider(PackOutput output, ResourceKey<? extends Registry<T>> key, Function<T, ResourceKey<T>> func, CompletableFuture<HolderLookup.Provider> lookupProvider, String modId, @Nullable ExistingFileHelper existingFileHelper, DeferredRegister<T> registry) {
public RegiliteTagProvider(PackOutput output, ResourceKey<? extends Registry<T>> key, Function<T, ResourceKey<T>> func, CompletableFuture<HolderLookup.Provider> lookupProvider, String modId, @Nullable ExistingFileHelper existingFileHelper, List<DeferredHolder<T, ? extends T>> registered) {
super(output, key, lookupProvider, func, modId, existingFileHelper);
this.registry = registry;
this.registered = registered;
}

@Override
protected void addTags(HolderLookup.Provider p_256380_) {
for (DeferredHolder<T, ? extends T> entry : registry.getEntries()) {
for (DeferredHolder<T, ? extends T> entry : registered) {
if (entry instanceof ITagagble) {
Set<TagKey<T>> tag = ((ITagagble<T>) entry).getTags();

Expand All @@ -36,4 +40,27 @@ protected void addTags(HolderLookup.Provider p_256380_) {
}
}
}

public static class FluidTagProvider extends IntrinsicHolderTagsProvider<Fluid> {

public final List<DeferredHolder<FluidType, ? extends FluidType>> registered;

public FluidTagProvider(PackOutput output, ResourceKey<Registry<Fluid>> key, Function<Fluid, ResourceKey<Fluid>> func, CompletableFuture<HolderLookup.Provider> lookupProvider, String modId, @Nullable ExistingFileHelper existingFileHelper, List<DeferredHolder<FluidType, ? extends FluidType>> registered) {
super(output, key, lookupProvider, func, modId, existingFileHelper);
this.registered = registered;
}

@Override
protected void addTags(HolderLookup.Provider p_256380_) {
for (DeferredHolder<FluidType, ? extends FluidType> entry : registered) {
if (entry instanceof RegiliteFluid<? extends FluidType> fluidtype) {
Set<TagKey<Fluid>> tag = ((ITagagble<Fluid>) entry).getTags();

if (tag != null) {
tag.forEach(t -> tag(t).add(fluidtype.getSource()));
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@
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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;

public class BlockEntityRegistry extends DeferredRegister<BlockEntityType<?>> {

private static List<DeferredHolder<BlockEntityType<?>, ? extends BlockEntityType<?>>> registered = new ArrayList<>();

protected BlockEntityRegistry(String namespace) {
super(BuiltInRegistries.BLOCK_ENTITY_TYPE.key(), namespace);
}
Expand Down Expand Up @@ -66,15 +72,14 @@ public static <T extends BlockEntity> BlockEntityRegistry create(String modid) {
@Override
public void register(IEventBus bus) {
super.register(bus);
this.onGatherData(bus);
registered.addAll(this.getEntries());
if (FMLEnvironment.dist.isClient()) {
bus.addListener(new BlockEntityRendererEvents(this)::registerBER);
bus.addListener(new BlockEntityCapabilityEvents(this)::registerCapabilities);
}
}

private void onGatherData(IEventBus bus) {
RegiliteDataProvider provider = RegiliteDataProvider.register(getNamespace(), bus);
provider.addServerSubProvider((packOutput, existingFileHelper, lookup) -> new RegiliteTagProvider<>(packOutput, this.getRegistryKey(), b -> b.builtInRegistryHolder().key(), lookup, getNamespace(), existingFileHelper, this));
public static List<DeferredHolder<BlockEntityType<?>, ? extends BlockEntityType<?>>> getRegistered() {
return registered;
}
}
15 changes: 8 additions & 7 deletions src/main/java/com/enderio/regilite/registry/BlockRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.FlowingFluid;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
Expand All @@ -23,6 +24,7 @@
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
Expand All @@ -31,6 +33,9 @@
import java.util.function.Supplier;

public class BlockRegistry extends DeferredRegister.Blocks {

private static List<DeferredHolder<Block, ? extends Block>> registered = new ArrayList<>();

protected BlockRegistry(String namespace) {
super(namespace);
}
Expand Down Expand Up @@ -126,17 +131,13 @@ public static BlockRegistry createRegistry(String modid) {
@Override
public void register(IEventBus bus) {
super.register(bus);
this.onGatherData(bus);
registered.addAll(this.getEntries());
if (FMLEnvironment.dist.isClient()) {
bus.addListener(new ColorEvents.Blocks(this)::registerBlockColor);
}
}

private void onGatherData(IEventBus bus) {
RegiliteDataProvider provider = RegiliteDataProvider.register(getNamespace(), bus);
provider.addServerSubProvider((packOutput, existingFileHelper, lookup) -> new RegiliteTagProvider<>(packOutput, this.getRegistryKey(), b -> b.builtInRegistryHolder().key(), lookup, getNamespace(), existingFileHelper, this));
provider.addServerSubProvider((packOutput, existingFileHelper, lookup) -> new RegiliteBlockStateProvider(packOutput, getNamespace(), existingFileHelper, this));
provider.addServerSubProvider((packOutput, existingFileHelper, lookup) -> new LootTableProvider(packOutput, Collections.emptySet(),
List.of(new LootTableProvider.SubProviderEntry(() -> new RegiliteBlockLootProvider(Set.of(), this), LootContextParamSets.BLOCK))));
public static List<DeferredHolder<Block, ? extends Block>> getRegistered() {
return registered;
}
}
13 changes: 9 additions & 4 deletions src/main/java/com/enderio/regilite/registry/EntityRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
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.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;

public class EntityRegistry extends DeferredRegister<EntityType<?>> {
private static List<DeferredHolder<EntityType<?>, ? extends EntityType<?>>> registered = new ArrayList<>();

protected EntityRegistry(String namespace) {
super(BuiltInRegistries.ENTITY_TYPE.key(), namespace);
}
Expand Down Expand Up @@ -54,14 +60,13 @@ public static EntityRegistry create(String modid) {
@Override
public void register(IEventBus bus) {
super.register(bus);
onGatherData(bus);
registered.addAll(this.getEntries());
if (FMLEnvironment.dist.isClient()) {
bus.addListener(new EntityRendererEvents(this)::registerER);
}
}

private void onGatherData(IEventBus bus) {
RegiliteDataProvider provider = RegiliteDataProvider.register(getNamespace(), bus);
provider.addServerSubProvider((packOutput, existingFileHelper, lookup) -> new RegiliteTagProvider<>(packOutput, this.getRegistryKey(), b -> b.builtInRegistryHolder().key(), lookup, getNamespace(), existingFileHelper, this));
public static List<DeferredHolder<EntityType<?>, ? extends EntityType<?>>> getRegistered() {
return registered;
}
}
Loading

0 comments on commit e7dcd85

Please sign in to comment.