Added BiomeTag handling

This commit is contained in:
Frank 2022-03-17 17:40:28 +01:00
parent 7cb0d5de6c
commit 03eda716fe
3 changed files with 54 additions and 4 deletions

View file

@ -3,6 +3,7 @@ package ru.bclib.api.tag;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.minecraft.core.Registry;
import net.minecraft.data.BuiltinRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.tags.Tag;
@ -14,6 +15,7 @@ import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import ru.bclib.api.biomes.BiomeAPI;
import ru.bclib.mixin.common.DiggerItemAccessor;
import java.util.Map;
@ -23,6 +25,7 @@ import java.util.function.BiConsumer;
public class TagAPI {
private static final Map<ResourceLocation, Set<ResourceLocation>> TAGS_BLOCK = Maps.newConcurrentMap();
private static final Map<ResourceLocation, Set<ResourceLocation>> TAGS_ITEM = Maps.newConcurrentMap();
private static final Map<ResourceLocation, Set<ResourceLocation>> TAGS_BIOME = Maps.newConcurrentMap();
/**
* Get or create {@link TagKey}.
@ -126,7 +129,52 @@ public class TagAPI {
addItemTag(CommonItemTags.IRON_INGOTS, Items.IRON_INGOT);
addItemTag(CommonItemTags.FURNACES, Blocks.FURNACE);
}
/**
* Adds multiple Tags to one Biome.
* @param tagIDs array of {@link TagLocation<Biome>} tag IDs.
* @param biome The {@link Biome} to add tag.
*/
@SafeVarargs
public static void addBiomeTags(Biome biome, TagLocation<Biome>... tagIDs) {
for (TagLocation<Biome> tagID : tagIDs) {
addBiomeTagUntyped(tagID, biome);
}
}
/**
* Adds one Tag to multiple Biomes.
* @param tagID {@link TagLocation<Biome>} tag ID.
* @param biomes array of {@link Biome} to add into tag.
*/
public static void addBiomeTag(TagLocation<Biome> tagID, Biome... biomes) {
addBiomeTagUntyped(tagID, biomes);
}
/**
* Adds one Tag to multiple Biomes.
* @param tagID {@link TagKey<Biome>} tag ID.
* @param biomes array of {@link Biome} to add into tag.
*/
public static void addBiomeTag(TagKey<Biome> tagID, Biome... biomes) {
addBiomeTagUntyped(tagID.location(), biomes);
}
/**
* Adds one Tag to multiple Biomes.
* @param tagID {@link ResourceLocation} tag ID.
* @param biomes array of {@link Biome} to add into tag.
*/
protected static void addBiomeTagUntyped(ResourceLocation tagID, Biome... biomes) {
Set<ResourceLocation> set = TAGS_BIOME.computeIfAbsent(tagID, k -> Sets.newHashSet());
for (Biome biome : biomes) {
ResourceLocation id = BiomeAPI.getBiomeID(biome);
if (id != null) {
set.add(id);
}
}
}
/**
* Adds multiple Tags to one Block.
* @param tagIDs array of {@link TagLocation<Block>} tag IDs.
@ -228,7 +276,6 @@ public class TagAPI {
* @return The {@code tagsMap} Parameter.
*/
public static <T> Map<ResourceLocation, Tag.Builder> apply(String directory, Map<ResourceLocation, Tag.Builder> tagsMap) {
System.out.println("TAG DIRECTORY: " + directory);
final BiConsumer<ResourceLocation, Set<ResourceLocation>> consumer;
consumer = (id, ids) -> apply(tagsMap.computeIfAbsent(id, key -> Tag.Builder.tag()), ids);
if ("tags/blocks".equals(directory)) {
@ -237,6 +284,9 @@ public class TagAPI {
else if ("tags/items".equals(directory)) {
TAGS_ITEM.forEach(consumer);
}
else if ("tags/worldgen/biome".equals(directory)) {
TAGS_BIOME.forEach(consumer);
}
return tagsMap;
}

View file

@ -20,7 +20,6 @@ public class TagLoaderMixin {
@ModifyArg(method = "loadAndBuild", at = @At(value = "INVOKE", target = "Lnet/minecraft/tags/TagLoader;build(Ljava/util/Map;)Ljava/util/Map;"))
public Map<ResourceLocation, Tag.Builder> be_modifyTags(Map<ResourceLocation, Tag.Builder> tagsMap) {
//TODO: 1.18.2 Check if this still works as expected
return TagAPI.apply(directory, tagsMap);
}
}

View file

@ -18,6 +18,7 @@ import net.minecraft.world.level.levelgen.placement.PlacedFeature;
import org.jetbrains.annotations.Nullable;
import ru.bclib.BCLib;
import ru.bclib.api.biomes.BiomeAPI;
import ru.bclib.api.tag.TagAPI;
import ru.bclib.util.WeightedList;
import java.util.List;
@ -210,7 +211,7 @@ public class BCLBiome extends BCLBiomeSettings {
if (!this.structureTags.isEmpty()) {
//TODO: 1.18.2 This need to be done by BiomeTags now
structureTags.forEach(tagKey -> {System.out.println("Code to add Tags to Biomes should go here. " + tagKey);});
structureTags.forEach(tagKey -> TagAPI.addBiomeTag(tagKey, actualBiome.value()));
}
if (this.surfaceInit != null) {