diff --git a/common/src/main/java/dev/latvian/kubejs/block/BlockBuilder.java b/common/src/main/java/dev/latvian/kubejs/block/BlockBuilder.java index 77d50d046..d2c2e6e05 100644 --- a/common/src/main/java/dev/latvian/kubejs/block/BlockBuilder.java +++ b/common/src/main/java/dev/latvian/kubejs/block/BlockBuilder.java @@ -40,6 +40,7 @@ */ public class BlockBuilder extends BuilderBase { + @Deprecated public transient BlockType type = null; public transient MaterialJS material = MaterialListJS.INSTANCE.map.get("wood"); public transient float hardness = 0.5F; @@ -73,9 +74,7 @@ public class BlockBuilder extends BuilderBase { public transient boolean redstoneConductor = true; public transient boolean transparent = false; - public transient Block block; - - public BlockBuilder(ResourceLocation id) { + public BlockBuilder(ResourceLocation id) { super(id); color.defaultReturnValue(0xFFFFFFFF); textureAll(id.getNamespace() + ":block/" + id.getPath()); @@ -84,7 +83,7 @@ public BlockBuilder(ResourceLocation id) { lootTable = loot -> loot.addPool(pool -> { pool.survivesExplosion(); - pool.addItem(new ItemStack(block)); + pool.addItem(new ItemStack(get())); }); } @@ -95,11 +94,10 @@ public RegistryInfo getRegistryType() { @Override public Block createObject() { - if (type == null) { - return new BasicBlockJS(this); - } - return this.type.createBlock(this); - } + return type == null + ? new BasicBlockJS(this) + : this.type.createBlock(this); + } @Override public Block transformObject(Block obj) { @@ -576,4 +574,18 @@ public Block.Properties createProperties() { return properties; } + + /** + * same as {@link BlockBuilder#get()} + */ + public Block getBlock() { + return get(); + } + + @JSInfo(""" + I'm curious now, why call this method?""") + @Deprecated + public void setBlock(Block block) { + this.object = block; + } } \ No newline at end of file diff --git a/common/src/main/java/dev/latvian/kubejs/block/custom/BasicBlockJS.java b/common/src/main/java/dev/latvian/kubejs/block/custom/BasicBlockJS.java index e4fc5f4f7..3969a96fe 100644 --- a/common/src/main/java/dev/latvian/kubejs/block/custom/BasicBlockJS.java +++ b/common/src/main/java/dev/latvian/kubejs/block/custom/BasicBlockJS.java @@ -3,6 +3,7 @@ import dev.latvian.kubejs.block.BlockBuilder; import dev.latvian.kubejs.block.RandomTickCallbackJS; import dev.latvian.kubejs.world.BlockContainerJS; +import lombok.val; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.core.BlockPos; @@ -20,21 +21,25 @@ import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Objects; import java.util.Random; /** * @author LatvianModder */ public class BasicBlockJS extends Block { + private static BlockBuilder builderRef; + public final BlockBuilder properties; public final VoxelShape shape; - public BasicBlockJS(BlockBuilder p) { - super(p.createProperties()); - properties = p; - shape = p.createShape(); + public BasicBlockJS(@NotNull BlockBuilder builder) { + super(createPropAndCacheBuilderReferenceBecauseBlockConstructorUsedBlockBuilderEvenBeforeItsInitializedAllBecauseItsPoorlyDesigned(builder)); + properties = builder; + shape = builder.createShape(); if (properties.waterlogged) { registerDefaultState(stateDefinition.any().setValue(BlockStateProperties.WATERLOGGED, false)); @@ -49,7 +54,7 @@ public VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos, @Override protected void createBlockStateDefinition(StateDefinition.Builder builder) { - if (properties.waterlogged) { + if (builderOrFallback().waterlogged) { builder.add(BlockStateProperties.WATERLOGGED); } } @@ -89,7 +94,7 @@ public boolean propagatesSkylightDown(BlockState state, BlockGetter level, Block @Deprecated public void randomTick(BlockState state, ServerLevel level, BlockPos pos, Random random) { if (properties.randomTickCallback != null) { - BlockContainerJS containerJS = new BlockContainerJS(level, pos); + val containerJS = new BlockContainerJS(level, pos); try { properties.randomTickCallback.accept(new RandomTickCallbackJS(containerJS, random)); } catch (Exception e) { @@ -122,4 +127,24 @@ public float getShadeBrightness(BlockState state, BlockGetter level, BlockPos po public boolean skipRendering(BlockState state, BlockState state2, Direction direction) { return properties.transparent ? (state2.is(this) || super.skipRendering(state, state2, direction)) : super.skipRendering(state, state2, direction); } + + /** + * @see BasicBlockJS#builderOrFallback() + */ + private static Properties createPropAndCacheBuilderReferenceBecauseBlockConstructorUsedBlockBuilderEvenBeforeItsInitializedAllBecauseItsPoorlyDesigned(@NotNull BlockBuilder builder) { + return (builderRef = Objects.requireNonNull(builder)).createProperties(); + } + + /** + * dirty hack to make sure there's a valid {@link BlockBuilder} for + * {@link Block#createBlockStateDefinition(StateDefinition.Builder)} when {@link Block} is on initialization + */ + private BlockBuilder builderOrFallback() { + if (properties != null) { + return properties; + } + val tmp = builderRef; + builderRef = null; + return tmp; + } } \ No newline at end of file diff --git a/common/src/main/java/dev/latvian/kubejs/block/custom/ShapedBlockType.java b/common/src/main/java/dev/latvian/kubejs/block/custom/ShapedBlockType.java index 701f298fd..391bad12a 100644 --- a/common/src/main/java/dev/latvian/kubejs/block/custom/ShapedBlockType.java +++ b/common/src/main/java/dev/latvian/kubejs/block/custom/ShapedBlockType.java @@ -83,7 +83,7 @@ public void applyDefaults(BlockBuilder builder) { @Override public void generateAssets(BlockBuilder builder, AssetJsonGenerator generator) { - if (builder.block instanceof CustomBlockJS custom) { + if (builder.get() instanceof CustomBlockJS custom) { custom.generateAssets(builder, generator); } else { super.generateAssets(builder, generator); diff --git a/common/src/main/java/dev/latvian/kubejs/block/events/BlockLandingEventJS.java b/common/src/main/java/dev/latvian/kubejs/block/events/BlockLandingEventJS.java index 09faafa63..2a16e8512 100644 --- a/common/src/main/java/dev/latvian/kubejs/block/events/BlockLandingEventJS.java +++ b/common/src/main/java/dev/latvian/kubejs/block/events/BlockLandingEventJS.java @@ -2,12 +2,7 @@ import dev.latvian.kubejs.entity.EntityEventJS; import dev.latvian.kubejs.entity.EntityJS; -import dev.latvian.kubejs.server.ServerJS; -import dev.latvian.kubejs.world.ClientWorldJS; -import dev.latvian.kubejs.world.ServerWorldJS; -import dev.latvian.kubejs.world.WorldJS; import lombok.AllArgsConstructor; -import lombok.Getter; import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerLevel; @@ -21,13 +16,10 @@ @AllArgsConstructor public class BlockLandingEventJS extends EntityEventJS { private final Level level; - @Getter - private final BlockPos pos; - @Getter - private final BlockState fallState; - @Getter - private final BlockState landOn; - private final FallingBlockEntity entity; + public final BlockPos pos; + public final BlockState fallState; + public final BlockState landOn; + public final FallingBlockEntity entity; public boolean isServerSide() { return level instanceof ServerLevel; @@ -37,17 +29,11 @@ public boolean isClientSide() { return level instanceof ClientLevel; } - public WorldJS getWorld() { - if (isServerSide()) { - return new ServerWorldJS(ServerJS.instance, (ServerLevel) level); - } - if (isClientSide()) { - return ClientWorldJS.getInstance(); - } - return null; + public Level getLevelVanilla() { + return level; } - public FallingBlockEntity getEntityRaw() { + public FallingBlockEntity getEntityVanilla() { return entity; } diff --git a/common/src/main/java/dev/latvian/kubejs/block/events/BlockRightClickEventJS.java b/common/src/main/java/dev/latvian/kubejs/block/events/BlockRightClickEventJS.java index 9b4372e59..215ad2533 100644 --- a/common/src/main/java/dev/latvian/kubejs/block/events/BlockRightClickEventJS.java +++ b/common/src/main/java/dev/latvian/kubejs/block/events/BlockRightClickEventJS.java @@ -35,11 +35,7 @@ public EntityJS getEntity() { } public BlockContainerJS getBlock() { - if (block == null) { - block = new BlockContainerJS(player.level, pos); - } - - return block; + return block == null ? (block = new BlockContainerJS(player.level, pos)) : block; } public ItemStackJS getItem() { diff --git a/common/src/main/java/dev/latvian/kubejs/client/KubeJSClientEventHandler.java b/common/src/main/java/dev/latvian/kubejs/client/KubeJSClientEventHandler.java index f5a1fece6..1abc6637a 100644 --- a/common/src/main/java/dev/latvian/kubejs/client/KubeJSClientEventHandler.java +++ b/common/src/main/java/dev/latvian/kubejs/client/KubeJSClientEventHandler.java @@ -47,7 +47,6 @@ import net.minecraft.world.item.SpawnEggItem; import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.material.Fluid; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL12; @@ -112,9 +111,9 @@ private void renderLayers() { for (var builder : RegistryInfos.BLOCK.objects.values()) { var bBuilder = (BlockBuilder) builder; switch (bBuilder.renderType) { - case "cutout" -> RenderTypes.register(RenderType.cutout(), bBuilder.block); - case "cutout_mipped" -> RenderTypes.register(RenderType.cutoutMipped(), bBuilder.block); - case "translucent" -> RenderTypes.register(RenderType.translucent(), bBuilder.block); + case "cutout" -> RenderTypes.register(RenderType.cutout(), bBuilder.get()); + case "cutout_mipped" -> RenderTypes.register(RenderType.cutoutMipped(), bBuilder.get()); + case "translucent" -> RenderTypes.register(RenderType.translucent(), bBuilder.get()); //default: // RenderTypeLookup.setRenderLayer(block, RenderType.getSolid()); } @@ -271,7 +270,9 @@ private void blockColors() { RegistryInfos.BLOCK.objects.values() .stream().map(o -> (BlockBuilder) o) .filter(builder -> !builder.color.isEmpty()) - .forEach(builder -> ColorHandlers.registerBlockColors((state, world, pos, index) -> builder.color.get(index), builder.block)); + .forEach(builder -> ColorHandlers.registerBlockColors((state, world, pos, index) -> builder.color.get(index), + builder.getBlock() + )); } private void postAtlasStitch(TextureAtlas atlas) { diff --git a/common/src/main/java/dev/latvian/kubejs/item/BlockItemJS.java b/common/src/main/java/dev/latvian/kubejs/item/BlockItemJS.java index 3d96a81d2..95db6409e 100644 --- a/common/src/main/java/dev/latvian/kubejs/item/BlockItemJS.java +++ b/common/src/main/java/dev/latvian/kubejs/item/BlockItemJS.java @@ -8,6 +8,6 @@ */ public class BlockItemJS extends BlockItem { public BlockItemJS(BlockItemBuilder p) { - super(p.blockBuilder.block, p.createItemProperties()); + super(p.blockBuilder.get(), p.createItemProperties()); } } \ No newline at end of file