Skip to content

Commit

Permalink
Fix missing registries error (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
Minecraftschurli authored Jul 23, 2024
1 parent 0e367b2 commit 53354c6
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 52 deletions.
33 changes: 29 additions & 4 deletions Xplat/src/main/java/vazkii/patchouli/api/IVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,26 +142,51 @@ static IVariable wrapList(Iterable<IVariable> elems, HolderLookup.Provider regis
return wrap(arr, registries);
}

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

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

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

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

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

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

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

class Serializer implements JsonDeserializer<IVariable> {

private HolderLookup.Provider registryCache;

@Override
public IVariable deserialize(JsonElement elem, Type t, JsonDeserializationContext c) {
return IVariable.wrap(elem, RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY));
if (registryCache == null || registryCache.listRegistries().findFirst().isEmpty()) {
registryCache = RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY);
}
return IVariable.wrap(elem, registryCache);
}

public void setRegistries(HolderLookup.Provider registries) {
this.registryCache = registries;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package vazkii.patchouli.api;

import net.minecraft.core.HolderLookup;

import java.util.function.UnaryOperator;

public interface IVariablesAvailableCallback {
/**
* Called when variables are available, before the template component is built
*
* @param lookup Call with arbitrary text, and it will be expanded:
* first expanding all inline variables,
* then attempting to apply "->" derivations if possible, otherwise looking up the string as a
* plain variable from the template environment<br />
* Gracefully handles nulls given as input, but will never return null itself.
* @param lookup Call with arbitrary text, and it will be expanded:
* first expanding all inline variables,
* then attempting to apply "->" derivations if possible, otherwise looking up the string as a
* plain variable from the template environment<br />
* Gracefully handles nulls given as input, but will never return null itself.
* @param registries Provider for accessing registries in IVariable#wrap operations.
*/
void onVariablesAvailable(UnaryOperator<IVariable> lookup);
void onVariablesAvailable(UnaryOperator<IVariable> lookup, HolderLookup.Provider registries);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import vazkii.patchouli.common.book.Book;
import vazkii.patchouli.common.book.BookRegistry;
import vazkii.patchouli.common.util.ItemStackUtil;
import vazkii.patchouli.common.util.SerializationUtil;

import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -69,6 +70,7 @@ public void addRecipeMapping(ItemStackUtil.StackWrapper stack, BookEntry entry,

public static BookContents loadAndBuildFor(Level level, Book book, boolean singleBookReload) {
BookContentsBuilder builder = new BookContentsBuilder(book, singleBookReload);
SerializationUtil.VARIABLE_SERIALIZER.setRegistries(level.registryAccess());
builder.loadFiles();
return builder.build(level);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.annotations.SerializedName;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.core.HolderLookup;
import net.minecraft.world.level.Level;

import vazkii.patchouli.api.IComponentProcessor;
Expand Down Expand Up @@ -90,11 +91,11 @@ public boolean mouseClicked(BookPage page, double mouseX, double mouseY, int mou
}

@Override
public void onVariablesAvailable(UnaryOperator<IVariable> lookup) {
public void onVariablesAvailable(UnaryOperator<IVariable> lookup, HolderLookup.Provider registries) {
// TODO fix this up to use IVariable more intelligently
group = lookup.apply(IVariable.wrap(group)).asString();
flag = lookup.apply(IVariable.wrap(flag)).asString();
advancement = lookup.apply(IVariable.wrap(advancement)).asString();
guardPass = (guard == null || lookup.apply(IVariable.wrap(guard)).asBoolean());
group = lookup.apply(IVariable.wrap(group, registries)).asString();
flag = lookup.apply(IVariable.wrap(flag, registries)).asString();
advancement = lookup.apply(IVariable.wrap(advancement, registries)).asString();
guardPass = (guard == null || lookup.apply(IVariable.wrap(guard, registries)).asBoolean());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ public String qualifyName(String name) {
/**
* Attempt to look up a variable in local scope.
*/
public IVariable attemptVariableLookup(String key) {
public IVariable attemptVariableLookup(String key, HolderLookup.Provider registries) {
if (key.startsWith("#")) {
key = key.substring(1);
}
IVariable result = IVariable.wrap(localBindings.get(key), RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY));
IVariable result = IVariable.wrap(localBindings.get(key), registries);
return result.asString().isEmpty() || isUpreference(result) ? null : result;
}

Expand All @@ -120,12 +120,12 @@ public IVariableProvider wrapProvider(IVariableProvider provider) {
return new IVariableProvider() {
@Override
public boolean has(String key) {
return attemptVariableLookup(key) != null || provider.has(qualifyName(key));
return attemptVariableLookup(key, RegistryAccess.EMPTY) != null || provider.has(qualifyName(key));
}

@Override
public IVariable get(String key, HolderLookup.Provider registries) {
IVariable vari = attemptVariableLookup(key);
IVariable vari = attemptVariableLookup(key, registries);
return vari == null ? provider.get(qualifyName(key), registries) : vari;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void assignVariableHolders(Level level, IVariablesAvailableCallbac
}
}
return input;
});
}, level.registryAccess());
}

private static IVariable resolveString(Level level, @Nullable String curr, Context c) {
Expand Down Expand Up @@ -118,7 +118,7 @@ private static IVariable resolveStringVar(Level level, String original, Context

if (curr.startsWith("#")) {
if (c.encapsulation != null) {
val = c.encapsulation.attemptVariableLookup(curr);
val = c.encapsulation.attemptVariableLookup(curr, level.registryAccess());
if (val != null) {
return val;
} else {
Expand All @@ -139,34 +139,34 @@ private static IVariable resolveStringVar(Level level, String original, Context

return val == null ? IVariable.empty() : val;
}
return IVariable.wrap(curr);
return IVariable.wrap(curr, level.registryAccess());
}

private static BiFunction<IVariable, HolderLookup.Provider, IVariable> wrapStringFunc(Function<String, String> inner) {
return (x, r) -> IVariable.wrap(inner.apply(x.asString()));
return (x, r) -> IVariable.wrap(inner.apply(x.asString()), r);
}

private static IVariable iname(IVariable arg, HolderLookup.Provider registries) {
ItemStack stack = arg.as(ItemStack.class);
return IVariable.wrap(stack.getHoverName().getString());
return IVariable.wrap(stack.getHoverName().getString(), registries);
}

private static IVariable icount(IVariable arg, HolderLookup.Provider registries) {
ItemStack stack = arg.as(ItemStack.class);
return IVariable.wrap(stack.getCount());
return IVariable.wrap(stack.getCount(), registries);
}

private static IVariable exists(IVariable arg, HolderLookup.Provider registries) {
return IVariable.wrap(!arg.unwrap().isJsonNull());
return IVariable.wrap(!arg.unwrap().isJsonNull(), registries);
}

private static IVariable iexists(IVariable arg, HolderLookup.Provider registries) {
ItemStack stack = arg.as(ItemStack.class);
return IVariable.wrap(stack != null && !stack.isEmpty());
return IVariable.wrap(stack != null && !stack.isEmpty(), registries);
}

private static IVariable inv(IVariable arg, HolderLookup.Provider registries) {
return IVariable.wrap(!arg.unwrap().getAsBoolean());
return IVariable.wrap(!arg.unwrap().getAsBoolean(), registries);
}

private static IVariable stacks(IVariable arg, HolderLookup.Provider registries) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.annotations.SerializedName;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.core.HolderLookup;

import vazkii.patchouli.api.ICustomComponent;
import vazkii.patchouli.api.IVariable;
Expand All @@ -22,12 +23,12 @@ public class ComponentCustom extends TemplateComponent {
private transient ICustomComponent callbacks;

@Override
public void onVariablesAvailable(UnaryOperator<IVariable> lookup) {
super.onVariablesAvailable(lookup);
public void onVariablesAvailable(UnaryOperator<IVariable> lookup, HolderLookup.Provider registries) {
super.onVariablesAvailable(lookup, registries);
try {
Class<?> classObj = Class.forName(clazz);
callbacks = (ICustomComponent) SerializationUtil.RAW_GSON.fromJson(sourceObject, classObj);
callbacks.onVariablesAvailable(lookup);
callbacks.onVariablesAvailable(lookup, registries);
} catch (Exception e) {
throw new RuntimeException("Failed to create custom component " + clazz, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.annotations.SerializedName;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.core.HolderLookup;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
Expand Down Expand Up @@ -58,8 +59,8 @@ public void render(GuiGraphics graphics, BookPage page, int mouseX, int mouseY,
}

@Override
public void onVariablesAvailable(UnaryOperator<IVariable> lookup) {
super.onVariablesAvailable(lookup);
public void onVariablesAvailable(UnaryOperator<IVariable> lookup, HolderLookup.Provider registries) {
super.onVariablesAvailable(lookup, registries);
entityId = lookup.apply(entityId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.annotations.SerializedName;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.core.HolderLookup;
import net.minecraft.network.chat.Component;

import vazkii.patchouli.api.IVariable;
Expand Down Expand Up @@ -57,8 +58,8 @@ public void render(GuiGraphics graphics, BookPage page, int mouseX, int mouseY,
}

@Override
public void onVariablesAvailable(UnaryOperator<IVariable> lookup) {
super.onVariablesAvailable(lookup);
public void onVariablesAvailable(UnaryOperator<IVariable> lookup, HolderLookup.Provider registries) {
super.onVariablesAvailable(lookup, registries);
actualText = lookup.apply(text).as(Component.class);
colorStr = lookup.apply(colorStr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mojang.blaze3d.systems.RenderSystem;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.core.HolderLookup;
import net.minecraft.resources.ResourceLocation;

import vazkii.patchouli.api.IVariable;
Expand Down Expand Up @@ -33,9 +34,9 @@ public void build(BookContentsBuilder builder, BookPage page, BookEntry entry, i
}

@Override
public void onVariablesAvailable(UnaryOperator<IVariable> lookup) {
super.onVariablesAvailable(lookup);
image = lookup.apply(IVariable.wrap(image)).asString();
public void onVariablesAvailable(UnaryOperator<IVariable> lookup, HolderLookup.Provider registries) {
super.onVariablesAvailable(lookup, registries);
image = lookup.apply(IVariable.wrap(image, registries)).asString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mojang.blaze3d.systems.RenderSystem;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.core.HolderLookup;
import net.minecraft.world.item.ItemStack;

import vazkii.patchouli.api.IVariable;
Expand Down Expand Up @@ -33,8 +34,8 @@ public void build(BookContentsBuilder builder, BookPage page, BookEntry entry, i
}

@Override
public void onVariablesAvailable(UnaryOperator<IVariable> lookup) {
super.onVariablesAvailable(lookup);
public void onVariablesAvailable(UnaryOperator<IVariable> lookup, HolderLookup.Provider registries) {
super.onVariablesAvailable(lookup, registries);
items = lookup.apply(item).as(ItemStack[].class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.annotations.SerializedName;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.core.HolderLookup;
import net.minecraft.network.chat.Component;

import vazkii.patchouli.api.IVariable;
Expand Down Expand Up @@ -39,8 +40,8 @@ public void build(BookContentsBuilder builder, BookPage page, BookEntry entry, i
}

@Override
public void onVariablesAvailable(UnaryOperator<IVariable> lookup) {
super.onVariablesAvailable(lookup);
public void onVariablesAvailable(UnaryOperator<IVariable> lookup, HolderLookup.Provider registries) {
super.onVariablesAvailable(lookup, registries);
actualText = lookup.apply(text).as(Component.class);
colorStr = lookup.apply(colorStr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.annotations.SerializedName;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.core.HolderLookup;
import net.minecraft.network.chat.Component;

import vazkii.patchouli.api.IVariable;
Expand All @@ -23,8 +24,8 @@ public class ComponentTooltip extends TemplateComponent {
transient List<Component> tooltip;

@Override
public void onVariablesAvailable(UnaryOperator<IVariable> lookup) {
super.onVariablesAvailable(lookup);
public void onVariablesAvailable(UnaryOperator<IVariable> lookup, HolderLookup.Provider registries) {
super.onVariablesAvailable(lookup, registries);
for (int i = 0; i < tooltipRaw.length; i++) {
tooltipRaw[i] = lookup.apply(tooltipRaw[i]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.core.HolderLookup;
import net.minecraft.network.chat.Component;

import vazkii.patchouli.api.IComponentRenderContext;
Expand Down Expand Up @@ -35,7 +36,7 @@ public boolean mouseClicked(IComponentRenderContext context, double mouseX, doub
}

@Override
public void onVariablesAvailable(UnaryOperator<IVariable> lookup) {
text = lookup.apply(IVariable.wrap("First we eat #spaghet#, then we drink #pop#")).asString();
public void onVariablesAvailable(UnaryOperator<IVariable> lookup, HolderLookup.Provider registries) {
text = lookup.apply(IVariable.wrap("First we eat #spaghet#, then we drink #pop#", registries)).asString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void setup(Level level, IVariableProvider variables) {
@Override
public IVariable process(Level level, String key) {
if (key.equals("name")) {
return IVariable.wrap(entityName);
return IVariable.wrap(entityName, level.registryAccess());
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public IVariable process(Level level, String key) {
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());
return IVariable.wrap(out.getCount() + "x$(br)" + out.getHoverName(), level.registryAccess());
} else if (key.equals("icount")) {
return IVariable.wrap(recipe.getResultItem(level.registryAccess()).getCount());
return IVariable.wrap(recipe.getResultItem(level.registryAccess()).getCount(), level.registryAccess());
} else if (key.equals("iname")) {
return IVariable.wrap(recipe.getResultItem(level.registryAccess()).getHoverName().getString());
return IVariable.wrap(recipe.getResultItem(level.registryAccess()).getHoverName().getString(), level.registryAccess());
}

return null;
Expand Down
Loading

0 comments on commit 53354c6

Please sign in to comment.