Skip to content

Instantly share code, notes, and snippets.

@BomBardyGamer
Created July 9, 2023 21:27
Show Gist options
  • Save BomBardyGamer/5113ed8ac6e52921357c81566c7ce54a to your computer and use it in GitHub Desktop.
Save BomBardyGamer/5113ed8ac6e52921357c81566c7ce54a to your computer and use it in GitHub Desktop.
Bell Minestom placement rule
package dev.emortal.minestom.core.placement;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.utils.Direction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Locale;
public final class BellPlacementRule extends BlockPlacementRule {
public BellPlacementRule() {
super(Block.BELL);
}
@Override
public @Nullable Block blockPlace(@NotNull PlacementState placementState) {
Block.Getter instance = placementState.instance();
Block block = placementState.block();
int x = placementState.placePosition().blockX();
int y = placementState.placePosition().blockY();
int z = placementState.placePosition().blockZ();
BlockFace clickedFace = placementState.blockFace();
Direction clickedDirection = clickedFace.toDirection();
Direction facing = BlockFace.fromYaw(placementState.playerPosition().yaw()).toDirection();
if (clickedFace == BlockFace.BOTTOM) {
return block.withProperty("facing", facing.name().toLowerCase(Locale.ROOT)).withProperty("attachment", "ceiling");
}
if (clickedFace == BlockFace.TOP) {
return block.withProperty("facing", facing.name().toLowerCase(Locale.ROOT)).withProperty("attachment", "floor");
}
Direction oppositeDirection = clickedDirection.opposite();
block = block.withProperty("facing", oppositeDirection.name().toLowerCase(Locale.ROOT));
Block placedOn = instance.getBlock(x - clickedDirection.normalX(), y, z - clickedDirection.normalZ());
if (placedOn.compare(Block.BELL)) return block.withProperty("attachment", "floor");
Block oppositePlacedOn = instance.getBlock(x - oppositeDirection.normalX(), y, z - oppositeDirection.normalZ());
String attachment = isAir(oppositePlacedOn) ? "single_wall" : "double_wall";
return block.withProperty("attachment", attachment);
}
@Override
public @NotNull Block blockUpdate(@NotNull UpdateState updateState) {
Block.Getter instance = updateState.instance();
Block block = updateState.currentBlock();
int x = updateState.blockPosition().blockX();
int y = updateState.blockPosition().blockY();
int z = updateState.blockPosition().blockZ();
Direction facing = Direction.valueOf(block.getProperty("facing").toUpperCase(Locale.ROOT));
String attachment = block.getProperty("attachment");
if (attachment == null) return updateState.currentBlock();
if (attachment.equals("ceiling") || attachment.equals("floor")) return updateState.currentBlock();
Direction opposite = facing.opposite();
Block placedOn = instance.getBlock(x + facing.normalX(), y, z + facing.normalZ());
Block oppositePlacedOn = instance.getBlock(x + opposite.normalX(), y, z + opposite.normalZ());
if (isAir(placedOn) && isAir(oppositePlacedOn)) return block.withProperty("attachment", "double_wall");
if (attachment.equals("single_wall") && !isAir(placedOn) && !isAir(oppositePlacedOn)) {
return block.withProperty("attachment", "double_wall");
}
if (attachment.equals("double_wall")) {
if (isAir(placedOn)) {
return block.withProperty("facing", opposite.name().toLowerCase(Locale.ROOT)).withProperty("attachment", "single_wall");
}
if (isAir(oppositePlacedOn)) {
return block.withProperty("facing", facing.name().toLowerCase(Locale.ROOT)).withProperty("attachment", "single_wall");
}
}
return block;
}
private static boolean isAir(@NotNull Block block) {
return block.compare(Block.AIR) || block.compare(Block.VOID_AIR) || block.compare(Block.CAVE_AIR);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment