diff --git a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java index b943e4214..ed3c7b6aa 100644 --- a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java +++ b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java @@ -123,7 +123,6 @@ 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; import dev.latvian.mods.kubejs.script.DataComponentTypeInfoRegistry; import dev.latvian.mods.kubejs.script.PlatformWrapper; @@ -172,6 +171,7 @@ import net.minecraft.util.valueproviders.IntProvider; import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.entity.MobCategory; +import net.minecraft.world.entity.decoration.PaintingVariant; import net.minecraft.world.item.ArmorItem; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; @@ -278,6 +278,8 @@ public void registerBuilderTypes(BuilderTypeRegistry registry) { registry.addDefault(Registries.CREATIVE_MODE_TAB, CreativeTabBuilder.class, CreativeTabBuilder::new); registry.addDefault(Registries.ARMOR_MATERIAL, ArmorMaterialBuilder.class, ArmorMaterialBuilder::new); registry.addDefault(Registries.JUKEBOX_SONG, JukeboxSongBuilder.class, JukeboxSongBuilder::new); + + registry.serverRegistry(Registries.PAINTING_VARIANT, PaintingVariant.DIRECT_CODEC, PaintingVariant.class); } @Override @@ -529,7 +531,6 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) { registry.register(ParticleOptions.class, ParticleOptionsWrapper::wrap); registry.register(ItemTintFunction.class, ItemTintFunction::of); registry.register(BlockTintFunction.class, BlockTintFunction::of); - registry.register(RegistryInfo.class, RegistryInfo::wrap); registry.register(ChancedItem.class, ChancedItem::wrap); // components // diff --git a/src/main/java/dev/latvian/mods/kubejs/KubeJS.java b/src/main/java/dev/latvian/mods/kubejs/KubeJS.java index de3bae05d..621988715 100644 --- a/src/main/java/dev/latvian/mods/kubejs/KubeJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/KubeJS.java @@ -15,7 +15,6 @@ import dev.latvian.mods.kubejs.plugin.KubeJSPlugin; import dev.latvian.mods.kubejs.plugin.KubeJSPlugins; import dev.latvian.mods.kubejs.recipe.KubeJSRecipeSerializers; -import dev.latvian.mods.kubejs.registry.BuilderTypeRegistryHandler; import dev.latvian.mods.kubejs.registry.RegistryKubeEvent; import dev.latvian.mods.kubejs.registry.RegistryType; import dev.latvian.mods.kubejs.script.ConsoleJS; @@ -149,7 +148,6 @@ public KubeJS(IEventBus bus, Dist dist, ModContainer mod) throws Throwable { LOGGER.info("Done in " + pluginTimer.stop()); KubeJSPlugins.forEachPlugin(KubeJSPlugin::init); - KubeJSPlugins.forEachPlugin(new BuilderTypeRegistryHandler(), KubeJSPlugin::registerBuilderTypes); startupScriptManager = new StartupScriptManager(); startupScriptManager.reload(); diff --git a/src/main/java/dev/latvian/mods/kubejs/bindings/BlockWrapper.java b/src/main/java/dev/latvian/mods/kubejs/bindings/BlockWrapper.java index 1fdefb559..78d11845f 100644 --- a/src/main/java/dev/latvian/mods/kubejs/bindings/BlockWrapper.java +++ b/src/main/java/dev/latvian/mods/kubejs/bindings/BlockWrapper.java @@ -3,7 +3,6 @@ import dev.latvian.mods.kubejs.block.predicate.BlockEntityPredicate; import dev.latvian.mods.kubejs.block.predicate.BlockIDPredicate; import dev.latvian.mods.kubejs.block.predicate.BlockPredicate; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.typings.Info; import dev.latvian.mods.kubejs.util.Tags; import dev.latvian.mods.rhino.Context; @@ -75,21 +74,21 @@ public static Map getFacing() { @Info("Gets a Block from a block id") public static Block getBlock(ResourceLocation id) { - return RegistryInfo.BLOCK.getValue(id); + return BuiltInRegistries.BLOCK.get(id); } @Info("Gets a blocks id from the Block") @Nullable public static ResourceLocation getId(Block block) { - return RegistryInfo.BLOCK.getId(block); + return BuiltInRegistries.BLOCK.getKey(block); } @Info("Gets a list of the classname of all registered blocks") public static List getTypeList() { var list = new ArrayList(); - for (var block : RegistryInfo.BLOCK.entrySet()) { - list.add(block.getKey().location().toString()); + for (var block : BuiltInRegistries.BLOCK) { + list.add(block.kjs$getId()); } return list; diff --git a/src/main/java/dev/latvian/mods/kubejs/bindings/ItemWrapper.java b/src/main/java/dev/latvian/mods/kubejs/bindings/ItemWrapper.java index 599e147d6..ef405c836 100644 --- a/src/main/java/dev/latvian/mods/kubejs/bindings/ItemWrapper.java +++ b/src/main/java/dev/latvian/mods/kubejs/bindings/ItemWrapper.java @@ -6,7 +6,6 @@ import com.mojang.authlib.properties.PropertyMap; import com.mojang.serialization.MapCodec; import dev.latvian.mods.kubejs.item.ItemStackJS; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.typings.Info; import dev.latvian.mods.kubejs.util.JsonUtils; import net.minecraft.Util; @@ -74,18 +73,17 @@ static Fireworks fireworks(Fireworks fireworks) { @Info("Gets an Item from an item id") static Item getItem(ResourceLocation id) { - return RegistryInfo.ITEM.getValue(id); + return BuiltInRegistries.ITEM.get(id); } - @Nullable @Info("Gets an items id from the Item") static ResourceLocation getId(Item item) { - return RegistryInfo.ITEM.getId(item); + return BuiltInRegistries.ITEM.getKey(item); } @Info("Checks if the provided item id exists in the registry") static boolean exists(ResourceLocation id) { - return RegistryInfo.ITEM.hasValue(id); + return BuiltInRegistries.ITEM.containsKey(id); } @Info(""" diff --git a/src/main/java/dev/latvian/mods/kubejs/bindings/RegistryWrapper.java b/src/main/java/dev/latvian/mods/kubejs/bindings/RegistryWrapper.java index bd9890660..8228e6207 100644 --- a/src/main/java/dev/latvian/mods/kubejs/bindings/RegistryWrapper.java +++ b/src/main/java/dev/latvian/mods/kubejs/bindings/RegistryWrapper.java @@ -3,6 +3,7 @@ import dev.latvian.mods.kubejs.script.KubeJSContext; import dev.latvian.mods.rhino.Context; import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -13,7 +14,7 @@ import java.util.Set; import java.util.stream.Collectors; -public record RegistryWrapper(Registry registry) implements Iterable { +public record RegistryWrapper(Registry registry, ResourceKey unknownKey) implements Iterable { public static RegistryWrapper of(Context cx, ResourceLocation id) { return ((KubeJSContext) cx).getRegistries().wrapRegistry(id); } @@ -47,6 +48,11 @@ public ResourceLocation getId(T value) { return registry.getKey(value); } + @Nullable + public ResourceKey getKey(T value) { + return registry.getResourceKey(value).orElse(unknownKey); + } + @NotNull @Override public ListIterator iterator() { diff --git a/src/main/java/dev/latvian/mods/kubejs/bindings/UtilsWrapper.java b/src/main/java/dev/latvian/mods/kubejs/bindings/UtilsWrapper.java index ff54f9397..df89b395b 100644 --- a/src/main/java/dev/latvian/mods/kubejs/bindings/UtilsWrapper.java +++ b/src/main/java/dev/latvian/mods/kubejs/bindings/UtilsWrapper.java @@ -1,6 +1,5 @@ package dev.latvian.mods.kubejs.bindings; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.typings.Info; import dev.latvian.mods.kubejs.util.CountingMap; import dev.latvian.mods.kubejs.util.Lazy; @@ -8,6 +7,7 @@ import dev.latvian.mods.kubejs.util.UtilsJS; import dev.latvian.mods.kubejs.util.WrappedJS; import net.minecraft.Util; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundEvent; import net.minecraft.stats.Stat; @@ -94,7 +94,7 @@ static Stat getStat(ResourceLocation id) { @Nullable @Info("Gets a SoundEvent from the id") static SoundEvent getSound(ResourceLocation id) { - return RegistryInfo.SOUND_EVENT.getValue(id); + return BuiltInRegistries.SOUND_EVENT.get(id); } @Info("Gets a random object from the list using the passed in random") diff --git a/src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java index fb3c00ba6..e314e99e3 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java @@ -22,7 +22,6 @@ import dev.latvian.mods.kubejs.item.ItemBuilder; import dev.latvian.mods.kubejs.registry.AdditionalObjectRegistry; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.script.ConsoleJS; import dev.latvian.mods.kubejs.script.ScriptType; import dev.latvian.mods.kubejs.typings.Info; @@ -31,6 +30,7 @@ import dev.latvian.mods.rhino.util.ReturnsSelf; import net.minecraft.core.Direction; import net.minecraft.core.Holder; +import net.minecraft.core.registries.Registries; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.level.block.Block; @@ -148,11 +148,6 @@ public BlockBuilder(ResourceLocation i) { canBeReplacedFunction = null; } - @Override - public final RegistryInfo getRegistryType() { - return RegistryInfo.BLOCK; - } - @Override public Block transformObject(Block obj) { obj.kjs$setBlockBuilder(this); @@ -162,11 +157,11 @@ public Block transformObject(Block obj) { @Override public void createAdditionalObjects(AdditionalObjectRegistry registry) { if (itemBuilder != null) { - registry.add(RegistryInfo.ITEM, itemBuilder); + registry.add(Registries.ITEM, itemBuilder); } if (blockEntityInfo != null) { - registry.add(RegistryInfo.BLOCK_ENTITY_TYPE, new BlockEntityBuilder(id, blockEntityInfo)); + registry.add(Registries.BLOCK_ENTITY_TYPE, new BlockEntityBuilder(id, blockEntityInfo)); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/KubeJSBlockEventHandler.java b/src/main/java/dev/latvian/mods/kubejs/block/KubeJSBlockEventHandler.java index 8e5d40cc7..643fb5ae7 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/KubeJSBlockEventHandler.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/KubeJSBlockEventHandler.java @@ -15,7 +15,7 @@ public class KubeJSBlockEventHandler { @SubscribeEvent public static void rightClick(PlayerInteractEvent.RightClickBlock event) { var state = event.getLevel().getBlockState(event.getPos()); - var key = state.getBlock().kjs$getRegistryKey(); + var key = state.getBlock().kjs$getKey(); if (event.getLevel() instanceof Level level && BlockEvents.RIGHT_CLICKED.hasListeners(key) && !event.getEntity().getCooldowns().isOnCooldown(event.getEntity().getItemInHand(event.getHand()).getItem())) { BlockEvents.RIGHT_CLICKED.post(level, key, new BlockRightClickedKubeEvent(null, event.getEntity(), event.getHand(), event.getPos(), event.getFace(), event.getHitVec())).applyCancel(event); @@ -25,7 +25,7 @@ public static void rightClick(PlayerInteractEvent.RightClickBlock event) { @SubscribeEvent public static void leftClick(PlayerInteractEvent.LeftClickBlock event) { var state = event.getLevel().getBlockState(event.getPos()); - var key = state.getBlock().kjs$getRegistryKey(); + var key = state.getBlock().kjs$getKey(); if (event.getLevel() instanceof Level level && BlockEvents.LEFT_CLICKED.hasListeners(key)) { BlockEvents.LEFT_CLICKED.post(level, key, new BlockLeftClickedKubeEvent(event)).applyCancel(event); @@ -34,7 +34,7 @@ public static void leftClick(PlayerInteractEvent.LeftClickBlock event) { @SubscribeEvent public static void blockBreak(BlockEvent.BreakEvent event) { - var key = event.getState().getBlock().kjs$getRegistryKey(); + var key = event.getState().getBlock().kjs$getKey(); if (event.getLevel() instanceof Level level && BlockEvents.BROKEN.hasListeners(key)) { BlockEvents.BROKEN.post(level, key, new BlockBrokenKubeEvent(event)).applyCancel(event); @@ -43,7 +43,7 @@ public static void blockBreak(BlockEvent.BreakEvent event) { @SubscribeEvent public static void drops(BlockDropsEvent event) { - var key = event.getState().getBlock().kjs$getRegistryKey(); + var key = event.getState().getBlock().kjs$getKey(); if (event.getLevel() instanceof ServerLevel level && BlockEvents.DROPS.hasListeners(key)) { BlockEvents.DROPS.post(level, key, new BlockDropsKubeEvent(event)).applyCancel(event); @@ -52,7 +52,7 @@ public static void drops(BlockDropsEvent event) { @SubscribeEvent public static void blockPlace(BlockEvent.EntityPlaceEvent event) { - var key = event.getPlacedBlock().getBlock().kjs$getRegistryKey(); + var key = event.getPlacedBlock().getBlock().kjs$getKey(); if (event.getLevel() instanceof Level level && BlockEvents.PLACED.hasListeners(key)) { BlockEvents.PLACED.post(level, key, new BlockPlacedKubeEvent(event)).applyCancel(event); @@ -61,7 +61,7 @@ public static void blockPlace(BlockEvent.EntityPlaceEvent event) { @SubscribeEvent public static void farmlandTrample(BlockEvent.FarmlandTrampleEvent event) { - var key = event.getState().getBlock().kjs$getRegistryKey(); + var key = event.getState().getBlock().kjs$getKey(); if (event.getLevel() instanceof Level level && BlockEvents.FARMLAND_TRAMPLED.hasListeners(key)) { BlockEvents.FARMLAND_TRAMPLED.post(level, key, new FarmlandTrampledKubeEvent(event)).applyCancel(event); diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityBuilder.java index 975e21e73..361be32e0 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityBuilder.java @@ -1,7 +1,6 @@ package dev.latvian.mods.kubejs.block.entity; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.level.block.entity.BlockEntityType; @@ -13,11 +12,6 @@ public BlockEntityBuilder(ResourceLocation i, BlockEntityInfo info) { this.info = info; } - @Override - public RegistryInfo getRegistryType() { - return RegistryInfo.BLOCK_ENTITY_TYPE; - } - @Override public BlockEntityType createObject() { info.entityType = BlockEntityType.Builder.of(info::createBlockEntity, info.blockBuilder.get()).build(null); diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityJS.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityJS.java index 6b908f285..7ffdd7419 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityJS.java @@ -38,7 +38,7 @@ public class BlockEntityJS extends BlockEntity { public BlockEntityJS(BlockPos blockPos, BlockState blockState, BlockEntityInfo entityInfo) { super(entityInfo.entityType, blockPos, blockState); this.info = entityInfo; - this.blockKey = blockState.kjs$getRegistryKey(); + this.blockKey = blockState.kjs$getKey(); this.x = blockPos.getX(); this.y = blockPos.getY(); this.z = blockPos.getZ(); diff --git a/src/main/java/dev/latvian/mods/kubejs/block/predicate/BlockEntityPredicate.java b/src/main/java/dev/latvian/mods/kubejs/block/predicate/BlockEntityPredicate.java index 8ebb2b8ed..570757b84 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/predicate/BlockEntityPredicate.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/predicate/BlockEntityPredicate.java @@ -1,7 +1,7 @@ package dev.latvian.mods.kubejs.block.predicate; import dev.latvian.mods.kubejs.level.BlockContainerJS; -import dev.latvian.mods.kubejs.registry.RegistryInfo; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; public class BlockEntityPredicate implements BlockPredicate { @@ -20,7 +20,7 @@ public BlockEntityPredicate data(BlockEntityPredicateDataCheck cd) { @Override public boolean check(BlockContainerJS block) { var tileEntity = block.getEntity(); - return tileEntity != null && id.equals(RegistryInfo.BLOCK_ENTITY_TYPE.getId(tileEntity.getType())) && (checkData == null || checkData.checkData(block.getEntityData())); + return tileEntity != null && id.equals(BuiltInRegistries.BLOCK_ENTITY_TYPE.getKey(tileEntity.getType())) && (checkData == null || checkData.checkData(block.getEntityData())); } @Override diff --git a/src/main/java/dev/latvian/mods/kubejs/block/predicate/BlockIDPredicate.java b/src/main/java/dev/latvian/mods/kubejs/block/predicate/BlockIDPredicate.java index 103152eb0..2c65c614b 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/predicate/BlockIDPredicate.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/predicate/BlockIDPredicate.java @@ -1,8 +1,8 @@ package dev.latvian.mods.kubejs.block.predicate; import dev.latvian.mods.kubejs.level.BlockContainerJS; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.util.Cast; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; @@ -65,7 +65,7 @@ public BlockIDPredicate with(String key, String value) { private Block getBlock() { if (cachedBlock == null) { - cachedBlock = RegistryInfo.BLOCK.getValue(id); + cachedBlock = BuiltInRegistries.BLOCK.get(id); if (cachedBlock == null) { cachedBlock = Blocks.AIR; diff --git a/src/main/java/dev/latvian/mods/kubejs/block/state/BlockStatePredicate.java b/src/main/java/dev/latvian/mods/kubejs/block/state/BlockStatePredicate.java index eee3c8ce5..f8e29d9d2 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/state/BlockStatePredicate.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/state/BlockStatePredicate.java @@ -7,7 +7,6 @@ import dev.latvian.mods.kubejs.level.ruletest.AnyMatchRuleTest; import dev.latvian.mods.kubejs.level.ruletest.InvertRuleTest; import dev.latvian.mods.kubejs.recipe.match.ReplacementMatch; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.script.KubeJSContext; import dev.latvian.mods.kubejs.util.ListJS; import dev.latvian.mods.kubejs.util.MapJS; @@ -68,7 +67,7 @@ static BlockStatePredicate fromString(String s) { return new StateMatch(state); } } else { - var block = RegistryInfo.BLOCK.getValue(ResourceLocation.parse(s)); + var block = BuiltInRegistries.BLOCK.get(ResourceLocation.parse(s)); if (block != Blocks.AIR) { return new BlockMatch(block); @@ -177,11 +176,7 @@ default Set getBlockIds() { Set set = new LinkedHashSet<>(); for (var block : getBlocks()) { - var blockId = RegistryInfo.BLOCK.getId(block); - - if (blockId != null) { - set.add(blockId); - } + set.add(block.kjs$getIdLocation()); } return set; @@ -251,8 +246,7 @@ public Collection getBlockStates() { @Override public Set getBlockIds() { - var blockId = RegistryInfo.BLOCK.getId(block); - return blockId == null ? Collections.emptySet() : Collections.singleton(blockId); + return Set.of(block.kjs$getIdLocation()); } @Override @@ -284,8 +278,7 @@ public Collection getBlockStates() { @Override public Set getBlockIds() { - var blockId = RegistryInfo.BLOCK.getId(state.getBlock()); - return blockId == null ? Collections.emptySet() : Collections.singleton(blockId); + return Set.of(state.getBlock().kjs$getIdLocation()); } @Override @@ -435,8 +428,8 @@ public NotMatch(BlockStatePredicate predicate) { cachedStates = new LinkedHashSet<>(); - for (var entry : RegistryInfo.BLOCK.entrySet()) { - for (var state : entry.getValue().getStateDefinition().getPossibleStates()) { + for (var block : BuiltInRegistries.BLOCK) { + for (var state : block.getStateDefinition().getPossibleStates()) { if (!predicate.test(state)) { cachedStates.add(state); } @@ -473,7 +466,7 @@ public Set getBlockIds() { var set = new HashSet(); for (var block : getBlocks()) { - set.add(RegistryInfo.BLOCK.getId(block)); + set.add(block.kjs$getIdLocation()); } return set; @@ -493,8 +486,8 @@ public AndMatch(List list) { this.list = list; cachedStates = new LinkedHashSet<>(); - for (var entry : RegistryInfo.BLOCK.entrySet()) { - for (var state : entry.getValue().getStateDefinition().getPossibleStates()) { + for (var block : BuiltInRegistries.BLOCK) { + for (var state : block.getStateDefinition().getPossibleStates()) { var match = true; for (var predicate : list) { if (!predicate.test(state)) { diff --git a/src/main/java/dev/latvian/mods/kubejs/client/ClientAssetPacks.java b/src/main/java/dev/latvian/mods/kubejs/client/ClientAssetPacks.java index c64ee7688..4692b1aa9 100644 --- a/src/main/java/dev/latvian/mods/kubejs/client/ClientAssetPacks.java +++ b/src/main/java/dev/latvian/mods/kubejs/client/ClientAssetPacks.java @@ -6,7 +6,7 @@ import dev.latvian.mods.kubejs.bindings.event.ClientEvents; import dev.latvian.mods.kubejs.plugin.KubeJSPlugin; import dev.latvian.mods.kubejs.plugin.KubeJSPlugins; -import dev.latvian.mods.kubejs.registry.RegistryInfo; +import dev.latvian.mods.kubejs.registry.RegistryObjectStorage; import dev.latvian.mods.kubejs.script.ConsoleJS; import dev.latvian.mods.kubejs.script.ScriptType; import dev.latvian.mods.kubejs.script.data.GeneratedDataStage; @@ -55,7 +55,7 @@ public List inject(List original) { internalAssetPack.reset(); - for (var builder : RegistryInfo.ALL_BUILDERS) { + for (var builder : RegistryObjectStorage.ALL_BUILDERS) { builder.generateAssetJsons(internalAssetPack); } @@ -65,7 +65,7 @@ public List inject(List original) { var langEvents = new HashMap(); var enUsLangEvent = langEvents.computeIfAbsent("en_us", s -> new LangKubeEvent(s, langMap)); - for (var builder : RegistryInfo.ALL_BUILDERS) { + for (var builder : RegistryObjectStorage.ALL_BUILDERS) { builder.generateLang(enUsLangEvent); } diff --git a/src/main/java/dev/latvian/mods/kubejs/command/ArgumentTypeWrappers.java b/src/main/java/dev/latvian/mods/kubejs/command/ArgumentTypeWrappers.java index 5a34b00d7..e7508b888 100644 --- a/src/main/java/dev/latvian/mods/kubejs/command/ArgumentTypeWrappers.java +++ b/src/main/java/dev/latvian/mods/kubejs/command/ArgumentTypeWrappers.java @@ -9,7 +9,6 @@ import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.script.ConsoleJS; import dev.latvian.mods.kubejs.util.ClassWrapper; import net.minecraft.Util; @@ -46,6 +45,7 @@ import net.minecraft.commands.arguments.item.ItemPredicateArgument; import net.minecraft.commands.synchronization.ArgumentTypeInfos; import net.minecraft.core.Registry; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; @@ -167,7 +167,7 @@ private static Map> getOrCacheByName() { if (byNameCache == null) { return byNameCache = Util.make(new HashMap<>(), map -> { for (var entry : ArgumentTypeInfos.BY_CLASS.entrySet()) { - var id = RegistryInfo.COMMAND_ARGUMENT_TYPE.getId(entry.getValue()); + var id = BuiltInRegistries.COMMAND_ARGUMENT_TYPE.getKey(entry.getValue()); byNameCache.put(id, new ClassWrapper<>(entry.getKey())); } }); diff --git a/src/main/java/dev/latvian/mods/kubejs/core/BlockKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/BlockKJS.java index 9611084b1..94c49b15a 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/BlockKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/BlockKJS.java @@ -3,9 +3,12 @@ import dev.latvian.mods.kubejs.bindings.BlockWrapper; import dev.latvian.mods.kubejs.block.BlockBuilder; import dev.latvian.mods.kubejs.recipe.match.Replaceable; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.util.RemapPrefixForJS; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceKey; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockState; @@ -14,8 +17,13 @@ @RemapPrefixForJS("kjs$") public interface BlockKJS extends BlockBuilderProvider, RegistryObjectKJS, Replaceable { @Override - default RegistryInfo kjs$getKubeRegistry() { - return RegistryInfo.BLOCK; + default ResourceKey> kjs$getRegistryId() { + return Registries.BLOCK; + } + + @Override + default Registry kjs$getRegistry() { + return BuiltInRegistries.BLOCK; } default void kjs$setBlockBuilder(BlockBuilder b) { diff --git a/src/main/java/dev/latvian/mods/kubejs/core/BlockStateKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/BlockStateKJS.java index 1af2a00ab..41c8b2b46 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/BlockStateKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/BlockStateKJS.java @@ -4,12 +4,14 @@ import dev.latvian.mods.kubejs.bindings.event.BlockEvents; import dev.latvian.mods.kubejs.block.RandomTickKubeEvent; import dev.latvian.mods.kubejs.recipe.match.Replaceable; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.script.ScriptType; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.util.RemapPrefixForJS; import net.minecraft.core.BlockPos; import net.minecraft.core.Holder; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceKey; import net.minecraft.server.level.ServerLevel; import net.minecraft.util.RandomSource; @@ -20,8 +22,13 @@ @RemapPrefixForJS("kjs$") public interface BlockStateKJS extends RegistryObjectKJS, Replaceable { @Override - default RegistryInfo kjs$getKubeRegistry() { - return RegistryInfo.BLOCK; + default ResourceKey> kjs$getRegistryId() { + return Registries.BLOCK; + } + + @Override + default Registry kjs$getRegistry() { + return BuiltInRegistries.BLOCK; } @Override @@ -30,8 +37,8 @@ public interface BlockStateKJS extends RegistryObjectKJS, Replaceable { } @Override - default ResourceKey kjs$getRegistryKey() { - return ((BlockBehaviour.BlockStateBase) this).getBlock().kjs$getRegistryKey(); + default ResourceKey kjs$getKey() { + return ((BlockBehaviour.BlockStateBase) this).getBlock().kjs$getKey(); } @Override @@ -52,8 +59,8 @@ public interface BlockStateKJS extends RegistryObjectKJS, Replaceable { } default boolean kjs$randomTickOverride(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) { - if (BlockEvents.RANDOM_TICK.hasListeners(state.kjs$getRegistryKey())) { - return BlockEvents.RANDOM_TICK.post(ScriptType.SERVER, state.kjs$getRegistryKey(), new RandomTickKubeEvent(level, pos, state, random)).interruptFalse(); + if (BlockEvents.RANDOM_TICK.hasListeners(state.kjs$getKey())) { + return BlockEvents.RANDOM_TICK.post(ScriptType.SERVER, state.kjs$getKey(), new RandomTickKubeEvent(level, pos, state, random)).interruptFalse(); } return false; diff --git a/src/main/java/dev/latvian/mods/kubejs/core/EntityKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/EntityKJS.java index 9053d2d99..ffacc73ec 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/EntityKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/EntityKJS.java @@ -5,7 +5,6 @@ import dev.latvian.mods.kubejs.entity.RayTraceResultJS; import dev.latvian.mods.kubejs.level.BlockContainerJS; import dev.latvian.mods.kubejs.player.EntityArrayList; -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; @@ -55,7 +54,7 @@ public interface EntityKJS extends WithPersistentData, MessageSenderKJS, ScriptT } default String kjs$getType() { - return String.valueOf(RegistryInfo.ENTITY_TYPE.getId(kjs$self().getType())); + return kjs$self().getType().kjs$getId(); } default GameProfile kjs$getProfile() { diff --git a/src/main/java/dev/latvian/mods/kubejs/core/EntityTypeKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/EntityTypeKJS.java index 88e230326..b3552226e 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/EntityTypeKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/EntityTypeKJS.java @@ -1,11 +1,19 @@ package dev.latvian.mods.kubejs.core; -import dev.latvian.mods.kubejs.registry.RegistryInfo; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceKey; import net.minecraft.world.entity.EntityType; public interface EntityTypeKJS extends RegistryObjectKJS> { @Override - default RegistryInfo> kjs$getKubeRegistry() { - return RegistryInfo.ENTITY_TYPE; + default ResourceKey>> kjs$getRegistryId() { + return Registries.ENTITY_TYPE; + } + + @Override + default Registry> kjs$getRegistry() { + return BuiltInRegistries.ENTITY_TYPE; } } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/FluidKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/FluidKJS.java index e04133ec5..91ce9c1ba 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/FluidKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/FluidKJS.java @@ -1,8 +1,11 @@ package dev.latvian.mods.kubejs.core; import dev.latvian.mods.kubejs.fluid.FluidLike; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.rhino.util.RemapPrefixForJS; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceKey; import net.minecraft.world.level.material.Fluid; import net.minecraft.world.level.material.Fluids; import net.neoforged.neoforge.fluids.FluidType; @@ -15,8 +18,13 @@ public interface FluidKJS extends RegistryObjectKJS, FluidLike { } @Override - default RegistryInfo kjs$getKubeRegistry() { - return RegistryInfo.FLUID; + default ResourceKey> kjs$getRegistryId() { + return Registries.FLUID; + } + + @Override + default Registry kjs$getRegistry() { + return BuiltInRegistries.FLUID; } @Override diff --git a/src/main/java/dev/latvian/mods/kubejs/core/ItemKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/ItemKJS.java index 28bcbdd94..0a2f3c32c 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/ItemKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/ItemKJS.java @@ -2,10 +2,13 @@ import dev.latvian.mods.kubejs.item.ItemBuilder; import dev.latvian.mods.kubejs.item.ItemStackKey; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.rhino.util.RemapForJS; import dev.latvian.mods.rhino.util.RemapPrefixForJS; +import net.minecraft.core.Registry; import net.minecraft.core.component.DataComponentType; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceKey; import net.minecraft.world.item.Item; import org.jetbrains.annotations.Nullable; @@ -19,8 +22,13 @@ public interface ItemKJS extends IngredientSupplierKJS, RegistryObjectKJS } @Override - default RegistryInfo kjs$getKubeRegistry() { - return RegistryInfo.ITEM; + default ResourceKey> kjs$getRegistryId() { + return Registries.ITEM; + } + + @Override + default Registry kjs$getRegistry() { + return BuiltInRegistries.ITEM; } @Nullable diff --git a/src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java index 8cdd0dc61..72666394c 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java @@ -7,7 +7,6 @@ import dev.latvian.mods.kubejs.item.ItemStackJS; import dev.latvian.mods.kubejs.level.BlockContainerJS; import dev.latvian.mods.kubejs.recipe.match.Replaceable; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.script.KubeJSContext; import dev.latvian.mods.kubejs.util.ID; import dev.latvian.mods.kubejs.util.WithCodec; @@ -17,7 +16,10 @@ import dev.latvian.mods.rhino.util.SpecialEquality; import dev.latvian.mods.rhino.util.ToStringJS; import net.minecraft.core.Holder; +import net.minecraft.core.Registry; import net.minecraft.core.component.DataComponents; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; import net.minecraft.nbt.Tag; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceKey; @@ -76,8 +78,13 @@ default boolean specialEquals(Context cx, Object o, boolean shallow) { } @Override - default RegistryInfo kjs$getKubeRegistry() { - return RegistryInfo.ITEM; + default ResourceKey> kjs$getRegistryId() { + return Registries.ITEM; + } + + @Override + default Registry kjs$getRegistry() { + return BuiltInRegistries.ITEM; } @Override @@ -91,8 +98,8 @@ default boolean specialEquals(Context cx, Object o, boolean shallow) { } @Override - default ResourceKey kjs$getRegistryKey() { - return kjs$self().getItem().kjs$getRegistryKey(); + default ResourceKey kjs$getKey() { + return kjs$self().getItem().kjs$getKey(); } @Override diff --git a/src/main/java/dev/latvian/mods/kubejs/core/LivingEntityKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/LivingEntityKJS.java index dcd434b95..d22efb1d3 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/LivingEntityKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/LivingEntityKJS.java @@ -45,7 +45,7 @@ public interface LivingEntityKJS extends EntityKJS { b.foodBuilder.eaten.accept(event); } - var key = i.kjs$getRegistryKey(); + var key = i.kjs$getKey(); if (ItemEvents.FOOD_EATEN.hasListeners(key)) { ItemEvents.FOOD_EATEN.post(entity, key, event); diff --git a/src/main/java/dev/latvian/mods/kubejs/core/MenuTypeKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/MenuTypeKJS.java index ab41d59d0..715227aab 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/MenuTypeKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/MenuTypeKJS.java @@ -1,11 +1,19 @@ package dev.latvian.mods.kubejs.core; -import dev.latvian.mods.kubejs.registry.RegistryInfo; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceKey; import net.minecraft.world.inventory.MenuType; public interface MenuTypeKJS extends RegistryObjectKJS> { @Override - default RegistryInfo> kjs$getKubeRegistry() { - return RegistryInfo.MENU; + default ResourceKey>> kjs$getRegistryId() { + return Registries.MENU; + } + + @Override + default Registry> kjs$getRegistry() { + return BuiltInRegistries.MENU; } } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/MinecraftClientKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/MinecraftClientKJS.java index 742ebe096..fc073d3fc 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/MinecraftClientKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/MinecraftClientKJS.java @@ -108,7 +108,7 @@ public interface MinecraftClientKJS extends MinecraftEnvironmentKJS { if (ItemEvents.FIRST_LEFT_CLICKED.hasListeners()) { var player = kjs$self().player; var stack = player.getItemInHand(InteractionHand.MAIN_HAND); - var key = stack.getItem().kjs$getRegistryKey(); + var key = stack.getItem().kjs$getKey(); if (ItemEvents.FIRST_LEFT_CLICKED.hasListeners(key)) { ItemEvents.FIRST_LEFT_CLICKED.post(ScriptType.CLIENT, key, new ItemClickedKubeEvent(player, InteractionHand.MAIN_HAND, stack)); @@ -125,7 +125,7 @@ public interface MinecraftClientKJS extends MinecraftEnvironmentKJS { for (var hand : InteractionHand.values()) { var stack = player.getItemInHand(hand); - var key = stack.getItem().kjs$getRegistryKey(); + var key = stack.getItem().kjs$getKey(); if (ItemEvents.FIRST_RIGHT_CLICKED.hasListeners(key)) { ItemEvents.FIRST_RIGHT_CLICKED.post(ScriptType.CLIENT, key, new ItemClickedKubeEvent(player, hand, stack)); diff --git a/src/main/java/dev/latvian/mods/kubejs/core/RecipeHolderKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/RecipeHolderKJS.java index 3a68ed593..ee2b1b960 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/RecipeHolderKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/RecipeHolderKJS.java @@ -3,11 +3,11 @@ import dev.latvian.mods.kubejs.recipe.match.ItemMatch; import dev.latvian.mods.kubejs.recipe.match.ReplacementMatchInfo; 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.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.Recipe; @@ -51,7 +51,7 @@ public interface RecipeHolderKJS extends RecipeLikeKJS { @Override default ResourceLocation kjs$getType() { - return RegistryInfo.RECIPE_SERIALIZER.getId(kjs$getSerializer()); + return BuiltInRegistries.RECIPE_SERIALIZER.getKey(kjs$getSerializer()); } @Override diff --git a/src/main/java/dev/latvian/mods/kubejs/core/RegistryObjectKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/RegistryObjectKJS.java index 686b65da6..224467eb6 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/RegistryObjectKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/RegistryObjectKJS.java @@ -1,10 +1,9 @@ package dev.latvian.mods.kubejs.core; -import dev.latvian.mods.kubejs.registry.RegistryInfo; -import dev.latvian.mods.kubejs.util.Cast; -import dev.latvian.mods.rhino.Context; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.util.RemapPrefixForJS; import net.minecraft.core.Holder; +import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.TagKey; @@ -13,25 +12,34 @@ @RemapPrefixForJS("kjs$") public interface RegistryObjectKJS { - default RegistryInfo kjs$getKubeRegistry() { + default ResourceKey> kjs$getRegistryId() { throw new NoMixinException(); } + default Registry kjs$getRegistry() { + return RegistryAccessContainer.current.wrapRegistry(kjs$getRegistryId().location()).registry(); + } + + @SuppressWarnings("unchecked") default Holder kjs$asHolder() { - return kjs$getKubeRegistry().getHolderOf(Cast.to(this)); + try { + return kjs$getRegistry().wrapAsHolder((T) this); + } catch (Exception ex) { + return (Holder) Holder.direct(this); + } } - default ResourceKey kjs$getRegistryKey() { + default ResourceKey kjs$getKey() { try { var h = kjs$asHolder(); return h instanceof Holder.Reference ref ? ref.key() : h.unwrapKey().orElseThrow(); } catch (Exception ex) { - return kjs$getKubeRegistry().getKeyOf(Cast.to(this)); + return kjs$getRegistry().getResourceKey((T) this).orElseThrow(); } } default ResourceLocation kjs$getIdLocation() { - return kjs$getRegistryKey().location(); + return kjs$getKey().location(); } default String kjs$getId() { @@ -42,11 +50,11 @@ public interface RegistryObjectKJS { return kjs$getIdLocation().getNamespace(); } - default Collection kjs$getTags(Context cx) { + default Collection kjs$getTags() { return kjs$asHolder().tags().map(TagKey::location).toList(); } default boolean kjs$hasTag(ResourceLocation tag) { - return kjs$asHolder().is(TagKey.create(kjs$getKubeRegistry().key, tag)); + return kjs$asHolder().is(TagKey.create(kjs$getRegistryId(), tag)); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/TagLoaderKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/TagLoaderKJS.java index 6dde45822..125955bd1 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/TagLoaderKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/TagLoaderKJS.java @@ -3,7 +3,7 @@ import com.google.gson.JsonArray; import dev.latvian.mods.kubejs.bindings.event.ServerEvents; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; +import dev.latvian.mods.kubejs.registry.RegistryObjectStorage; import dev.latvian.mods.kubejs.script.ConsoleJS; import dev.latvian.mods.kubejs.server.DataExport; import dev.latvian.mods.kubejs.server.tag.TagEventFilter; @@ -28,30 +28,39 @@ public interface TagLoaderKJS { return; } - var regInfo = RegistryInfo.of((ResourceKey) reg.key()); + var objStorage = RegistryObjectStorage.of((ResourceKey) reg.key()); - if (regInfo.hasDefaultTags || ServerEvents.TAGS.hasListeners(regInfo.key)) { + boolean hasDefaultTags = false; + + for (var builder : (Collection>) objStorage.objects.values()) { + if (!builder.defaultTags.isEmpty()) { + hasDefaultTags = true; + break; + } + } + + if (hasDefaultTags || ServerEvents.TAGS.hasListeners(objStorage.key)) { var preEvent = kjs$getResources().kjs$getServerScriptManager().preTagEvents.get(reg.key()); - var event = new TagKubeEvent(regInfo, reg); + var event = new TagKubeEvent(objStorage.key, reg); for (var entry : map.entrySet()) { var w = new TagWrapper(event, entry.getKey(), entry.getValue()); event.tags.put(w.id, w); if (ConsoleJS.SERVER.shouldPrintDebug()) { - ConsoleJS.SERVER.debug("Tags %s/#%s; %d".formatted(regInfo, w.id, w.entries.size())); + ConsoleJS.SERVER.debug("Tags %s/#%s; %d".formatted(objStorage, w.id, w.entries.size())); } } - for (var builder : (Collection>) regInfo.objects.values()) { + for (var builder : (Collection>) objStorage.objects.values()) { for (var s : builder.defaultTags) { event.add(s, new TagEventFilter.ID(builder.id)); } } if (preEvent == null) { - ServerEvents.TAGS.post(event, regInfo.key); + ServerEvents.TAGS.post(event, objStorage.key); } else { for (var a : preEvent.actions) { a.accept(event); @@ -65,14 +74,14 @@ public interface TagLoaderKJS { } if (event.totalAdded > 0 || event.totalRemoved > 0 || ConsoleJS.SERVER.shouldPrintDebug()) { - ConsoleJS.SERVER.info("[%s] Found %d tags, added %d objects, removed %d objects".formatted(regInfo, event.tags.size(), event.totalAdded, event.totalRemoved)); + ConsoleJS.SERVER.info("[%s] Found %d tags, added %d objects, removed %d objects".formatted(objStorage, event.tags.size(), event.totalAdded, event.totalRemoved)); } } kjs$resources.kjs$getServerScriptManager().getRegistries().cacheTags(reg, map); if (DataExport.export != null) { - var loc = "tags/" + regInfo + "/"; + var loc = "tags/" + objStorage + "/"; for (var entry : map.entrySet()) { var list = new ArrayList(); diff --git a/src/main/java/dev/latvian/mods/kubejs/core/mixin/BlockMixin.java b/src/main/java/dev/latvian/mods/kubejs/core/mixin/BlockMixin.java index 18b8a0be8..d391eb22f 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/mixin/BlockMixin.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/mixin/BlockMixin.java @@ -33,7 +33,7 @@ public abstract class BlockMixin implements BlockKJS { } @Override - public ResourceKey kjs$getRegistryKey() { + public ResourceKey kjs$getKey() { return builtInRegistryHolder.key(); } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/mixin/EntityTypeMixin.java b/src/main/java/dev/latvian/mods/kubejs/core/mixin/EntityTypeMixin.java index 51d10256f..e9542ed21 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/mixin/EntityTypeMixin.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/mixin/EntityTypeMixin.java @@ -27,9 +27,9 @@ public abstract class EntityTypeMixin implements EntityTypeKJS { } @Override - public ResourceKey> kjs$getRegistryKey() { + public ResourceKey> kjs$getKey() { if (kjs$registryKey == null) { - kjs$registryKey = EntityTypeKJS.super.kjs$getRegistryKey(); + kjs$registryKey = EntityTypeKJS.super.kjs$getKey(); } return kjs$registryKey; diff --git a/src/main/java/dev/latvian/mods/kubejs/core/mixin/FluidMixin.java b/src/main/java/dev/latvian/mods/kubejs/core/mixin/FluidMixin.java index db532412e..18123e315 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/mixin/FluidMixin.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/mixin/FluidMixin.java @@ -29,9 +29,9 @@ public abstract class FluidMixin implements FluidKJS { } @Override - public ResourceKey kjs$getRegistryKey() { + public ResourceKey kjs$getKey() { if (kjs$registryKey == null) { - kjs$registryKey = FluidKJS.super.kjs$getRegistryKey(); + kjs$registryKey = FluidKJS.super.kjs$getKey(); } return kjs$registryKey; diff --git a/src/main/java/dev/latvian/mods/kubejs/core/mixin/ItemMixin.java b/src/main/java/dev/latvian/mods/kubejs/core/mixin/ItemMixin.java index b44c65a76..cbd8419a2 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/mixin/ItemMixin.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/mixin/ItemMixin.java @@ -77,9 +77,9 @@ public abstract class ItemMixin implements ItemKJS { } @Override - public ResourceKey kjs$getRegistryKey() { + public ResourceKey kjs$getKey() { if (kjs$registryKey == null) { - kjs$registryKey = ItemKJS.super.kjs$getRegistryKey(); + kjs$registryKey = ItemKJS.super.kjs$getKey(); } return kjs$registryKey; diff --git a/src/main/java/dev/latvian/mods/kubejs/core/mixin/MenuTypeMixin.java b/src/main/java/dev/latvian/mods/kubejs/core/mixin/MenuTypeMixin.java index 2b89af8bd..3a3385769 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/mixin/MenuTypeMixin.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/mixin/MenuTypeMixin.java @@ -15,9 +15,9 @@ public abstract class MenuTypeMixin implements MenuTypeKJS { private String kjs$id; @Override - public ResourceKey> kjs$getRegistryKey() { + public ResourceKey> kjs$getKey() { if (kjs$registryKey == null) { - kjs$registryKey = MenuTypeKJS.super.kjs$getRegistryKey(); + kjs$registryKey = MenuTypeKJS.super.kjs$getKey(); } return kjs$registryKey; diff --git a/src/main/java/dev/latvian/mods/kubejs/entity/EntityPotionEffectsJS.java b/src/main/java/dev/latvian/mods/kubejs/entity/EntityPotionEffectsJS.java index 79bb96cb6..11974efad 100644 --- a/src/main/java/dev/latvian/mods/kubejs/entity/EntityPotionEffectsJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/entity/EntityPotionEffectsJS.java @@ -1,6 +1,5 @@ package dev.latvian.mods.kubejs.entity; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import net.minecraft.core.Holder; import net.minecraft.world.effect.MobEffect; import net.minecraft.world.effect.MobEffectInstance; @@ -29,49 +28,34 @@ public Map, MobEffectInstance> getMap() { return entity.getActiveEffectsMap(); } - public boolean isHolderActive(Holder mobEffect) { + public boolean isActive(Holder mobEffect) { return mobEffect != null && entity.hasEffect(mobEffect); } - public boolean isActive(MobEffect mobEffect) { - return isHolderActive(RegistryInfo.MOB_EFFECT.getHolderOf(mobEffect)); - } - - public int getDuration(MobEffect mobEffect) { - return getHolderDuration(RegistryInfo.MOB_EFFECT.getHolderOf(mobEffect)); - } - - public int getHolderDuration(Holder mobEffect) { + public int getDuration(Holder mobEffect) { var i = entity.getEffect(mobEffect); return i == null ? 0 : i.getDuration(); } @Nullable - public MobEffectInstance getActive(MobEffect mobEffect) { - return getHolderActive(RegistryInfo.MOB_EFFECT.getHolderOf(mobEffect)); - } - - @Nullable - public MobEffectInstance getHolderActive(Holder mobEffect) { + public MobEffectInstance getActive(Holder mobEffect) { return mobEffect == null ? null : entity.getEffect(mobEffect); } - public void add(MobEffect mobEffect) { + public void add(Holder mobEffect) { add(mobEffect, 200); } - public void add(MobEffect mobEffect, int duration) { + public void add(Holder mobEffect, int duration) { add(mobEffect, duration, 0); } - public void add(MobEffect mobEffect, int duration, int amplifier) { + public void add(Holder mobEffect, int duration, int amplifier) { add(mobEffect, duration, amplifier, false, true); } - public void add(MobEffect mobEffect, int duration, int amplifier, boolean ambient, boolean showParticles) { - if (mobEffect != null) { - entity.addEffect(new MobEffectInstance(RegistryInfo.MOB_EFFECT.getHolderOf(mobEffect), duration, amplifier, ambient, showParticles)); - } + public void add(Holder mobEffect, int duration, int amplifier, boolean ambient, boolean showParticles) { + entity.addEffect(new MobEffectInstance(mobEffect, duration, amplifier, ambient, showParticles)); } public boolean isApplicable(MobEffectInstance effect) { diff --git a/src/main/java/dev/latvian/mods/kubejs/entity/KubeJSEntityEventHandler.java b/src/main/java/dev/latvian/mods/kubejs/entity/KubeJSEntityEventHandler.java index 44496189b..5c072e13b 100644 --- a/src/main/java/dev/latvian/mods/kubejs/entity/KubeJSEntityEventHandler.java +++ b/src/main/java/dev/latvian/mods/kubejs/entity/KubeJSEntityEventHandler.java @@ -15,7 +15,7 @@ public class KubeJSEntityEventHandler { @SubscribeEvent public static void checkSpawn(FinalizeSpawnEvent event) { - var key = event.getEntity().getType().kjs$getRegistryKey(); + var key = event.getEntity().getType().kjs$getKey(); if (event.getLevel() instanceof ServerLevel level && EntityEvents.CHECK_SPAWN.hasListeners(key)) { var result = EntityEvents.CHECK_SPAWN.post(level, key, new CheckLivingEntitySpawnKubeEvent( @@ -37,7 +37,7 @@ public static void checkSpawn(FinalizeSpawnEvent event) { @SubscribeEvent public static void livingDeath(LivingDeathEvent event) { - var key = event.getEntity().getType().kjs$getRegistryKey(); + var key = event.getEntity().getType().kjs$getKey(); if (EntityEvents.DEATH.hasListeners(key)) { EntityEvents.DEATH.post(event.getEntity(), key, new LivingEntityDeathKubeEvent(event.getEntity(), event.getSource())).applyCancel(event); @@ -46,7 +46,7 @@ public static void livingDeath(LivingDeathEvent event) { @SubscribeEvent public static void beforeLivingHurt(LivingDamageEvent.Pre event) { - var key = event.getEntity().getType().kjs$getRegistryKey(); + var key = event.getEntity().getType().kjs$getKey(); if (EntityEvents.BEFORE_HURT.hasListeners(key)) { if (EntityEvents.BEFORE_HURT.post(event.getEntity(), key, new BeforeLivingEntityHurtKubeEvent(event)).interruptFalse()) { @@ -57,7 +57,7 @@ public static void beforeLivingHurt(LivingDamageEvent.Pre event) { @SubscribeEvent public static void afterLivingHurt(LivingDamageEvent.Post event) { - var key = event.getEntity().getType().kjs$getRegistryKey(); + var key = event.getEntity().getType().kjs$getKey(); if (EntityEvents.AFTER_HURT.hasListeners(key)) { EntityEvents.AFTER_HURT.post(event.getEntity(), key, new AfterLivingEntityHurtKubeEvent(event)); @@ -66,7 +66,7 @@ public static void afterLivingHurt(LivingDamageEvent.Post event) { @SubscribeEvent public static void entitySpawned(EntityJoinLevelEvent event) { - var key = event.getEntity().getType().kjs$getRegistryKey(); + var key = event.getEntity().getType().kjs$getKey(); if (EntityEvents.SPAWNED.hasListeners(key) && event.getLevel() instanceof ServerLevel level) { EntityEvents.SPAWNED.post(level, key, new EntitySpawnedKubeEvent(event.getEntity(), level)).applyCancel(event); @@ -75,7 +75,7 @@ public static void entitySpawned(EntityJoinLevelEvent event) { @SubscribeEvent public static void livingDrops(LivingDropsEvent event) { - var key = event.getEntity().getType().kjs$getRegistryKey(); + var key = event.getEntity().getType().kjs$getKey(); if (EntityEvents.ENTITY_DROPS.hasListeners(key)) { var e = new LivingEntityDropsKubeEvent(event); diff --git a/src/main/java/dev/latvian/mods/kubejs/event/EventTargetType.java b/src/main/java/dev/latvian/mods/kubejs/event/EventTargetType.java index 69124df6d..9beafc93c 100644 --- a/src/main/java/dev/latvian/mods/kubejs/event/EventTargetType.java +++ b/src/main/java/dev/latvian/mods/kubejs/event/EventTargetType.java @@ -87,7 +87,7 @@ private static ResourceKey toKey(ResourceKey registry, Object object) { } else if (object instanceof ResourceKey rl) { return rl; } else if (object instanceof RegistryObjectKJS wrk) { - return wrk.kjs$getRegistryKey(); + return wrk.kjs$getKey(); } else if (object instanceof ResourceLocation rl) { return ResourceKey.create(registry, rl); } else { diff --git a/src/main/java/dev/latvian/mods/kubejs/fluid/FlowingFluidBuilder.java b/src/main/java/dev/latvian/mods/kubejs/fluid/FlowingFluidBuilder.java index 24f7c1501..ffb3a3ffd 100644 --- a/src/main/java/dev/latvian/mods/kubejs/fluid/FlowingFluidBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/fluid/FlowingFluidBuilder.java @@ -1,7 +1,6 @@ package dev.latvian.mods.kubejs.fluid; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import net.minecraft.world.level.material.FlowingFluid; import net.neoforged.neoforge.fluids.BaseFlowingFluid; @@ -13,11 +12,6 @@ public FlowingFluidBuilder(FluidBuilder b) { fluidBuilder = b; } - @Override - public final RegistryInfo getRegistryType() { - return RegistryInfo.FLUID; - } - @Override public FlowingFluid createObject() { return new BaseFlowingFluid.Flowing(fluidBuilder.createProperties()); diff --git a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBuilder.java b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBuilder.java index 8a80ee2b6..7fa91740e 100644 --- a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBuilder.java @@ -5,12 +5,13 @@ import dev.latvian.mods.kubejs.color.SimpleColor; import dev.latvian.mods.kubejs.registry.AdditionalObjectRegistry; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.rhino.util.ReturnsSelf; +import net.minecraft.core.registries.Registries; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.level.material.FlowingFluid; import net.neoforged.neoforge.fluids.BaseFlowingFluid; +import net.neoforged.neoforge.registries.NeoForgeRegistries; import java.util.function.Consumer; import java.util.function.Supplier; @@ -53,11 +54,6 @@ public BuilderBase displayName(Component name) { return super.displayName(name); } - @Override - public final RegistryInfo getRegistryType() { - return RegistryInfo.FLUID; - } - public BaseFlowingFluid.Properties createProperties() { if (properties == null) { properties = new BaseFlowingFluid.Properties(fluidType, this, flowingFluid); @@ -79,15 +75,15 @@ public FlowingFluid createObject() { @Override public void createAdditionalObjects(AdditionalObjectRegistry registry) { - registry.add(RegistryInfo.FLUID_TYPE, fluidType); - registry.add(RegistryInfo.FLUID, flowingFluid); + registry.add(NeoForgeRegistries.Keys.FLUID_TYPES, fluidType); + registry.add(Registries.FLUID, flowingFluid); if (block != null) { - registry.add(RegistryInfo.BLOCK, block); + registry.add(Registries.BLOCK, block); } if (bucketItem != null) { - registry.add(RegistryInfo.ITEM, bucketItem); + registry.add(Registries.ITEM, bucketItem); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidTypeBuilder.java b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidTypeBuilder.java index 19719eaf0..a3185a8d5 100644 --- a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidTypeBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidTypeBuilder.java @@ -3,7 +3,6 @@ import dev.latvian.mods.kubejs.block.BlockRenderType; import dev.latvian.mods.kubejs.color.Color; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.rhino.util.ReturnsSelf; import net.minecraft.client.Minecraft; import net.minecraft.core.particles.ParticleOptions; @@ -84,11 +83,6 @@ public FluidTypeBuilder(ResourceLocation id) { sound(SoundActions.FLUID_VAPORIZE, SoundEvents.FIRE_EXTINGUISH); } - @Override - public final RegistryInfo getRegistryType() { - return RegistryInfo.FLUID_TYPE; - } - @Override public FluidType createObject() { return new KubeFluidType(this); diff --git a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidWrapper.java b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidWrapper.java index 7bd1855e9..5fe3b1737 100644 --- a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidWrapper.java +++ b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidWrapper.java @@ -4,13 +4,13 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.serialization.DynamicOps; import dev.latvian.mods.kubejs.bindings.DataComponentWrapper; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.util.RegExpKJS; import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.type.TypeInfo; import net.minecraft.core.HolderSet; import net.minecraft.core.component.DataComponentMap; import net.minecraft.core.component.DataComponentPredicate; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.Tag; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.FluidTags; @@ -120,14 +120,14 @@ static FluidStack lava(int amount) { } static Fluid getType(ResourceLocation id) { - return RegistryInfo.FLUID.getValue(id); + return BuiltInRegistries.FLUID.get(id); } static List getTypes() { var types = new ArrayList(); - for (var entry : RegistryInfo.FLUID.entrySet()) { - types.add(entry.getKey().location().toString()); + for (var fluid : BuiltInRegistries.FLUID) { + types.add(fluid.kjs$getId()); } return types; @@ -138,11 +138,11 @@ static FluidStack getEmpty() { } static boolean exists(ResourceLocation id) { - return RegistryInfo.FLUID.hasValue(id); + return BuiltInRegistries.FLUID.containsKey(id); } static ResourceLocation getId(Fluid fluid) { - return RegistryInfo.FLUID.getId(fluid); + return BuiltInRegistries.FLUID.getKey(fluid); } static FluidStack ofString(DynamicOps registryOps, String s) { @@ -187,7 +187,7 @@ static FluidStack read(DynamicOps registryOps, StringReader reader) throws } var fluidId = ResourceLocation.read(reader); - var fluidStack = new FluidStack(RegistryInfo.FLUID.getValue(fluidId), amount); + var fluidStack = new FluidStack(BuiltInRegistries.FLUID.get(fluidId), amount); var next = reader.canRead() ? reader.peek() : 0; @@ -239,7 +239,7 @@ static FluidIngredient readIngredient(DynamicOps registryOps, StringReader } var fluidId = ResourceLocation.read(reader); - var fluid = RegistryInfo.FLUID.getValue(fluidId); + var fluid = BuiltInRegistries.FLUID.get(fluidId); var next = reader.canRead() ? reader.peek() : 0; diff --git a/src/main/java/dev/latvian/mods/kubejs/item/ArmorMaterialBuilder.java b/src/main/java/dev/latvian/mods/kubejs/item/ArmorMaterialBuilder.java index b70dbc526..195d623fa 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/ArmorMaterialBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/ArmorMaterialBuilder.java @@ -1,7 +1,6 @@ package dev.latvian.mods.kubejs.item; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.rhino.util.ReturnsSelf; import net.minecraft.core.Holder; import net.minecraft.resources.ResourceLocation; @@ -37,11 +36,6 @@ public ArmorMaterialBuilder(ResourceLocation i) { knockbackResistance = 0F; } - @Override - public RegistryInfo getRegistryType() { - return RegistryInfo.ARMOR_MATERIAL; - } - @Override public ArmorMaterial createObject() { return new ArmorMaterial( @@ -71,8 +65,8 @@ public ArmorMaterialBuilder enchantmentValue(int v) { return this; } - public ArmorMaterialBuilder equipSound(ResourceLocation id) { - equipSound = RegistryInfo.SOUND_EVENT.getHolder(id); + public ArmorMaterialBuilder equipSound(Holder sound) { + equipSound = sound; return this; } diff --git a/src/main/java/dev/latvian/mods/kubejs/item/FoodBuilder.java b/src/main/java/dev/latvian/mods/kubejs/item/FoodBuilder.java index 254e6ccaa..1a0c136b8 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/FoodBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/FoodBuilder.java @@ -1,9 +1,9 @@ package dev.latvian.mods.kubejs.item; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.typings.Info; import dev.latvian.mods.kubejs.typings.Param; import net.minecraft.core.Holder; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.effect.MobEffect; import net.minecraft.world.effect.MobEffectInstance; @@ -139,10 +139,10 @@ public EffectSupplier(ResourceLocation id, int duration, int amplifier) { @Override public MobEffectInstance get() { if (cachedEffect == null) { - cachedEffect = RegistryInfo.MOB_EFFECT.getHolder(id); + cachedEffect = BuiltInRegistries.MOB_EFFECT.getHolder(id).orElse(null); if (cachedEffect == null) { - var effectIds = RegistryInfo.MOB_EFFECT.entrySet().stream().map(entry -> entry.getKey().location()).collect(Collectors.toSet()); + var effectIds = BuiltInRegistries.MOB_EFFECT.entrySet().stream().map(entry -> entry.getKey().location()).collect(Collectors.toSet()); throw new RuntimeException(String.format("Missing effect '%s'. Check spelling or maybe potion id was used instead of effect id. Possible ids: %s", id, effectIds)); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java b/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java index 8cef884c7..a0ae442bc 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java @@ -7,7 +7,6 @@ import dev.latvian.mods.kubejs.generator.KubeAssetGenerator; import dev.latvian.mods.kubejs.generator.KubeDataGenerator; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.script.ConsoleJS; import dev.latvian.mods.kubejs.typings.Info; import dev.latvian.mods.rhino.util.ReturnsSelf; @@ -108,11 +107,6 @@ public ItemBuilder(ResourceLocation i) { itemAttributeModifiers = null; } - @Override - public final RegistryInfo getRegistryType() { - return RegistryInfo.ITEM; - } - @Override public Item transformObject(Item obj) { obj.kjs$setItemBuilder(this); diff --git a/src/main/java/dev/latvian/mods/kubejs/item/ItemStackJS.java b/src/main/java/dev/latvian/mods/kubejs/item/ItemStackJS.java index b4678f22b..2be04098e 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/ItemStackJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/ItemStackJS.java @@ -9,7 +9,6 @@ import dev.latvian.mods.kubejs.bindings.DataComponentWrapper; import dev.latvian.mods.kubejs.ingredient.RegExIngredient; import dev.latvian.mods.kubejs.ingredient.TagIngredient; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.util.ID; import dev.latvian.mods.kubejs.util.Lazy; import dev.latvian.mods.kubejs.util.MapJS; @@ -19,6 +18,7 @@ import dev.latvian.mods.rhino.Wrapper; import dev.latvian.mods.rhino.regexp.NativeRegExp; import dev.latvian.mods.rhino.type.TypeInfo; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.StringTag; import net.minecraft.nbt.Tag; import net.minecraft.resources.ResourceLocation; @@ -49,8 +49,8 @@ public interface ItemStackJS { Lazy> CACHED_ITEM_TYPE_LIST = Lazy.of(() -> { var cachedItemTypeList = new ArrayList(); - for (var entry : RegistryInfo.ITEM.entrySet()) { - cachedItemTypeList.add(entry.getKey().location().toString()); + for (var item : BuiltInRegistries.ITEM) { + cachedItemTypeList.add(item.kjs$getId()); } return cachedItemTypeList; @@ -73,7 +73,7 @@ public interface ItemStackJS { for (var itemId : CACHED_ITEM_TYPE_LIST.get()) { var itemRl = ResourceLocation.parse(itemId); - map.computeIfAbsent(itemRl, id -> Set.of(RegistryInfo.ITEM.getValue(id).getDefaultInstance())); + map.computeIfAbsent(itemRl, id -> Set.of(BuiltInRegistries.ITEM.get(id).getDefaultInstance())); } return map; @@ -93,7 +93,7 @@ static ItemStack wrap(RegistryAccessContainer registries, @Nullable Object o) { } else if (o instanceof Ingredient ingr) { return ingr.kjs$getFirst(); } else if (o instanceof ResourceLocation id) { - var item = RegistryInfo.ITEM.getValue(id); + var item = BuiltInRegistries.ITEM.get(id); if (item == null || item == Items.AIR) { return ItemStack.EMPTY; @@ -143,7 +143,7 @@ static ItemStack wrap(RegistryAccessContainer registries, @Nullable Object o) { if (map != null) { if (map.containsKey("item")) { var id = ID.mc(map.get("item").toString()); - var item = RegistryInfo.ITEM.getValue(id); + var item = BuiltInRegistries.ITEM.get(id); if (item == null || item == Items.AIR) { return ItemStack.EMPTY; @@ -180,7 +180,7 @@ static Item getRawItem(RegistryAccessContainer registries, @Nullable Object o) { if (s.isEmpty()) { return Items.AIR; } else if (s.charAt(0) != '#') { - return RegistryInfo.ITEM.getValue(ID.mc(s)); + return BuiltInRegistries.ITEM.get(ID.mc(s)); } } @@ -259,7 +259,7 @@ static ItemStack read(DynamicOps registryOps, StringReader reader) throws C } var itemId = ResourceLocation.read(reader); - var itemStack = new ItemStack(RegistryInfo.ITEM.getValue(itemId), count); + var itemStack = new ItemStack(BuiltInRegistries.ITEM.get(itemId), count); var next = reader.canRead() ? reader.peek() : 0; diff --git a/src/main/java/dev/latvian/mods/kubejs/item/JukeboxSongBuilder.java b/src/main/java/dev/latvian/mods/kubejs/item/JukeboxSongBuilder.java index 705478ded..fbbb5075c 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/JukeboxSongBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/JukeboxSongBuilder.java @@ -1,7 +1,6 @@ package dev.latvian.mods.kubejs.item; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.rhino.util.ReturnsSelf; import net.minecraft.Util; import net.minecraft.core.Holder; @@ -26,11 +25,6 @@ public JukeboxSongBuilder(ResourceLocation id) { this.comparatorOutput = 0; } - @Override - public final RegistryInfo getRegistryType() { - return RegistryInfo.JUKEBOX_SONG; - } - @Override public JukeboxSong createObject() { return new JukeboxSong(sound, description, lengthInSeconds, comparatorOutput); diff --git a/src/main/java/dev/latvian/mods/kubejs/item/KubeJSItemEventHandler.java b/src/main/java/dev/latvian/mods/kubejs/item/KubeJSItemEventHandler.java index 2f102e3bb..9661a15f3 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/KubeJSItemEventHandler.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/KubeJSItemEventHandler.java @@ -17,7 +17,7 @@ public class KubeJSItemEventHandler { @SubscribeEvent public static void rightClick(PlayerInteractEvent.RightClickItem event) { var stack = event.getItemStack(); - var key = stack.getItem().kjs$getRegistryKey(); + var key = stack.getItem().kjs$getKey(); if (ItemEvents.RIGHT_CLICKED.hasListeners(key) && !event.getEntity().getCooldowns().isOnCooldown(stack.getItem())) { ItemEvents.RIGHT_CLICKED.post(event.getEntity(), key, new ItemClickedKubeEvent(event.getEntity(), event.getHand(), stack)).applyCancel(event); @@ -31,7 +31,7 @@ public static void leftClickEmpty(PlayerInteractEvent.LeftClickEmpty event) { @SubscribeEvent public static void itemPickupPre(ItemEntityPickupEvent.Pre event) { - var key = event.getItemEntity().getItem().getItem().kjs$getRegistryKey(); + var key = event.getItemEntity().getItem().getItem().kjs$getKey(); if (ItemEvents.CAN_PICK_UP.hasListeners(key)) { ItemEvents.CAN_PICK_UP.post(event.getPlayer(), key, new ItemPickedUpKubeEvent(event.getPlayer(), event.getItemEntity(), event.getItemEntity().getItem())).applyTristate(event::setCanPickup); @@ -40,7 +40,7 @@ public static void itemPickupPre(ItemEntityPickupEvent.Pre event) { @SubscribeEvent public static void itemPickupPost(ItemEntityPickupEvent.Post event) { - var key = event.getCurrentStack().getItem().kjs$getRegistryKey(); + var key = event.getCurrentStack().getItem().kjs$getKey(); if (ItemEvents.PICKED_UP.hasListeners(key)) { ItemEvents.PICKED_UP.post(event.getPlayer(), key, new ItemPickedUpKubeEvent(event.getPlayer(), event.getItemEntity(), event.getCurrentStack())); @@ -49,7 +49,7 @@ public static void itemPickupPost(ItemEntityPickupEvent.Post event) { @SubscribeEvent public static void itemDrop(ItemTossEvent event) { - var key = event.getEntity().getItem().getItem().kjs$getRegistryKey(); + var key = event.getEntity().getItem().getItem().kjs$getKey(); if (ItemEvents.DROPPED.hasListeners(key)) { ItemEvents.DROPPED.post(event.getPlayer(), key, new ItemDroppedKubeEvent(event.getPlayer(), event.getEntity())).applyCancel(event); @@ -59,7 +59,7 @@ public static void itemDrop(ItemTossEvent event) { @SubscribeEvent public static void entityInteract(PlayerInteractEvent.EntityInteract event) { var stack = event.getItemStack(); - var key = stack.getItem().kjs$getRegistryKey(); + var key = stack.getItem().kjs$getKey(); if (ItemEvents.ENTITY_INTERACTED.hasListeners(key)) { ItemEvents.ENTITY_INTERACTED.post(event.getEntity(), key, new ItemEntityInteractedKubeEvent(event.getEntity(), event.getTarget(), event.getHand(), stack)).applyCancel(event); @@ -69,7 +69,7 @@ public static void entityInteract(PlayerInteractEvent.EntityInteract event) { @SubscribeEvent public static void crafted(PlayerEvent.ItemCraftedEvent event) { if (!event.getCrafting().isEmpty()) { - var key = event.getCrafting().getItem().kjs$getRegistryKey(); + var key = event.getCrafting().getItem().kjs$getKey(); if (ItemEvents.CRAFTED.hasListeners(key)) { ItemEvents.CRAFTED.post(event.getEntity(), key, new ItemCraftedKubeEvent(event.getEntity(), event.getCrafting(), event.getInventory())); @@ -84,7 +84,7 @@ public static void crafted(PlayerEvent.ItemCraftedEvent event) { @SubscribeEvent public static void smelted(PlayerEvent.ItemSmeltedEvent event) { if (!event.getSmelting().isEmpty()) { - var key = event.getSmelting().getItem().kjs$getRegistryKey(); + var key = event.getSmelting().getItem().kjs$getKey(); if (ItemEvents.SMELTED.hasListeners(key)) { ItemEvents.SMELTED.post(event.getEntity(), key, new ItemSmeltedKubeEvent(event.getEntity(), event.getSmelting())); @@ -98,7 +98,7 @@ public static void smelted(PlayerEvent.ItemSmeltedEvent event) { @SubscribeEvent public static void itemDestroyed(PlayerDestroyItemEvent event) { - var key = event.getOriginal().getItem().kjs$getRegistryKey(); + var key = event.getOriginal().getItem().kjs$getKey(); if (ItemEvents.ITEM_DESTROYED.hasListeners(key)) { ItemEvents.ITEM_DESTROYED.post(event.getEntity(), key, new ItemDestroyedKubeEvent(event)); diff --git a/src/main/java/dev/latvian/mods/kubejs/item/creativetab/CreativeTabBuilder.java b/src/main/java/dev/latvian/mods/kubejs/item/creativetab/CreativeTabBuilder.java index 59adf128b..57caac689 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/creativetab/CreativeTabBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/creativetab/CreativeTabBuilder.java @@ -1,7 +1,6 @@ package dev.latvian.mods.kubejs.item.creativetab; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.rhino.util.ReturnsSelf; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; @@ -18,11 +17,6 @@ public CreativeTabBuilder(ResourceLocation i) { this.content = CreativeTabContentSupplier.DEFAULT; } - @Override - public final RegistryInfo getRegistryType() { - return RegistryInfo.CREATIVE_MODE_TAB; - } - @Override public CreativeModeTab createObject() { return CreativeModeTab.builder() diff --git a/src/main/java/dev/latvian/mods/kubejs/item/creativetab/KubeJSCreativeTabs.java b/src/main/java/dev/latvian/mods/kubejs/item/creativetab/KubeJSCreativeTabs.java index 74f6f6ee1..a6d3af69f 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/creativetab/KubeJSCreativeTabs.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/creativetab/KubeJSCreativeTabs.java @@ -2,7 +2,7 @@ import dev.latvian.mods.kubejs.CommonProperties; import dev.latvian.mods.kubejs.KubeJS; -import dev.latvian.mods.kubejs.registry.RegistryInfo; +import dev.latvian.mods.kubejs.registry.RegistryObjectStorage; import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import net.minecraft.core.registries.Registries; import net.minecraft.world.item.CreativeModeTab; @@ -22,7 +22,7 @@ public interface KubeJSCreativeTabs { return is.isEmpty() ? Items.PURPLE_DYE.getDefaultInstance() : is; }) .displayItems((params, output) -> { - for (var b : RegistryInfo.ITEM) { + for (var b : RegistryObjectStorage.ITEM) { output.accept(b.get().getDefaultInstance()); } }) diff --git a/src/main/java/dev/latvian/mods/kubejs/item/custom/ArmorItemBuilder.java b/src/main/java/dev/latvian/mods/kubejs/item/custom/ArmorItemBuilder.java index b7b5d0497..b311691e3 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/custom/ArmorItemBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/custom/ArmorItemBuilder.java @@ -1,7 +1,6 @@ package dev.latvian.mods.kubejs.item.custom; import dev.latvian.mods.kubejs.item.ItemBuilder; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.rhino.util.ReturnsSelf; import net.minecraft.core.Holder; import net.minecraft.resources.ResourceLocation; @@ -80,8 +79,8 @@ public Item createObject() { return new ArmorItem(material, armorType, createItemProperties()); } - public ArmorItemBuilder material(ResourceLocation id) { - material = RegistryInfo.ARMOR_MATERIAL.getHolder(id); + public ArmorItemBuilder material(Holder material) { + this.material = material; return this; } } diff --git a/src/main/java/dev/latvian/mods/kubejs/item/ingredient/IngredientJS.java b/src/main/java/dev/latvian/mods/kubejs/item/ingredient/IngredientJS.java index 9e575abe7..f3893c7f0 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/ingredient/IngredientJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/ingredient/IngredientJS.java @@ -15,7 +15,6 @@ import dev.latvian.mods.kubejs.ingredient.RegExIngredient; import dev.latvian.mods.kubejs.ingredient.TagIngredient; import dev.latvian.mods.kubejs.item.ItemStackJS; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.util.ListJS; import dev.latvian.mods.kubejs.util.MapJS; import dev.latvian.mods.kubejs.util.RegExpKJS; @@ -26,6 +25,7 @@ import dev.latvian.mods.rhino.type.TypeInfo; import net.minecraft.core.HolderSet; import net.minecraft.core.component.DataComponentPredicate; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.ItemTags; import net.minecraft.tags.TagKey; @@ -189,7 +189,7 @@ static Ingredient read(RegistryAccessContainer registries, StringReader reader) } default -> { var itemId = ResourceLocation.read(reader); - var item = RegistryInfo.ITEM.getValue(itemId); + var item = BuiltInRegistries.ITEM.get(itemId); var next = reader.canRead() ? reader.peek() : 0; diff --git a/src/main/java/dev/latvian/mods/kubejs/level/BlockContainerJS.java b/src/main/java/dev/latvian/mods/kubejs/level/BlockContainerJS.java index af339cd35..43bd7a2f3 100644 --- a/src/main/java/dev/latvian/mods/kubejs/level/BlockContainerJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/level/BlockContainerJS.java @@ -2,13 +2,13 @@ import dev.latvian.mods.kubejs.core.InventoryKJS; import dev.latvian.mods.kubejs.player.EntityArrayList; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.util.Cast; import dev.latvian.mods.kubejs.util.Tags; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.util.SpecialEquality; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; @@ -136,8 +136,8 @@ public void setBlockState(BlockState state, int flags) { cachedState = state; } - public String getId() { - return RegistryInfo.BLOCK.getId(getBlockState().getBlock()).toString(); + public ResourceLocation getId() { + return getBlockState().getBlock().kjs$getIdLocation(); } public Collection getTags() { @@ -200,7 +200,7 @@ public BlockEntity getEntity() { public String getEntityId() { var entity = getEntity(); - return entity == null ? "minecraft:air" : RegistryInfo.BLOCK_ENTITY_TYPE.getId(entity.getType()).toString(); + return entity == null ? "minecraft:air" : BuiltInRegistries.BLOCK_ENTITY_TYPE.getKey(entity.getType()).toString(); } @Nullable @@ -264,10 +264,10 @@ public String toString() { var properties = getProperties(); if (properties.isEmpty()) { - return id; + return id.toString(); } - var builder = new StringBuilder(id); + var builder = new StringBuilder(id.toString()); builder.append('['); var first = true; diff --git a/src/main/java/dev/latvian/mods/kubejs/misc/BasicMobEffect.java b/src/main/java/dev/latvian/mods/kubejs/misc/BasicMobEffect.java index a6ed906a9..5d23e7b8e 100644 --- a/src/main/java/dev/latvian/mods/kubejs/misc/BasicMobEffect.java +++ b/src/main/java/dev/latvian/mods/kubejs/misc/BasicMobEffect.java @@ -1,8 +1,8 @@ package dev.latvian.mods.kubejs.misc; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.script.ScriptType; import net.minecraft.core.Holder; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.effect.MobEffect; import net.minecraft.world.entity.LivingEntity; @@ -42,7 +42,7 @@ public boolean applyEffectTick(@NotNull LivingEntity livingEntity, int i) { private void applyAttributeModifications() { if (!modified) { - modifierMap.forEach((r, m) -> attributeModifiers.put(RegistryInfo.ATTRIBUTE.getHolder(r), m)); + modifierMap.forEach((r, m) -> BuiltInRegistries.ATTRIBUTE.getHolder(r).ifPresent(h -> attributeModifiers.put(h, m))); modified = true; } } diff --git a/src/main/java/dev/latvian/mods/kubejs/misc/CustomStatBuilder.java b/src/main/java/dev/latvian/mods/kubejs/misc/CustomStatBuilder.java index 2c640166a..1986ba683 100644 --- a/src/main/java/dev/latvian/mods/kubejs/misc/CustomStatBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/misc/CustomStatBuilder.java @@ -1,7 +1,6 @@ package dev.latvian.mods.kubejs.misc; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import net.minecraft.resources.ResourceLocation; public class CustomStatBuilder extends BuilderBase { @@ -9,11 +8,6 @@ public CustomStatBuilder(ResourceLocation i) { super(i); } - @Override - public final RegistryInfo getRegistryType() { - return RegistryInfo.CUSTOM_STAT; - } - @Override public ResourceLocation createObject() { return id; diff --git a/src/main/java/dev/latvian/mods/kubejs/misc/MobEffectBuilder.java b/src/main/java/dev/latvian/mods/kubejs/misc/MobEffectBuilder.java index e0265bdff..ed5d9ae21 100644 --- a/src/main/java/dev/latvian/mods/kubejs/misc/MobEffectBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/misc/MobEffectBuilder.java @@ -2,7 +2,6 @@ import dev.latvian.mods.kubejs.color.Color; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.rhino.util.ReturnsSelf; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.effect.MobEffect; @@ -39,8 +38,8 @@ public MobEffectBuilder(ResourceLocation i) { } @Override - public final RegistryInfo getRegistryType() { - return RegistryInfo.MOB_EFFECT; + public String getTranslationKeyGroup() { + return "effect"; } public MobEffectBuilder modifyAttribute(ResourceLocation attribute, ResourceLocation id, double d, AttributeModifier.Operation operation) { diff --git a/src/main/java/dev/latvian/mods/kubejs/misc/PaintingVariantBuilder.java b/src/main/java/dev/latvian/mods/kubejs/misc/PaintingVariantBuilder.java index 9429b3e80..ac3607765 100644 --- a/src/main/java/dev/latvian/mods/kubejs/misc/PaintingVariantBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/misc/PaintingVariantBuilder.java @@ -1,33 +1,36 @@ package dev.latvian.mods.kubejs.misc; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.rhino.util.ReturnsSelf; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.decoration.PaintingVariant; -import java.util.Objects; - @ReturnsSelf public class PaintingVariantBuilder extends BuilderBase { - public transient PaintingVariant paintingVariant; + public transient int width; + public transient int height; + public transient ResourceLocation assetId; - public PaintingVariantBuilder(ResourceLocation i) { - super(i); + public PaintingVariantBuilder(ResourceLocation id) { + super(id); + this.width = 1; + this.height = 1; + this.assetId = id; } @Override - public final RegistryInfo getRegistryType() { - return RegistryInfo.PAINTING_VARIANT; + public PaintingVariant createObject() { + return new PaintingVariant(width, height, assetId); } - @Override - public PaintingVariant createObject() { - return Objects.requireNonNull(paintingVariant); + public PaintingVariantBuilder size(int width, int height) { + this.width = width; + this.height = height; + return this; } - public PaintingVariantBuilder painting(int width, int height, ResourceLocation texture) { - this.paintingVariant = new PaintingVariant(width, height, texture); + public PaintingVariantBuilder assetId(ResourceLocation assetId) { + this.assetId = assetId; return this; } } diff --git a/src/main/java/dev/latvian/mods/kubejs/misc/ParticleTypeBuilder.java b/src/main/java/dev/latvian/mods/kubejs/misc/ParticleTypeBuilder.java index 80d45c35c..0e16e9e20 100644 --- a/src/main/java/dev/latvian/mods/kubejs/misc/ParticleTypeBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/misc/ParticleTypeBuilder.java @@ -2,7 +2,6 @@ import com.mojang.serialization.MapCodec; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.rhino.util.ReturnsSelf; import net.minecraft.core.particles.ParticleOptions; import net.minecraft.core.particles.ParticleType; @@ -21,11 +20,6 @@ public ParticleTypeBuilder(ResourceLocation i) { overrideLimiter = false; } - @Override - public final RegistryInfo getRegistryType() { - return RegistryInfo.PARTICLE_TYPE; - } - @Override public ParticleType createObject() { if (codec != null && streamCodec != null) { diff --git a/src/main/java/dev/latvian/mods/kubejs/misc/PoiTypeBuilder.java b/src/main/java/dev/latvian/mods/kubejs/misc/PoiTypeBuilder.java index 20dce92f5..929162b52 100644 --- a/src/main/java/dev/latvian/mods/kubejs/misc/PoiTypeBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/misc/PoiTypeBuilder.java @@ -1,7 +1,6 @@ package dev.latvian.mods.kubejs.misc; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.rhino.util.ReturnsSelf; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.ai.village.poi.PoiType; @@ -23,11 +22,6 @@ public PoiTypeBuilder(ResourceLocation i) { validRange = 1; } - @Override - public final RegistryInfo getRegistryType() { - return RegistryInfo.POINT_OF_INTEREST_TYPE; - } - @Override public PoiType createObject() { return new PoiType(blockStates, maxTickets, validRange); diff --git a/src/main/java/dev/latvian/mods/kubejs/misc/PotionBuilder.java b/src/main/java/dev/latvian/mods/kubejs/misc/PotionBuilder.java index 90691276c..9574cde79 100644 --- a/src/main/java/dev/latvian/mods/kubejs/misc/PotionBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/misc/PotionBuilder.java @@ -1,8 +1,8 @@ package dev.latvian.mods.kubejs.misc; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.rhino.util.ReturnsSelf; +import net.minecraft.core.Holder; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.effect.MobEffect; import net.minecraft.world.effect.MobEffectInstance; @@ -21,11 +21,6 @@ public PotionBuilder(ResourceLocation i) { mobEffects = new ArrayList<>(); } - @Override - public RegistryInfo getRegistryType() { - return RegistryInfo.POTION; - } - @Override public Potion createObject() { return new Potion(id.getPath(), mobEffects.toArray(new MobEffectInstance[0])); @@ -36,27 +31,27 @@ public PotionBuilder addEffect(MobEffectInstance effect) { return this; } - public PotionBuilder effect(MobEffect effect) { + public PotionBuilder effect(Holder effect) { return effect(effect, 0, 0); } - public PotionBuilder effect(MobEffect effect, int duration) { + public PotionBuilder effect(Holder effect, int duration) { return effect(effect, duration, 0); } - public PotionBuilder effect(MobEffect effect, int duration, int amplifier) { + public PotionBuilder effect(Holder effect, int duration, int amplifier) { return effect(effect, duration, amplifier, false, true); } - public PotionBuilder effect(MobEffect effect, int duration, int amplifier, boolean ambient, boolean visible) { + public PotionBuilder effect(Holder effect, int duration, int amplifier, boolean ambient, boolean visible) { return effect(effect, duration, amplifier, ambient, visible, visible); } - public PotionBuilder effect(MobEffect effect, int duration, int amplifier, boolean ambient, boolean visible, boolean showIcon) { + public PotionBuilder effect(Holder effect, int duration, int amplifier, boolean ambient, boolean visible, boolean showIcon) { return effect(effect, duration, amplifier, ambient, visible, showIcon, null); } - public PotionBuilder effect(MobEffect effect, int duration, int amplifier, boolean ambient, boolean visible, boolean showIcon, @Nullable MobEffectInstance hiddenEffect) { - return addEffect(new MobEffectInstance(RegistryInfo.MOB_EFFECT.getHolderOf(effect), duration, amplifier, ambient, visible, showIcon, hiddenEffect)); + public PotionBuilder effect(Holder effect, int duration, int amplifier, boolean ambient, boolean visible, boolean showIcon, @Nullable MobEffectInstance hiddenEffect) { + return addEffect(new MobEffectInstance(effect, duration, amplifier, ambient, visible, showIcon, hiddenEffect)); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/misc/SoundEventBuilder.java b/src/main/java/dev/latvian/mods/kubejs/misc/SoundEventBuilder.java index c470c7fc8..bb7b59bd0 100644 --- a/src/main/java/dev/latvian/mods/kubejs/misc/SoundEventBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/misc/SoundEventBuilder.java @@ -1,7 +1,6 @@ package dev.latvian.mods.kubejs.misc; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundEvent; @@ -10,11 +9,6 @@ public SoundEventBuilder(ResourceLocation i) { super(i); } - @Override - public final RegistryInfo getRegistryType() { - return RegistryInfo.SOUND_EVENT; - } - @Override public SoundEvent createObject() { return SoundEvent.createVariableRangeEvent(id); diff --git a/src/main/java/dev/latvian/mods/kubejs/misc/VillagerProfessionBuilder.java b/src/main/java/dev/latvian/mods/kubejs/misc/VillagerProfessionBuilder.java index 6dd9dd9d0..ffa3d3aa1 100644 --- a/src/main/java/dev/latvian/mods/kubejs/misc/VillagerProfessionBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/misc/VillagerProfessionBuilder.java @@ -3,7 +3,6 @@ import com.google.common.collect.ImmutableSet; import com.mojang.datafixers.util.Either; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.rhino.util.ReturnsSelf; import net.minecraft.core.Holder; import net.minecraft.core.registries.Registries; @@ -36,11 +35,6 @@ public VillagerProfessionBuilder(ResourceLocation i) { workSound = null; } - @Override - public final RegistryInfo getRegistryType() { - return RegistryInfo.VILLAGER_PROFESSION; - } - @Override public VillagerProfession createObject() { Predicate> validPois = holder -> poiType.map(holder::is, holder::is); diff --git a/src/main/java/dev/latvian/mods/kubejs/misc/VillagerTypeBuilder.java b/src/main/java/dev/latvian/mods/kubejs/misc/VillagerTypeBuilder.java index 5e05f3801..2656854c0 100644 --- a/src/main/java/dev/latvian/mods/kubejs/misc/VillagerTypeBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/misc/VillagerTypeBuilder.java @@ -1,7 +1,6 @@ package dev.latvian.mods.kubejs.misc; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.npc.VillagerType; @@ -10,11 +9,6 @@ public VillagerTypeBuilder(ResourceLocation i) { super(i); } - @Override - public final RegistryInfo getRegistryType() { - return RegistryInfo.VILLAGER_TYPE; - } - @Override public VillagerType createObject() { return new VillagerType(id.getPath()); diff --git a/src/main/java/dev/latvian/mods/kubejs/neoforge/KubeJSNeoForgeClient.java b/src/main/java/dev/latvian/mods/kubejs/neoforge/KubeJSNeoForgeClient.java index 32473a798..d24155663 100644 --- a/src/main/java/dev/latvian/mods/kubejs/neoforge/KubeJSNeoForgeClient.java +++ b/src/main/java/dev/latvian/mods/kubejs/neoforge/KubeJSNeoForgeClient.java @@ -21,7 +21,7 @@ import dev.latvian.mods.kubejs.item.ItemBuilder; import dev.latvian.mods.kubejs.item.ItemModelPropertiesKubeEvent; import dev.latvian.mods.kubejs.kubedex.KubedexHighlight; -import dev.latvian.mods.kubejs.registry.RegistryInfo; +import dev.latvian.mods.kubejs.registry.RegistryObjectStorage; import dev.latvian.mods.kubejs.script.ScriptType; import dev.latvian.mods.kubejs.util.ID; import net.minecraft.client.KeyMapping; @@ -64,7 +64,7 @@ public static void addClientPacks(AddPackFindersEvent event) { private static void setupClient0() { ItemEvents.MODEL_PROPERTIES.post(ScriptType.STARTUP, new ItemModelPropertiesKubeEvent()); - for (var builder : RegistryInfo.BLOCK) { + for (var builder : RegistryObjectStorage.BLOCK) { if (builder instanceof BlockBuilder b) { switch (b instanceof FluidBlockBuilder fb ? fb.fluidBuilder.fluidType.renderType : b.renderType) { // TODO: Move these to model json @@ -75,7 +75,7 @@ private static void setupClient0() { } } - for (var builder : RegistryInfo.FLUID) { + for (var builder : RegistryObjectStorage.FLUID) { if (builder instanceof FluidBuilder b) { switch (b.fluidType.renderType) { case CUTOUT -> { @@ -97,7 +97,7 @@ private static void setupClient0() { @SubscribeEvent public static void blockColors(RegisterColorHandlersEvent.Block event) { - for (var builder : RegistryInfo.BLOCK) { + for (var builder : RegistryObjectStorage.BLOCK) { if (builder instanceof BlockBuilder b && b.tint != null) { event.register(new BlockTintFunctionWrapper(b.tint), b.get()); } @@ -106,7 +106,7 @@ public static void blockColors(RegisterColorHandlersEvent.Block event) { @SubscribeEvent public static void itemColors(RegisterColorHandlersEvent.Item event) { - for (var builder : RegistryInfo.ITEM) { + for (var builder : RegistryObjectStorage.ITEM) { if (builder instanceof ItemBuilder b && b.tint != null) { event.register(new ItemTintFunctionWrapper(b.tint), b.get()); } diff --git a/src/main/java/dev/latvian/mods/kubejs/net/FirstClickPayload.java b/src/main/java/dev/latvian/mods/kubejs/net/FirstClickPayload.java index 0755f8104..e4506d467 100644 --- a/src/main/java/dev/latvian/mods/kubejs/net/FirstClickPayload.java +++ b/src/main/java/dev/latvian/mods/kubejs/net/FirstClickPayload.java @@ -24,7 +24,7 @@ public void handle(IPayloadContext ctx) { if (clickType == 0 && ItemEvents.FIRST_LEFT_CLICKED.hasListeners()) { ctx.enqueueWork(() -> { var stack = serverPlayer.getItemInHand(InteractionHand.MAIN_HAND); - var key = stack.getItem().kjs$getRegistryKey(); + var key = stack.getItem().kjs$getKey(); if (ItemEvents.FIRST_LEFT_CLICKED.hasListeners(key)) { ItemEvents.FIRST_LEFT_CLICKED.post(ScriptType.SERVER, key, new ItemClickedKubeEvent(serverPlayer, InteractionHand.MAIN_HAND, stack)); @@ -34,7 +34,7 @@ public void handle(IPayloadContext ctx) { ctx.enqueueWork(() -> { for (var hand : InteractionHand.values()) { var stack = serverPlayer.getItemInHand(hand); - var key = stack.getItem().kjs$getRegistryKey(); + var key = stack.getItem().kjs$getKey(); if (ItemEvents.FIRST_RIGHT_CLICKED.hasListeners(key)) { ItemEvents.FIRST_RIGHT_CLICKED.post(ScriptType.SERVER, key, new ItemClickedKubeEvent(serverPlayer, hand, stack)); diff --git a/src/main/java/dev/latvian/mods/kubejs/player/KubeJSInventoryListener.java b/src/main/java/dev/latvian/mods/kubejs/player/KubeJSInventoryListener.java index 58ad19bbc..406985307 100644 --- a/src/main/java/dev/latvian/mods/kubejs/player/KubeJSInventoryListener.java +++ b/src/main/java/dev/latvian/mods/kubejs/player/KubeJSInventoryListener.java @@ -16,7 +16,7 @@ public KubeJSInventoryListener(Player p) { @Override public void slotChanged(AbstractContainerMenu container, int index, ItemStack stack) { if (!stack.isEmpty() && container.getSlot(index).container == player.getInventory()) { - var key = stack.getItem().kjs$getRegistryKey(); + var key = stack.getItem().kjs$getKey(); if (PlayerEvents.INVENTORY_CHANGED.hasListeners(key)) { PlayerEvents.INVENTORY_CHANGED.post(player, key, new InventoryChangedKubeEvent(player, stack, index)); diff --git a/src/main/java/dev/latvian/mods/kubejs/player/KubeJSPlayerEventHandler.java b/src/main/java/dev/latvian/mods/kubejs/player/KubeJSPlayerEventHandler.java index f3604040e..ed0d5d2d3 100644 --- a/src/main/java/dev/latvian/mods/kubejs/player/KubeJSPlayerEventHandler.java +++ b/src/main/java/dev/latvian/mods/kubejs/player/KubeJSPlayerEventHandler.java @@ -121,7 +121,7 @@ public static void inventoryOpened(PlayerContainerEvent.Open event) { ResourceKey> key; try { - key = menu.getType().kjs$getRegistryKey(); + key = menu.getType().kjs$getKey(); } catch (Exception ex) { return; } @@ -146,7 +146,7 @@ public static void inventoryClosed(PlayerContainerEvent.Close event) { ResourceKey> key; try { - key = menu.getType().kjs$getRegistryKey(); + key = menu.getType().kjs$getKey(); } catch (Exception ex) { return; } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java index cfceb00f8..0032bd3c0 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/RecipesKubeEvent.java @@ -23,7 +23,6 @@ import dev.latvian.mods.kubejs.recipe.schema.RecipeSchemaType; import dev.latvian.mods.kubejs.recipe.schema.UnknownRecipeSchema; import dev.latvian.mods.kubejs.recipe.special.SpecialRecipeSerializerManager; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.script.ConsoleJS; import dev.latvian.mods.kubejs.script.ScriptType; import dev.latvian.mods.kubejs.script.SourceLine; @@ -37,6 +36,7 @@ import dev.latvian.mods.kubejs.util.UtilsJS; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.util.HideFromJS; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.GsonHelper; import net.minecraft.world.item.ItemStack; @@ -86,7 +86,7 @@ public class RecipesKubeEvent implements KubeEvent { private String recipeToString(Recipe recipe) { var map = new LinkedHashMap(); - map.put("type", RegistryInfo.RECIPE_SERIALIZER.getId(recipe.getSerializer())); + map.put("type", BuiltInRegistries.RECIPE_SERIALIZER.getKey(recipe.getSerializer())); try { var in = new ArrayList<>(); @@ -220,7 +220,7 @@ public RecipesKubeEvent(ServerScriptManager manager) { recipeFunctions.put("smithing", smithing); recipeFunctions.put("smithingTrim", smithingTrim); - stageSerializer = RegistryInfo.RECIPE_SERIALIZER.getValue(ResourceLocation.parse("recipestages:stage")); + stageSerializer = BuiltInRegistries.RECIPE_SERIALIZER.get(ResourceLocation.parse("recipestages:stage")); } @HideFromJS @@ -589,7 +589,7 @@ public void printTypes(Context cx) { public void printAllTypes() { ConsoleJS.SERVER.info("== All recipe types [available] =="); - printTypes(t -> RegistryInfo.RECIPE_SERIALIZER.getValue(t.id) != null); + printTypes(t -> BuiltInRegistries.RECIPE_SERIALIZER.get(t.id) != null); } public void printExamples(String type) { diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/EnumComponent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/EnumComponent.java index 07d3b3c93..2a1448127 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/EnumComponent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/EnumComponent.java @@ -9,7 +9,7 @@ import net.minecraft.util.StringRepresentable; public record EnumComponent & StringRepresentable>(EnumTypeInfo enumTypeInfo, Codec codec) implements RecipeComponent { - public static final RecipeComponentFactory FACTORY = (storage, reader) -> { + public static final RecipeComponentFactory FACTORY = (registries, storage, reader) -> { reader.skipWhitespace(); reader.expect('<'); reader.skipWhitespace(); diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/NumberComponent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/NumberComponent.java index a2064c4a1..80c2e5395 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/NumberComponent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/NumberComponent.java @@ -33,7 +33,7 @@ static DoubleRange doubleRange(double min, double max) { } static RecipeComponentFactory createFactory(T zero, NumberComponent range, StringReaderFunction numFunc) { - return (storage, reader) -> { + return (registries, storage, reader) -> { reader.skipWhitespace(); if (!reader.canRead() || reader.peek() != '<') { diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/RegistryComponent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/RegistryComponent.java index 935671452..b014a1753 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/RegistryComponent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/RegistryComponent.java @@ -6,45 +6,48 @@ import dev.latvian.mods.kubejs.item.ItemStackJS; import dev.latvian.mods.kubejs.recipe.KubeRecipe; import dev.latvian.mods.kubejs.recipe.schema.RecipeComponentFactory; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.registry.RegistryType; import dev.latvian.mods.kubejs.script.KubeJSContext; import dev.latvian.mods.kubejs.util.ID; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.type.TypeInfo; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.resources.RegistryFixedCodec; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.material.Fluid; import net.neoforged.neoforge.fluids.FluidStack; +import org.jetbrains.annotations.Nullable; -public record RegistryComponent(RegistryInfo registry) implements RecipeComponent { +public record RegistryComponent(Registry registry, @Nullable RegistryType regType, Codec codec) implements RecipeComponent { @SuppressWarnings({"unchecked", "rawtypes"}) - public static final RecipeComponentFactory FACTORY = (storage, reader) -> { + public static final RecipeComponentFactory FACTORY = (registries, storage, reader) -> { reader.skipWhitespace(); reader.expect('<'); reader.skipWhitespace(); var regId = ResourceLocation.read(reader); reader.expect('>'); - return new RegistryComponent(RegistryInfo.of(ResourceKey.createRegistryKey(regId))); + var key = ResourceKey.createRegistryKey(regId); + return new RegistryComponent(registries.access().registry(key).orElseThrow(), RegistryType.ofKey(key), RegistryFixedCodec.create(key)); }; @Override public Codec codec() { - return registry.valueByNameCodec(); + return codec; } @Override public TypeInfo typeInfo() { - var t = RegistryType.ofKey(registry.key); - return t == null || t.type() == TypeInfo.STRING ? TypeInfo.STRING : TypeInfo.STRING.or(t.type()); + return regType == null || regType.type() == TypeInfo.STRING ? TypeInfo.STRING : TypeInfo.STRING.or(regType.type()); } @Override @SuppressWarnings("unchecked") public T wrap(Context cx, KubeRecipe recipe, Object from) { - if (registry == RegistryInfo.ITEM) { + if (registry == BuiltInRegistries.ITEM) { if (from instanceof ItemStack is) { return (T) is.getItem(); } else if (from instanceof Item) { @@ -52,7 +55,7 @@ public T wrap(Context cx, KubeRecipe recipe, Object from) { } else { return (T) ItemStackJS.wrap(((KubeJSContext) cx).getRegistries(), from).getItem(); } - } else if (registry == RegistryInfo.FLUID) { + } else if (registry == BuiltInRegistries.FLUID) { if (from instanceof FluidStack fs) { return (T) fs.getFluid(); } else if (from instanceof Fluid) { @@ -61,25 +64,22 @@ public T wrap(Context cx, KubeRecipe recipe, Object from) { return (T) FluidWrapper.wrap(((KubeJSContext) cx).getRegistries(), from).getFluid(); } } else { - var regType = RegistryType.ofKey(registry.key); - if (regType != null && regType.baseClass().isInstance(from)) { return (T) from; } - return registry.getValue(ID.mc(from)); + return registry.get(ID.mc(from)); } } @Override public boolean hasPriority(Context cx, KubeRecipe recipe, Object from) { - var regType = RegistryType.ofKey(registry.key); return (regType != null && regType.baseClass().isInstance(from)) || (from instanceof CharSequence && ID.mc(from.toString()) != null) || (from instanceof JsonPrimitive json && json.isString() && ID.mc(json.getAsString()) != null); } @Override public void buildUniqueId(UniqueIdBuilder builder, T value) { - var id = registry.getId(value); + var id = registry.getKey(value); if (id != null) { builder.append(id); @@ -88,6 +88,6 @@ public void buildUniqueId(UniqueIdBuilder builder, T value) { @Override public String toString() { - return "registry_element<" + registry + ">"; + return "registry_element<" + registry.key().location() + ">"; } } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/TagKeyComponent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/TagKeyComponent.java index 2ddf62ed9..fbe28d853 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/TagKeyComponent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/TagKeyComponent.java @@ -27,7 +27,7 @@ public record TagKeyComponent(ResourceKey> registry, Ty public static final RecipeComponent> BIOME = new TagKeyComponent<>(Registries.BIOME, TypeInfo.of(Biome.class)); public static final RecipeComponent> FLUID = new TagKeyComponent<>(Registries.FLUID, TypeInfo.of(Fluid.class)); - public static final RecipeComponentFactory FACTORY = (storage, reader) -> { + public static final RecipeComponentFactory FACTORY = (registries, storage, reader) -> { reader.skipWhitespace(); reader.expect('<'); reader.skipWhitespace(); diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/schema/JsonRecipeSchemaLoader.java b/src/main/java/dev/latvian/mods/kubejs/recipe/schema/JsonRecipeSchemaLoader.java index 1a0f93bc6..a0ba3fc30 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/schema/JsonRecipeSchemaLoader.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/schema/JsonRecipeSchemaLoader.java @@ -7,6 +7,7 @@ import dev.latvian.mods.kubejs.recipe.component.ComponentRole; import dev.latvian.mods.kubejs.script.ConsoleJS; import dev.latvian.mods.kubejs.util.JsonUtils; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.resources.ResourceManager; @@ -240,7 +241,7 @@ private RecipeSchema getSchema(DynamicOps jsonOps) { } } - public static void load(RecipeSchemaStorage storage, RecipeSchemaRegistry event, ResourceManager resourceManager, DynamicOps jsonOps) { + public static void load(RecipeSchemaStorage storage, RegistryAccessContainer registries, RecipeSchemaRegistry event, ResourceManager resourceManager) { var map = new HashMap(); for (var entry : resourceManager.listResources("kubejs/recipe_schema", path -> path.getPath().endsWith(".json")).entrySet()) { @@ -292,7 +293,7 @@ public static void load(RecipeSchemaStorage storage, RecipeSchemaRegistry event, default -> ComponentRole.OTHER; }; - var type = storage.getComponent(keyJson.get("type").getAsString()); + var type = storage.getComponent(registries, keyJson.get("type").getAsString()); var key = type.key(name, role); if (keyJson.has("optional")) { @@ -302,7 +303,7 @@ public static void load(RecipeSchemaStorage storage, RecipeSchemaRegistry event, key.defaultOptional(); } else { try { - key.optional = new RecipeOptional.Constant(key.codec.decode(jsonOps, optionalJson).getOrThrow().getFirst()); + key.optional = new RecipeOptional.Constant(key.codec.decode(registries.json(), optionalJson).getOrThrow().getFirst()); } catch (Exception ex) { throw new IllegalArgumentException("Failed to create optional value for key '" + key + "' of '" + holder.id + "' from " + optionalJson, ex); } @@ -395,7 +396,7 @@ public static void load(RecipeSchemaStorage storage, RecipeSchemaRegistry event, } for (var holder : map.values()) { - var schema = holder.getSchema(jsonOps); + var schema = holder.getSchema(registries.json()); event.namespace(holder.id.getNamespace()).register(holder.id.getPath(), schema); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeComponentFactory.java b/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeComponentFactory.java index ac683913a..9b6870d5f 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeComponentFactory.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeComponentFactory.java @@ -2,20 +2,21 @@ import com.mojang.brigadier.StringReader; import dev.latvian.mods.kubejs.recipe.component.RecipeComponent; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import java.util.function.BiFunction; import java.util.function.Function; @FunctionalInterface public interface RecipeComponentFactory { - RecipeComponent readComponent(RecipeSchemaStorage storage, StringReader reader) throws Exception; + RecipeComponent readComponent(RegistryAccessContainer registries, RecipeSchemaStorage storage, StringReader reader) throws Exception; static RecipeComponentFactory readOneComponent(Function, RecipeComponent> factory) { - return (storage, reader) -> { + return (registries, storage, reader) -> { reader.skipWhitespace(); reader.expect('<'); reader.skipWhitespace(); - var component = storage.readComponent(reader); + var component = storage.readComponent(registries, reader); reader.skipWhitespace(); reader.expect('>'); return factory.apply(component); @@ -23,15 +24,15 @@ static RecipeComponentFactory readOneComponent(Function, Reci } static RecipeComponentFactory readTwoComponents(BiFunction, RecipeComponent, RecipeComponent> factory) { - return (storage, reader) -> { + return (registries, storage, reader) -> { reader.skipWhitespace(); reader.expect('<'); reader.skipWhitespace(); - var key = storage.readComponent(reader); + var key = storage.readComponent(registries, reader); reader.skipWhitespace(); reader.expect(','); reader.skipWhitespace(); - var component = storage.readComponent(reader); + var component = storage.readComponent(registries, reader); reader.skipWhitespace(); reader.expect('>'); return factory.apply(key, component); diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeSchemaStorage.java b/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeSchemaStorage.java index d0d491b7d..5ed7fcd04 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeSchemaStorage.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeSchemaStorage.java @@ -1,17 +1,16 @@ package dev.latvian.mods.kubejs.recipe.schema; import com.google.gson.JsonArray; -import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.mojang.brigadier.StringReader; -import com.mojang.serialization.DynamicOps; import dev.latvian.mods.kubejs.bindings.event.ServerEvents; import dev.latvian.mods.kubejs.plugin.KubeJSPlugin; import dev.latvian.mods.kubejs.plugin.KubeJSPlugins; import dev.latvian.mods.kubejs.recipe.component.RecipeComponent; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.script.ScriptType; import dev.latvian.mods.kubejs.util.JsonUtils; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.resources.ResourceManager; @@ -45,7 +44,7 @@ public RecipeNamespace namespace(String namespace) { return namespaces.computeIfAbsent(namespace, n -> new RecipeNamespace(this, n)); } - public void fireEvents(ResourceManager resourceManager, DynamicOps jsonOps) { + public void fireEvents(RegistryAccessContainer registries, ResourceManager resourceManager) { recipeTypes.clear(); namespaces.clear(); mappings.clear(); @@ -91,20 +90,20 @@ public void fireEvents(ResourceManager resourceManager, DynamicOps var json = JsonUtils.GSON.fromJson(reader, JsonObject.class); for (var entry1 : json.entrySet()) { - simpleComponents.put(entry1.getKey(), getComponent(entry1.getValue().getAsString())); + simpleComponents.put(entry1.getKey(), getComponent(registries, entry1.getValue().getAsString())); } } catch (Exception ex) { ex.printStackTrace(); } } - for (var entry : RegistryInfo.RECIPE_SERIALIZER.entrySet()) { + for (var entry : BuiltInRegistries.RECIPE_SERIALIZER.entrySet()) { var ns = namespace(entry.getKey().location().getNamespace()); ns.put(entry.getKey().location().getPath(), new UnknownRecipeSchemaType(ns, entry.getKey().location(), entry.getValue())); } var schemaRegistry = new RecipeSchemaRegistry(this); - JsonRecipeSchemaLoader.load(this, schemaRegistry, resourceManager, jsonOps); + JsonRecipeSchemaLoader.load(this, registries, schemaRegistry, resourceManager); shapedSchema = Objects.requireNonNull(namespace("minecraft").get("shaped").schema); shapelessSchema = Objects.requireNonNull(namespace("minecraft").get("shapeless").schema); @@ -114,12 +113,12 @@ public void fireEvents(ResourceManager resourceManager, DynamicOps ServerEvents.RECIPE_SCHEMA_REGISTRY.post(ScriptType.SERVER, schemaRegistry); } - public RecipeComponent getComponent(String string) { + public RecipeComponent getComponent(RegistryAccessContainer registries, String string) { var c = componentCache.get(string); if (c == null) { try { - c = readComponent(new StringReader(string)); + c = readComponent(registries, new StringReader(string)); componentCache.put(string, c); } catch (Exception ex) { throw new IllegalArgumentException(ex); @@ -129,7 +128,7 @@ public RecipeComponent getComponent(String string) { return c; } - public RecipeComponent readComponent(StringReader reader) throws Exception { + public RecipeComponent readComponent(RegistryAccessContainer registries, StringReader reader) throws Exception { reader.skipWhitespace(); var key = reader.readUnquotedString(); @@ -144,7 +143,7 @@ public RecipeComponent readComponent(StringReader reader) throws Exception { var d = dynamicComponents.get(key); if (d != null) { - component = d.readComponent(this, reader); + component = d.readComponent(registries, this, reader); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeSchemaType.java b/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeSchemaType.java index 162f4912a..a840c390d 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeSchemaType.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeSchemaType.java @@ -1,7 +1,7 @@ package dev.latvian.mods.kubejs.recipe.schema; import dev.latvian.mods.kubejs.error.KubeRuntimeException; -import dev.latvian.mods.kubejs.registry.RegistryInfo; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.crafting.RecipeSerializer; @@ -24,7 +24,7 @@ public RecipeSerializer getSerializer() { var serializerId = schema.typeOverride == null ? id : schema.typeOverride; if (serializer == null) { - serializer = Optional.ofNullable(RegistryInfo.RECIPE_SERIALIZER.getValue(serializerId)); + serializer = Optional.ofNullable(BuiltInRegistries.RECIPE_SERIALIZER.get(serializerId)); } var s = serializer.orElse(null); diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/special/SpecialRecipeSerializerManager.java b/src/main/java/dev/latvian/mods/kubejs/recipe/special/SpecialRecipeSerializerManager.java index 166d8c14a..981d86df3 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/special/SpecialRecipeSerializerManager.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/special/SpecialRecipeSerializerManager.java @@ -2,7 +2,7 @@ import dev.latvian.mods.kubejs.event.EventResult; import dev.latvian.mods.kubejs.event.KubeEvent; -import dev.latvian.mods.kubejs.registry.RegistryInfo; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.crafting.Recipe; import net.neoforged.bus.api.Event; @@ -30,7 +30,7 @@ public void afterPosted(EventResult result) { } public boolean isSpecial(Recipe recipe) { - return data.getOrDefault(RegistryInfo.RECIPE_SERIALIZER.getId(recipe.getSerializer()), recipe.isSpecial()); + return data.getOrDefault(BuiltInRegistries.RECIPE_SERIALIZER.getKey(recipe.getSerializer()), recipe.isSpecial()); } public void ignoreSpecialFlag(ResourceLocation id) { @@ -47,7 +47,7 @@ public void addSpecialFlag(ResourceLocation id) { public void ignoreSpecialMod(String modid) { synchronized (data) { - for (var entry : RegistryInfo.RECIPE_SERIALIZER.entrySet()) { + for (var entry : BuiltInRegistries.RECIPE_SERIALIZER.entrySet()) { if (entry.getKey().location().getNamespace().equals(modid)) { data.put(entry.getKey().location(), false); } @@ -57,7 +57,7 @@ public void ignoreSpecialMod(String modid) { public void addSpecialMod(String modid) { synchronized (data) { - for (var entry : RegistryInfo.RECIPE_SERIALIZER.entrySet()) { + for (var entry : BuiltInRegistries.RECIPE_SERIALIZER.entrySet()) { if (entry.getKey().location().getNamespace().equals(modid)) { data.put(entry.getKey().location(), true); } diff --git a/src/main/java/dev/latvian/mods/kubejs/registry/AdditionalObjectRegistry.java b/src/main/java/dev/latvian/mods/kubejs/registry/AdditionalObjectRegistry.java index 2cee453aa..a968d9fd4 100644 --- a/src/main/java/dev/latvian/mods/kubejs/registry/AdditionalObjectRegistry.java +++ b/src/main/java/dev/latvian/mods/kubejs/registry/AdditionalObjectRegistry.java @@ -6,8 +6,4 @@ @FunctionalInterface public interface AdditionalObjectRegistry { void add(ResourceKey> registry, BuilderBase builder); - - default void add(RegistryInfo registryInfo, BuilderBase builder) { - add(registryInfo.key, builder); - } } diff --git a/src/main/java/dev/latvian/mods/kubejs/registry/BuilderBase.java b/src/main/java/dev/latvian/mods/kubejs/registry/BuilderBase.java index 409163af3..11f733f3e 100644 --- a/src/main/java/dev/latvian/mods/kubejs/registry/BuilderBase.java +++ b/src/main/java/dev/latvian/mods/kubejs/registry/BuilderBase.java @@ -1,13 +1,18 @@ package dev.latvian.mods.kubejs.registry; import dev.latvian.mods.kubejs.client.LangKubeEvent; +import dev.latvian.mods.kubejs.error.KubeRuntimeException; import dev.latvian.mods.kubejs.generator.KubeAssetGenerator; import dev.latvian.mods.kubejs.generator.KubeDataGenerator; +import dev.latvian.mods.kubejs.script.SourceLine; import dev.latvian.mods.kubejs.typings.Info; import dev.latvian.mods.kubejs.util.UtilsJS; +import dev.latvian.mods.rhino.util.HideFromJS; import dev.latvian.mods.rhino.util.ReturnsSelf; import net.minecraft.Util; +import net.minecraft.core.Registry; import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import java.util.Arrays; @@ -18,6 +23,8 @@ @ReturnsSelf public abstract class BuilderBase implements Supplier { public final ResourceLocation id; + public SourceLine sourceLine; + public ResourceKey> registryKey; protected T object; public String translationKey; public Component displayName; @@ -25,20 +32,21 @@ public abstract class BuilderBase implements Supplier { public transient boolean dummyBuilder; public transient Set defaultTags; - public BuilderBase(ResourceLocation i) { - id = i; - object = null; - translationKey = ""; - displayName = null; - formattedDisplayName = false; - dummyBuilder = false; - defaultTags = new HashSet<>(); + public BuilderBase(ResourceLocation id) { + this.id = id; + this.sourceLine = SourceLine.UNKNOWN; + this.object = null; + this.translationKey = ""; + this.displayName = null; + this.formattedDisplayName = false; + this.dummyBuilder = false; + this.defaultTags = new HashSet<>(); } - public abstract RegistryInfo getRegistryType(); - + @HideFromJS public abstract T createObject(); + @HideFromJS public T transformObject(T obj) { return obj; } @@ -49,18 +57,23 @@ public final T get() { return object; } catch (Exception ex) { if (dummyBuilder) { - throw new RuntimeException("Object '" + id + "' of registry '" + getRegistryType().key.location() + "' is from a dummy builder and doesn't have a value!"); + throw new KubeRuntimeException("Object '" + id + "' of registry '" + registryKey.location() + "' is from a dummy builder and doesn't have a value!").source(sourceLine); } else { - throw new RuntimeException("Object '" + id + "' of registry '" + getRegistryType().key.location() + "' hasn't been registered yet!", ex); + throw new KubeRuntimeException("Object '" + id + "' of registry '" + registryKey.location() + "' hasn't been registered yet!", ex).source(sourceLine); } } } + @HideFromJS public void createAdditionalObjects(AdditionalObjectRegistry registry) { } public String getTranslationKeyGroup() { - return getRegistryType().languageKeyPrefix; + if (registryKey == null) { + return "unknown_registry"; + } + + return registryKey.location().getPath().replace('/', '.'); } @Info(""" @@ -101,10 +114,10 @@ public BuilderBase formattedDisplayName(Component name) { """) public BuilderBase tag(ResourceLocation[] tag) { defaultTags.addAll(Arrays.asList(tag)); - getRegistryType().hasDefaultTags = true; return this; } + @HideFromJS public ResourceLocation newID(String pre, String post) { if (pre.isEmpty() && post.isEmpty()) { return id; @@ -113,9 +126,11 @@ public ResourceLocation newID(String pre, String post) { return ResourceLocation.fromNamespaceAndPath(id.getNamespace(), pre + id.getPath() + post); } + @HideFromJS public void generateDataJsons(KubeDataGenerator generator) { } + @HideFromJS public void generateAssetJsons(KubeAssetGenerator generator) { } @@ -127,6 +142,7 @@ public String getBuilderTranslationKey() { return translationKey; } + @HideFromJS public void generateLang(LangKubeEvent lang) { if (displayName != null) { lang.add(id.getNamespace(), getBuilderTranslationKey(), displayName.getString()); @@ -135,7 +151,8 @@ public void generateLang(LangKubeEvent lang) { } } - protected T createTransformedObject() { + @HideFromJS + public T createTransformedObject() { object = transformObject(createObject()); return object; } diff --git a/src/main/java/dev/latvian/mods/kubejs/registry/BuilderTypeRegistry.java b/src/main/java/dev/latvian/mods/kubejs/registry/BuilderTypeRegistry.java index c08eb5582..854edf84f 100644 --- a/src/main/java/dev/latvian/mods/kubejs/registry/BuilderTypeRegistry.java +++ b/src/main/java/dev/latvian/mods/kubejs/registry/BuilderTypeRegistry.java @@ -1,11 +1,12 @@ package dev.latvian.mods.kubejs.registry; +import com.mojang.serialization.Codec; +import dev.latvian.mods.rhino.type.TypeInfo; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import java.util.function.Consumer; -@FunctionalInterface public interface BuilderTypeRegistry { interface Callback { void addDefault(Class> builderType, BuilderFactory factory); @@ -18,4 +19,10 @@ interface Callback { default void addDefault(ResourceKey> registry, Class> builderType, BuilderFactory factory) { of(registry, reg -> reg.addDefault(builderType, factory)); } + + void serverRegistry(ResourceKey> registry, Codec directCodec, TypeInfo typeInfo); + + default void serverRegistry(ResourceKey> registry, Codec directCodec, Class type) { + serverRegistry(registry, directCodec, TypeInfo.of(type)); + } } diff --git a/src/main/java/dev/latvian/mods/kubejs/registry/BuilderTypeRegistryHandler.java b/src/main/java/dev/latvian/mods/kubejs/registry/BuilderTypeRegistryHandler.java index b1f147d57..b65f1e368 100644 --- a/src/main/java/dev/latvian/mods/kubejs/registry/BuilderTypeRegistryHandler.java +++ b/src/main/java/dev/latvian/mods/kubejs/registry/BuilderTypeRegistryHandler.java @@ -1,41 +1,100 @@ package dev.latvian.mods.kubejs.registry; +import com.mojang.serialization.Codec; +import dev.latvian.mods.kubejs.plugin.KubeJSPlugin; +import dev.latvian.mods.kubejs.plugin.KubeJSPlugins; import dev.latvian.mods.kubejs.script.ConsoleJS; +import dev.latvian.mods.kubejs.util.Lazy; +import dev.latvian.mods.rhino.type.TypeInfo; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; +import org.jetbrains.annotations.Nullable; +import java.util.IdentityHashMap; import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.function.Consumer; -public class BuilderTypeRegistryHandler implements BuilderTypeRegistry { +public record BuilderTypeRegistryHandler(Map, Info> map) implements BuilderTypeRegistry { + public static final Lazy, Info>> INFO = Lazy.of(() -> { + var handler = new BuilderTypeRegistryHandler(new IdentityHashMap<>()); + KubeJSPlugins.forEachPlugin(handler, KubeJSPlugin::registerBuilderTypes); + return handler.map; + }); + + public static Info info(ResourceKey> key) { + return (Info) INFO.get().get(key); + } + + public static class Info { + private static final Info EMPTY = new Info<>(); + + private BuilderType defaultType; + private Map> types; + private Codec directCodec; + private TypeInfo typeInfo; + + @Nullable + public BuilderType defaultType() { + return defaultType; + } + + public List> types() { + return types == null ? List.of() : List.copyOf(types.values()); + } + + @Nullable + public BuilderType namedType(String name) { + return types == null ? null : types.get(name); + } + + @Nullable + public Codec directCodec() { + return directCodec; + } + + @Nullable + public TypeInfo typeInfo() { + return typeInfo; + } + } + @Override public void of(ResourceKey> registry, Consumer> callback) { - callback.accept(new RegConsumer<>(RegistryInfo.of(registry))); + callback.accept(new RegConsumer<>((Info) map.computeIfAbsent(registry, k -> new Info<>()))); + } + + @Override + public void serverRegistry(ResourceKey> registry, Codec directCodec, TypeInfo typeInfo) { + var info = map.computeIfAbsent(registry, k -> new Info<>()); + info.directCodec = (Codec) directCodec; + info.typeInfo = typeInfo == null ? TypeInfo.NONE : typeInfo; } - private record RegConsumer(RegistryInfo registryInfo) implements BuilderTypeRegistry.Callback { + private record RegConsumer(Info info) implements BuilderTypeRegistry.Callback { @Override public void addDefault(Class> builderType, BuilderFactory factory) { - if (registryInfo.defaultType != null) { - ConsoleJS.STARTUP.warn("Previous default type '" + registryInfo.defaultType.builderClass().getName() + "' for registry '" + registryInfo + "' replaced with '" + builderType.getName() + "'!"); + if (info.defaultType != null) { + ConsoleJS.STARTUP.warn("Previous default type '" + info.defaultType.builderClass().getName() + "' for registry '" + info + "' replaced with '" + builderType.getName() + "'!"); } - registryInfo.defaultType = new BuilderType<>("default", builderType, factory); + info.defaultType = new BuilderType<>("default", builderType, factory); } @Override public void add(String type, Class> builderType, BuilderFactory factory) { - if (registryInfo.types == null) { - registryInfo.types = new LinkedHashMap<>(); + if (info.types == null) { + info.types = new LinkedHashMap<>(); } - var prev = registryInfo.types.get(type); + var prev = info.types.get(type); if (prev != null) { - ConsoleJS.STARTUP.warn("Previous '" + type + "' type '" + prev.builderClass().getName() + "' for registry '" + registryInfo + "' replaced with '" + builderType.getName() + "'!"); + ConsoleJS.STARTUP.warn("Previous '" + type + "' type '" + prev.builderClass().getName() + "' for registry '" + info + "' replaced with '" + builderType.getName() + "'!"); } - registryInfo.types.put(type, new BuilderType<>(type, builderType, factory)); + info.types.put(type, new BuilderType<>(type, builderType, factory)); } } } diff --git a/src/main/java/dev/latvian/mods/kubejs/registry/CustomBuilderObject.java b/src/main/java/dev/latvian/mods/kubejs/registry/CustomBuilderObject.java index 8c30e097f..e88ca9d95 100644 --- a/src/main/java/dev/latvian/mods/kubejs/registry/CustomBuilderObject.java +++ b/src/main/java/dev/latvian/mods/kubejs/registry/CustomBuilderObject.java @@ -4,32 +4,12 @@ import java.util.function.Supplier; -// Class that acts as a holder for a custom registry object, usually based off a class from another mod. public class CustomBuilderObject extends BuilderBase { private final Supplier object; - private final RegistryInfo registry; - public CustomBuilderObject(ResourceLocation i, Supplier object, RegistryInfo registry) { + public CustomBuilderObject(ResourceLocation i, Supplier object) { super(i); this.object = object; - this.registry = registry; - // This, along with overriding getTranslationKeyGroup, is to avoid a crash occurring from getRegistryType returning null - // when called from the super constructor, because the value it returns is not set until after the super constructor is run - this.translationKey = getTranslationKeyGroup() + "." + id.getNamespace() + "." + id.getPath(); - } - - @Override - public String getTranslationKeyGroup() { - if (getRegistryType() == null) { - return "If you see this something broke. Please file a bug report."; - } else { - return super.getTranslationKeyGroup(); - } - } - - @Override - public RegistryInfo getRegistryType() { - return registry; } @Override diff --git a/src/main/java/dev/latvian/mods/kubejs/registry/RegistryEventHandler.java b/src/main/java/dev/latvian/mods/kubejs/registry/RegistryEventHandler.java index ac4484f1c..72717cf6c 100644 --- a/src/main/java/dev/latvian/mods/kubejs/registry/RegistryEventHandler.java +++ b/src/main/java/dev/latvian/mods/kubejs/registry/RegistryEventHandler.java @@ -1,9 +1,9 @@ package dev.latvian.mods.kubejs.registry; -import dev.latvian.mods.kubejs.CommonProperties; import dev.latvian.mods.kubejs.DevProperties; import dev.latvian.mods.kubejs.KubeJS; import dev.latvian.mods.kubejs.script.ConsoleJS; +import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import net.neoforged.bus.api.EventPriority; import net.neoforged.bus.api.SubscribeEvent; @@ -15,46 +15,40 @@ public class RegistryEventHandler { @SuppressWarnings({"rawtypes", "unchecked"}) @SubscribeEvent(priority = EventPriority.LOW) public static void registerAll(RegisterEvent event) { - handleRegistryEvent(RegistryInfo.of((ResourceKey) event.getRegistryKey()), event); + handleRegistryEvent((ResourceKey) event.getRegistryKey(), event); } - private static void handleRegistryEvent(RegistryInfo registryInfo, RegisterEvent event) { - if (!registryInfo.bypassServerOnly && CommonProperties.get().serverOnly) { - if (DevProperties.get().logRegistryEventObjects) { - KubeJS.LOGGER.warn("Skipping " + registryInfo + " registry - server only"); - } - - return; - } + private static void handleRegistryEvent(ResourceKey> registryKey, RegisterEvent event) { + var objStorage = RegistryObjectStorage.of(registryKey); - if (registryInfo.objects.isEmpty()) { + if (objStorage.objects.isEmpty()) { if (DevProperties.get().logRegistryEventObjects) { - KubeJS.LOGGER.info("Skipping " + registryInfo + " registry - no objects to build"); + KubeJS.LOGGER.info("Skipping " + registryKey.location() + " registry - no objects to build"); } return; } if (DevProperties.get().logRegistryEventObjects) { - KubeJS.LOGGER.info("Building " + registryInfo.objects.size() + " objects of " + registryInfo + " registry"); + KubeJS.LOGGER.info("Building " + objStorage.objects.size() + " objects of " + registryKey.location() + " registry"); } int added = 0; - for (var builder : registryInfo) { + for (var builder : objStorage) { if (!builder.dummyBuilder) { - event.register(registryInfo.key, builder.id, builder::createTransformedObject); + event.register(registryKey, builder.id, builder::createTransformedObject); if (DevProperties.get().logRegistryEventObjects) { - ConsoleJS.STARTUP.info("+ " + registryInfo + " | " + builder.id); + ConsoleJS.STARTUP.info("+ " + registryKey.location() + " | " + builder.id); } added++; } } - if (!registryInfo.objects.isEmpty() && DevProperties.get().logRegistryEventObjects) { - KubeJS.LOGGER.info("Registered " + added + "/" + registryInfo.objects.size() + " objects of " + registryInfo); + if (!objStorage.objects.isEmpty() && DevProperties.get().logRegistryEventObjects) { + KubeJS.LOGGER.info("Registered " + added + "/" + objStorage.objects.size() + " objects of " + registryKey.location()); } } } diff --git a/src/main/java/dev/latvian/mods/kubejs/registry/RegistryInfo.java b/src/main/java/dev/latvian/mods/kubejs/registry/RegistryInfo.java deleted file mode 100644 index 9962d39f9..000000000 --- a/src/main/java/dev/latvian/mods/kubejs/registry/RegistryInfo.java +++ /dev/null @@ -1,209 +0,0 @@ -package dev.latvian.mods.kubejs.registry; - -import com.mojang.serialization.Codec; -import dev.latvian.mods.kubejs.core.RegistryObjectKJS; -import dev.latvian.mods.kubejs.util.Cast; -import dev.latvian.mods.kubejs.util.ID; -import net.minecraft.commands.synchronization.ArgumentTypeInfo; -import net.minecraft.core.Holder; -import net.minecraft.core.Registry; -import net.minecraft.core.particles.ParticleType; -import net.minecraft.core.registries.BuiltInRegistries; -import net.minecraft.core.registries.Registries; -import net.minecraft.resources.ResourceKey; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.sounds.SoundEvent; -import net.minecraft.world.effect.MobEffect; -import net.minecraft.world.entity.EntityType; -import net.minecraft.world.entity.ai.attributes.Attribute; -import net.minecraft.world.entity.ai.village.poi.PoiType; -import net.minecraft.world.entity.decoration.PaintingVariant; -import net.minecraft.world.entity.npc.VillagerProfession; -import net.minecraft.world.entity.npc.VillagerType; -import net.minecraft.world.inventory.MenuType; -import net.minecraft.world.item.ArmorMaterial; -import net.minecraft.world.item.CreativeModeTab; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.JukeboxSong; -import net.minecraft.world.item.alchemy.Potion; -import net.minecraft.world.item.crafting.RecipeSerializer; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.entity.BlockEntityType; -import net.minecraft.world.level.material.Fluid; -import net.neoforged.neoforge.fluids.FluidType; -import net.neoforged.neoforge.registries.NeoForgeRegistries; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.lang.ref.WeakReference; -import java.util.Collection; -import java.util.IdentityHashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; - -public final class RegistryInfo implements Iterable> { - private static final Object LOCK = new Object(); - private static final Map>, RegistryInfo> MAP = new IdentityHashMap<>(); - public static final List> ALL_BUILDERS = new LinkedList<>(); - - public static final Codec> CODEC = ResourceLocation.CODEC.xmap(rl -> RegistryInfo.of(ResourceKey.createRegistryKey(rl)), ri -> ri.key.location()); - - public static RegistryInfo of(ResourceKey> key) { - synchronized (LOCK) { - return Cast.to(MAP.computeIfAbsent(key, RegistryInfo::new)); - } - } - - public static RegistryInfo wrap(Object from) { - return switch (from) { - case RegistryInfo r -> r; - case ResourceKey k -> of(k); - case Registry r -> of(r.key()); - case RegistryType t -> of(t.key()); - case String ignore -> of(ResourceKey.createRegistryKey(ID.mc(from))); - case RegistryObjectKJS r -> r.kjs$getKubeRegistry(); - case Holder h -> of(h.unwrapKey().orElseThrow().registryKey()); - case null, default -> throw new IllegalArgumentException("Invalid registry: " + from); - }; - } - - public static final RegistryInfo SOUND_EVENT = of(Registries.SOUND_EVENT); - public static final RegistryInfo FLUID_TYPE = of(NeoForgeRegistries.Keys.FLUID_TYPES); - public static final RegistryInfo FLUID = of(Registries.FLUID); - public static final RegistryInfo MOB_EFFECT = of(Registries.MOB_EFFECT).languageKeyPrefix("effect"); - public static final RegistryInfo BLOCK = of(Registries.BLOCK); - public static final RegistryInfo> ENTITY_TYPE = of(Registries.ENTITY_TYPE); - public static final RegistryInfo ITEM = of(Registries.ITEM); - public static final RegistryInfo POTION = of(Registries.POTION); - public static final RegistryInfo> PARTICLE_TYPE = of(Registries.PARTICLE_TYPE); - public static final RegistryInfo> BLOCK_ENTITY_TYPE = of(Registries.BLOCK_ENTITY_TYPE); - public static final RegistryInfo PAINTING_VARIANT = of(Registries.PAINTING_VARIANT); - public static final RegistryInfo CUSTOM_STAT = of(Registries.CUSTOM_STAT); - public static final RegistryInfo> MENU = of(Registries.MENU); - public static final RegistryInfo> RECIPE_SERIALIZER = of(Registries.RECIPE_SERIALIZER); - public static final RegistryInfo ATTRIBUTE = of(Registries.ATTRIBUTE); - public static final RegistryInfo VILLAGER_PROFESSION = of(Registries.VILLAGER_PROFESSION); - public static final RegistryInfo VILLAGER_TYPE = of(Registries.VILLAGER_TYPE); - public static final RegistryInfo ARMOR_MATERIAL = of(Registries.ARMOR_MATERIAL); - public static final RegistryInfo POINT_OF_INTEREST_TYPE = of(Registries.POINT_OF_INTEREST_TYPE); - public static final RegistryInfo> COMMAND_ARGUMENT_TYPE = of(Registries.COMMAND_ARGUMENT_TYPE); - public static final RegistryInfo CREATIVE_MODE_TAB = of(Registries.CREATIVE_MODE_TAB); - public static final RegistryInfo JUKEBOX_SONG = of(Registries.JUKEBOX_SONG); - - public final ResourceKey> key; - BuilderType defaultType; - Map> types; - public final Map> objects; - public final ResourceKey unknownKey; - public boolean hasDefaultTags = false; - public boolean bypassServerOnly; - public String languageKeyPrefix; - private WeakReference> vanillaRegistry; - private Codec codec; - - private RegistryInfo(ResourceKey key) { - this.key = key; - this.objects = new LinkedHashMap<>(); - this.unknownKey = ResourceKey.create(key, ID.UNKNOWN); - this.bypassServerOnly = false; - this.languageKeyPrefix = key.location().getPath().replace('/', '.'); - } - - public RegistryInfo bypassServerOnly() { - this.bypassServerOnly = true; - return this; - } - - public RegistryInfo languageKeyPrefix(String prefix) { - this.languageKeyPrefix = prefix; - return this; - } - - @Override - public int hashCode() { - return key.hashCode(); - } - - @Override - public boolean equals(Object obj) { - return obj == this || obj instanceof RegistryInfo ri && key.equals(ri.key); - } - - @Override - public String toString() { - return key.location().toString(); - } - - @NotNull - @Override - public Iterator> iterator() { - return objects.values().iterator(); - } - - public Registry getVanillaRegistry() { - var reg = vanillaRegistry == null ? null : vanillaRegistry.get(); - - if (reg == null) { - reg = BuiltInRegistries.REGISTRY.get((ResourceKey) key); - - if (reg != null) { - vanillaRegistry = new WeakReference<>(reg); - } - } - - return reg; - } - - public Codec valueByNameCodec() { - if (codec == null) { - codec = getVanillaRegistry().byNameCodec(); - } - - return codec; - } - - public Set, T>> entrySet() { - return getVanillaRegistry().entrySet(); - } - - public ResourceLocation getId(T value) { - return getVanillaRegistry().getKey(value); - } - - public T getValue(ResourceLocation id) { - return getVanillaRegistry().get(id); - } - - public Holder.Reference getHolder(ResourceLocation id) { - return getVanillaRegistry().getHolder(id).orElseThrow(); - } - - public Holder.Reference getHolder(ResourceKey key) { - return getVanillaRegistry().getHolderOrThrow(key); - } - - public Holder getHolderOf(T value) { - return getVanillaRegistry().wrapAsHolder(value); - } - - public boolean hasValue(ResourceLocation id) { - return getVanillaRegistry().containsKey(id); - } - - public ResourceKey getKeyOf(T value) { - return getVanillaRegistry().getResourceKey(value).orElse(unknownKey); - } - - @Nullable - public BuilderType getDefaultType() { - return defaultType; - } - - public Collection> getTypes() { - return types == null ? List.of() : List.copyOf(types.values()); - } -} diff --git a/src/main/java/dev/latvian/mods/kubejs/registry/RegistryKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/registry/RegistryKubeEvent.java index 13bb7fbea..e876b6342 100644 --- a/src/main/java/dev/latvian/mods/kubejs/registry/RegistryKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/registry/RegistryKubeEvent.java @@ -1,10 +1,13 @@ package dev.latvian.mods.kubejs.registry; import dev.latvian.mods.kubejs.DevProperties; +import dev.latvian.mods.kubejs.error.KubeRuntimeException; import dev.latvian.mods.kubejs.event.EventResult; import dev.latvian.mods.kubejs.event.KubeStartupEvent; import dev.latvian.mods.kubejs.script.ConsoleJS; +import dev.latvian.mods.kubejs.script.SourceLine; import dev.latvian.mods.kubejs.util.ID; +import dev.latvian.mods.rhino.Context; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; @@ -13,60 +16,74 @@ import java.util.function.Supplier; public class RegistryKubeEvent implements KubeStartupEvent, AdditionalObjectRegistry { - private final RegistryInfo registryInfo; + private final ResourceKey> registryKey; + private final BuilderTypeRegistryHandler.Info builderInfo; public final List> created; public RegistryKubeEvent(ResourceKey> registryKey) { - this.registryInfo = RegistryInfo.of(registryKey); + this.registryKey = registryKey; + this.builderInfo = BuilderTypeRegistryHandler.info(registryKey); this.created = new LinkedList<>(); } - public BuilderBase create(String id, String type) { - var t = registryInfo.types == null ? null : registryInfo.types.get(type); + public BuilderBase create(Context cx, String id, String type) { + var sourceLine = SourceLine.of(cx); + var t = builderInfo.namedType(type); if (t == null) { - throw new IllegalArgumentException("Unknown type '" + type + "' for object '" + id + "'!"); + throw new KubeRuntimeException("Unknown type '" + type + "' for object '" + id + "'!").source(sourceLine); } var b = t.factory().createBuilder(ID.kjs(id)); if (b == null) { - throw new IllegalArgumentException("Unknown type '" + type + "' for object '" + id + "'!"); + throw new KubeRuntimeException("Unknown type '" + type + "' for object '" + id + "'!").source(sourceLine); + } else if (builderInfo.directCodec() != null) { + throw new KubeRuntimeException("Type '" + type + "' for object '" + id + "' is a datapack registry type!").source(sourceLine); } else { - addBuilder(registryInfo, b); + b.sourceLine = sourceLine; + b.registryKey = registryKey; + addBuilder(b); created.add(b); } return b; } - public BuilderBase create(String id) { - var t = registryInfo.defaultType; + public BuilderBase create(Context cx, String id) { + var sourceLine = SourceLine.of(cx); + var t = builderInfo.defaultType(); if (t == null) { - throw new IllegalArgumentException("Registry '" + registryInfo.key.location() + "' doesn't have a default type registered!"); + throw new KubeRuntimeException("Registry '" + registryKey.location() + "' doesn't have a default type registered!").source(sourceLine); } var b = t.factory().createBuilder(ID.kjs(id)); if (b == null) { - throw new IllegalArgumentException("Unknown type '" + t.type() + "' for object '" + id + "'!"); + throw new KubeRuntimeException("Unknown type '" + t.type() + "' for object '" + id + "'!").source(sourceLine); } else { - addBuilder(registryInfo, b); + b.sourceLine = sourceLine; + b.registryKey = registryKey; + addBuilder(b); created.add(b); } return b; } - public CustomBuilderObject createCustom(String id, Supplier object) { + public CustomBuilderObject createCustom(Context cx, String id, Supplier object) { + var sourceLine = SourceLine.of(cx); + if (object == null) { - throw new IllegalArgumentException("Tried to register a null object with id: " + id); + throw new KubeRuntimeException("Tried to register a null object with id: " + id).source(sourceLine); } var rl = ID.kjs(id); - var b = new CustomBuilderObject(rl, object, registryInfo); - addBuilder(registryInfo, b); + var b = new CustomBuilderObject(rl, object); + b.sourceLine = sourceLine; + b.registryKey = registryKey; + addBuilder(b); created.add(b); return b; } @@ -80,29 +97,27 @@ public void afterPosted(EventResult result) { @Override public void add(ResourceKey> registry, BuilderBase builder) { - addBuilder(RegistryInfo.of(registry), builder); + builder.registryKey = (ResourceKey) registry; + addBuilder(builder); } - @Override - public void add(RegistryInfo registry, BuilderBase builder) { - addBuilder(registry, builder); - } - - private void addBuilder(RegistryInfo registry, BuilderBase builder) { + private void addBuilder(BuilderBase builder) { if (builder == null) { - throw new IllegalArgumentException("Can't add null builder in registry '" + registry + "'!"); + throw new IllegalArgumentException("Can't add null builder in registry '" + builder.registryKey.location() + "'!"); } if (DevProperties.get().logRegistryEventObjects) { - ConsoleJS.STARTUP.info("~ " + registry + " | " + builder.id); + ConsoleJS.STARTUP.info("~ " + builder.registryKey.location() + " | " + builder.id); } - if (registry.objects.containsKey(builder.id)) { - throw new IllegalArgumentException("Duplicate key '" + builder.id + "' in registry '" + registry + "'!"); + var objStorage = RegistryObjectStorage.of(builder.registryKey); + + if (objStorage.objects.containsKey(builder.id)) { + throw new IllegalArgumentException("Duplicate key '" + builder.id + "' in registry '" + builder.registryKey.location() + "'!"); } - registry.objects.put(builder.id, builder); - RegistryInfo.ALL_BUILDERS.add(builder); + objStorage.objects.put(builder.id, (BuilderBase) builder); + RegistryObjectStorage.ALL_BUILDERS.add(builder); // registry.deferredRegister.register() } diff --git a/src/main/java/dev/latvian/mods/kubejs/registry/RegistryObjectStorage.java b/src/main/java/dev/latvian/mods/kubejs/registry/RegistryObjectStorage.java new file mode 100644 index 000000000..e1df7920d --- /dev/null +++ b/src/main/java/dev/latvian/mods/kubejs/registry/RegistryObjectStorage.java @@ -0,0 +1,51 @@ +package dev.latvian.mods.kubejs.registry; + +import com.mojang.serialization.Codec; +import dev.latvian.mods.kubejs.util.Cast; +import net.minecraft.core.Registry; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.Item; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.material.Fluid; +import org.jetbrains.annotations.NotNull; + +import java.util.IdentityHashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public final class RegistryObjectStorage implements Iterable> { + private static final Object LOCK = new Object(); + private static final Map>, RegistryObjectStorage> MAP = new IdentityHashMap<>(); + public static final List> ALL_BUILDERS = new LinkedList<>(); + + public static final Codec> CODEC = ResourceLocation.CODEC.xmap(rl -> RegistryObjectStorage.of(ResourceKey.createRegistryKey(rl)), ri -> ri.key.location()); + + public static RegistryObjectStorage of(ResourceKey> key) { + synchronized (LOCK) { + return Cast.to(MAP.computeIfAbsent(key, RegistryObjectStorage::new)); + } + } + + public static final RegistryObjectStorage FLUID = of(Registries.FLUID); + public static final RegistryObjectStorage BLOCK = of(Registries.BLOCK); + public static final RegistryObjectStorage ITEM = of(Registries.ITEM); + + public final ResourceKey> key; + public final Map> objects; + + private RegistryObjectStorage(ResourceKey key) { + this.key = key; + this.objects = new LinkedHashMap<>(); + } + + @NotNull + @Override + public Iterator> iterator() { + return objects.values().iterator(); + } +} diff --git a/src/main/java/dev/latvian/mods/kubejs/registry/ServerRegistryKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/registry/ServerRegistryKubeEvent.java index 8729c7dbc..eb015748a 100644 --- a/src/main/java/dev/latvian/mods/kubejs/registry/ServerRegistryKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/registry/ServerRegistryKubeEvent.java @@ -3,34 +3,94 @@ import com.google.gson.JsonElement; import com.mojang.serialization.Codec; import com.mojang.serialization.DynamicOps; +import dev.latvian.mods.kubejs.error.KubeRuntimeException; import dev.latvian.mods.kubejs.event.KubeEvent; -import dev.latvian.mods.kubejs.generator.KubeDataGenerator; +import dev.latvian.mods.kubejs.script.SourceLine; import dev.latvian.mods.kubejs.util.ID; +import dev.latvian.mods.rhino.Context; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; -import net.minecraft.resources.ResourceLocation; + +import java.util.List; +import java.util.function.Supplier; public class ServerRegistryKubeEvent implements KubeEvent { - public final KubeDataGenerator generator; - public final ResourceKey> key; + public final ResourceKey> registryKey; + private final BuilderTypeRegistryHandler.Info builderInfo; public final DynamicOps jsonOps; public final Codec codec; + private final List> builders; - public ServerRegistryKubeEvent(KubeDataGenerator generator, ResourceKey> key, DynamicOps jsonOps, Codec codec) { - this.generator = generator; - this.key = key; + public ServerRegistryKubeEvent(ResourceKey> registryKey, DynamicOps jsonOps, Codec codec, List> builders) { + this.registryKey = registryKey; + this.builderInfo = BuilderTypeRegistryHandler.info(registryKey); this.jsonOps = jsonOps; this.codec = codec; + this.builders = builders; } - public void createFromJson(String id, JsonElement json) { - var id1 = ID.kjs(id); - var k = key.location(); + public BuilderBase create(Context cx, String id, String type) { + var sourceLine = SourceLine.of(cx); + + var t = builderInfo.namedType(type); + + if (t == null) { + throw new IllegalArgumentException("Unknown type '" + type + "' for object '" + id + "'!"); + } + + var b = t.factory().createBuilder(ID.kjs(id)); - if (k.getNamespace().equals("minecraft")) { - generator.json(ResourceLocation.fromNamespaceAndPath(id1.getNamespace(), k.getPath() + "/" + id1.getPath()), json); + if (b == null) { + throw new KubeRuntimeException("Unknown type '" + type + "' for object '" + id + "'!").source(sourceLine); } else { - generator.json(ResourceLocation.fromNamespaceAndPath(id1.getNamespace(), k.getNamespace() + "/" + k.getPath() + "/" + id1.getPath()), json); + b.sourceLine = sourceLine; + b.registryKey = registryKey; + builders.add(b); + return b; } } + + public BuilderBase create(Context cx, String id) { + var sourceLine = SourceLine.of(cx); + var t = builderInfo.defaultType(); + + if (t == null) { + throw new KubeRuntimeException("Registry '" + registryKey.location() + "' doesn't have a default type registered!").source(sourceLine); + } + + var b = t.factory().createBuilder(ID.kjs(id)); + + if (b == null) { + throw new KubeRuntimeException("Unknown type '" + t.type() + "' for object '" + id + "'!").source(sourceLine); + } else { + b.sourceLine = sourceLine; + b.registryKey = registryKey; + builders.add(b); + return b; + } + } + + public CustomBuilderObject createCustom(Context cx, String id, Supplier object) { + var sourceLine = SourceLine.of(cx); + + if (object == null) { + throw new KubeRuntimeException("Tried to register a null object with id: " + id).source(sourceLine); + } + + var b = new CustomBuilderObject(ID.kjs(id), object); + b.sourceLine = sourceLine; + b.registryKey = registryKey; + builders.add(b); + return b; + } + + public CustomBuilderObject createFromJson(Context cx, String id, JsonElement json) { + var sourceLine = SourceLine.of(cx); + + var b = new CustomBuilderObject(ID.kjs(id), () -> codec.parse(jsonOps, json).result().orElseThrow()); + b.sourceLine = sourceLine; + b.registryKey = registryKey; + builders.add(b); + return b; + } } \ No newline at end of file diff --git a/src/main/java/dev/latvian/mods/kubejs/server/ServerScriptManager.java b/src/main/java/dev/latvian/mods/kubejs/server/ServerScriptManager.java index 83dbaf338..3348ca487 100644 --- a/src/main/java/dev/latvian/mods/kubejs/server/ServerScriptManager.java +++ b/src/main/java/dev/latvian/mods/kubejs/server/ServerScriptManager.java @@ -1,10 +1,12 @@ package dev.latvian.mods.kubejs.server; import com.google.gson.JsonElement; +import com.mojang.serialization.Codec; 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.RecipeManagerKJS; +import dev.latvian.mods.kubejs.error.KubeRuntimeException; import dev.latvian.mods.kubejs.plugin.KubeJSPlugin; import dev.latvian.mods.kubejs.plugin.KubeJSPlugins; import dev.latvian.mods.kubejs.recipe.CompostableRecipesKubeEvent; @@ -12,16 +14,21 @@ import dev.latvian.mods.kubejs.recipe.schema.RecipeSchemaStorage; import dev.latvian.mods.kubejs.recipe.special.SpecialRecipeSerializerManager; import dev.latvian.mods.kubejs.recipe.viewer.server.RecipeViewerData; -import dev.latvian.mods.kubejs.registry.RegistryInfo; +import dev.latvian.mods.kubejs.registry.AdditionalObjectRegistry; +import dev.latvian.mods.kubejs.registry.BuilderBase; +import dev.latvian.mods.kubejs.registry.RegistryObjectStorage; import dev.latvian.mods.kubejs.registry.ServerRegistryKubeEvent; import dev.latvian.mods.kubejs.script.ConsoleJS; import dev.latvian.mods.kubejs.script.ScriptManager; import dev.latvian.mods.kubejs.script.ScriptType; +import dev.latvian.mods.kubejs.script.SourceLine; import dev.latvian.mods.kubejs.script.data.GeneratedDataStage; import dev.latvian.mods.kubejs.script.data.KubeFileResourcePack; import dev.latvian.mods.kubejs.script.data.VirtualDataPack; import dev.latvian.mods.kubejs.server.tag.PreTagKubeEvent; +import dev.latvian.mods.kubejs.util.Cast; import dev.latvian.mods.kubejs.util.RegistryAccessContainer; +import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.PackResources; @@ -33,6 +40,7 @@ import java.nio.file.Files; import java.util.ArrayList; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -115,9 +123,18 @@ public void loadFromDirectory() { } } + private record AdditionalServerRegistryHandler(SourceLine sourceLine, List> builders) implements AdditionalObjectRegistry { + @Override + public void add(ResourceKey> registry, BuilderBase builder) { + builder.sourceLine = sourceLine; + builder.registryKey = (ResourceKey) registry; + builders.add(builder); + } + } + @Override public void loadAdditional() { - for (var builder : RegistryInfo.ALL_BUILDERS) { + for (var builder : RegistryObjectStorage.ALL_BUILDERS) { builder.generateDataJsons(internalDataPack); } @@ -127,13 +144,49 @@ public void loadAdditional() { firstLoad = false; if (ServerEvents.REGISTRY.hasListeners()) { + var builders = new ArrayList>(); + var ops = RegistryAccessContainer.current.json(); + var codecs = new IdentityHashMap, Codec>(); + for (var reg : DataPackRegistriesHooks.getDataPackRegistries()) { var key = (ResourceKey) reg.key(); + codecs.put(key, reg.elementCodec()); if (ServerEvents.REGISTRY.hasListeners(key)) { - ServerEvents.REGISTRY.post(ScriptType.SERVER, key, new ServerRegistryKubeEvent(registriesDataPack, key, ops, reg.elementCodec())); + ServerEvents.REGISTRY.post(ScriptType.SERVER, key, new ServerRegistryKubeEvent(key, ops, reg.elementCodec(), builders)); + } + } + + for (var b : List.copyOf(builders)) { + b.createAdditionalObjects(new AdditionalServerRegistryHandler(b.sourceLine, builders)); + } + + for (var b : builders) { + if (b.registryKey == null) { + ConsoleJS.SERVER.error("", new KubeRuntimeException("Failed to register object '" + b.id + "' - unknown registry").source(b.sourceLine)); + continue; + } + + try { + var codec = codecs.get(b.registryKey); + + if (codec == null) { + throw new KubeRuntimeException("Don't know how to encode '" + b.id + "' of '" + b.registryKey.location() + "'!").source(b.sourceLine); + } + + var obj = b.createTransformedObject(); + var json = codec.encodeStart(ops, Cast.to(obj)).getOrThrow(); + var k = b.registryKey.location(); + + if (k.getNamespace().equals("minecraft")) { + registriesDataPack.json(ResourceLocation.fromNamespaceAndPath(b.id.getNamespace(), k.getPath() + "/" + b.id.getPath()), json); + } else { + registriesDataPack.json(ResourceLocation.fromNamespaceAndPath(b.id.getNamespace(), k.getNamespace() + "/" + k.getPath() + "/" + b.id.getPath()), json); + } + } catch (Exception ex) { + ConsoleJS.SERVER.error("", new KubeRuntimeException("Failed to register object '" + b.id + "' of registry '" + b.registryKey.location() + "'!", ex).source(b.sourceLine)); } } } @@ -168,7 +221,7 @@ public boolean recipes(RecipeManagerKJS recipeManager, ResourceManager resourceM boolean result = false; RecipesKubeEvent.TEMP_ITEM_TAG_LOOKUP.setValue(getRegistries().cachedItemTags); - recipeSchemaStorage.fireEvents(resourceManager, getRegistries().json()); + recipeSchemaStorage.fireEvents(getRegistries(), resourceManager); SpecialRecipeSerializerManager.INSTANCE.reset(); ServerEvents.SPECIAL_RECIPES.post(ScriptType.SERVER, SpecialRecipeSerializerManager.INSTANCE); diff --git a/src/main/java/dev/latvian/mods/kubejs/server/tag/PreTagKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/server/tag/PreTagKubeEvent.java index 159dc0680..7fe4b9c69 100644 --- a/src/main/java/dev/latvian/mods/kubejs/server/tag/PreTagKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/server/tag/PreTagKubeEvent.java @@ -3,7 +3,6 @@ import dev.latvian.mods.kubejs.DevProperties; import dev.latvian.mods.kubejs.KubeJS; import dev.latvian.mods.kubejs.bindings.event.ServerEvents; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.script.ScriptType; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; @@ -21,20 +20,20 @@ public static void handle(Map, PreTagKubeEvent> tagEventHolders) if (ServerEvents.TAGS.hasListeners()) { for (var id : ServerEvents.TAGS.findUniqueExtraIds(ScriptType.SERVER)) { - var e = new PreTagKubeEvent(RegistryInfo.of((ResourceKey) id)); + var e = new PreTagKubeEvent(id); try { ServerEvents.TAGS.post(ScriptType.SERVER, id, e); } catch (Exception ex) { e.invalid = true; if (DevProperties.get().logEventErrorStackTrace) { - KubeJS.LOGGER.warn("Pre Tag event for " + e.registry + " failed:"); + KubeJS.LOGGER.warn("Pre Tag event for " + e.registryKey.location() + " failed:"); ex.printStackTrace(); } } if (!e.invalid) { - tagEventHolders.put(e.registry.key, e); + tagEventHolders.put(e.registryKey, e); } } } @@ -51,8 +50,8 @@ public void accept(TagKubeEvent e) { public final List> actions; public boolean invalid; - public PreTagKubeEvent(RegistryInfo registry) { - super(registry, null); + public PreTagKubeEvent(ResourceKey registryKey) { + super(registryKey, null); this.tags = new ConcurrentHashMap<>(); this.actions = new ArrayList<>(); } diff --git a/src/main/java/dev/latvian/mods/kubejs/server/tag/TagEventFilter.java b/src/main/java/dev/latvian/mods/kubejs/server/tag/TagEventFilter.java index 841565d28..af1718ed6 100644 --- a/src/main/java/dev/latvian/mods/kubejs/server/tag/TagEventFilter.java +++ b/src/main/java/dev/latvian/mods/kubejs/server/tag/TagEventFilter.java @@ -192,7 +192,7 @@ public int add(TagWrapper wrapper) { wrapper.entries.add(new TagLoader.EntryWithSource(TagEntry.element(id), TagKubeEvent.SOURCE)); return 1; } else { - var msg = "No such element %s in registry %s".formatted(id, wrapper.event.registry); + var msg = "No such element %s in registry %s".formatted(id, wrapper.event.registryKey.location()); if (DevProperties.get().strictTags) { throw new EmptyTagTargetException(msg); diff --git a/src/main/java/dev/latvian/mods/kubejs/server/tag/TagKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/server/tag/TagKubeEvent.java index caed40677..250067d38 100644 --- a/src/main/java/dev/latvian/mods/kubejs/server/tag/TagKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/server/tag/TagKubeEvent.java @@ -3,7 +3,6 @@ import dev.latvian.mods.kubejs.error.EmptyTagTargetException; import dev.latvian.mods.kubejs.event.EventExceptionHandler; import dev.latvian.mods.kubejs.event.KubeEvent; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.script.ConsoleJS; import dev.latvian.mods.kubejs.util.Cast; import net.minecraft.core.Holder; @@ -39,23 +38,23 @@ public class TagKubeEvent implements KubeEvent { public static final String SOURCE = "KubeJS Custom Tags"; - public final RegistryInfo registry; + public final ResourceKey registryKey; public final Registry vanillaRegistry; public final Map tags; public int totalAdded; public int totalRemoved; private Set elementIds; - public TagKubeEvent(RegistryInfo ri, Registry vr) { - registry = ri; - vanillaRegistry = vr; - tags = new ConcurrentHashMap<>(); - totalAdded = 0; - totalRemoved = 0; + public TagKubeEvent(ResourceKey registryKey, Registry vr) { + this.registryKey = registryKey; + this.vanillaRegistry = vr; + this.tags = new ConcurrentHashMap<>(); + this.totalAdded = 0; + this.totalRemoved = 0; } public ResourceLocation getType() { - return registry.key.location(); + return registryKey.location(); } public TagWrapper get(ResourceLocation id) { diff --git a/src/main/java/dev/latvian/mods/kubejs/util/RegistryAccessContainer.java b/src/main/java/dev/latvian/mods/kubejs/util/RegistryAccessContainer.java index 647502dd2..2cad32b0a 100644 --- a/src/main/java/dev/latvian/mods/kubejs/util/RegistryAccessContainer.java +++ b/src/main/java/dev/latvian/mods/kubejs/util/RegistryAccessContainer.java @@ -128,11 +128,16 @@ public T decodeMap(Context cx, MapCodec codec, Object o) { } } + private RegistryWrapper createRegistryWrapper(ResourceLocation id) { + var key = ResourceKey.createRegistryKey(id); + return new RegistryWrapper<>(access.registryOrThrow(key), ResourceKey.create(key, ID.UNKNOWN)); + } + public RegistryWrapper wrapRegistry(ResourceLocation id) { if (cachedRegistryWrappers == null) { cachedRegistryWrappers = new HashMap<>(); } - return cachedRegistryWrappers.computeIfAbsent(id, i -> new RegistryWrapper(access.registryOrThrow(ResourceKey.createRegistryKey(i)))); + return cachedRegistryWrappers.computeIfAbsent(id, this::createRegistryWrapper); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/util/UtilsJS.java b/src/main/java/dev/latvian/mods/kubejs/util/UtilsJS.java index d2092ae85..06d801d30 100644 --- a/src/main/java/dev/latvian/mods/kubejs/util/UtilsJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/util/UtilsJS.java @@ -9,7 +9,6 @@ import dev.latvian.mods.kubejs.bindings.event.ItemEvents; import dev.latvian.mods.kubejs.block.BlockModificationKubeEvent; import dev.latvian.mods.kubejs.item.ItemModificationKubeEvent; -import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.script.ScriptType; import dev.latvian.mods.rhino.BaseFunction; import dev.latvian.mods.rhino.Context; @@ -19,6 +18,7 @@ import net.minecraft.advancements.critereon.MinMaxBounds; import net.minecraft.commands.arguments.selector.EntitySelector; import net.minecraft.commands.arguments.selector.EntitySelectorParser; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.EndTag; import net.minecraft.nbt.NumericTag; import net.minecraft.nbt.StringTag; @@ -401,7 +401,7 @@ public static EntitySelector entitySelector(@Nullable Object o) { @Nullable public static CreativeModeTab findCreativeTab(ResourceLocation id) { - return RegistryInfo.CREATIVE_MODE_TAB.getValue(id); + return BuiltInRegistries.CREATIVE_MODE_TAB.get(id); } public static T makeFunctionProxy(Context cx, TypeInfo targetClass, BaseFunction function) {