Skip to content

[DOCS] Incorrect implementation of ExampleBlock #313

@stalker57241

Description

@stalker57241

Documentation Issue Type

  • Typo or grammatical error
  • Incorrect or outdated information
  • Unclear explanation or confusing content
  • Missing documentation for a topic
  • Broken links or images
  • Code example not working
  • Other (please describe)

Location

Page/File: https://hytalemodding.dev/en/docs/guides/plugin/block-components

Section: ExampleBlock

Current Content

void runBlockAction(x, y, z, world) is execution logic. I think, rules of ECS talks, that logic like this must be moved to ExampleSystem

public class ExampleBlock implements Component<ChunkStore> {
    public static final BuilderCodec CODEC;
    public ExampleBlock() {
    }
    public static ComponentType getComponentType() {
        return ExamplePlugin.get().getExampleBlockComponentType();
    }
    public void runBlockAction(int x, int y, int z, World world) {
        world.execute(() -> {
            world.setBlock(x + 1, y, z, "Rock_Ice");
        });
    }
    @Nullable
    public Component<ChunkStore> clone() {
        return new ExampleBlock();
    }
    static {
        CODEC = BuilderCodec.builder(ExampleBlock.class, ExampleBlock::new).build();
    }
}

This code breaks ECS logic, putting execution code right into Component.

Suggested Improvement

Move code to system

public class ExampleSystem extends EntityTickingSystem {
    private static final Query QUERY = Query.and(BlockSection.getComponentType(), ChunkSection.getComponentType());
    public void tick(float dt, int index, @Nonnull ArchetypeChunk archetypeChunk, @Nonnull Store store, @Nonnull CommandBuffer commandBuffer) {
        BlockSection blocks = (BlockSection) archetypeChunk.getComponent(index, BlockSection.getComponentType());
        assert blocks != null;
        if (blocks.getTickingBlocksCountCopy() != 0) {
            ChunkSection section = (ChunkSection) archetypeChunk.getComponent(index, ChunkSection.getComponentType());
            assert section != null;
            BlockComponentChunk blockComponentChunk = (BlockComponentChunk) commandBuffer.getComponent(section.getChunkColumnReference(), BlockComponentChunk.getComponentType());
            assert blockComponentChunk != null;
            blocks.forEachTicking(blockComponentChunk, commandBuffer, section.getY(), (blockComponentChunk1, commandBuffer1, localX, localY, localZ, blockId) ->
            {
                Ref<ChunkStore> blockRef = blockComponentChunk1.getEntityReference(ChunkUtil.indexBlockInColumn(localX, localY, localZ));
                if (blockRef == null) {
                    return BlockTickStrategy.IGNORED;
                } else {
                    ExampleBlock exampleBlock = (ExampleBlock) commandBuffer1.getComponent(blockRef, ExampleBlock.getComponentType());
                    if (exampleBlock != null) {
                        WorldChunk worldChunk = (WorldChunk) commandBuffer.getComponent(section.getChunkColumnReference(), WorldChunk.getComponentType());
                        int globalX = localX + (worldChunk.getX() * 32);
                        int globalZ = localZ + (worldChunk.getZ() * 32);
                        this.run(globalX, localY, globalZ, worldChunk.getWorld());
                        return BlockTickStrategy.CONTINUE;
                    } else {
                        return BlockTickStrategy.IGNORED;
                    }
                }
            });
        }
    }

    // May be made static, or put dt, and/or ExampleBlock instance to args.
    public void run(int x, int y, int z, World world) {
        world.execute(() -> {
            world.setBlock(x + 1, y, z, "Rock_Ice");
        });
    }

    @Nullable
    public Query getQuery() {
        return QUERY;
    }
}

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions