Skip to content

Commit

Permalink
Added RecipeComponent#asConditionalList and asConditionalListOrSelf, …
Browse files Browse the repository at this point in the history
…changed component array syntax, `?` inside `[?]` means conditional, `?` on outside `[]?` means list or self. Can be combined as `[?]?`, fixed optional values not being initialized, moved `/kubejs export` command to `/kubejs export debug`
  • Loading branch information
LatvianModder committed Jul 23, 2024
1 parent 93e4aff commit d8a6589
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
10 changes: 8 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.JsonOps;
Expand Down Expand Up @@ -64,8 +65,13 @@ static JsonElement numberProviderJson(NumberProvider gen) {
return toJsonOrThrow(gen, NumberProviders.CODEC);
}

// TODO: Check if this is correct
static <T> Codec<List<T>> listOfOrSelf(Codec<T> codec) {
return Codec.withAlternative(codec.listOf(), codec.xmap(List::of, List::getFirst));
return listOfOrSelf(codec.listOf(), codec);
}

// TODO: Check if this is correct
static <T> Codec<List<T>> listOfOrSelf(Codec<List<T>> listCodec, Codec<T> codec) {
return Codec.either(listCodec, codec).xmap(either -> either.map(Function.identity(), List::of), Either::left);
// return Codec.withAlternative(listCodec, codec.xmap(List::of, List::getFirst));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
)
.then(Commands.literal("export")
.requires(spOrOP)
.executes(context -> export(context.getSource()))
.then(Commands.literal("debug")
.executes(context -> export(context.getSource()))
)
.then(Commands.literal("pack-zips")
.executes(context -> exportPacks(context.getSource(), true))
)
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public void serialize() {
}
}

@Nullable
public <T> T getValue(RecipeKey<T> key) {
var v = valueMap.getHolder(key);

Expand Down Expand Up @@ -160,11 +161,11 @@ public void initValues(boolean created) {

if (created) {
for (var v : valueMap.holders) {
if (v.key.alwaysWrite || !v.key.optional()) {
if (v.key.alwaysWrite) {
v.value = Cast.to(v.key.optional.getDefaultValue(type.schemaType));
}
if (v.key.optional()) {
v.value = Cast.to(v.key.optional.getDefaultValue(type.schemaType));
}

if (v.key.alwaysWrite) {
v.write();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@
import dev.latvian.mods.kubejs.recipe.match.ReplacementMatchInfo;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.TypeInfo;
import net.neoforged.neoforge.common.conditions.ConditionalOps;
import net.neoforged.neoforge.common.util.NeoForgeExtraCodecs;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public record ListRecipeComponent<T>(RecipeComponent<T> component, boolean canWriteSelf, TypeInfo listTypeInfo, Codec<List<T>> listCodec) implements RecipeComponent<List<T>> {
static <L> ListRecipeComponent<L> create(RecipeComponent<L> component, boolean canWriteSelf) {
public record ListRecipeComponent<T>(RecipeComponent<T> component, boolean canWriteSelf, TypeInfo listTypeInfo, Codec<List<T>> listCodec, boolean conditional) implements RecipeComponent<List<T>> {
static <L> ListRecipeComponent<L> create(RecipeComponent<L> component, boolean canWriteSelf, boolean conditional) {
var typeInfo = component.typeInfo();
var codec = component.codec();
var listCodec = conditional ? NeoForgeExtraCodecs.listWithOptionalElements(ConditionalOps.createConditionalCodec(codec)) : codec.listOf();

if (canWriteSelf) {
return new ListRecipeComponent<>(component, true, TypeInfo.RAW_LIST.withParams(typeInfo).or(typeInfo), KubeJSCodecs.listOfOrSelf(codec));
return new ListRecipeComponent<>(component, true, TypeInfo.RAW_LIST.withParams(typeInfo).or(typeInfo), KubeJSCodecs.listOfOrSelf(listCodec, codec), conditional);
} else {
return new ListRecipeComponent<>(component, false, TypeInfo.RAW_LIST.withParams(typeInfo), codec.listOf());
return new ListRecipeComponent<>(component, false, TypeInfo.RAW_LIST.withParams(typeInfo), listCodec, conditional);
}
}

Expand Down Expand Up @@ -141,7 +144,7 @@ public void buildUniqueId(UniqueIdBuilder builder, List<T> value) {

@Override
public String toString() {
return component + (canWriteSelf ? "[?]" : "[]");
return component + (canWriteSelf ? "[?]" : "[]") + (conditional ? "?" : "");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,19 @@ default void buildUniqueId(UniqueIdBuilder builder, T value) {
}

default RecipeComponent<List<T>> asList() {
return ListRecipeComponent.create(this, false);
return ListRecipeComponent.create(this, false, false);
}

default RecipeComponent<List<T>> asListOrSelf() {
return ListRecipeComponent.create(this, true);
return ListRecipeComponent.create(this, true, false);
}

default RecipeComponent<List<T>> asConditionalList() {
return ListRecipeComponent.create(this, false, true);
}

default RecipeComponent<List<T>> asConditionalListOrSelf() {
return ListRecipeComponent.create(this, true, true);
}

default RecipeComponent<T> orSelf() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,21 @@ public RecipeComponent<?> readComponent(RegistryAccessContainer registries, Stri
while (reader.canRead() && reader.peek() == '[') {
reader.skip();
reader.skipWhitespace();
boolean self = reader.canRead() && reader.peek() == '?';
boolean conditional = reader.canRead() && reader.peek() == '?';

if (self) {
if (conditional) {
reader.skip();
reader.skipWhitespace();
}

reader.expect(']');
component = self ? component.asListOrSelf() : component.asList();

if (reader.canRead() && reader.peek() == '?') {
reader.skip();
component = conditional ? component.asConditionalListOrSelf() : component.asListOrSelf();
} else {
component = conditional ? component.asConditionalList() : component.asList();
}
}

return component;
Expand Down

0 comments on commit d8a6589

Please sign in to comment.