Skip to content

Commit

Permalink
Rewrote registries a bit again - Added server registry builders, got …
Browse files Browse the repository at this point in the history
…rid of a lot of unnecessary wrapping
  • Loading branch information
LatvianModder committed Jul 8, 2024
1 parent c87c458 commit 60ed636
Show file tree
Hide file tree
Showing 91 changed files with 699 additions and 714 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 //
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/KubeJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,21 +74,21 @@ public static Map<String, Direction> 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<String> getTypeList() {
var list = new ArrayList<String>();

for (var block : RegistryInfo.BLOCK.entrySet()) {
list.add(block.getKey().location().toString());
for (var block : BuiltInRegistries.BLOCK) {
list.add(block.kjs$getId());
}

return list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,7 +14,7 @@
import java.util.Set;
import java.util.stream.Collectors;

public record RegistryWrapper<T>(Registry<T> registry) implements Iterable<T> {
public record RegistryWrapper<T>(Registry<T> registry, ResourceKey<T> unknownKey) implements Iterable<T> {
public static RegistryWrapper<?> of(Context cx, ResourceLocation id) {
return ((KubeJSContext) cx).getRegistries().wrapRegistry(id);
}
Expand Down Expand Up @@ -47,6 +48,11 @@ public ResourceLocation getId(T value) {
return registry.getKey(value);
}

@Nullable
public ResourceKey<T> getKey(T value) {
return registry.getResourceKey(value).orElse(unknownKey);
}

@NotNull
@Override
public ListIterator<T> iterator() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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;
import dev.latvian.mods.kubejs.util.RegExpKJS;
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;
Expand Down Expand Up @@ -94,7 +94,7 @@ static Stat<ResourceLocation> 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")
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 60ed636

Please sign in to comment.