Skip to content

Commit

Permalink
Rewrote recipe components Part II
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jun 4, 2024
1 parent 1cfd88e commit d1cf81d
Show file tree
Hide file tree
Showing 52 changed files with 1,095 additions and 573 deletions.
59 changes: 29 additions & 30 deletions src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@
import dev.latvian.mods.kubejs.recipe.component.BlockComponent;
import dev.latvian.mods.kubejs.recipe.component.BlockStateComponent;
import dev.latvian.mods.kubejs.recipe.component.BooleanComponent;
import dev.latvian.mods.kubejs.recipe.component.CharacterComponent;
import dev.latvian.mods.kubejs.recipe.component.EnumComponent;
import dev.latvian.mods.kubejs.recipe.component.ItemComponents;
import dev.latvian.mods.kubejs.recipe.component.IngredientComponent;
import dev.latvian.mods.kubejs.recipe.component.ItemStackComponent;
import dev.latvian.mods.kubejs.recipe.component.MapRecipeComponent;
import dev.latvian.mods.kubejs.recipe.component.NestedRecipeComponent;
import dev.latvian.mods.kubejs.recipe.component.NumberComponent;
Expand All @@ -100,11 +102,12 @@
import dev.latvian.mods.kubejs.recipe.filter.RecipeFilter;
import dev.latvian.mods.kubejs.recipe.ingredientaction.IngredientActionFilter;
import dev.latvian.mods.kubejs.recipe.schema.RecipeComponentFactoryRegistryEvent;
import dev.latvian.mods.kubejs.recipe.schema.RecipeFactoryRegistryKubeEvent;
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchemaRegistryKubeEvent;
import dev.latvian.mods.kubejs.recipe.schema.minecraft.CookingRecipeSchema;
import dev.latvian.mods.kubejs.recipe.schema.UnknownKubeRecipe;
import dev.latvian.mods.kubejs.recipe.schema.minecraft.ShapedKubeRecipe;
import dev.latvian.mods.kubejs.recipe.schema.minecraft.ShapelessKubeRecipe;
import dev.latvian.mods.kubejs.recipe.schema.minecraft.SmithingTransformRecipeSchema;
import dev.latvian.mods.kubejs.recipe.schema.minecraft.SmithingTrimRecipeSchema;
import dev.latvian.mods.kubejs.recipe.schema.minecraft.StonecuttingRecipeSchema;
import dev.latvian.mods.kubejs.registry.BuilderTypeRegistry;
import dev.latvian.mods.kubejs.registry.RegistryInfo;
import dev.latvian.mods.kubejs.script.BindingsEvent;
Expand Down Expand Up @@ -520,43 +523,32 @@ public void registerTypeWrappers(WrapperRegistry registry) {
}

@Override
public void registerRecipeSchemas(RecipeSchemaRegistryKubeEvent event) {
event.namespace("kubejs")
.shaped("shaped")
.shapeless("shapeless")
;

event.namespace("minecraft")
.shaped("crafting_shaped")
.shapeless("crafting_shapeless")
.register("stonecutting", StonecuttingRecipeSchema.SCHEMA)
.register("smelting", CookingRecipeSchema.SCHEMA)
.register("blasting", CookingRecipeSchema.SCHEMA)
.register("smoking", CookingRecipeSchema.SCHEMA)
.register("campfire_cooking", CookingRecipeSchema.SCHEMA)
.register("smithing_transform", SmithingTransformRecipeSchema.SCHEMA)
.register("smithing_trim", SmithingTrimRecipeSchema.SCHEMA)
;
public void registerRecipeFactories(RecipeFactoryRegistryKubeEvent event) {
event.register(UnknownKubeRecipe.RECIPE_FACTORY);
event.register(ShapedKubeRecipe.RECIPE_FACTORY);
event.register(ShapelessKubeRecipe.RECIPE_FACTORY);
}

@Override
public void registerRecipeComponents(RecipeComponentFactoryRegistryEvent event) {
event.register("bool", BooleanComponent.BOOLEAN);
event.register("boolean", BooleanComponent.BOOLEAN);

event.register("int_range", NumberComponent.INT_FACTORY);
event.register("long_range", NumberComponent.LONG_FACTORY);
event.register("float_range", NumberComponent.FLOAT_FACTORY);
event.register("double_range", NumberComponent.DOUBLE_FACTORY);
event.register("int", NumberComponent.INT_FACTORY);
event.register("long", NumberComponent.LONG_FACTORY);
event.register("float", NumberComponent.FLOAT_FACTORY);
event.register("double", NumberComponent.DOUBLE_FACTORY);

event.register("string", StringComponent.ANY);
event.register("non_empty_string", StringComponent.NON_EMPTY);
event.register("non_blank_string", StringComponent.NON_BLANK);
event.register("id", StringComponent.ID);
event.register("character", StringComponent.CHARACTER);
event.register("character", CharacterComponent.CHARACTER);

event.register("ingredient", ItemComponents.INPUT);
event.register("unwrapped_ingredient_list", ItemComponents.UNWRAPPED_INPUT_LIST);
event.register("item_stack", ItemComponents.OUTPUT);
event.register("ingredient", IngredientComponent.INGREDIENT);
event.register("non_empty_ingredient", IngredientComponent.NON_EMPTY_INGREDIENT);
event.register("unwrapped_ingredient_list", IngredientComponent.UNWRAPPED_INGREDIENT_LIST);
event.register("item_stack", ItemStackComponent.ITEM_STACK);
event.register("strict_item_stack", ItemStackComponent.STRICT_ITEM_STACK);

// event.register("inputFluid", FluidComponents.INPUT);
// event.register("inputFluidArray", FluidComponents.INPUT_ARRAY);
Expand Down Expand Up @@ -590,6 +582,13 @@ public void registerRecipeComponents(RecipeComponentFactoryRegistryEvent event)
event.register("map", MapRecipeComponent.FACTORY);
}

@Override
public void registerRecipeSchemas(RecipeSchemaRegistryKubeEvent event) {
event.namespace("minecraft")
.register("smithing_transform", SmithingTransformRecipeSchema.SCHEMA)
;
}

@Override
public void registerBlockEntityAttachments(List<BlockEntityAttachmentType> types) {
types.add(InventoryAttachment.TYPE);
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/dev/latvian/mods/kubejs/KubeJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import dev.latvian.mods.kubejs.generator.DataJsonGenerator;
import dev.latvian.mods.kubejs.recipe.RecipesKubeEvent;
import dev.latvian.mods.kubejs.recipe.schema.RecipeComponentFactoryRegistryEvent;
import dev.latvian.mods.kubejs.recipe.schema.RecipeFactoryRegistryKubeEvent;
import dev.latvian.mods.kubejs.recipe.schema.RecipeMappingRegistryKubeEvent;
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchemaRegistryKubeEvent;
import dev.latvian.mods.kubejs.registry.BuilderTypeRegistry;
import dev.latvian.mods.kubejs.script.BindingsEvent;
Expand Down Expand Up @@ -61,12 +63,18 @@ default void registerBindings(BindingsEvent event) {
default void registerTypeWrappers(WrapperRegistry registry) {
}

default void registerRecipeSchemas(RecipeSchemaRegistryKubeEvent event) {
default void registerRecipeFactories(RecipeFactoryRegistryKubeEvent event) {
}

default void registerRecipeMappings(RecipeMappingRegistryKubeEvent event) {
}

default void registerRecipeComponents(RecipeComponentFactoryRegistryEvent event) {
}

default void registerRecipeSchemas(RecipeSchemaRegistryKubeEvent event) {
}

default void registerBlockEntityAttachments(List<BlockEntityAttachmentType> types) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import dev.latvian.mods.kubejs.recipe.AfterRecipesLoadedKubeEvent;
import dev.latvian.mods.kubejs.recipe.CompostableRecipesKubeEvent;
import dev.latvian.mods.kubejs.recipe.RecipesKubeEvent;
import dev.latvian.mods.kubejs.recipe.schema.RecipeMappingRegistryKubeEvent;
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchemaRegistryKubeEvent;
import dev.latvian.mods.kubejs.recipe.special.SpecialRecipeSerializerManager;
import dev.latvian.mods.kubejs.script.data.DataPackKubeEvent;
import dev.latvian.mods.kubejs.server.CommandKubeEvent;
Expand All @@ -29,6 +31,8 @@ public interface ServerEvents {
EventHandler COMMAND_REGISTRY = GROUP.server("commandRegistry", () -> CommandRegistryKubeEvent.class);
SpecializedEventHandler<String> COMMAND = GROUP.server("command", Extra.STRING, () -> CommandKubeEvent.class).hasResult();
SpecializedEventHandler<String> CUSTOM_COMMAND = GROUP.server("customCommand", Extra.STRING, () -> CustomCommandKubeEvent.class).hasResult();
EventHandler RECIPE_MAPPING_REGISTRY = GROUP.server("recipeMappingRegistry", () -> RecipeMappingRegistryKubeEvent.class);
EventHandler RECIPE_SCHEMA_REGISTRY = GROUP.server("recipeSchemaRegistry", () -> RecipeSchemaRegistryKubeEvent.class);
EventHandler RECIPES = GROUP.server("recipes", () -> RecipesKubeEvent.class);
EventHandler RECIPES_AFTER_LOADED = GROUP.server("afterRecipes", () -> AfterRecipesLoadedKubeEvent.class);
EventHandler SPECIAL_RECIPES = GROUP.server("specialRecipeSerializers", () -> SpecialRecipeSerializerManager.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import dev.latvian.mods.kubejs.event.KubeStartupEvent;
import dev.latvian.mods.kubejs.event.SpecializedEventHandler;
import dev.latvian.mods.kubejs.item.creativetab.CreativeTabKubeEvent;
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchemaRegistryKubeEvent;
import dev.latvian.mods.kubejs.registry.RegistryKubeEvent;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
Expand All @@ -18,6 +17,5 @@ public interface StartupEvents {
EventHandler INIT = GROUP.startup("init", () -> KubeStartupEvent.class);
EventHandler POST_INIT = GROUP.startup("postInit", () -> KubeStartupEvent.class);
SpecializedEventHandler<ResourceKey<Registry<?>>> REGISTRY = GROUP.startup("registry", Extra.REGISTRY, () -> RegistryKubeEvent.class).required();
EventHandler RECIPE_SCHEMA_REGISTRY = GROUP.startup("recipeSchemaRegistry", () -> RecipeSchemaRegistryKubeEvent.class);
SpecializedEventHandler<ResourceLocation> MODIFY_CREATIVE_TAB = GROUP.startup("modifyCreativeTab", Extra.ID, () -> CreativeTabKubeEvent.class).required();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ public abstract class RecipeManagerMixin implements RecipeManagerKJS {
@Unique
private ReloadableServerResourcesKJS kjs$resources;

@Inject(method = "apply*", at = @At("HEAD"), cancellable = true)
@Inject(method = "apply(Ljava/util/Map;Lnet/minecraft/server/packs/resources/ResourceManager;Lnet/minecraft/util/profiling/ProfilerFiller;)V", at = @At("HEAD"), cancellable = true)
private void customRecipesHead(Map<ResourceLocation, JsonElement> map, ResourceManager resourceManager, ProfilerFiller profiler, CallbackInfo ci) {
if (ServerEvents.COMPOSTABLE_RECIPES.hasListeners()) {
ServerEvents.COMPOSTABLE_RECIPES.post(ScriptType.SERVER, new CompostableRecipesKubeEvent());
}

var manager = kjs$resources.kjs$getServerScriptManager();
manager.recipeSchemaStorage.fireEvents(resourceManager);

if (manager.recipesEvent != null) {
manager.recipeSchemaStorage.fireEvents();
manager.recipesEvent.post(Cast.to(this), map);
manager.recipesEvent = null;
ci.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import dev.latvian.mods.rhino.BaseFunction;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.Scriptable;
import dev.latvian.mods.rhino.util.HideFromJS;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.regex.Pattern;

public class ErroredKubeRecipe extends KubeRecipe {
Expand Down Expand Up @@ -60,8 +60,9 @@ public boolean replaceOutput(Context cx, ReplacementMatch match, OutputReplaceme
}

@Override
public Map<String, RecipeComponentValue<?>> getAllValueMap() {
return Map.of();
@HideFromJS
public RecipeComponentValue<?>[] getRecipeComponentValues() {
return RecipeComponentValue.EMPTY_ARRAY;
}

@Override
Expand Down
38 changes: 3 additions & 35 deletions src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import dev.latvian.mods.kubejs.DevProperties;
import dev.latvian.mods.kubejs.core.RecipeLikeKJS;
import dev.latvian.mods.kubejs.helpers.RecipeHelper;
import dev.latvian.mods.kubejs.item.InputItem;
import dev.latvian.mods.kubejs.item.OutputItem;
import dev.latvian.mods.kubejs.recipe.component.MissingComponentException;
import dev.latvian.mods.kubejs.recipe.component.RecipeComponentBuilderMap;
import dev.latvian.mods.kubejs.recipe.component.RecipeComponentValue;
Expand Down Expand Up @@ -41,9 +39,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KubeRecipe implements RecipeLikeKJS, CustomJavaToJsWrapper {
public static boolean itemErrors = false;
Expand All @@ -57,7 +53,6 @@ public class KubeRecipe implements RecipeLikeKJS, CustomJavaToJsWrapper {
private RecipeComponentBuilderMap valueMap = RecipeComponentBuilderMap.EMPTY;
private RecipeComponentValue<?>[] inputValues;
private RecipeComponentValue<?>[] outputValues;
private Map<String, RecipeComponentValue<?>> allValueMap;

public JsonObject originalJson = null;
private MutableObject<Recipe<?>> originalRecipe = null;
Expand Down Expand Up @@ -173,18 +168,9 @@ public void initValues(boolean created) {
}
}

public Map<String, RecipeComponentValue<?>> getAllValueMap() {
if (allValueMap == null) {
allValueMap = new HashMap<>();

for (var v : valueMap.holders) {
for (var n : v.key.names) {
allValueMap.put(n, v);
}
}
}

return allValueMap;
@HideFromJS
public RecipeComponentValue<?>[] getRecipeComponentValues() {
return valueMap.holders;
}

/**
Expand Down Expand Up @@ -609,22 +595,4 @@ public final KubeRecipe modifyResult(ModifyRecipeResultCallback callback) {
save();
return this;
}

// Default component serialization methods for ItemComponents and FluidComponents //

// -- Items -- //

public boolean inputItemHasPriority(Object from) {
return from instanceof InputItem || from instanceof ItemStack || from instanceof Ingredient || !InputItem.of(from).isEmpty();
}

public boolean outputItemHasPriority(Object from) {
return from instanceof OutputItem || from instanceof ItemStack || !OutputItem.of(from).isEmpty();
}

public OutputItem readOutputItem(Object from) {
return OutputItem.of(from);
}

// -- End -- //
}
45 changes: 36 additions & 9 deletions src/main/java/dev/latvian/mods/kubejs/recipe/RecipeFunction.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.latvian.mods.kubejs.recipe;

import dev.latvian.mods.kubejs.recipe.component.RecipeComponentValueFunction;
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchemaFunction;
import dev.latvian.mods.rhino.BaseFunction;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.NativeJavaObject;
import dev.latvian.mods.rhino.Scriptable;
Expand All @@ -11,19 +13,44 @@

public class RecipeFunction extends NativeJavaObject {
public final KubeRecipe recipe;
public final Map<String, RecipeComponentValueFunction> builderFunctions;
public final Map<String, BaseFunction> builderFunctions;

private static boolean isValidIdentifier(char[] name) {
if (name.length == 0 || !Character.isJavaIdentifierStart(name[0])) {
return false;
}

for (int i = 1; i < name.length; i++) {
if (!Character.isJavaIdentifierPart(name[i])) {
return false;
}
}

return true;
}

public RecipeFunction(Context cx, Scriptable scope, TypeInfo staticType, KubeRecipe recipe) {
super(scope, recipe, staticType, cx);
this.recipe = recipe;
var map = recipe.getAllValueMap();
this.builderFunctions = new HashMap<>(map.size());

for (var entry : map.entrySet()) {
var key = entry.getKey();
var value = entry.getValue();
if (!value.key.noBuilders) {
builderFunctions.put(key, new RecipeComponentValueFunction(recipe, value));
this.builderFunctions = new HashMap<>();

for (var value : recipe.getRecipeComponentValues()) {
var names = value.key.functionNames == null ? value.key.names : value.key.functionNames;

if (!names.isEmpty()) {
var func = new RecipeComponentValueFunction(recipe, value);

for (var name : names) {
if (isValidIdentifier(name.toCharArray())) {
builderFunctions.put(name, func);
}
}
}
}

for (var entry : recipe.type.schemaType.schema.functions.entrySet()) {
if (isValidIdentifier(entry.getKey().toCharArray())) {
builderFunctions.put(entry.getKey(), new RecipeSchemaFunction.JSFunction(recipe, entry.getValue()));
}
}
}
Expand Down
Loading

0 comments on commit d1cf81d

Please sign in to comment.