Skip to content

Commit

Permalink
started work on custin tag builder so fabru=icated forge tags dep can…
Browse files Browse the repository at this point in the history
… be removed'
  • Loading branch information
Trinsdar committed Sep 2, 2023
1 parent 1897873 commit f411f6e
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package muramasa.antimatter.datagen.builder;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.core.Registry;
import net.minecraft.data.tags.TagsProvider;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.Tag;
import net.minecraft.tags.TagKey;
import net.minecraft.util.GsonHelper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class AntimatterTagBuilder<T> {
public final Tag.Builder builder;
public final Registry<T> registry;
public final List<T> removeElements = new ArrayList<>();
private final String source;
boolean replace = false;

public AntimatterTagBuilder(Tag.Builder builder, Registry<T> registry, String string) {
this.builder = builder;
this.registry = registry;
this.source = string;
}

public AntimatterTagBuilder<T> add(T item) {
this.builder.addElement(this.registry.getKey(item), this.source);
return this;
}

public AntimatterTagBuilder<T> add(Tag.BuilderEntry builderEntry){
this.builder.add(builderEntry);
return this;
}

public AntimatterTagBuilder<T> add(ResourceKey<T>... resourceKeys) {
for(ResourceKey<T> resourceKey : resourceKeys) {
this.builder.addElement(resourceKey.location(), this.source);
}

return this;
}

public AntimatterTagBuilder<T> addOptional(ResourceLocation location) {
this.builder.addOptionalElement(location, this.source);
return this;
}

public AntimatterTagBuilder<T> addTag(TagKey<T> tag) {
this.builder.addTag(tag.location(), this.source);
return this;
}

public AntimatterTagBuilder<T> addOptionalTag(ResourceLocation location) {
this.builder.addOptionalTag(location, this.source);
return this;
}

@SafeVarargs
public final AntimatterTagBuilder<T> add(T... toAdd) {
Stream.of(toAdd).map(this.registry::getKey).forEach(resourceLocation -> this.builder.addElement(resourceLocation, this.source));
return this;
}

@SafeVarargs
public final AntimatterTagBuilder<T> remove(T... remove){
removeElements.addAll(Arrays.asList(remove));
return this;
}

public AntimatterTagBuilder<T> replace() {
return replace(true);
}

public AntimatterTagBuilder<T> replace(boolean value) {
replace = value;
return this;
}

public AntimatterTagBuilder<T> addFromJson(JsonObject json, String source) {
builder.addFromJson(json, source);
return this;
}

public JsonObject serializeToJson() {
JsonObject jsonObject = builder.serializeToJson();
jsonObject.addProperty("replace", replace);
return jsonObject;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import muramasa.antimatter.block.BlockStone;
import muramasa.antimatter.block.BlockStorage;
import muramasa.antimatter.datagen.IAntimatterProvider;
import muramasa.antimatter.datagen.builder.AntimatterTagBuilder;
import muramasa.antimatter.item.ItemFluidCell;
import muramasa.antimatter.material.Material;
import muramasa.antimatter.material.MaterialItem;
Expand Down Expand Up @@ -47,7 +48,7 @@

public class AntimatterItemTagProvider extends AntimatterTagProvider<Item> implements IAntimatterProvider {
private final boolean replace;
private final Function<TagKey<Block>, Tag.Builder> blockTags;
private final Function<TagKey<Block>, AntimatterTagBuilder<Block>> blockTags;

public AntimatterItemTagProvider(String providerDomain, String providerName, boolean replace, AntimatterBlockTagProvider p) {
super(Registry.ITEM, providerDomain, providerName, "items");
Expand Down Expand Up @@ -164,9 +165,9 @@ protected void processSubtags() {
}

protected void copy(TagKey<Block> blockTag, TagKey<Item> itemTag) {
Tag.Builder builder = this.getOrCreateRawBuilder(itemTag);
Tag.Builder builder2 = this.blockTags.apply(blockTag);
Stream<Tag.BuilderEntry> stream = builder2.getEntries();
AntimatterTagBuilder<Item> builder = this.getOrCreateRawBuilder(itemTag);
AntimatterTagBuilder<Block> builder2 = this.blockTags.apply(blockTag);
Stream<Tag.BuilderEntry> stream = builder2.builder.getEntries();
Objects.requireNonNull(builder);
stream.forEach(builder::add);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import muramasa.antimatter.datagen.AntimatterDynamics;
import muramasa.antimatter.datagen.IAntimatterProvider;
import muramasa.antimatter.datagen.builder.AntimatterTagBuilder;
import net.devtech.arrp.json.tags.JTag;
import net.minecraft.core.Registry;
import net.minecraft.data.HashCache;
Expand All @@ -16,17 +17,20 @@
import net.minecraft.tags.TagKey;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class AntimatterTagProvider<T> implements IAntimatterProvider {
private final String providerDomain, providerName, prefix;
protected final Map<ResourceLocation, Tag.Builder> builders;
protected final Map<ResourceLocation, AntimatterTagBuilder<T>> builders;
protected final Registry<T> registry;
public Object2ObjectMap<ResourceLocation, JsonObject> TAGS = new Object2ObjectOpenHashMap<>();
public static Object2ObjectOpenHashMap<ResourceLocation, JsonObject> TAGS_GLOBAL = new Object2ObjectOpenHashMap<>();

public Object2ObjectMap<ResourceLocation, List<T>> OBJECTS_TO_REMOVE = new Object2ObjectOpenHashMap<>();

public static Object2ObjectOpenHashMap<Registry<?>, Map<ResourceLocation, List<Object>>> TAGS_TO_REMOVE = new Object2ObjectOpenHashMap<>();

public AntimatterTagProvider(Registry<T> registry, String providerDomain, String providerName, String prefix) {
Expand All @@ -40,10 +44,14 @@ public AntimatterTagProvider(Registry<T> registry, String providerDomain, String

@Override
public void run() {
Map<ResourceLocation, Tag.Builder> b = new HashMap<>(this.builders);
Map<ResourceLocation, AntimatterTagBuilder<T>> b = new HashMap<>(this.builders);
this.builders.clear();
processTags(providerDomain);
builders.forEach(this::addTag);
builders.forEach((r, builder) -> {
List<T> list = OBJECTS_TO_REMOVE.computeIfAbsent(r, r2 -> new ArrayList<>());
list.addAll(builder.removeElements);
});
builders.putAll(b);
}

Expand All @@ -64,13 +72,12 @@ public String getName() {
return providerName;
}

protected TagsProvider.TagAppender<T> tag(TagKey<T> tag) {
Tag.Builder builder = this.getOrCreateRawBuilder(tag);
return new TagsProvider.TagAppender<>(builder, registry, providerDomain);
protected AntimatterTagBuilder<T> tag(TagKey<T> tag) {
return getOrCreateRawBuilder(tag);
}

protected Tag.Builder getOrCreateRawBuilder(TagKey<T> tag) {
return this.builders.computeIfAbsent(tag.location(), (location) -> new Tag.Builder());
protected AntimatterTagBuilder<T> getOrCreateRawBuilder(TagKey<T> tag) {
return this.builders.computeIfAbsent(tag.location(), (location) -> new AntimatterTagBuilder<>(new Tag.Builder(), registry, providerDomain));
}

// Must append 's' in the identifier
Expand All @@ -95,7 +102,7 @@ public static JTag fromJson(JsonObject obj){

// Must append 's' in the identifier
// Appends data to the tag.
public void addTag(ResourceLocation loc, Tag.Builder obj) {
public void addTag(ResourceLocation loc, AntimatterTagBuilder<T> obj) {
JsonObject json = TAGS.get(loc);
//if no tag just put this one in.
if (json == null) {
Expand Down

0 comments on commit f411f6e

Please sign in to comment.