Skip to content

Commit

Permalink
Updated rhino, added ServerEvents.modifyRecipeResult replacing callba…
Browse files Browse the repository at this point in the history
…ck function in recipe with ID
  • Loading branch information
LatvianModder committed Jun 12, 2024
1 parent 8367c89 commit 37dcf00
Show file tree
Hide file tree
Showing 63 changed files with 371 additions and 256 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod_version=2006.7.0

neoforge_version=20.6.115
parchment_version=2024.05.01
rhino_version=2006.2.5-build.23
rhino_version=2006.2.5-build.25
architectury_version=12.1.3

rei_version=15.0.728
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import dev.latvian.mods.kubejs.item.ChancedIngredient;
import dev.latvian.mods.kubejs.item.ChancedItem;
import dev.latvian.mods.kubejs.item.ItemBuilder;
import dev.latvian.mods.kubejs.item.ItemEnchantmentsWrapper;
import dev.latvian.mods.kubejs.item.ItemStackJS;
import dev.latvian.mods.kubejs.item.ItemTintFunction;
import dev.latvian.mods.kubejs.item.creativetab.CreativeTabBuilder;
Expand Down Expand Up @@ -118,7 +119,8 @@
import dev.latvian.mods.kubejs.script.BindingRegistry;
import dev.latvian.mods.kubejs.script.PlatformWrapper;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.script.WrapperRegistry;
import dev.latvian.mods.kubejs.script.TypeDescriptionRegistry;
import dev.latvian.mods.kubejs.script.TypeWrapperRegistry;
import dev.latvian.mods.kubejs.server.ScheduledServerEvent;
import dev.latvian.mods.kubejs.server.ServerScriptManager;
import dev.latvian.mods.kubejs.util.ClassFilter;
Expand All @@ -139,6 +141,8 @@
import dev.latvian.mods.kubejs.util.TimeJS;
import dev.latvian.mods.kubejs.util.UtilsJS;
import dev.latvian.mods.kubejs.util.registrypredicate.RegistryPredicate;
import dev.latvian.mods.rhino.type.RecordTypeInfo;
import dev.latvian.mods.rhino.type.TypeInfo;
import dev.latvian.mods.unit.Unit;
import net.minecraft.commands.arguments.selector.EntitySelector;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -169,6 +173,7 @@
import net.minecraft.world.item.component.Fireworks;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.SoundType;
Expand Down Expand Up @@ -448,7 +453,7 @@ public void registerBindings(BindingRegistry bindings) {
}

@Override
public void registerTypeWrappers(WrapperRegistry registry) {
public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(RegistryPredicate.class, RegistryPredicate::of);

// Java / Minecraft //
Expand Down Expand Up @@ -480,6 +485,7 @@ public void registerTypeWrappers(WrapperRegistry registry) {
registry.register(Item.class, ItemStackJS::getRawItem);
registry.register(ItemLike.class, ItemStackJS::getRawItem);
registry.registerEnumFromStringCodec(MobCategory.class, MobCategory.CODEC);
registry.register(ItemEnchantments.class, ItemEnchantmentsWrapper::from);

registry.register(AABB.class, AABBWrapper::wrap);
registry.register(IntProvider.class, KubeJSTypeWrappers::intProviderOf);
Expand Down Expand Up @@ -531,6 +537,11 @@ public void registerTypeWrappers(WrapperRegistry registry) {
registry.registerMapCodec(ChancedIngredient.class, ChancedIngredient.CODEC);
}

@Override
public void registerTypeDescriptions(TypeDescriptionRegistry registry) {
registry.register(SlotFilter.class, ((RecordTypeInfo) TypeInfo.of(SlotFilter.class)).createCombinedType(TypeInfo.INT, IngredientJS.TYPE_INFO));
}

@Override
public void registerRecipeFactories(RecipeFactoryRegistry registry) {
registry.register(UnknownKubeRecipe.RECIPE_FACTORY);
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import dev.latvian.mods.kubejs.registry.BuilderTypeRegistry;
import dev.latvian.mods.kubejs.script.BindingRegistry;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.script.WrapperRegistry;
import dev.latvian.mods.kubejs.script.TypeDescriptionRegistry;
import dev.latvian.mods.kubejs.script.TypeWrapperRegistry;
import dev.latvian.mods.kubejs.server.DataExport;
import dev.latvian.mods.kubejs.util.AttachedData;
import dev.latvian.mods.kubejs.util.ClassFilter;
Expand Down Expand Up @@ -58,7 +59,10 @@ default void registerClasses(ScriptType type, ClassFilter filter) {
default void registerBindings(BindingRegistry bindings) {
}

default void registerTypeWrappers(WrapperRegistry registry) {
default void registerTypeWrappers(TypeWrapperRegistry registry) {
}

default void registerTypeDescriptions(TypeDescriptionRegistry registry) {
}

default void registerRecipeFactories(RecipeFactoryRegistry registry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ static SizedIngredient read(DynamicOps<Tag> registryOps, StringReader reader) th

if (StringReader.isAllowedNumber(reader.peek())) {
count = Mth.ceil(reader.readDouble());
reader.skipWhitespace();
reader.expect('x');
reader.skipWhitespace();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dev.latvian.mods.kubejs.item.ItemModificationKubeEvent;
import dev.latvian.mods.kubejs.item.ItemPickedUpKubeEvent;
import dev.latvian.mods.kubejs.item.ItemSmeltedKubeEvent;
import dev.latvian.mods.kubejs.item.ItemStackJS;
import dev.latvian.mods.kubejs.item.ItemTooltipKubeEvent;
import dev.latvian.mods.kubejs.item.custom.ItemToolTierRegistryKubeEvent;
import net.minecraft.core.registries.Registries;
Expand All @@ -26,7 +27,7 @@ public interface ItemEvents {

EventHandler MODIFICATION = GROUP.startup("modification", () -> ItemModificationKubeEvent.class);
EventHandler TOOL_TIER_REGISTRY = GROUP.startup("toolTierRegistry", () -> ItemToolTierRegistryKubeEvent.class);
SpecializedEventHandler<ResourceKey<Item>> RIGHT_CLICKED = GROUP.common("rightClicked", SUPPORTS_ITEM, () -> ItemClickedKubeEvent.class).hasResult();
SpecializedEventHandler<ResourceKey<Item>> RIGHT_CLICKED = GROUP.common("rightClicked", SUPPORTS_ITEM, () -> ItemClickedKubeEvent.class).hasResult(ItemStackJS.TYPE_INFO);
SpecializedEventHandler<ResourceKey<Item>> CAN_PICK_UP = GROUP.common("canPickUp", SUPPORTS_ITEM, () -> ItemPickedUpKubeEvent.class).hasResult();
SpecializedEventHandler<ResourceKey<Item>> PICKED_UP = GROUP.common("pickedUp", SUPPORTS_ITEM, () -> ItemPickedUpKubeEvent.class);
SpecializedEventHandler<ResourceKey<Item>> DROPPED = GROUP.common("dropped", SUPPORTS_ITEM, () -> ItemDroppedKubeEvent.class).hasResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import dev.latvian.mods.kubejs.event.EventHandler;
import dev.latvian.mods.kubejs.event.Extra;
import dev.latvian.mods.kubejs.event.SpecializedEventHandler;
import dev.latvian.mods.kubejs.item.ItemStackJS;
import dev.latvian.mods.kubejs.recipe.AfterRecipesLoadedKubeEvent;
import dev.latvian.mods.kubejs.recipe.CompostableRecipesKubeEvent;
import dev.latvian.mods.kubejs.recipe.ModifyRecipeResultKubeEvent;
import dev.latvian.mods.kubejs.recipe.RecipesKubeEvent;
import dev.latvian.mods.kubejs.recipe.schema.RecipeMappingRegistry;
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchemaRegistry;
Expand Down Expand Up @@ -37,4 +39,5 @@ public interface ServerEvents {
EventHandler RECIPES_AFTER_LOADED = GROUP.server("afterRecipes", () -> AfterRecipesLoadedKubeEvent.class);
EventHandler SPECIAL_RECIPES = GROUP.server("specialRecipeSerializers", () -> SpecialRecipeSerializerManager.class);
EventHandler COMPOSTABLE_RECIPES = GROUP.server("compostableRecipes", () -> CompostableRecipesKubeEvent.class);
SpecializedEventHandler<String> MODIFY_RECIPE_RESULT = GROUP.server("modifyRecipeResult", Extra.STRING, () -> ModifyRecipeResultKubeEvent.class).required().hasResult(ItemStackJS.TYPE_INFO);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import dev.latvian.mods.kubejs.registry.RegistryInfo;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.typings.ReturnsSelf;
import dev.latvian.mods.kubejs.util.Cast;
import dev.latvian.mods.kubejs.util.ConsoleJS;
import dev.latvian.mods.rhino.util.HideFromJS;
import dev.latvian.mods.rhino.util.ReturnsSelf;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
import net.minecraft.network.chat.Component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.bindings.event.BlockEvents;
import dev.latvian.mods.kubejs.generator.AssetJsonGenerator;
import dev.latvian.mods.kubejs.typings.ReturnsSelf;
import dev.latvian.mods.rhino.util.ReturnsSelf;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import dev.latvian.mods.kubejs.client.ModelGenerator;
import dev.latvian.mods.kubejs.client.VariantBlockStateGenerator;
import dev.latvian.mods.kubejs.generator.AssetJsonGenerator;
import dev.latvian.mods.kubejs.typings.ReturnsSelf;
import dev.latvian.mods.rhino.util.ReturnsSelf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.block.Block;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import dev.latvian.mods.kubejs.client.VariantBlockStateGenerator;
import dev.latvian.mods.kubejs.generator.AssetJsonGenerator;
import dev.latvian.mods.kubejs.typings.ReturnsSelf;
import dev.latvian.mods.rhino.util.ReturnsSelf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.block.Block;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import dev.latvian.mods.kubejs.generator.AssetJsonGenerator;
import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.typings.ReturnsSelf;
import dev.latvian.mods.rhino.util.ReturnsSelf;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.latvian.mods.kubejs.block.BlockBuilder;
import dev.latvian.mods.kubejs.color.Color;
import dev.latvian.mods.kubejs.typings.ReturnsSelf;
import dev.latvian.mods.rhino.util.ReturnsSelf;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.ColorRGBA;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import dev.latvian.mods.kubejs.client.ModelGenerator;
import dev.latvian.mods.kubejs.client.VariantBlockStateGenerator;
import dev.latvian.mods.kubejs.generator.AssetJsonGenerator;
import dev.latvian.mods.kubejs.typings.ReturnsSelf;
import dev.latvian.mods.rhino.util.ReturnsSelf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.block.Block;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import dev.latvian.mods.kubejs.client.ModelGenerator;
import dev.latvian.mods.kubejs.client.VariantBlockStateGenerator;
import dev.latvian.mods.kubejs.generator.AssetJsonGenerator;
import dev.latvian.mods.kubejs.typings.ReturnsSelf;
import dev.latvian.mods.rhino.util.ReturnsSelf;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import dev.latvian.mods.kubejs.client.ModelGenerator;
import dev.latvian.mods.kubejs.client.VariantBlockStateGenerator;
import dev.latvian.mods.kubejs.generator.AssetJsonGenerator;
import dev.latvian.mods.kubejs.typings.ReturnsSelf;
import dev.latvian.mods.rhino.util.ReturnsSelf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.block.Block;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ private static int dumpEvents(CommandSourceStack source) {

builder.append("- Valid script types: ").append(handler.scriptTypePredicate.getValidTypes()).append("\n\n");

builder.append("- Has result? ").append(handler.getHasResult() ? UNICODE_TICK : UNICODE_CROSS).append("\n\n");
builder.append("- Has result? ").append(handler.getResult() != null ? UNICODE_TICK : UNICODE_CROSS).append("\n\n");

builder.append("- Event class: ");

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/core/EnchantmentKJS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.latvian.mods.kubejs.core;

import dev.latvian.mods.kubejs.registry.RegistryInfo;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.world.item.enchantment.Enchantment;

@RemapPrefixForJS("kjs$")
public interface EnchantmentKJS extends RegistryObjectKJS<Enchantment> {
@Override
default RegistryInfo<Enchantment> kjs$getKubeRegistry() {
return RegistryInfo.ENCHANTMENT;
}
}
15 changes: 9 additions & 6 deletions src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
import dev.latvian.mods.kubejs.recipe.ReplacementMatch;
import dev.latvian.mods.kubejs.registry.RegistryInfo;
import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.kubejs.typings.ReturnsSelf;
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.kubejs.util.WithCodec;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.Undefined;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import dev.latvian.mods.rhino.util.ReturnsSelf;
import dev.latvian.mods.rhino.util.SpecialEquality;
import dev.latvian.mods.rhino.util.ToStringJS;
import net.minecraft.core.Holder;
Expand All @@ -36,6 +36,7 @@
import net.minecraft.world.item.component.ItemLore;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
Expand Down Expand Up @@ -203,7 +204,7 @@ default boolean specialEquals(Context cx, Object o, boolean shallow) {
}

default ItemEnchantments kjs$getEnchantments() {
return kjs$self().get(DataComponents.ENCHANTMENTS);
return EnchantmentHelper.getEnchantmentsForCrafting(kjs$self());
}

default boolean kjs$hasEnchantment(Enchantment enchantment, int level) {
Expand All @@ -219,12 +220,14 @@ default boolean specialEquals(Context cx, Object o, boolean shallow) {
}

@ReturnsSelf(copy = true)
default ItemStack kjs$enchant(Map<Enchantment, Integer> enchantments) {
default ItemStack kjs$enchant(ItemEnchantments enchantments) {
var is = kjs$self().copy();

for (var entry : enchantments.entrySet()) {
is.enchant(entry.getKey(), entry.getValue());
}
EnchantmentHelper.updateEnchantments(is, mutable -> {
for (var entry : enchantments.entrySet()) {
mutable.upgrade(entry.getKey().value(), entry.getValue());
}
});

return is;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package dev.latvian.mods.kubejs.core.mixin;

import dev.latvian.mods.kubejs.core.EnchantmentKJS;
import net.minecraft.core.Holder;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.enchantment.Enchantment;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;

@Mixin(Enchantment.class)
public abstract class EnchantmentMixin implements EnchantmentKJS {
@Shadow
@Final
private Holder.Reference<Enchantment> builtInRegistryHolder;

@Unique
private ResourceKey<Enchantment> kjs$registryKey;

@Unique
private String kjs$id;

@Override
public Holder<Enchantment> kjs$asHolder() {
return builtInRegistryHolder;
}

@Override
public ResourceKey<Enchantment> kjs$getRegistryKey() {
if (kjs$registryKey == null) {
kjs$registryKey = EnchantmentKJS.super.kjs$getRegistryKey();
}

return kjs$registryKey;
}

@Override
public String kjs$getId() {
if (kjs$id == null) {
kjs$id = EnchantmentKJS.super.kjs$getId();
}

return kjs$id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

Expand All @@ -14,4 +15,8 @@ public abstract class ItemStackMixin implements ItemStackKJS {
@Shadow
@HideFromJS
public abstract void enchant(Enchantment enchantment, int level);

@Shadow
@HideFromJS
public abstract ItemEnchantments getEnchantments();
}
Loading

0 comments on commit 37dcf00

Please sign in to comment.