Skip to content

Commit

Permalink
fix: fix barrel open_bit
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed May 11, 2024
1 parent 2ac1d02 commit c94ed2f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,4 +14,23 @@
*/
public interface BlockBaseComponent extends OnNeighborUpdate, OnRandomUpdate, Place, OnPlace, OnInteract, OnReplace, OnScheduledUpdate, BlockComponent {
BlockType<? extends BlockBehavior> getBlockType();

default <DATATYPE> void updateBlockProperty(BlockPropertyType<DATATYPE> propertyType, DATATYPE value, int x, int y, int z, Dimension dimension) {
updateBlockProperty(propertyType, value, x, y, z, dimension, 0);
}

default <DATATYPE> void updateBlockProperty(BlockPropertyType<DATATYPE> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends BlockBehavior> blockType) {
super(blockType);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,6 +29,36 @@ public BlockEntityBarrelBaseComponentImpl(BlockEntityInitInfo<BlockEntityBarrel>
super(info);
}

@Override
public void onInitFinish(ComponentInitInfo initInfo) {
super.onInitFinish(initInfo);
var container = containerHolderComponent.<BarrelContainer>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);
Expand Down

0 comments on commit c94ed2f

Please sign in to comment.