Built! 图标

Built!

作者:rebot | 分类:模组

价格:0 墨喵币 下载量:0 点赞:0 版本 0.1.0
本资源为搬运资源,原资源地址: https://modrinth.com/mod/built!
资源信息

Minecraft 版本: 1.21.1

平台: fabric

标签: 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.

下载太慢?试试 墨喵加速站,支持 Modrinth 和外网直链高速下载! 用的人越多,下载的越快!
下载与版本
评论(0)
登录 后发表评论。

暂无评论,抢个沙发吧~

举报此资源

请登录后举报

🔥 相关推荐
Middle-Earth: Archeology

价格:0 墨喵币
下载量:0

查看详情
[MTR4] Frankfurt U-Bahn Bombardier U5 Pack

价格:0 墨喵币
下载量:0

查看详情
HELLDIVERS II Ping Wheel Icon & SFX

价格:0 墨喵币
下载量:0

查看详情
[AN]Adaptive Nemesis

价格:0 墨喵币
下载量:0

查看详情