Skip to content

Commit

Permalink
Merge pull request #11 from ZZZank/block-builder-fix2
Browse files Browse the repository at this point in the history
block registration fix
  • Loading branch information
ZZZank authored Oct 7, 2024
2 parents 104cfe0 + fef6365 commit b704f9e
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 48 deletions.
30 changes: 21 additions & 9 deletions common/src/main/java/dev/latvian/kubejs/block/BlockBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
*/
public class BlockBuilder extends BuilderBase<Block> {

@Deprecated
public transient BlockType type = null;
public transient MaterialJS material = MaterialListJS.INSTANCE.map.get("wood");
public transient float hardness = 0.5F;
Expand Down Expand Up @@ -73,9 +74,7 @@ public class BlockBuilder extends BuilderBase<Block> {
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());
Expand All @@ -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()));
});
}

Expand All @@ -95,11 +94,10 @@ public RegistryInfo<Block> 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) {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand All @@ -49,7 +54,7 @@ public VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos,

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
if (properties.waterlogged) {
if (builderOrFallback().waterlogged) {
builder.add(BlockStateProperties.WATERLOGGED);
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}

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

0 comments on commit b704f9e

Please sign in to comment.