▶️ ЗАБЕРИ СВОИ 8 ПОДАРКОВ 🎁 ПРИ СОЗДАНИИ СВОЕГО МАЙНКРАФТ СЕРВЕРА
Моды/Built!
Built!

Built!

A simple multiblock handling library!

35
0

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.✨t("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.✨t("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.

Совместимость

Minecraft: Java Edition

1.21.x

Платформы

Создатели

Детали

Лицензия:LicenseRef-All-Rights-Reserved
Опубликован:1 месяц назад
Обновлён:1 месяц назад
Главная