diff --git a/Allay-API/src/main/java/org/allaymc/api/block/component/common/BlockBaseComponent.java b/Allay-API/src/main/java/org/allaymc/api/block/component/common/BlockBaseComponent.java index 1369cbf90..8128a4545 100644 --- a/Allay-API/src/main/java/org/allaymc/api/block/component/common/BlockBaseComponent.java +++ b/Allay-API/src/main/java/org/allaymc/api/block/component/common/BlockBaseComponent.java @@ -3,7 +3,9 @@ import org.allaymc.api.block.BlockBehavior; import org.allaymc.api.block.component.BlockComponent; import org.allaymc.api.block.function.*; +import org.allaymc.api.block.property.type.BlockPropertyType; import org.allaymc.api.block.type.BlockType; +import org.allaymc.api.world.Dimension; /** * Allay Project 2023/4/8 @@ -12,4 +14,23 @@ */ public interface BlockBaseComponent extends OnNeighborUpdate, OnRandomUpdate, Place, OnPlace, OnInteract, OnReplace, OnScheduledUpdate, BlockComponent { BlockType getBlockType(); + + default void updateBlockProperty(BlockPropertyType propertyType, DATATYPE value, int x, int y, int z, Dimension dimension) { + updateBlockProperty(propertyType, value, x, y, z, dimension, 0); + } + + default void updateBlockProperty(BlockPropertyType propertyType, DATATYPE value, int x, int y, int z, Dimension dimension, int layer) { + var chunk = dimension.getChunkService().getChunkByLevelPos(x, z); + if (chunk == null) return; + int xIndex = x & 15; + int zIndex = z & 15; + var oldBlockState = chunk.getBlockState(xIndex, y, zIndex, layer); + if (oldBlockState.getBlockType() != getBlockType()) { + throw new IllegalArgumentException("Block type is not match! Expected: " + getBlockType().getIdentifier() + ", actual: " + oldBlockState.getBlockType().getIdentifier()); + } + var newBlockState = oldBlockState.setProperty(propertyType, value); + if (oldBlockState == newBlockState) return; + chunk.setBlockState(xIndex, y, zIndex, newBlockState, layer); + chunk.sendChunkPacket(Dimension.createBlockUpdatePacket(newBlockState, x, y, z, layer)); + } } diff --git a/Allay-API/src/main/java/org/allaymc/api/world/Dimension.java b/Allay-API/src/main/java/org/allaymc/api/world/Dimension.java index 83ae7edae..328aae5b4 100644 --- a/Allay-API/src/main/java/org/allaymc/api/world/Dimension.java +++ b/Allay-API/src/main/java/org/allaymc/api/world/Dimension.java @@ -4,6 +4,7 @@ import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import org.allaymc.api.block.data.BlockFace; import org.allaymc.api.block.data.BlockStateWithPos; +import org.allaymc.api.block.property.type.BlockPropertyType; import org.allaymc.api.block.type.BlockState; import org.allaymc.api.blockentity.BlockEntity; import org.allaymc.api.entity.Entity; diff --git a/Allay-Server/src/main/java/org/allaymc/server/block/component/barrel/BlockBarrelBaseComponentImpl.java b/Allay-Server/src/main/java/org/allaymc/server/block/component/barrel/BlockBarrelBaseComponentImpl.java index f94194eb5..b8a506906 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/block/component/barrel/BlockBarrelBaseComponentImpl.java +++ b/Allay-Server/src/main/java/org/allaymc/server/block/component/barrel/BlockBarrelBaseComponentImpl.java @@ -22,6 +22,7 @@ * @author daoge_cmd */ @RequireBlockProperty(type = BlockPropertyType.Type.INT, name = "facing_direction") +@RequireBlockProperty(type = BlockPropertyType.Type.BOOLEAN, name = "open_bit") public class BlockBarrelBaseComponentImpl extends BlockBaseComponentImpl { public BlockBarrelBaseComponentImpl(BlockType blockType) { super(blockType); diff --git a/Allay-Server/src/main/java/org/allaymc/server/blockentity/component/barrel/BlockEntityBarrelBaseComponentImpl.java b/Allay-Server/src/main/java/org/allaymc/server/blockentity/component/barrel/BlockEntityBarrelBaseComponentImpl.java index 22147f881..b5aa83e75 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/blockentity/component/barrel/BlockEntityBarrelBaseComponentImpl.java +++ b/Allay-Server/src/main/java/org/allaymc/server/blockentity/component/barrel/BlockEntityBarrelBaseComponentImpl.java @@ -1,10 +1,15 @@ package org.allaymc.server.blockentity.component.barrel; +import org.allaymc.api.block.interfaces.BlockBarrelBehavior; +import org.allaymc.api.block.type.BlockTypes; import org.allaymc.api.blockentity.component.common.BlockEntityContainerHolderComponent; import org.allaymc.api.blockentity.init.BlockEntityInitInfo; import org.allaymc.api.blockentity.interfaces.BlockEntityBarrel; import org.allaymc.api.component.annotation.Dependency; +import org.allaymc.api.component.interfaces.ComponentInitInfo; import org.allaymc.api.container.FullContainerType; +import org.allaymc.api.container.impl.BarrelContainer; +import org.allaymc.api.data.VanillaBlockPropertyTypes; import org.allaymc.server.blockentity.component.common.BlockEntityBaseComponentImpl; import org.cloudburstmc.nbt.NbtMap; import org.cloudburstmc.nbt.NbtType; @@ -24,6 +29,36 @@ public BlockEntityBarrelBaseComponentImpl(BlockEntityInitInfo super(info); } + @Override + public void onInitFinish(ComponentInitInfo initInfo) { + super.onInitFinish(initInfo); + var container = containerHolderComponent.getContainer(); + container.addOnOpenListener(viewer -> { + if (container.getViewers().size() == 1) { + BlockTypes.BARREL_TYPE.getBlockBehavior().updateBlockProperty( + VanillaBlockPropertyTypes.OPEN_BIT, + true, + position.x(), + position.y(), + position.z(), + position.dimension() + ); + } + }); + container.addOnCloseListener(viewer -> { + if (container.getViewers().isEmpty()) { + BlockTypes.BARREL_TYPE.getBlockBehavior().updateBlockProperty( + VanillaBlockPropertyTypes.OPEN_BIT, + false, + position.x(), + position.y(), + position.z(), + position.dimension() + ); + } + }); + } + @Override public void loadNBT(NbtMap nbt) { super.loadNBT(nbt);