Skip to content

Commit

Permalink
Feed registry access where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrbysco committed Jun 9, 2024
1 parent e458ff3 commit 52439fc
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 71 deletions.
42 changes: 23 additions & 19 deletions Xplat/src/main/java/vazkii/patchouli/api/IVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import com.google.gson.JsonNull;
import com.google.gson.JsonPrimitive;

import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;

import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Type;
Expand Down Expand Up @@ -87,77 +91,77 @@ default boolean asBoolean(boolean def) {
/**
* Get this IVariable as a {@code Stream<IVariable>}, assuming it's backed by a JsonArray.
*/
default Stream<IVariable> asStream() {
default Stream<IVariable> asStream(HolderLookup.Provider registries) {
return StreamSupport.stream(unwrap().getAsJsonArray().spliterator(), false)
.map(IVariable::wrap);
.map((json) -> IVariable.wrap(json, registries));
}

/**
* Get this IVariable as a {@code List<IVariable>}, returning as singleton if it's not a JsonArray.
*/
default Stream<IVariable> asStreamOrSingleton() {
return unwrap().isJsonArray() ? asStream() : Stream.of(this);
default Stream<IVariable> asStreamOrSingleton(HolderLookup.Provider registries) {
return unwrap().isJsonArray() ? asStream(registries) : Stream.of(this);
}

/**
* Get this IVariable as a {@code List<IVariable>}, assuming it's backed by a JsonArray.
*/
default List<IVariable> asList() {
return asStream().collect(Collectors.toList());
default List<IVariable> asList(HolderLookup.Provider registries) {
return asStream(registries).collect(Collectors.toList());
}

/**
* Get this IVariable as a {@code List<IVariable>}, returning as singleton if it's not a JsonArray.
*/
default List<IVariable> asListOrSingleton() {
return asStreamOrSingleton().collect(Collectors.toList());
default List<IVariable> asListOrSingleton(HolderLookup.Provider registries) {
return asStreamOrSingleton(registries).collect(Collectors.toList());
}

/**
* Convenience method to create an IVariable from {@link VariableHelper#createFromObject}.
*/
static <T> IVariable from(@Nullable T object) {
return object != null ? VariableHelper.instance().createFromObject(object) : empty();
static <T> IVariable from(@Nullable T object, HolderLookup.Provider registries) {
return object != null ? VariableHelper.instance().createFromObject(object, registries) : empty();
}

/**
* Convenience method to create an IVariable from a JsonElement with {@link VariableHelper#createFromJson}.
*/
static IVariable wrap(@Nullable JsonElement elem) {
return elem != null ? VariableHelper.instance().createFromJson(elem) : empty();
static IVariable wrap(@Nullable JsonElement elem, HolderLookup.Provider registries) {
return elem != null ? VariableHelper.instance().createFromJson(elem, registries) : empty();
}

/**
* Convenience method to create an IVariable from a list of IVariables.
*/
static IVariable wrapList(Iterable<IVariable> elems) {
static IVariable wrapList(Iterable<IVariable> elems, HolderLookup.Provider registries) {
JsonArray arr = new JsonArray();
for (IVariable v : elems) {
arr.add(v.unwrap());
}
return wrap(arr);
return wrap(arr, registries);
}

static IVariable wrap(@Nullable Number n) {
return n != null ? wrap(new JsonPrimitive(n)) : empty();
return n != null ? wrap(new JsonPrimitive(n), RegistryAccess.EMPTY) : empty();
}

static IVariable wrap(@Nullable Boolean b) {
return b != null ? wrap(new JsonPrimitive(b)) : empty();
return b != null ? wrap(new JsonPrimitive(b), RegistryAccess.EMPTY) : empty();
}

static IVariable wrap(@Nullable String s) {
return s != null ? wrap(new JsonPrimitive(s)) : empty();
return s != null ? wrap(new JsonPrimitive(s), RegistryAccess.EMPTY) : empty();
}

static IVariable empty() {
return wrap(JsonNull.INSTANCE);
return wrap(JsonNull.INSTANCE, RegistryAccess.EMPTY);
}

class Serializer implements JsonDeserializer<IVariable> {
@Override
public IVariable deserialize(JsonElement elem, Type t, JsonDeserializationContext c) {
return IVariable.wrap(elem);
return IVariable.wrap(elem, RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package vazkii.patchouli.api;

import net.minecraft.core.HolderLookup;

/**
* A provider of variables to a template. Probably a JSON.
*/
Expand All @@ -9,7 +11,7 @@ public interface IVariableProvider {
* Gets the value assigned to the variable passed in.
* May throw an exception if it doesn't exist.
*/
IVariable get(String key);
IVariable get(String key, HolderLookup.Provider registries);

/**
* Returns if a variable exists or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.gson.JsonElement;

import net.minecraft.core.HolderLookup;

/**
* An instance of a class implementing this interface
* provides conversions for the given type to/from {@link JsonElement}.
Expand All @@ -12,10 +14,10 @@ public interface IVariableSerializer<T> {
/**
* Deserialize an object of type T from JSON.
*/
T fromJson(JsonElement json);
T fromJson(JsonElement json, HolderLookup.Provider registries);

/**
* Serialize an object of type T to JSON.
*/
JsonElement toJson(T object);
JsonElement toJson(T object, HolderLookup.Provider registries);
}
6 changes: 4 additions & 2 deletions Xplat/src/main/java/vazkii/patchouli/api/VariableHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.common.base.Suppliers;
import com.google.gson.JsonElement;

import net.minecraft.core.HolderLookup;

import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;
Expand Down Expand Up @@ -30,14 +32,14 @@ static VariableHelper instance() {
/**
* Create an {@link IVariable} from a given object.
*/
default <T> IVariable createFromObject(T object) {
default <T> IVariable createFromObject(T object, HolderLookup.Provider registries) {
return null;
}

/**
* Create an {@link IVariable} backed by the given {@link JsonElement}.
*/
default IVariable createFromJson(JsonElement elem) {
default IVariable createFromJson(JsonElement elem, HolderLookup.Provider registries) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import net.minecraft.core.HolderLookup;

import vazkii.patchouli.api.IVariable;
import vazkii.patchouli.api.IVariableProvider;

Expand All @@ -15,13 +17,13 @@ public JsonVariableWrapper(JsonObject source) {
}

@Override
public IVariable get(String key) {
public IVariable get(String key, HolderLookup.Provider registries) {
JsonElement prim = source.get(key);
if (prim == null) {
throw new IllegalArgumentException("Attempted to get variable " + key + " when it's not present");
}

return IVariable.wrap(prim);
return IVariable.wrap(prim, registries);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;

import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.Level;

import vazkii.patchouli.api.IComponentProcessor;
Expand Down Expand Up @@ -87,7 +90,7 @@ public String qualifyName(String name) {
String query = prefixed ? name.substring(1) : name;

// if it's an upreference, return the upreference
String result = IVariable.wrap(localBindings.get(query)).asString();
String result = IVariable.wrap(localBindings.get(query), RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY)).asString();
if (result.startsWith("#")) {
return result.substring(1);
}
Expand All @@ -102,7 +105,7 @@ public IVariable attemptVariableLookup(String key) {
if (key.startsWith("#")) {
key = key.substring(1);
}
IVariable result = IVariable.wrap(localBindings.get(key));
IVariable result = IVariable.wrap(localBindings.get(key), RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY));
return result.asString().isEmpty() || isUpreference(result) ? null : result;
}

Expand All @@ -121,9 +124,9 @@ public boolean has(String key) {
}

@Override
public IVariable get(String key) {
public IVariable get(String key, HolderLookup.Provider registries) {
IVariable vari = attemptVariableLookup(key);
return vari == null ? provider.get(qualifyName(key)) : vari;
return vari == null ? provider.get(qualifyName(key), registries) : vari;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package vazkii.patchouli.client.book.template;

import net.minecraft.client.resources.language.I18n;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.Level;
Expand Down Expand Up @@ -133,7 +135,7 @@ private static IVariable resolveStringVar(Level level, String original, Context
}

if (val == null && c.variables.has(key)) {
val = c.variables.get(key);
val = c.variables.get(key, level.registryAccess());
}

return val == null ? IVariable.empty() : val;
Expand Down Expand Up @@ -169,7 +171,7 @@ private static IVariable inv(IVariable arg) {
}

private static IVariable stacks(IVariable arg) {
return IVariable.from(arg.as(Ingredient.class).getItems());
return IVariable.from(arg.as(Ingredient.class).getItems(), RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY));
}

private static String ename(String arg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class EntityTestProcessor implements IComponentProcessor {

@Override
public void setup(Level level, IVariableProvider variables) {
String entityType = variables.get("entity").unwrap().getAsString();
String entityType = variables.get("entity", level.registryAccess()).unwrap().getAsString();
if (entityType.contains("{")) {
entityType = entityType.substring(0, entityType.indexOf("{"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class RecipeTestProcessor implements IComponentProcessor {
@Override
public void setup(Level level, IVariableProvider variables) {
// TODO probably add a recipe serializer?
String recipeId = variables.get("recipe").asString();
String recipeId = variables.get("recipe", level.registryAccess()).asString();
RecipeManager manager = level.getRecipeManager();
recipe = manager.byKey(new ResourceLocation(recipeId)).orElseThrow(IllegalArgumentException::new).value();
}
Expand All @@ -31,7 +31,7 @@ public IVariable process(Level level, String key) {
ItemStack[] stacks = ingredient.getItems();
ItemStack stack = stacks.length == 0 ? ItemStack.EMPTY : stacks[0];

return IVariable.from(stack);
return IVariable.from(stack, level.registryAccess());
} else if (key.equals("text")) {
ItemStack out = recipe.getResultItem(level.registryAccess());
return IVariable.wrap(out.getCount() + "x$(br)" + out.getHoverName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;

import net.minecraft.core.HolderLookup;

import vazkii.patchouli.api.IVariableSerializer;

import java.lang.reflect.Array;
Expand All @@ -20,23 +22,23 @@ public GenericArrayVariableSerializer(IVariableSerializer<T> inner, Class<T> typ
}

@Override
public T[] fromJson(JsonElement json) {
public T[] fromJson(JsonElement json, HolderLookup.Provider registries) {
if (json.isJsonArray()) {
JsonArray array = json.getAsJsonArray();
List<T> stacks = new ArrayList<>();
for (JsonElement e : array) {
stacks.add(inner.fromJson(e));
stacks.add(inner.fromJson(e, registries));
}
return stacks.toArray(empty);
}
throw new IllegalArgumentException("Can't create an array of objects from a non-array JSON!");
}

@Override
public JsonArray toJson(T[] array) {
public JsonArray toJson(T[] array, HolderLookup.Provider registries) {
JsonArray result = new JsonArray();
for (T elem : array) {
result.add(inner.toJson(elem));
result.add(inner.toJson(elem, registries));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonElement;
import com.mojang.serialization.JsonOps;

import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.item.crafting.Ingredient;
Expand All @@ -12,12 +13,12 @@

public class IngredientVariableSerializer implements IVariableSerializer<Ingredient> {
@Override
public Ingredient fromJson(JsonElement json) {
public Ingredient fromJson(JsonElement json, HolderLookup.Provider registries) {
return (json.isJsonPrimitive()) ? ItemStackUtil.loadIngredientFromString(json.getAsString(), RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY)) : Ingredient.CODEC.parse(RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY).createSerializationContext(JsonOps.INSTANCE), json).result().orElseThrow();
}

@Override
public JsonElement toJson(Ingredient stack) {
return Ingredient.CODEC.encodeStart(JsonOps.INSTANCE, stack).result().orElseThrow();
public JsonElement toJson(Ingredient stack, HolderLookup.Provider registries) {
return Ingredient.CODEC.encodeStart(registries.createSerializationContext(JsonOps.INSTANCE), stack).result().orElseThrow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;

import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.HolderLookup;
import net.minecraft.world.item.ItemStack;

import vazkii.patchouli.common.util.ItemStackUtil;
Expand All @@ -19,27 +18,27 @@ public ItemStackArrayVariableSerializer() {
}

@Override
public ItemStack[] fromJson(JsonElement json) {
public ItemStack[] fromJson(JsonElement json, HolderLookup.Provider registries) {
if (json.isJsonArray()) {
JsonArray array = json.getAsJsonArray();
List<ItemStack> stacks = new ArrayList<>();
for (JsonElement e : array) {
stacks.addAll(Arrays.asList(fromNonArray(e)));
stacks.addAll(Arrays.asList(fromNonArray(e, registries)));
}
return stacks.toArray(empty);
}
return fromNonArray(json);
return fromNonArray(json, registries);
}

public ItemStack[] fromNonArray(JsonElement json) {
public ItemStack[] fromNonArray(JsonElement json, HolderLookup.Provider registries) {
if (json.isJsonNull()) {
return empty;
}
if (json.isJsonPrimitive()) {
return ItemStackUtil.loadStackListFromString(json.getAsString(), RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY)).toArray(empty);
return ItemStackUtil.loadStackListFromString(json.getAsString(), registries).toArray(empty);
}
if (json.isJsonObject()) {
return new ItemStack[] { ItemStackUtil.loadStackFromJson(json.getAsJsonObject()) };
return new ItemStack[] { ItemStackUtil.loadStackFromJson(json.getAsJsonObject(), registries) };
}
throw new IllegalArgumentException("Can't make an ItemStack from an array!");
}
Expand Down
Loading

0 comments on commit 52439fc

Please sign in to comment.