Skip to content

Commit

Permalink
add tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ferriarnus committed Dec 28, 2023
1 parent 331b80b commit 2636d84
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 39 deletions.

This file was deleted.

5 changes: 5 additions & 0 deletions src/generated/resources/data/minecraft/tags/blocks/logs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"values": [
"examplemod:example_block"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"values": [
"examplemod:example_block"
]
}
5 changes: 5 additions & 0 deletions src/generated/resources/data/minecraft/tags/items/planks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"values": [
"examplemod:example_block"
]
}
2 changes: 1 addition & 1 deletion src/main/java/com/example/examplemod/ExampleMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class ExampleMod
// Creates a new Block with the id "examplemod:example_block", combining the namespace and path
public static final EnderDeferredBlock<Block> EXAMPLE_BLOCK = BLOCKS
.registerBlock("example_block", BlockBehaviour.Properties.of().mapColor(MapColor.STONE))
.addBlockTags(BlockTags.MUSHROOM_GROW_BLOCK)
.addBlockTags(BlockTags.MUSHROOM_GROW_BLOCK, BlockTags.LOGS)
.setBlockStateProvider(BlockStateProvider::simpleBlock)
.setLootTable(EnderBlockLootProvider::dropSelf)
.createBlockItem()
Expand Down

This file was deleted.

10 changes: 6 additions & 4 deletions src/main/java/com/example/examplemod/data/EnderDataProvider.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.examplemod.data;

import com.example.examplemod.ExampleMod;
import net.minecraft.core.HolderLookup;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataProvider;
import net.minecraft.data.PackOutput;
Expand All @@ -11,6 +12,7 @@
import net.neoforged.neoforge.common.data.ExistingFileHelper;
import net.neoforged.neoforge.data.event.GatherDataEvent;
import net.neoforged.neoforge.registries.DeferredHolder;
import org.apache.commons.lang3.function.TriFunction;
import org.apache.logging.log4j.util.TriConsumer;

import java.util.*;
Expand All @@ -24,7 +26,7 @@ public class EnderDataProvider implements DataProvider {
private final List<DeferredHolder<Block, ? extends Block>> BLOCKS = new ArrayList<>();
private final List<DeferredHolder<Item, ? extends Item>> ITEMS = new ArrayList<>();
private final List<DataProvider> subProviders = new ArrayList<>();
private final List<BiFunction<PackOutput, ExistingFileHelper, DataProvider>> serverSubProviderConsumers = new ArrayList<>();
private final List<TriFunction<PackOutput, ExistingFileHelper, CompletableFuture<HolderLookup.Provider>, DataProvider>> serverSubProviderConsumers = new ArrayList<>();
private static final Map<String, EnderDataProvider> INSTANCES = new HashMap<>();

protected EnderDataProvider(String modid) {
Expand All @@ -49,7 +51,7 @@ public void addItems(Collection<DeferredHolder<Item, ? extends Item>> items) {
ITEMS.addAll(items);
}

public void addServerSubProvider(BiFunction<PackOutput, ExistingFileHelper, DataProvider> function) {
public void addServerSubProvider(TriFunction<PackOutput, ExistingFileHelper, CompletableFuture<HolderLookup.Provider>, DataProvider> function) {
serverSubProviderConsumers.add(function);
}

Expand All @@ -71,8 +73,8 @@ public String getName() {
static void onGatherData(GatherDataEvent event) {
for (EnderDataProvider provider : INSTANCES.values()) {
if (event.includeServer()) {
for (BiFunction<PackOutput, ExistingFileHelper, DataProvider> function : provider.serverSubProviderConsumers) {
provider.subProviders.add(function.apply(event.getGenerator().getPackOutput(), event.getExistingFileHelper()));
for (TriFunction<PackOutput, ExistingFileHelper, CompletableFuture<HolderLookup.Provider>, DataProvider> function : provider.serverSubProviderConsumers) {
provider.subProviders.add(function.apply(event.getGenerator().getPackOutput(), event.getExistingFileHelper(), event.getLookupProvider()));
}
}
EnderLangProvider enUs = new EnderLangProvider(event.getGenerator().getPackOutput(), provider.modid, "en_us");
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/example/examplemod/data/EnderTagProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.examplemod.data;

import com.example.examplemod.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.neoforged.neoforge.common.data.ExistingFileHelper;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;
import org.jetbrains.annotations.Nullable;

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

public class EnderTagProvider<T> extends IntrinsicHolderTagsProvider<T> {
private final DeferredRegister<T> registry;

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

@Override
protected void addTags(HolderLookup.Provider p_256380_) {
for (DeferredHolder<T, ? extends T> entry : registry.getEntries()) {
Set<TagKey<T>> tag = ((ITagagble<T>) entry).getTags();
tag.forEach(t -> tag(t).add(entry.get()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ public void register(IEventBus bus) {
private void onGatherData() {
EnderDataProvider provider = EnderDataProvider.getInstance(getNamespace());
provider.addBlocks(this.getEntries());
provider.addServerSubProvider((packOutput, existingFileHelper) -> new EnderBlockStateProvider(packOutput, getNamespace(), existingFileHelper, this));
provider.addServerSubProvider((packOutput, existingFileHelper) -> new LootTableProvider(packOutput, Collections.emptySet(),
provider.addServerSubProvider((packOutput, existingFileHelper, lookup) -> new EnderTagProvider<>(packOutput, this.getRegistryKey(), b -> b.builtInRegistryHolder().key(), lookup, getNamespace(), existingFileHelper, this));
provider.addServerSubProvider((packOutput, existingFileHelper, lookup) -> new EnderBlockStateProvider(packOutput, getNamespace(), existingFileHelper, this));
provider.addServerSubProvider((packOutput, existingFileHelper, lookup) -> new LootTableProvider(packOutput, Collections.emptySet(),
List.of(new LootTableProvider.SubProviderEntry(() -> new EnderBlockLootProvider(Set.of(), this), LootContextParamSets.BLOCK))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.function.Function;
import java.util.function.Supplier;

public class EnderDeferredBlock<T extends Block> extends DeferredBlock<T> implements ITranslatable{
public class EnderDeferredBlock<T extends Block> extends DeferredBlock<T> implements ITranslatable, ITagagble<Block>{
private String translation = StringUtils.capitalize(getId().getPath().replace('_', ' '));
private Set<TagKey<Block>> blockTags = Set.of();
@Nullable
Expand Down Expand Up @@ -50,7 +50,7 @@ public final EnderDeferredBlock<T> addBlockTags(TagKey<Block>... tags) {
return this;
}

public Set<TagKey<Block>> getBlockTags() {
public Set<TagKey<Block>> getTags() {
return blockTags;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.function.Consumer;
import java.util.function.Supplier;

public class EnderDeferredItem<T extends Item> extends DeferredItem<T> implements ITranslatable{
public class EnderDeferredItem<T extends Item> extends DeferredItem<T> implements ITranslatable, ITagagble<Item>{
protected String translation = StringUtils.capitalize(getId().getPath().replace('_', ' '));
protected Set<TagKey<Item>> ItemTags = new HashSet<>();
protected Map<ResourceKey<CreativeModeTab>, Consumer<CreativeModeTab.Output>> tab = new HashMap<>();
Expand All @@ -53,7 +53,7 @@ public final EnderDeferredItem<T> addItemTags(TagKey<Item>... tags) {
return this;
}

public Set<TagKey<Item>> getItemTags() {
public Set<TagKey<Item>> getTags() {
return ItemTags;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ public void register(IEventBus bus) {
private void onGatherData() {
EnderDataProvider provider = EnderDataProvider.getInstance(getNamespace());
provider.addItems(this.getEntries());
provider.addServerSubProvider((packOutput, existingFileHelper) -> new EnderItemModelProvider(packOutput, getNamespace(), existingFileHelper, this));
provider.addServerSubProvider((packOutput, existingFileHelper, lookup) -> new EnderTagProvider<>(packOutput, this.getRegistryKey(), b -> b.builtInRegistryHolder().key(), lookup, getNamespace(), existingFileHelper, this));
provider.addServerSubProvider((packOutput, existingFileHelper, lookup) -> new EnderItemModelProvider(packOutput, getNamespace(), existingFileHelper, this));
}

private void addCreative(BuildCreativeModeTabContentsEvent event) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/example/examplemod/registry/ITagagble.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.examplemod.registry;

import net.minecraft.tags.TagKey;
import net.minecraft.world.level.block.Block;

import java.util.Set;

public interface ITagagble<T> {

Set<TagKey<T>> getTags();
}

0 comments on commit 2636d84

Please sign in to comment.