Skip to content

Commit

Permalink
Rewrote recipe components Part I
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jun 2, 2024
1 parent 674874f commit 1cfd88e
Show file tree
Hide file tree
Showing 116 changed files with 1,469 additions and 1,742 deletions.
103 changes: 30 additions & 73 deletions src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
import dev.latvian.mods.kubejs.recipe.component.BooleanComponent;
import dev.latvian.mods.kubejs.recipe.component.EnumComponent;
import dev.latvian.mods.kubejs.recipe.component.ItemComponents;
import dev.latvian.mods.kubejs.recipe.component.MapRecipeComponent;
import dev.latvian.mods.kubejs.recipe.component.NestedRecipeComponent;
import dev.latvian.mods.kubejs.recipe.component.NumberComponent;
import dev.latvian.mods.kubejs.recipe.component.RegistryComponent;
import dev.latvian.mods.kubejs.recipe.component.StringComponent;
Expand All @@ -98,7 +100,7 @@
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.RegisterRecipeSchemasEvent;
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchemaRegistryKubeEvent;
import dev.latvian.mods.kubejs.recipe.schema.minecraft.CookingRecipeSchema;
import dev.latvian.mods.kubejs.recipe.schema.minecraft.SmithingTransformRecipeSchema;
import dev.latvian.mods.kubejs.recipe.schema.minecraft.SmithingTrimRecipeSchema;
Expand Down Expand Up @@ -518,7 +520,7 @@ public void registerTypeWrappers(WrapperRegistry registry) {
}

@Override
public void registerRecipeSchemas(RegisterRecipeSchemasEvent event) {
public void registerRecipeSchemas(RecipeSchemaRegistryKubeEvent event) {
event.namespace("kubejs")
.shaped("shaped")
.shapeless("shapeless")
Expand All @@ -534,68 +536,27 @@ public void registerRecipeSchemas(RegisterRecipeSchemasEvent event) {
.register("campfire_cooking", CookingRecipeSchema.SCHEMA)
.register("smithing_transform", SmithingTransformRecipeSchema.SCHEMA)
.register("smithing_trim", SmithingTrimRecipeSchema.SCHEMA)
.special("crafting_special_armordye")
.special("crafting_special_shulkerboxcoloring")
.special("crafting_special_bannerduplicate")
.special("crafting_special_suspiciousstew")
.special("crafting_special_bookcloning")
.special("crafting_special_mapextending")
.special("crafting_special_tippedarrow")
.special("crafting_special_firework_star")
.special("crafting_special_shielddecoration")
.special("crafting_special_firework_star_fade")
.special("crafting_special_firework_rocket")
.special("crafting_special_mapcloning")
.special("crafting_special_repairitem")
;

event.namespace("extendedcrafting")
.shaped("shaped_table")
.shapeless("shapeless_table")
;

event.mapRecipe("extendedCraftingShaped", "extendedcrafting:shaped_table");
event.mapRecipe("extendedCraftingShapeless", "extendedcrafting:shapeless_table");

event.namespace("dankstorage")
.shaped("upgrade")
;

event.mapRecipe("dankStorageUpgrade", "dankstorage:upgrade");
}

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

event.register("intNumber", NumberComponent.INT);
event.register("longNumber", NumberComponent.LONG);
event.register("floatNumber", NumberComponent.FLOAT);
event.register("doubleNumber", NumberComponent.DOUBLE);

event.register("anyIntNumber", NumberComponent.ANY_INT);
event.register("anyLongNumber", NumberComponent.ANY_LONG);
event.register("anyFloatNumber", NumberComponent.ANY_FLOAT);
event.register("anyDoubleNumber", NumberComponent.ANY_DOUBLE);

event.registerDynamic("intNumberRange", NumberComponent.DYNAMIC_INT);
event.registerDynamic("longNumberRange", NumberComponent.DYNAMIC_LONG);
event.registerDynamic("floatNumberRange", NumberComponent.DYNAMIC_FLOAT);
event.registerDynamic("doubleNumberRange", NumberComponent.DYNAMIC_DOUBLE);
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("anyString", StringComponent.ANY);
event.register("nonEmptyString", StringComponent.NON_EMPTY);
event.register("nonBlankString", StringComponent.NON_BLANK);
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.registerDynamic("filteredString", StringComponent.DYNAMIC);

event.register("inputItem", ItemComponents.INPUT);
event.register("inputItemArray", ItemComponents.INPUT_ARRAY);
event.register("unwrappedInputItemArray", ItemComponents.UNWRAPPED_INPUT_ARRAY);
event.register("outputItem", ItemComponents.OUTPUT);
event.register("outputItemArray", ItemComponents.OUTPUT_ARRAY);
event.register("outputItemIdWithCount", ItemComponents.OUTPUT_ID_WITH_COUNT);
event.register("ingredient", ItemComponents.INPUT);
event.register("unwrapped_ingredient_list", ItemComponents.UNWRAPPED_INPUT_LIST);
event.register("item_stack", ItemComponents.OUTPUT);

// event.register("inputFluid", FluidComponents.INPUT);
// event.register("inputFluidArray", FluidComponents.INPUT_ARRAY);
Expand All @@ -606,31 +567,27 @@ public void registerRecipeComponents(RecipeComponentFactoryRegistryEvent event)
// event.register("outputFluidOrItem", FluidComponents.OUTPUT_OR_ITEM);
// event.register("outputFluidOrItemArray", FluidComponents.OUTPUT_OR_ITEM_ARRAY);

event.register("inputBlock", BlockComponent.INPUT);
event.register("outputBlock", BlockComponent.OUTPUT);
event.register("otherBlock", BlockComponent.BLOCK);
event.register("block", BlockComponent.BLOCK);

event.register("inputBlockState", BlockStateComponent.INPUT);
event.register("outputBlockState", BlockStateComponent.OUTPUT);
event.register("otherBlockState", BlockStateComponent.BLOCK);
event.register("inputBlockStateString", BlockStateComponent.INPUT_STRING);
event.register("outputBlockStateString", BlockStateComponent.OUTPUT_STRING);
event.register("otherBlockStateString", BlockStateComponent.BLOCK_STRING);
event.register("block_state", BlockStateComponent.BLOCK);
event.register("block_state_string", BlockStateComponent.BLOCK_STRING);

event.register("ticks", TimeComponent.TICKS);
event.register("seconds", TimeComponent.SECONDS);
event.register("minutes", TimeComponent.MINUTES);
event.registerDynamic("time", TimeComponent.DYNAMIC);

event.register("blockTag", TagKeyComponent.BLOCK);
event.register("itemTag", TagKeyComponent.ITEM);
event.register("fluidTag", TagKeyComponent.FLUID);
event.register("entityTypeTag", TagKeyComponent.ENTITY_TYPE);
event.register("biomeTag", TagKeyComponent.BIOME);
event.registerDynamic("tag", TagKeyComponent.DYNAMIC);

event.registerDynamic("registryObject", RegistryComponent.DYNAMIC);
event.registerDynamic("enum", EnumComponent.DYNAMIC);
event.register("hours", TimeComponent.HOURS);

event.register("block_tag", TagKeyComponent.BLOCK);
event.register("item_tag", TagKeyComponent.ITEM);
event.register("fluid_tag", TagKeyComponent.FLUID);
event.register("entity_type_tag", TagKeyComponent.ENTITY_TYPE);
event.register("biome_tag", TagKeyComponent.BIOME);
event.register("tag", TagKeyComponent.FACTORY);

event.register("registry_element", RegistryComponent.FACTORY);
event.register("enum", EnumComponent.FACTORY);
event.register("nested_recipe", NestedRecipeComponent.RECIPE);
event.register("map", MapRecipeComponent.FACTORY);
}

@Override
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/KubeJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import dev.latvian.mods.kubejs.item.creativetab.CreativeTabKubeEvent;
import dev.latvian.mods.kubejs.item.creativetab.KubeJSCreativeTabs;
import dev.latvian.mods.kubejs.recipe.KubeJSRecipeSerializers;
import dev.latvian.mods.kubejs.recipe.schema.RecipeNamespace;
import dev.latvian.mods.kubejs.registry.BuilderTypeRegistryHandler;
import dev.latvian.mods.kubejs.registry.RegistryKubeEvent;
import dev.latvian.mods.kubejs.registry.RegistryType;
Expand Down Expand Up @@ -227,7 +226,6 @@ public static void loadComplete(FMLLoadCompleteEvent event) {
NeoForge.EVENT_BUS.post(new ScriptsLoadedEvent());
StartupEvents.POST_INIT.post(ScriptType.STARTUP, KubeStartupEvent.BASIC);
UtilsJS.postModificationEvents();
RecipeNamespace.getAll();

if (!ConsoleJS.STARTUP.errors.isEmpty()) {
var list = new ArrayList<String>();
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
import net.minecraft.world.level.storage.loot.providers.number.NumberProvider;
import net.minecraft.world.level.storage.loot.providers.number.NumberProviders;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;

public interface KubeJSCodecs {
Codec<Character> CHARACTER = Codec.STRING.xmap(str -> str.charAt(0), Object::toString);

StreamCodec<? super RegistryFriendlyByteBuf, IntProvider> INT_PROVIDER_STREAM_CODEC = ByteBufCodecs.fromCodecWithRegistries(IntProvider.CODEC);

static <E> Codec<E> stringResolverCodec(Function<E, String> toStringFunction, Function<String, E> fromStringFunction) {
Expand Down Expand Up @@ -60,4 +63,9 @@ static <T> String getUniqueId(T input, Codec<T> codec) {
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));
}
}
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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.RegisterRecipeSchemasEvent;
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchemaRegistryKubeEvent;
import dev.latvian.mods.kubejs.registry.BuilderTypeRegistry;
import dev.latvian.mods.kubejs.script.BindingsEvent;
import dev.latvian.mods.kubejs.script.ScriptType;
Expand Down Expand Up @@ -61,7 +61,7 @@ default void registerBindings(BindingsEvent event) {
default void registerTypeWrappers(WrapperRegistry registry) {
}

default void registerRecipeSchemas(RegisterRecipeSchemasEvent event) {
default void registerRecipeSchemas(RecipeSchemaRegistryKubeEvent event) {
}

default void registerRecipeComponents(RecipeComponentFactoryRegistryEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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.RecipeSchemaRegistryKubeEvent;
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.KubeJSPaths;
import dev.latvian.mods.kubejs.bindings.event.ServerEvents;
import dev.latvian.mods.kubejs.core.ReloadableServerResourcesKJS;
import dev.latvian.mods.kubejs.core.WithPersistentData;
import dev.latvian.mods.kubejs.event.EventGroups;
import dev.latvian.mods.kubejs.event.EventResult;
Expand Down Expand Up @@ -344,7 +343,7 @@ private static int dumpEvents(CommandSourceStack source) {
builder.append("\n\n");
}

var scriptManager = ((ReloadableServerResourcesKJS) source.getServer().getServerResources().managers()).kjs$getServerScriptManager();
var scriptManager = source.getServer().getServerResources().managers().kjs$getServerScriptManager();
var cx = (KubeJSContext) scriptManager.contextFactory.enter();

var members = JavaMembers.lookupClass(cx, cx.topLevelScope, eventType, null, false);
Expand Down Expand Up @@ -671,7 +670,7 @@ private static int reloadStartup(CommandSourceStack source) {

private static int reloadServer(CommandSourceStack source) {
var resources = source.getServer().getServerResources();
((ReloadableServerResourcesKJS) resources.managers()).kjs$getServerScriptManager().reload(resources.resourceManager());
resources.managers().kjs$getServerScriptManager().reload(resources.resourceManager());
source.sendSuccess(() -> Component.literal("Done! To reload recipes, tags, loot tables and other datapack things, run ")
.append(Component.literal("'/reload'")
.kjs$clickRunCommand("/reload")
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/core/EntityKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.latvian.mods.kubejs.registry.RegistryInfo;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.script.ScriptTypeHolder;
import dev.latvian.mods.kubejs.util.UtilsJS;
import dev.latvian.mods.rhino.util.HideFromJS;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.core.BlockPos;
Expand All @@ -28,11 +29,15 @@
import net.minecraft.world.entity.projectile.ProjectileUtil;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.scores.Team;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;

@RemapPrefixForJS("kjs$")
public interface EntityKJS extends WithPersistentData, MessageSenderKJS, ScriptTypeHolder {
Expand Down Expand Up @@ -323,6 +328,28 @@ public interface EntityKJS extends WithPersistentData, MessageSenderKJS, ScriptT
return kjs$rayTrace(distance, true);
}

@Nullable
default Entity kjs$rayTraceEntity(double distance, Predicate<Entity> filter) {
double d0 = Double.MAX_VALUE;
Entity entity = null;

var start = kjs$self().getEyePosition();
var end = start.add(kjs$self().getLookAngle().scale(distance));

for (Entity entity1 : kjs$self().level().getEntities(kjs$self(), new AABB(start, end), filter == null ? UtilsJS.ALWAYS_TRUE : filter)) {
double d1;
AABB aabb = entity1.getBoundingBox();
Optional<Vec3> optional = aabb.clip(start, end);
if (!optional.isPresent() || !((d1 = start.distanceToSqr(optional.get())) < d0)) {
continue;
}
entity = entity1;
d0 = d1;
}

return entity;
}

@Nullable
@HideFromJS
default CompoundTag kjs$getRawPersistentData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.core.Holder;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
Expand All @@ -16,9 +17,11 @@
import net.minecraft.world.entity.ai.behavior.BehaviorUtils;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Predicate;

@RemapPrefixForJS("kjs$")
public interface LivingEntityKJS extends EntityKJS {
Expand Down Expand Up @@ -223,6 +226,11 @@ public interface LivingEntityKJS extends EntityKJS {
return kjs$rayTrace(kjs$getReachDistance());
}

@Nullable
default Entity kjs$rayTraceEntity(Predicate<Entity> filter) {
return kjs$rayTraceEntity(kjs$getReachDistance(), filter);
}

default double kjs$getAttributeTotalValue(Holder<Attribute> attribute) {
AttributeInstance instance = kjs$self().getAttribute(attribute);
if (instance != null) {
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/dev/latvian/mods/kubejs/core/RecipeHolderKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import dev.latvian.mods.kubejs.recipe.ItemMatch;
import dev.latvian.mods.kubejs.recipe.OutputReplacement;
import dev.latvian.mods.kubejs.recipe.ReplacementMatch;
import dev.latvian.mods.kubejs.recipe.schema.RecipeNamespace;
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchema;
import dev.latvian.mods.kubejs.registry.RegistryInfo;
import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.kubejs.server.ServerScriptManager;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.core.HolderLookup;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;
Expand Down Expand Up @@ -45,9 +46,9 @@ public interface RecipeHolderKJS extends RecipeLikeKJS {
}

@Override
default RecipeSchema kjs$getSchema() {
default RecipeSchema kjs$getSchema(Context cx) {
var s = kjs$getType();
return RecipeNamespace.getAll().get(s.getNamespace()).get(s.getPath()).schema;
return ((ServerScriptManager) ((KubeJSContext) cx).kjsFactory.manager).recipeSchemaStorage.namespaces.get(s.getNamespace()).get(s.getPath()).schema;
}

@Override
Expand All @@ -56,7 +57,7 @@ public interface RecipeHolderKJS extends RecipeLikeKJS {
}

@Override
default boolean hasInput(HolderLookup.Provider registries, ReplacementMatch match) {
default boolean hasInput(Context cx, ReplacementMatch match) {
if (match instanceof ItemMatch m) {
for (var in : kjs$getRecipe().getIngredients()) {
if (m.contains(in)) {
Expand All @@ -69,14 +70,14 @@ default boolean hasInput(HolderLookup.Provider registries, ReplacementMatch matc
}

@Override
default boolean replaceInput(ReplacementMatch match, InputReplacement with) {
default boolean replaceInput(Context cx, ReplacementMatch match, InputReplacement with) {
return false;
}

@Override
default boolean hasOutput(HolderLookup.Provider registries, ReplacementMatch match) {
default boolean hasOutput(Context cx, ReplacementMatch match) {
if (match instanceof ItemMatch m) {
var result = kjs$getRecipe().getResultItem(registries);
var result = kjs$getRecipe().getResultItem(((KubeJSContext) cx).getRegistries());
//noinspection ConstantValue
return result != null && result != ItemStack.EMPTY && !result.isEmpty() && m.contains(result);
}
Expand All @@ -85,7 +86,7 @@ default boolean hasOutput(HolderLookup.Provider registries, ReplacementMatch mat
}

@Override
default boolean replaceOutput(ReplacementMatch match, OutputReplacement with) {
default boolean replaceOutput(Context cx, ReplacementMatch match, OutputReplacement with) {
return false;
}
}
Loading

0 comments on commit 1cfd88e

Please sign in to comment.