Skip to content

Commit

Permalink
Updated dependencies, renamed StaticRegistries to RegistryAccessConta…
Browse files Browse the repository at this point in the history
…iner, replaced Context with RAC in some wrappers. Added RecipeViewerEvents (WIP), new fluid registry (WIP)
  • Loading branch information
LatvianModder committed Jun 17, 2024
1 parent 4797b4d commit f0efecb
Show file tree
Hide file tree
Showing 56 changed files with 655 additions and 317 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ dependencies {
modApi("dev.latvian.mods:rhino:$rhino_version") { transitive(false) }

// todo: reevaluate if we want to depend on jei
modCompileOnly("mezz.jei:jei-1.20.4-neoforge:$jei_version") { transitive(false) }
modCompileOnly("mezz.jei:jei-1.20.6-neoforge:$jei_version") { transitive(false) }
modCompileOnly("me.shedaniel:RoughlyEnoughItems-neoforge:$rei_version") { transitive(false) }
}

Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ modrinth_id=umyGl7zF
minecraft_version=1.21
mod_version=2100.7.0

neoforge_version=21.0.8-beta
neoforge_version=21.0.14-beta
parchment_version=2024.06.02
rhino_version=2100.2.5-build.28
architectury_version=13.0.1

rei_version=15.0.728
rei_version=16.0.729
#rei_comp_version=9.0.16
#forge_bookshelf_version=11.0.3
#forge_gamestages_version=7.+
jei_version=17.3.0.52
jei_version=18.0.0.62
13 changes: 9 additions & 4 deletions src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dev.latvian.mods.kubejs.bindings.ItemWrapper;
import dev.latvian.mods.kubejs.bindings.JavaWrapper;
import dev.latvian.mods.kubejs.bindings.KMath;
import dev.latvian.mods.kubejs.bindings.ParticleOptionsWrapper;
import dev.latvian.mods.kubejs.bindings.SizedIngredientWrapper;
import dev.latvian.mods.kubejs.bindings.TextWrapper;
import dev.latvian.mods.kubejs.bindings.UUIDWrapper;
Expand Down Expand Up @@ -51,9 +52,9 @@
import dev.latvian.mods.kubejs.event.EventGroupWrapper;
import dev.latvian.mods.kubejs.event.EventGroups;
import dev.latvian.mods.kubejs.fluid.FluidBuilder;
import dev.latvian.mods.kubejs.fluid.FluidTypeBuilder;
import dev.latvian.mods.kubejs.fluid.FluidWrapper;
import dev.latvian.mods.kubejs.helpers.IngredientHelper;
import dev.latvian.mods.kubejs.integration.RecipeViewerEvents;
import dev.latvian.mods.kubejs.item.ArmorMaterialBuilder;
import dev.latvian.mods.kubejs.item.ChancedItem;
import dev.latvian.mods.kubejs.item.ItemBuilder;
Expand Down Expand Up @@ -114,6 +115,7 @@
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.viewer.RecipeViewerEvents;
import dev.latvian.mods.kubejs.registry.BuilderTypeRegistry;
import dev.latvian.mods.kubejs.registry.RegistryInfo;
import dev.latvian.mods.kubejs.script.BindingRegistry;
Expand Down Expand Up @@ -191,6 +193,7 @@
import net.neoforged.neoforge.common.crafting.SizedIngredient;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.crafting.FluidIngredient;
import net.neoforged.neoforge.registries.NeoForgeRegistries;
import org.joml.Matrix3f;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
Expand Down Expand Up @@ -262,6 +265,7 @@ public void registerBuilderTypes(BuilderTypeRegistry registry) {
reg.add("smithing_template", SmithingTemplateItemBuilder.class, SmithingTemplateItemBuilder::new);
});

registry.addDefault(NeoForgeRegistries.Keys.FLUID_TYPES, FluidTypeBuilder.class, FluidTypeBuilder::new);
registry.addDefault(Registries.FLUID, FluidBuilder.class, FluidBuilder::new);
// FIXME registry.addDefault(Registries.ENCHANTMENT, EnchantmentBuilder.class, EnchantmentBuilder::new);
registry.addDefault(Registries.MOB_EFFECT, BasicMobEffect.Builder.class, BasicMobEffect.Builder::new);
Expand Down Expand Up @@ -420,6 +424,7 @@ public void registerBindings(BindingRegistry bindings) {
bindings.add("Notification", NotificationToastData.class);
bindings.add("SizedIngredient", SizedIngredientWrapper.class);
bindings.add("ChancedItem", ChancedItem.class);
bindings.add("ParticleOptions", ParticleOptionsWrapper.class);

bindings.add("Fluid", FluidWrapper.class);

Expand Down Expand Up @@ -510,8 +515,8 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(Set.class, ListJS::ofSet);
registry.register(ItemStack.class, ItemStackJS::wrap);
registry.register(Ingredient.class, IngredientJS::wrap);
registry.register(InputReplacement.class, InputReplacement::of);
registry.register(OutputReplacement.class, OutputReplacement::of);
registry.register(InputReplacement.class, InputReplacement::wrap);
registry.register(OutputReplacement.class, OutputReplacement::wrap);
registry.register(SizedIngredient.class, SizedIngredientWrapper::wrap);
registry.register(BlockStatePredicate.class, BlockStatePredicate::of);
registry.register(RuleTest.class, BlockStatePredicate::ruleTestOf);
Expand All @@ -527,7 +532,7 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(Stat.class, PlayerStatsJS::statOf);
registry.register(MapColor.class, MapColorHelper::of);
registry.register(SoundType.class, SoundTypeWrapper.INSTANCE);
registry.register(ParticleOptions.class, UtilsWrapper::particleOptions);
registry.register(ParticleOptions.class, ParticleOptionsWrapper::wrap);
registry.register(ItemTintFunction.class, ItemTintFunction::of);
registry.register(BlockTintFunction.class, BlockTintFunction::of);
registry.register(RegistryInfo.class, RegistryInfo::wrap);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/CommonProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.mojang.datafixers.util.Pair;
import dev.latvian.mods.kubejs.util.StaticRegistries;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;

Expand Down Expand Up @@ -66,7 +66,7 @@ public void setPackMode(String s) {

public Component getCreativeModeTabName() {
if (!creativeModeTabName.isJsonNull()) {
return ComponentSerialization.CODEC.decode(StaticRegistries.BUILTIN.json(), creativeModeTabName).result().map(Pair::getFirst).orElse(KubeJS.NAME_COMPONENT);
return ComponentSerialization.CODEC.decode(RegistryAccessContainer.BUILTIN.json(), creativeModeTabName).result().map(Pair::getFirst).orElse(KubeJS.NAME_COMPONENT);
}

return KubeJS.NAME_COMPONENT;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import dev.latvian.mods.kubejs.recipe.schema.RecipeFactoryRegistry;
import dev.latvian.mods.kubejs.recipe.schema.RecipeMappingRegistry;
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchemaRegistry;
import dev.latvian.mods.kubejs.recipe.viewer.RecipeViewerEntryType;
import dev.latvian.mods.kubejs.registry.BuilderTypeRegistry;
import dev.latvian.mods.kubejs.script.BindingRegistry;
import dev.latvian.mods.kubejs.script.ScriptType;
Expand All @@ -29,6 +30,7 @@

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public interface KubeJSPlugin {
default void init() {
Expand Down Expand Up @@ -79,6 +81,9 @@ default void registerBlockEntityAttachments(List<BlockEntityAttachmentType> type
default void registerIngredientActionTypes(IngredientActionTypeRegistry registry) {
}

default void registerRecipeViewerEntryTypes(Consumer<RecipeViewerEntryType> registry) {
}

default void attachServerData(AttachedData<MinecraftServer> event) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,12 @@ public static BlockSetType setTypeOf(Context cx, Object from, TypeInfo target) {
default -> (BlockSetType) ((RecordTypeInfo) target).wrap(cx, from, target);
};
}

@Info("Parses a block state from the input string. May throw for invalid inputs!")
static BlockState parseBlockState(Object o) {
if (o instanceof BlockState bs) {
return bs;
}
return o == null ? Blocks.AIR.defaultBlockState() : BlockWrapper.parseBlockState(o.toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package dev.latvian.mods.kubejs.bindings;

import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;

public class DamageSourceWrapper {
public static DamageSource of(Context cx, Object from) {
public static DamageSource of(RegistryAccessContainer registries, Object from) {
return switch (from) {
case DamageSource source -> source;
case Player player -> ((KubeJSContext) cx).getRegistries().damageSources().get().playerAttack(player);
case LivingEntity livingEntity -> ((KubeJSContext) cx).getRegistries().damageSources().get().mobAttack(livingEntity);
case null, default -> ((KubeJSContext) cx).getRegistries().damageSources().get().source(ResourceKey.create(Registries.DAMAGE_TYPE, ID.mc(from)));
case Player player -> registries.damageSources().get().playerAttack(player);
case LivingEntity livingEntity -> registries.damageSources().get().mobAttack(livingEntity);
case null, default -> registries.damageSources().get().source(ResourceKey.create(Registries.DAMAGE_TYPE, ID.mc(from)));
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.serialization.DynamicOps;
import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.kubejs.util.Cast;
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponentType;
Expand Down Expand Up @@ -158,21 +157,19 @@ static DataComponentType<?> readComponentType(StringReader stringReader) throws
}
}

static DataComponentMap mapOf(Context cx, Object o) {
static DataComponentMap mapOf(RegistryAccessContainer registries, Object o) {
try {
return readMap(((KubeJSContext) cx).getNbtOps(), new StringReader(o.toString()));
return readMap(registries.nbt(), new StringReader(o.toString()));
} catch (CommandSyntaxException ex) {
((KubeJSContext) cx).getConsole().error("Error parsing DataComponentMap", ex);
return DataComponentMap.EMPTY;
throw new RuntimeException("Error parsing DataComponentMap from " + o, ex);
}
}

static DataComponentPatch patchOf(Context cx, Object o) {
static DataComponentPatch patchOf(RegistryAccessContainer registries, Object o) {
try {
return readPatch(((KubeJSContext) cx).getNbtOps(), new StringReader(o.toString()));
return readPatch(registries.nbt(), new StringReader(o.toString()));
} catch (CommandSyntaxException ex) {
((KubeJSContext) cx).getConsole().error("Error parsing DataComponentPatch", ex);
return DataComponentPatch.EMPTY;
throw new RuntimeException("Error parsing DataComponentPatch from " + o, ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.latvian.mods.kubejs.bindings;

import com.google.gson.JsonElement;
import com.mojang.brigadier.StringReader;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import net.minecraft.commands.arguments.ParticleArgument;
import net.minecraft.core.particles.DustParticleOptions;
import net.minecraft.core.particles.ParticleOptions;
import org.joml.Vector3f;

public interface ParticleOptionsWrapper {
DustParticleOptions ERROR = new DustParticleOptions(new Vector3f(0F, 0F, 0F), 1F);

static ParticleOptions wrap(RegistryAccessContainer registries, Object o) {
if (o instanceof ParticleOptions po) {
return po;
} else if (o != null) {
try {
var reader = new StringReader(o instanceof JsonElement j ? j.getAsString() : o.toString());
return ParticleArgument.readParticle(reader, registries.access());
} catch (Exception ex) {
throw new RuntimeException("Failed to parse ParticleOptions from " + o, ex);
}
}

return ERROR;
}

static ParticleOptions create(ParticleOptions options) {
return options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.serialization.DynamicOps;
import dev.latvian.mods.kubejs.item.ingredient.IngredientJS;
import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.rhino.type.TypeInfo;
import net.minecraft.nbt.Tag;
import net.minecraft.tags.TagKey;
Expand Down Expand Up @@ -35,7 +34,7 @@ static SizedIngredient ofTag(TagKey<Item> tag, int count) {
return SizedIngredient.of(tag, count);
}

static SizedIngredient wrap(Context cx, Object from) {
static SizedIngredient wrap(RegistryAccessContainer registries, Object from) {
if (from instanceof SizedIngredient s) {
return s;
} else if (from instanceof Ingredient ingredient) {
Expand All @@ -46,13 +45,13 @@ static SizedIngredient wrap(Context cx, Object from) {
return Ingredient.of(item).kjs$asStack();
} else if (from instanceof CharSequence) {
try {
return read(((KubeJSContext) cx).getNbtOps(), new StringReader(from.toString()));
return read(registries.nbt(), new StringReader(from.toString()));
} catch (Exception ex) {
return empty;
}
}

return IngredientJS.wrap(cx, from).kjs$asStack();
return IngredientJS.wrap(registries, from).kjs$asStack();
}

static SizedIngredient read(DynamicOps<Tag> registryOps, StringReader reader) throws CommandSyntaxException {
Expand Down
46 changes: 0 additions & 46 deletions src/main/java/dev/latvian/mods/kubejs/bindings/UtilsWrapper.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
package dev.latvian.mods.kubejs.bindings;

import com.google.gson.JsonElement;
import com.mojang.brigadier.StringReader;
import dev.latvian.mods.kubejs.registry.RegistryInfo;
import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.util.CountingMap;
import dev.latvian.mods.kubejs.util.Lazy;
import dev.latvian.mods.kubejs.util.RegExpJS;
import dev.latvian.mods.kubejs.util.UtilsJS;
import dev.latvian.mods.kubejs.util.WrappedJS;
import dev.latvian.mods.rhino.Context;
import net.minecraft.Util;
import net.minecraft.commands.arguments.ParticleArgument;
import net.minecraft.core.particles.DustParticleOptions;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector3f;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -38,13 +28,6 @@

@Info("A collection of utilities")
public interface UtilsWrapper {
DustParticleOptions ERROR_PARTICLE = new DustParticleOptions(new Vector3f(0F, 0F, 0F), 1F);

@Info("Immediately run the passed runnable function in a try-catch block, and log the exception if it throws")
static void queueIO(Runnable runnable) {
UtilsJS.queueIO(runnable);
}

@Info("Get a Random, for generating random numbers. Note this will always return the same Random instance")
static Random getRandom() {
return UtilsJS.RANDOM;
Expand Down Expand Up @@ -80,12 +63,6 @@ static CountingMap newCountingMap() {
return new CountingMap();
}

@Info("Typewraps the input string to a ResourceLocation. Format should be namespace:path")
static ResourceLocation id(ResourceLocation id) {
// TypeWrapper will convert any object into RL
return id;
}

@Info("Returns a regex pattern of the input")
static Pattern regex(Object s) {
var pattern = RegExpJS.wrap(s);
Expand Down Expand Up @@ -206,27 +183,4 @@ static String snakeCaseToCamelCase(String string) {
static String snakeCaseToTitleCase(String string) {
return UtilsJS.snakeCaseToTitleCase(string);
}

static ParticleOptions particleOptions(Context cx, Object o) {
if (o instanceof ParticleOptions po) {
return po;
} else if (o != null) {
try {
var reader = new StringReader(o instanceof JsonElement j ? j.getAsString() : o.toString());
return ParticleArgument.readParticle(reader, ((KubeJSContext) cx).getRegistries().access());
} catch (Exception ex) {
((KubeJSContext) cx).getConsole().warn("Failed to parse ParticleOptions from " + o + ": " + ex);
}
}

return ERROR_PARTICLE;
}

@Info("Parses a block state from the input string. May throw for invalid inputs!")
static BlockState parseBlockState(Object o) {
if (o instanceof BlockState bs) {
return bs;
}
return o == null ? Blocks.AIR.defaultBlockState() : BlockWrapper.parseBlockState(o.toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.latvian.mods.kubejs.block.entity;

import dev.latvian.mods.kubejs.item.ingredient.IngredientJS;
import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.rhino.ScriptRuntime;
import dev.latvian.mods.rhino.type.JSObjectTypeInfo;
import dev.latvian.mods.rhino.type.JSOptionalParam;
Expand Down Expand Up @@ -28,7 +29,7 @@ public class InventoryAttachment extends SimpleContainer implements BlockEntityA
(cx, map) -> {
var width = ScriptRuntime.toInt32(cx, map.get("width"));
var height = ScriptRuntime.toInt32(cx, map.get("height"));
var inputFilter = map.containsKey("inputFilter") ? IngredientJS.wrap(cx, map.get("inputFilter")) : null;
var inputFilter = map.containsKey("inputFilter") ? IngredientJS.wrap(((KubeJSContext) cx).getRegistries(), map.get("inputFilter")) : null;
return new InventoryAttachmentFactory(width, height, inputFilter);
}
);
Expand Down
Loading

0 comments on commit f0efecb

Please sign in to comment.