BukktAPI 判断玩家是否在树下
本文最后更新于 1484 天前,其中的信息可能已经有所发展或是发生改变。

BukkitAPI 1.16.4

前排提醒,这个算法有一个缺点就是灵活性不强,矮树/高密度的多棵树/树上有别的方块都有可能导致返回 false ,因此适用于要求并不精密的环境

fun LivingEntity.isUnderTree(): Boolean {
        val maxDistanceBetweenLogAndLeaves = 5
        val treeLeavesNumberAtLeast = 6
        val treeLogUpTo = 1
        val treeLogDownTo = 1

        val highestBlock = world.getHighestBlockAt(location)

        if (highestBlock.y <= location.y) return false
        if (!highestBlock.type.name.endsWith("LEAVES")) return false

        val blockList = highestBlock.location.get3DBlocksAround(maxDistanceBetweenLogAndLeaves)
        if (!blockList.stream().anyMatch { it.type.name.endsWith("LOG") }) return false
        if (blockList.filter { it.type.name.endsWith("LEAVES") }.count() < treeLeavesNumberAtLeast) return false

        val log = blockList.find { it.type.name.endsWith("LOG") }!!
        if (!log.location.getYBlocks(treeLogUpTo, treeLogDownTo).all { it.type.name.endsWith("LEAVES") || it.type == log.type }) return false

        return true
    }


    fun Location.get3DBlocksAround(radius: Int): List<Block> {
        val blockList = mutableListOf<Block>()
        val max = clone().add(radius.toDouble(), radius.toDouble(), radius.toDouble())
        val min = clone().subtract(radius.toDouble(), radius.toDouble(), radius.toDouble())
        for (loop_x in min.x.toInt()..max.x.toInt()) {
            for (loop_y in min.y.toInt()..max.y.toInt()) {
                for (loop_z in min.z.toInt()..max.z.toInt()) {
                    val block = Location(world, loop_x.toDouble(), loop_y.toDouble(), loop_z.toDouble()).block
                    if (!block.type.name.endsWith("AIR"))
                        blockList.add(block)
                }
            }
        }
        return blockList
    }

    fun Location.getYBlocks(up: Int, down: Int): List<Block> {
        val blockList = mutableListOf<Block>()
        for (loop_y in (-down)..up) {
            val block = clone().add(0.0, loop_y.toDouble(), 0.0).block
            if (!block.type.name.endsWith("AIR"))
                blockList.add(block)
        }
        return blockList
    }

扫码关注 HikariLan's Blog 微信公众号,及时获取最新博文!


微信公众号图片
暂无评论

发送评论 编辑评论

|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇