跳到正文

Built!

作者:gdpulsar

A simple multiblock handling library!

收藏

介绍

中文介绍正在准备中,下面暂时显示原文。

Built!

Built! is a multiblock handler for Minecraft, allowing simple creation of interconnected blocks and structures!

Networked Multiblocks

Networked multiblocks are a set of shapeless blocks that connected via neighbours, allowing creation of things like unstructured tanks or cables.

To create a networked multiblock, you must create the following:

The Multiblock Class

This acts as your block class, where you handle interfacing with the multiblock.

public class ExampleMultiblock extends NetworkedMultiblock<ExampleMultiblockEntity> {
    private static final MapCodec<ExampleMultiblock> CODEC = simpleCodec(ExampleMultiblock::new);

    public ExampleMultiblock(Properties properties) {
        super(properties);
    }

    @Override
    protected @NotNull MapCodec<? extends BaseEntityBlock> codec() {
        return CODEC;
    }

    @Override
    public @Nullable BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
        return new ExampleMultiblockEntity(blockPos, blockState);
    }
}

The Multiblock Entity Class

This is what handles per-block data, such as the amount of fluid in a single tank block, or the energy within a cable.

public class ExampleMultiblockEntity extends NetworkedMultiblockEntity<ExampleMultiblockNetwork> {
    private int value;

    public ExampleMultiblockEntity(BlockPos blockPos, BlockState blockState) {
        super(ExampleMod.EXAMPLE_MULTIBLOCK_ENTITY.get(), blockPos, blockState);
    }

    public int getValue() {
        return this.value;
    }

    public void setValue(int value) {
        this.value = value;
        this.getLevel().sendBlockUpdated(this.getBlockPos(), this.getBlockState(), this.getBlockState(), 8);
    }

    @Override
    public MultiblockNetworkType<ExampleMultiblockNetwork> getNetworkType() {
        return ExampleMod.EXAMPLE_NETWORK;
    }

    @Override
    public @Nullable Packet<ClientGamePacketListener> getUpdatePacket() {
        return ClientboundBlockEntityDataPacket.create(this);
    }

    @Override
    public CompoundTag getUpdateTag(HolderLookup.Provider provider) {
        CompoundTag tag = super.getUpdateTag(provider);
        saveAdditional(tag, provider);
        return tag;
    }

    @Override
    protected void loadAdditional(CompoundTag compoundTag, HolderLookup.Provider provider) {
        this.value = compoundTag.getInt("value");
        super.loadAdditional(compoundTag, provider);
    }

    @Override
    protected void saveAdditional(CompoundTag compoundTag, HolderLookup.Provider provider) {
        compoundTag.putInt("value", this.value);
        super.saveAdditional(compoundTag, provider);
    }
}

The Multiblock Network Class

This is what handles the entire network. One of these is created every time a networked block is placed, unless it is already connecting to an adjacent networked block. This also handles splitting, merging, adding and removing interactions.

public class ExampleMultiblockNetwork extends MultiblockNetwork {
    private int value = 0;

    public ExampleMultiblockNetwork(int networkId) {
        super(networkId);
    }

    public void updateValue() {
        for (BlockPos pos : this.getConnectedBlocks()) {
            BlockEntity entity = this.getLevel().getBlockEntity(pos);
            if (entity instanceof ExampleMultiblockEntity blockEntity) {
                blockEntity.setValue(value);
            }
        }
    }

    public boolean increment(int amount) {
        this.value += amount;
        this.updateValue();
        return true;
    }

    @Override
    public void addToNetwork(NetworkedMultiblockEntity<? extends MultiblockNetwork> blockEntity) {
        super.addToNetwork(blockEntity);
        updateValue();
    }

    @Override
    public void removeFromNetwork(NetworkedMultiblockEntity<? extends MultiblockNetwork> blockEntity) {
        super.removeFromNetwork(blockEntity);
        updateValue();
    }

    @Override
    public void splitAcross(List<? extends MultiblockNetwork> networks) {
        int perNetwork = this.value / networks.size();
        for (MultiblockNetwork network : networks) {
            if (network instanceof ExampleMultiblockNetwork exampleNetwork) {
                exampleNetwork.increment(perNetwork);
            }
        }
    }

    @Override
    public void mergeWith(List<? extends MultiblockNetwork> networks) {
        int total = 0;
        for (MultiblockNetwork network : networks) {
            if (network instanceof ExampleMultiblockNetwork exampleNetwork) {
                total += exampleNetwork.value;
            }
        }
        this.value = total;
    }

    @Override
    public CompoundTag saveToNbt(CompoundTag tag) {
        tag.putInt("value", this.value);
        return super.saveToNbt(tag);
    }

    @Override
    public void readFromNbt(CompoundTag tag) {
        super.readFromNbt(tag);
        this.value = tag.getInt("value");
        this.updateValue();
    }
}

Custom interactivity can be created for methods like updateValue or addToNetwork, as well as creating capacity systems.
Registering the network is also required, done statically in your initialization class like this:

public static final MultiblockNetworkType<ExampleMultiblockNetwork> EXAMPLE_NETWORK = MultiblockRegistry.registerNetworked(new MultiblockNetworkType<>(
            ResourceLocation.fromNamespaceAndPath(MOD_ID, "example_network"), ExampleMultiblockNetwork::new));

All the blocks and block entities are registered as usual.

常见问题

Built! 支持哪些 Minecraft 版本?

Built! 支持 Minecraft Java 版 1.21.1,可用的加载器:Fabric / NeoForge。下载时注意选择和你游戏版本一致的文件。

Built! 怎么安装?

先用 PCL2 或 HMCL 启动器安装对应版本的 Fabric / NeoForge,再把下载的 .jar 文件放进游戏目录的 mods 文件夹,重新启动游戏即可。开启了版本隔离的,mods 文件夹在 versions/版本名 下面。

评论

登录后评论和评分

gdpulsar 的其他作品

查看全部 →