Skip to content

Commit

Permalink
Dont allow Item.of(Ingredient) anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Oct 20, 2024
1 parent c2b49a4 commit 2f62e5d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ neoforge_version=21.1.42
parchment_mc_version=1.21
parchment_mapping_version=2024.07.28
rhino_version=2101.2.5-build.54
tiny_server_version=1.0.0-build.16
tiny_server_version=1.0.0-build.18
gif_lib_version=1.7

architectury_version=13.0.6
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
import com.mojang.serialization.JsonOps;
import dev.latvian.mods.kubejs.util.JsonUtils;
import dev.latvian.mods.kubejs.util.UtilsJS;
import dev.latvian.mods.rhino.type.EnumTypeInfo;
import dev.latvian.mods.rhino.type.TypeInfo;
import io.netty.buffer.ByteBuf;
import net.minecraft.core.Registry;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.Utf8String;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.StringRepresentable;
import net.minecraft.util.valueproviders.IntProvider;
Expand Down Expand Up @@ -61,6 +65,30 @@ public void encode(ByteBuf buffer, @Nullable JsonElement value) {
}
};

Codec<Class<?>> ENUM_CLASS_CODEC = Codec.STRING.flatXmap(str -> {
try {
var c = Class.forName(str);

if (!c.isEnum()) {
return DataResult.error(() -> "Class '" + str + "' is not an enum");
}

return DataResult.success(c);
} catch (ClassNotFoundException e) {
return DataResult.error(() -> "Could not find enum class: " + str);
}
}, c -> DataResult.success(c.getName()));

Codec<EnumTypeInfo> ENUM_TYPE_INFO_CODEC = ENUM_CLASS_CODEC.flatXmap(c -> {
if (TypeInfo.of(c) instanceof EnumTypeInfo info) {
return DataResult.success(info);
} else {
return DataResult.error(() -> "Class " + c.getTypeName() + " is not an enum!");
}
}, info -> DataResult.success(info.asClass()));

Codec<ResourceKey<? extends Registry<?>>> REGISTRY_KEY = ResourceLocation.CODEC.xmap(ResourceKey::createRegistryKey, ResourceKey::location);

static <E> Codec<E> stringResolverCodec(Function<E, String> toStringFunction, Function<String, E> fromStringFunction) {
return Codec.STRING.flatXmap(str -> Optional.ofNullable(fromStringFunction.apply(str))
.map(DataResult::success)
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/item/ItemStackJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.JsonOps;
import dev.latvian.mods.kubejs.component.DataComponentWrapper;
import dev.latvian.mods.kubejs.error.KubeRuntimeException;
import dev.latvian.mods.kubejs.ingredient.RegExIngredient;
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.kubejs.util.Lazy;
Expand Down Expand Up @@ -89,8 +90,8 @@ static ItemStack wrap(RegistryAccessContainer registries, @Nullable Object o) {
return ItemStack.EMPTY;
} else if (o instanceof ItemStack stack) {
return stack.isEmpty() ? ItemStack.EMPTY : stack;
} else if (o instanceof Ingredient ingr) {
return ingr.kjs$getFirst();
} else if (o instanceof Ingredient) {
throw new KubeRuntimeException("Use .first of an ingredient to get its ItemStack!");
} else if (o instanceof ResourceLocation id) {
var item = BuiltInRegistries.ITEM.get(id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import dev.latvian.mods.kubejs.KubeJSCodecs;
import dev.latvian.mods.kubejs.error.KubeRuntimeException;
import dev.latvian.mods.kubejs.util.Cast;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.rhino.Context;
Expand Down Expand Up @@ -54,7 +55,13 @@ public <T> void register(Class<T> target, ContextFromFunction<T> factory) {
}

public <T> void register(Class<T> target, RegistriesFromFunction<T> factory) {
typeWrappers.register(target, (cx, from, t) -> factory.apply(RegistryAccessContainer.of(cx), from));
typeWrappers.register(target, (cx, from, t) -> {
try {
return factory.apply(RegistryAccessContainer.of(cx), from);
} catch (KubeRuntimeException ex) {
throw ex.source(SourceLine.of(cx));
}
});
}

public <T> void register(Class<T> target, TypeWrapperValidator validator, DirectTypeWrapperFactory<T> factory) {
Expand Down

0 comments on commit 2f62e5d

Please sign in to comment.