[Change] TagDataProvider can force write TagKeys

This commit is contained in:
Frank 2023-06-13 22:38:37 +02:00
parent bde6757c6a
commit 296696d23b

View file

@ -4,12 +4,14 @@ import org.betterx.worlds.together.tag.v3.TagRegistry;
import net.minecraft.core.HolderLookup;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricTagProvider;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import org.jetbrains.annotations.Nullable;
@ -19,6 +21,8 @@ public class TagDataProvider<T> extends FabricTagProvider<T> {
protected final TagRegistry<T> tagRegistry;
private final Set<TagKey<T>> forceWrite;
/**
* Constructs a new {@link FabricTagProvider} with the default computed path.
*
@ -36,10 +40,34 @@ public class TagDataProvider<T> extends FabricTagProvider<T> {
@Nullable List<String> modIDs,
FabricDataOutput output,
CompletableFuture<HolderLookup.Provider> registriesFuture
) {
this(tagRegistry, modIDs, Set.of(), output, registriesFuture);
}
/**
* Constructs a new {@link FabricTagProvider} with the default computed path.
*
* <p>Common implementations of this class are provided.
*
* @param tagRegistry
* @param modIDs List of ModIDs that are allowed to inlcude data. All Resources in the namespace of the
* mod will be written to the tag. If null all elements get written, and empty list will
* write nothing
* @param forceWriteKeys the keys that should allways get written
* @param output the {@link FabricDataOutput} instance
* @param registriesFuture the backing registry for the tag type
*/
public TagDataProvider(
TagRegistry<T> tagRegistry,
@Nullable List<String> modIDs,
Set<TagKey<T>> forceWriteKeys,
FabricDataOutput output,
CompletableFuture<HolderLookup.Provider> registriesFuture
) {
super(output, tagRegistry.registryKey, registriesFuture);
this.tagRegistry = tagRegistry;
this.modIDs = modIDs;
this.forceWrite = forceWriteKeys;
}
protected boolean shouldAdd(ResourceLocation loc) {
@ -49,7 +77,7 @@ public class TagDataProvider<T> extends FabricTagProvider<T> {
@Override
protected void addTags(HolderLookup.Provider arg) {
tagRegistry.forEachTag((tag, locs, tags) -> {
if (locs.isEmpty() && tags.isEmpty()) return;
if (!forceWrite.contains(tag) && locs.isEmpty() && tags.isEmpty()) return;
final FabricTagProvider<T>.FabricTagBuilder builder = getOrCreateTagBuilder(tag);
@ -58,6 +86,6 @@ public class TagDataProvider<T> extends FabricTagProvider<T> {
locs.forEach(builder::add);
tags.forEach(builder::forceAddTag);
}, (tag, loc) -> shouldAdd(tag.location()) || this.shouldAdd(loc));
}, (tag, loc) -> forceWrite.contains(tag) || shouldAdd(tag.location()) || this.shouldAdd(loc));
}
}