Add support for optional tag values to Tag.Builder

Add optional tag values to data gen test
Fix data gen test not running by correcting gui_light value
This commit is contained in:
tterrag committed 2020-03-31 20:15:09 -04:00
1 parent 19444ffa18
commit 559bacbd98
10 files changed
+189 -47

No files matched your search

@@ -39,6 +39,7 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Queues;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
@@ -118,6 +119,7 @@ import net.minecraft.world.dimension.DimensionType;
import net.minecraft.world.storage.loot.LootContext;
import net.minecraft.world.storage.loot.LootTable;
import net.minecraft.world.storage.loot.LootTableManager;
import net.minecraftforge.common.data.IOptionalTagEntry;
import net.minecraftforge.common.loot.IGlobalLootModifier;
import net.minecraftforge.common.loot.LootModifierManager;
import net.minecraftforge.common.util.BlockSnapshot;
@@ -1095,12 +1097,9 @@ public class ForgeHooks
{
String s = JSONUtils.getString(entry, "value");
if (!s.startsWith("#"))
{
T value = valueGetter.apply(new ResourceLocation(s)).orElse(null);
if (value != null)
builder.add(value);
} else
builder.add(new OptionalTagEntry<>(new ResourceLocation(s.substring(1))));
builder.addOptional(valueGetter, Collections.singleton(new ResourceLocation(s)));
else
builder.addOptionalTag(new ResourceLocation(s.substring(1)));
}
}
@@ -1114,7 +1113,7 @@ public class ForgeHooks
T value = valueGetter.apply(new ResourceLocation(s)).orElse(null);
if (value != null)
{
Tag.ITagEntry<T> dummyEntry = new Tag.ListEntry<>(Collections.singletonList(value));
Tag.ITagEntry<T> dummyEntry = new Tag.ListEntry<>(Collections.singleton(value));
builder.remove(dummyEntry);
}
} else
@@ -1126,35 +1125,6 @@ public class ForgeHooks
}
}
private static class OptionalTagEntry<T> extends Tag.TagEntry<T>
{
private Tag<T> resolvedTag = null;
OptionalTagEntry(ResourceLocation referent)
{
super(referent);
}
@Override
public boolean resolve(@Nonnull Function<ResourceLocation, Tag<T>> resolver)
{
if (this.resolvedTag == null)
{
this.resolvedTag = resolver.apply(this.getSerializedId());
}
return true; // never fail if resolver returns null
}
@Override
public void populate(@Nonnull Collection<T> items)
{
if (this.resolvedTag != null)
{
items.addAll(this.resolvedTag.getAllElements());
}
}
}
private static final Map<IDataSerializer<?>, DataSerializerEntry> serializerEntries = GameData.getSerializerMap();
//private static final ForgeRegistry<DataSerializerEntry> serializerRegistry = (ForgeRegistry<DataSerializerEntry>) ForgeRegistries.DATA_SERIALIZERS;
// Do not reimplement this ^ it introduces a chicken-egg scenario by classloading registries during bootstrap
@@ -0,0 +1,8 @@
package net.minecraftforge.common.data;
import net.minecraft.tags.Tag.ITagEntry;
public interface IOptionalTagEntry<T> extends ITagEntry<T>
{
}
@@ -0,0 +1,85 @@
package net.minecraftforge.common.extensions;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Function;
import javax.annotation.Nonnull;
import com.google.gson.JsonArray;
import net.minecraft.tags.Tag;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.data.IOptionalTagEntry;
public interface IForgeTagBuilder<T>
{
default Tag.Builder<T> addOptional(Function<ResourceLocation, Optional<T>> valueGetter, Collection<ResourceLocation> locations)
{
return ((Tag.Builder<T>)this).add(new OptionalListEntry<>(locations, valueGetter));
}
default Tag.Builder<T> addOptionalTag(ResourceLocation tag)
{
return ((Tag.Builder<T>)this).add(new OptionalTagEntry<>(tag));
}
}
class OptionalListEntry<T> implements IOptionalTagEntry<T>
{
private final Collection<ResourceLocation> names;
private final Function<ResourceLocation, Optional<T>> valueGetter;
OptionalListEntry(Collection<ResourceLocation> names, Function<ResourceLocation, Optional<T>> valueGetter)
{
this.names = names;
this.valueGetter = valueGetter;
}
@Override
public void populate(Collection<T> itemsIn)
{
for (ResourceLocation rl : names)
{
valueGetter.apply(rl).ifPresent(itemsIn::add);
}
}
@Override
public void serialize(JsonArray array, Function<T, ResourceLocation> getNameForObject)
{
for (ResourceLocation rl : names)
{
array.add(rl.toString());
}
}
}
class OptionalTagEntry<T> extends Tag.TagEntry<T> implements IOptionalTagEntry<T>
{
private Tag<T> resolvedTag = null;
OptionalTagEntry(ResourceLocation referent)
{
super(referent);
}
@Override
public boolean resolve(@Nonnull Function<ResourceLocation, Tag<T>> resolver)
{
if (this.resolvedTag == null)
{
this.resolvedTag = resolver.apply(this.getSerializedId());
}
return true; // never fail if resolver returns null
}
@Override
public void populate(@Nonnull Collection<T> items)
{
if (this.resolvedTag != null)
{
items.addAll(this.resolvedTag.getAllElements());
}
}
}
@@ -24,7 +24,6 @@ import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MainWindow;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.fml.loading.progress.StartupMessageManager;
import org.apache.commons.lang3.tuple.Pair;