Skip to content

Commit

Permalink
Renamed recipe_schemas directory to recipe_schema, added type and…
Browse files Browse the repository at this point in the history
… key overrides
  • Loading branch information
LatvianModder committed Jul 7, 2024
1 parent e99ce9f commit c87c458
Show file tree
Hide file tree
Showing 44 changed files with 133 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public String toString() {
}

public KubeRuntimeException source(SourceLine sourceLine) {
this.sourceLine = sourceLine;
if (this.sourceLine.isUnknown()) {
this.sourceLine = sourceLine;
}

return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void initValues(boolean created) {
save();
}

if (type.schemaType.schema.keys.length > 0) {
if (!type.schemaType.schema.keys.isEmpty()) {
valueMap = new RecipeComponentBuilderMap(type.schemaType.schema.keys);

if (created) {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/dev/latvian/mods/kubejs/recipe/RecipeKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,6 @@ public RecipeKey<T> exclude() {
return this;
}

public boolean includeInAutoConstructors() {
return optional == null || !excluded;
}

/**
* Disables the generation of builder functions for this key.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public KubeRecipe call(Context cx, Scriptable scope, Scriptable thisObj, Object[
}

public KubeRecipe createRecipe(Context cx, Object[] args) {
var sourceLine = SourceLine.of(cx);

try {
for (int i = 0; i < args.length; i++) {
args[i] = Wrapper.unwrapped(args[i]);
Expand All @@ -56,13 +58,13 @@ public KubeRecipe createRecipe(Context cx, Object[] args) {

if (constructor == null) {
if (args.length == 1 && (args[0] instanceof Map<?, ?> || args[0] instanceof JsonObject)) {
var recipe = schemaType.schema.deserialize(SourceLine.of(cx), this, null, MapJS.json(cx, args[0]));
var recipe = schemaType.schema.deserialize(sourceLine, this, null, MapJS.json(cx, args[0]));
recipe.afterLoaded();
return event.addRecipe(recipe, true);
// throw new RecipeExceptionJS("Use event.custom(json) for json recipes!");
}

throw new KubeRuntimeException("Constructor for " + id + " with " + args.length + " arguments not found!");
throw new KubeRuntimeException("Constructor for " + id + " with " + args.length + " arguments not found!").source(sourceLine);
}

/*
Expand All @@ -80,13 +82,13 @@ public KubeRecipe createRecipe(Context cx, Object[] args) {
argMap.put(key, Wrapper.unwrapped(args[index++]));
}

var recipe = constructor.create(cx, this, schemaType, argMap);
var recipe = constructor.create(cx, sourceLine, this, schemaType, argMap);
recipe.afterLoaded();
return event.addRecipe(recipe, false);
} catch (KubeRuntimeException rex) {
throw rex;
throw rex.source(sourceLine);
} catch (Throwable ex) {
throw new KubeRuntimeException("Failed to create recipe for type '" + id + "' with args " + Arrays.stream(args).map(o -> o == null ? "null" : (o + ": " + o.getClass().getSimpleName())).collect(Collectors.joining(", ", "[", "]")), ex);
throw new KubeRuntimeException("Failed to create recipe for type '" + id + "' with args " + Arrays.stream(args).map(o -> o == null ? "null" : (o + ": " + o.getClass().getSimpleName())).collect(Collectors.joining(", ", "[", "]")), ex).source(sourceLine);
}
}

Expand All @@ -95,10 +97,6 @@ public String toString() {
return idString;
}

public String getMod() {
return id.getNamespace();
}

@Override
public int hashCode() {
return idString.hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jetbrains.annotations.NotNull;

import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -35,11 +36,11 @@ public RecipeComponentBuilderMap(RecipeComponentValue<?>[] holders) {
}
}

public RecipeComponentBuilderMap(RecipeKey<?>[] keys) {
this.holders = new RecipeComponentValue[keys.length];
public RecipeComponentBuilderMap(List<RecipeKey<?>> keys) {
this.holders = new RecipeComponentValue[keys.size()];

for (int i = 0; i < holders.length; i++) {
this.holders[i] = new RecipeComponentValue<>(keys[i], i);
this.holders[i] = new RecipeComponentValue<>(keys.get(i), i);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.serialization.JsonOps;
import com.mojang.serialization.DynamicOps;
import dev.latvian.mods.kubejs.recipe.RecipeKey;
import dev.latvian.mods.kubejs.recipe.component.ComponentRole;
import dev.latvian.mods.kubejs.script.ConsoleJS;
Expand All @@ -26,14 +26,17 @@ private record FunctionBuilder(String name, JsonObject json) {
private static final class RecipeSchemaBuilder {
private final ResourceLocation id;
private final JsonObject json;
private RecipeSchema schema;

private RecipeSchemaBuilder parent;
private ResourceLocation overrideType;
private List<RecipeKey<?>> keys;
private List<ConstructorBuilder> constructors;
private Map<String, FunctionBuilder> functions;
private KubeRecipeFactory recipeFactory;
private List<String> unique;
private boolean hidden;
private RecipeSchema schema;
private Map<String, JsonElement> overrideKeys;

private RecipeSchemaBuilder(ResourceLocation id, JsonObject json) {
this.id = id;
Expand Down Expand Up @@ -100,9 +103,9 @@ private boolean isHidden() {
}
}

private RecipeSchema getSchema() {
private RecipeSchema getSchema(DynamicOps<JsonElement> jsonOps) {
if (schema == null) {
if (keys != null || constructors != null || functions != null || recipeFactory != null || unique != null) {
if (overrideType != null || keys != null || constructors != null || functions != null || recipeFactory != null || unique != null || overrideKeys != null) {
var keys = getKeys();
var keyMap = new HashMap<String, RecipeKey<?>>();

Expand All @@ -113,7 +116,26 @@ private RecipeSchema getSchema() {
var functionMap = new HashMap<String, FunctionBuilder>();
gatherFunctions(functionMap);

schema = new RecipeSchema(getKeys().toArray(new RecipeKey[0]));
var keyOverrides = new IdentityHashMap<RecipeKey<?>, RecipeOptional<?>>(overrideKeys == null ? 0 : overrideKeys.size());

if (overrideKeys != null) {
for (var entry : overrideKeys.entrySet()) {
var key = keyMap.get(entry.getKey());

if (key != null) {
try {
keyOverrides.put(key, new RecipeOptional.Constant(key.codec.decode(jsonOps, entry.getValue()).getOrThrow().getFirst()));
} catch (Exception ex) {
throw new IllegalArgumentException("Failed to create optional value for key '" + key + "' of '" + id + "' from " + entry.getValue(), ex);
}
} else {
throw new NullPointerException("Key '" + entry.getKey() + "' not found in key overrides of recipe schema '" + id + "'");
}
}
}

schema = new RecipeSchema(keyOverrides, getKeys());
schema.typeOverride = overrideType;

var rf = getRecipeFactory();

Expand Down Expand Up @@ -147,7 +169,7 @@ private RecipeSchema getSchema() {

if (key != null) {
try {
constructor.overrides.put(key, new RecipeOptional.Constant(key.codec.decode(JsonOps.INSTANCE, entry.getValue()).getOrThrow().getFirst()));
constructor.overrides.put(key, new RecipeOptional.Constant(key.codec.decode(jsonOps, entry.getValue()).getOrThrow().getFirst()));
} catch (Exception ex) {
throw new IllegalArgumentException("Failed to create optional value for key '" + key + "' of '" + id + "' from " + entry.getValue(), ex);
}
Expand All @@ -173,7 +195,7 @@ private RecipeSchema getSchema() {
var key = keyMap.get(entry1.getKey());

if (key != null) {
map.put(key, key.codec.decode(JsonOps.INSTANCE, entry1.getValue()).getOrThrow().getFirst());
map.put(key, key.codec.decode(jsonOps, entry1.getValue()).getOrThrow().getFirst());
} else {
throw new NullPointerException("Key '" + entry1.getKey() + "' not found in function '" + entry1.getKey() + "' of recipe schema '" + id + "'");
}
Expand Down Expand Up @@ -207,9 +229,9 @@ private RecipeSchema getSchema() {

schema.hidden = isHidden();
} else if (parent != null) {
schema = parent.getSchema();
schema = parent.getSchema(jsonOps);
} else {
schema = new RecipeSchema();
schema = new RecipeSchema(Map.of(), List.of());
schema.constructor();
}
}
Expand All @@ -218,13 +240,13 @@ private RecipeSchema getSchema() {
}
}

public static void load(RecipeSchemaStorage storage, RecipeSchemaRegistry event, ResourceManager resourceManager) {
public static void load(RecipeSchemaStorage storage, RecipeSchemaRegistry event, ResourceManager resourceManager, DynamicOps<JsonElement> jsonOps) {
var map = new HashMap<ResourceLocation, RecipeSchemaBuilder>();

for (var entry : resourceManager.listResources("kubejs/recipe_schemas", path -> path.getPath().endsWith(".json")).entrySet()) {
for (var entry : resourceManager.listResources("kubejs/recipe_schema", path -> path.getPath().endsWith(".json")).entrySet()) {
try (var reader = entry.getValue().openAsReader()) {
var json = JsonUtils.GSON.fromJson(reader, JsonObject.class);
var holder = new RecipeSchemaBuilder(ResourceLocation.fromNamespaceAndPath(entry.getKey().getNamespace(), entry.getKey().getPath().substring("kubejs/recipe_schemas/".length(), entry.getKey().getPath().length() - ".json".length())), json);
var holder = new RecipeSchemaBuilder(ResourceLocation.fromNamespaceAndPath(entry.getKey().getNamespace(), entry.getKey().getPath().substring("kubejs/recipe_schema/".length(), entry.getKey().getPath().length() - ".json".length())), json);
map.put(holder.id, holder);

if (holder.json.has("mappings")) {
Expand All @@ -244,6 +266,10 @@ public static void load(RecipeSchemaStorage storage, RecipeSchemaRegistry event,

holder.parent = holder.json.has("parent") ? map.get(ResourceLocation.parse(holder.json.get("parent").getAsString())) : null;

if (holder.json.has("override_type")) {
holder.overrideType = ResourceLocation.parse(holder.json.get("override_type").getAsString());
}

if (holder.json.has("factory")) {
var fname = ResourceLocation.parse(holder.json.get("factory").getAsString());
holder.recipeFactory = storage.recipeTypes.get(fname);
Expand Down Expand Up @@ -276,7 +302,7 @@ public static void load(RecipeSchemaStorage storage, RecipeSchemaRegistry event,
key.defaultOptional();
} else {
try {
key.optional = new RecipeOptional.Constant(key.codec.decode(JsonOps.INSTANCE, optionalJson).getOrThrow().getFirst());
key.optional = new RecipeOptional.Constant(key.codec.decode(jsonOps, optionalJson).getOrThrow().getFirst());
} catch (Exception ex) {
throw new IllegalArgumentException("Failed to create optional value for key '" + key + "' of '" + holder.id + "' from " + optionalJson, ex);
}
Expand Down Expand Up @@ -358,10 +384,18 @@ public static void load(RecipeSchemaStorage storage, RecipeSchemaRegistry event,
holder.functions.put(entry.getKey(), new FunctionBuilder(entry.getKey(), entry.getValue().getAsJsonObject()));
}
}

if (holder.json.has("override_keys")) {
holder.overrideKeys = new HashMap<>();

for (var e : holder.json.getAsJsonObject("override_keys").entrySet()) {
holder.overrideKeys.put(e.getKey(), e.getValue());
}
}
}

for (var holder : map.values()) {
var schema = holder.getSchema();
var schema = holder.getSchema(jsonOps);
event.namespace(holder.id.getNamespace()).register(holder.id.getPath(), schema);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@
import dev.latvian.mods.kubejs.util.Cast;
import dev.latvian.mods.rhino.Context;

import java.util.Arrays;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class RecipeConstructor {
public final RecipeKey<?>[] keys;
public final List<RecipeKey<?>> keys;
public Map<RecipeKey<?>, RecipeOptional<?>> overrides;

public RecipeConstructor(RecipeKey<?>... keys) {
public RecipeConstructor(List<RecipeKey<?>> keys) {
this.keys = keys;
this.overrides = Map.of();
}

public RecipeConstructor(RecipeKey<?>... keys) {
this(List.of(keys));
}

public <T> RecipeConstructor override(RecipeKey<T> key, RecipeOptional<T> value) {
if (overrides.isEmpty()) {
overrides = new IdentityHashMap<>(1);
Expand All @@ -43,12 +47,12 @@ public RecipeConstructor overrides(Map<RecipeKey<?>, RecipeOptional<?>> map) {

@Override
public String toString() {
return Arrays.stream(keys).map(RecipeKey::toString).collect(Collectors.joining(", ", "(", ")"));
return keys.stream().map(RecipeKey::toString).collect(Collectors.joining(", ", "(", ")"));
}

public KubeRecipe create(Context cx, RecipeTypeFunction type, RecipeSchemaType schemaType, ComponentValueMap from) {
public KubeRecipe create(Context cx, SourceLine sourceLine, RecipeTypeFunction type, RecipeSchemaType schemaType, ComponentValueMap from) {
var r = schemaType.schema.recipeFactory.create();
r.sourceLine = SourceLine.of(cx);
r.sourceLine = sourceLine;
r.type = type;
r.json = new JsonObject();
r.json.addProperty("type", "unknown");
Expand All @@ -66,5 +70,9 @@ public void setValues(Context cx, KubeRecipe recipe, RecipeSchemaType schemaType
for (var entry : overrides.entrySet()) {
recipe.setValue(entry.getKey(), Cast.to(entry.getValue().getDefaultValue(schemaType)));
}

for (var entry : schemaType.schema.keyOverrides.entrySet()) {
recipe.setValue(entry.getKey(), Cast.to(entry.getValue().getDefaultValue(schemaType)));
}
}
}
Loading

0 comments on commit c87c458

Please sign in to comment.