Skip to content

Commit

Permalink
feat: impl trapdoor
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-xi committed Dec 30, 2024
1 parent 9a5c63a commit aa3e9b0
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.allaymc.server.block.component.trapdoor;

import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.dto.PlayerInteractInfo;
import org.allaymc.api.block.type.BlockType;
import org.allaymc.api.item.ItemStack;
import org.allaymc.api.world.Dimension;

public class BlockIronTrapdoorBaseComponentImpl extends BlockTrapdoorBaseComponentImpl {
public BlockIronTrapdoorBaseComponentImpl(BlockType<? extends BlockBehavior> blockType) {
super(blockType);
}

@Override
public boolean onInteract(ItemStack itemStack, Dimension dimension, PlayerInteractInfo interactInfo) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.allaymc.server.block.component.trapdoor;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.data.BlockFace;
import org.allaymc.api.block.dto.PlayerInteractInfo;
import org.allaymc.api.block.property.type.BlockPropertyTypes;
import org.allaymc.api.block.type.BlockState;
import org.allaymc.api.block.type.BlockType;
import org.allaymc.api.item.ItemStack;
import org.allaymc.api.world.Dimension;
import org.allaymc.server.block.component.BlockBaseComponentImpl;
import org.cloudburstmc.protocol.bedrock.data.SoundEvent;
import org.joml.Vector3i;
import org.joml.Vector3ic;

import static org.allaymc.api.block.property.type.BlockPropertyTypes.DIRECTION_4;
import static org.allaymc.api.block.property.type.BlockPropertyTypes.OPEN_BIT;

public class BlockTrapdoorBaseComponentImpl extends BlockBaseComponentImpl {
protected static final BiMap<BlockFace, Integer> TRAPDOOR_DIRECTION = HashBiMap.create(4);

static {
TRAPDOOR_DIRECTION.put(BlockFace.EAST, 1);
TRAPDOOR_DIRECTION.put(BlockFace.SOUTH, 3);
TRAPDOOR_DIRECTION.put(BlockFace.WEST, 0);
TRAPDOOR_DIRECTION.put(BlockFace.NORTH, 2);
}

public BlockTrapdoorBaseComponentImpl(BlockType<? extends BlockBehavior> blockType) {
super(blockType);
}

@Override
public boolean place(Dimension dimension, BlockState blockState, Vector3ic placeBlockPos, PlayerInteractInfo placementInfo) {
if (placementInfo == null) {
dimension.setBlockState(placeBlockPos.x(), placeBlockPos.y(), placeBlockPos.z(), blockState);
return true;
}

var face = placementInfo.player().getHorizontalFace();
blockState = blockState.setProperty(DIRECTION_4, TRAPDOOR_DIRECTION.get(face));

var blockFace = placementInfo.blockFace();
if ((placementInfo.clickedPos().y() > 0.5 && blockFace != BlockFace.UP) || blockFace == BlockFace.DOWN) {
blockState = blockState.setProperty(BlockPropertyTypes.UPSIDE_DOWN_BIT, true);
}

dimension.setBlockState(placeBlockPos.x(), placeBlockPos.y(), placeBlockPos.z(), blockState, placementInfo);
return true;
}

@Override
public boolean onInteract(ItemStack itemStack, Dimension dimension, PlayerInteractInfo interactInfo) {
if (super.onInteract(itemStack, dimension, interactInfo)) return true;
if (interactInfo == null) return false;
Vector3i pos = (Vector3i) interactInfo.clickedBlockPos();
var blockState = dimension.getBlockState(pos);
var isOpen = !blockState.getPropertyValue(OPEN_BIT);
dimension.updateBlockProperty(OPEN_BIT, isOpen, pos);
dimension.addLevelSoundEvent(pos.x(), pos.y(), pos.z(), isOpen ? SoundEvent.DOOR_OPEN : SoundEvent.DOOR_CLOSE);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.allaymc.server.block.component.sign.BlockWallSignBaseComponentImpl;
import org.allaymc.server.block.component.torch.BlockColoredTorchBaseComponentImpl;
import org.allaymc.server.block.component.torch.BlockTorchBaseComponentImpl;
import org.allaymc.server.block.component.trapdoor.BlockIronTrapdoorBaseComponentImpl;
import org.allaymc.server.block.component.trapdoor.BlockTrapdoorBaseComponentImpl;
import org.allaymc.server.block.impl.*;

import java.time.Duration;
Expand Down Expand Up @@ -1002,4 +1004,32 @@ public static void initFire() {
.setBaseComponentSupplier(BlockSoulFireBaseComponentImpl::new)
.build();
}
public static BlockType<BlockTrapdoorBehavior> buildTrapdoor(BlockId blockId) {
return AllayBlockType.builder(BlockTrapdoorBehaviorImpl.class)
.vanillaBlock(blockId)
.setProperties(BlockPropertyTypes.DIRECTION_4, BlockPropertyTypes.OPEN_BIT, BlockPropertyTypes.UPSIDE_DOWN_BIT)
.setBaseComponentSupplier(BlockTrapdoorBaseComponentImpl::new)
.build();
}

public static void initTrapdoor() {
BlockTypes.ACACIA_TRAPDOOR = buildTrapdoor(BlockId.ACACIA_TRAPDOOR);
BlockTypes.BAMBOO_TRAPDOOR = buildTrapdoor(BlockId.BAMBOO_TRAPDOOR);
BlockTypes.BIRCH_TRAPDOOR = buildTrapdoor(BlockId.BIRCH_TRAPDOOR);
BlockTypes.CHERRY_TRAPDOOR = buildTrapdoor(BlockId.CHERRY_TRAPDOOR);
BlockTypes.CRIMSON_TRAPDOOR = buildTrapdoor(BlockId.CRIMSON_TRAPDOOR);
BlockTypes.DARK_OAK_TRAPDOOR = buildTrapdoor(BlockId.DARK_OAK_TRAPDOOR);
BlockTypes.JUNGLE_TRAPDOOR = buildTrapdoor(BlockId.JUNGLE_TRAPDOOR);
BlockTypes.MANGROVE_TRAPDOOR = buildTrapdoor(BlockId.MANGROVE_TRAPDOOR);
BlockTypes.TRAPDOOR = buildTrapdoor(BlockId.TRAPDOOR);
BlockTypes.SPRUCE_TRAPDOOR = buildTrapdoor(BlockId.SPRUCE_TRAPDOOR);
BlockTypes.WARPED_TRAPDOOR = buildTrapdoor(BlockId.WARPED_TRAPDOOR);
BlockTypes.PALE_OAK_TRAPDOOR = buildTrapdoor(BlockId.PALE_OAK_TRAPDOOR);

BlockTypes.IRON_TRAPDOOR = AllayBlockType.builder(BlockTrapdoorBehaviorImpl.class)
.vanillaBlock(BlockId.IRON_TRAPDOOR)
.setProperties(BlockPropertyTypes.DIRECTION_4, BlockPropertyTypes.OPEN_BIT, BlockPropertyTypes.UPSIDE_DOWN_BIT)
.setBaseComponentSupplier(BlockIronTrapdoorBaseComponentImpl::new)
.build();
}
}

0 comments on commit aa3e9b0

Please sign in to comment.