亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在靜脈中獲取所有相似的塊

在靜脈中獲取所有相似的塊

慕村9548890 2021-06-30 10:11:38
我正在嘗試將所有類似的塊放在一起。但是我的代碼似乎只在 5 x 5 塊半徑內獲取塊。我嘗試使用一種方法public List<Block> getVein(Block b){    List<Block> blocks = similarNear(null, b);    ListIterator<Block> toCheck = blocks.listIterator();    while(toCheck.hasNext()) {        Block current = toCheck.next();        if(!blocks.contains(current))            blocks.add(current);        for(Block block : similarNear(blocks, current)) {            if(!blocks.contains(block))                toCheck.add(block);        }    }    return blocks;}我還想添加一個上限說示例只允許它打破 25 個塊,但我不知道如何添加它而不阻止它在一個方向上找到 25 個塊然后停止(如果有任何意義)。
查看完整描述

1 回答

?
MM們

TA貢獻1886條經驗 獲得超2個贊

在這里,我嘗試盡可能地注釋代碼,任何建議表示贊賞。


//Loops around a block to find other blocks that matches with the center (the given block)

private ArrayList<Block> search(Block center) {

    //The maximum amount of blocks to find (the 'cap', our limit)

    final int max = 25;


    ArrayList<Block> blocks = new ArrayList<>();

    Queue<Block> toSearch = new LinkedList<>();


    //Add the center to list, so it has something to start the search

    toSearch.add(center);


    //While we have something to search and have not reached the limit (the 'cap')

    while (toSearch.size() > 0 && blocks.size() < max) {

        Block b = toSearch.remove(); //Get the block on top of the queue, (and remove it)


        blocks.add(b); //Since this block is already of the type we want, we add it the found list (in this case is the 'blocks' var)


        //Find all its neighbours

        for (Block around : findNeighbours(b)) {

            //We do this check here too 'cause findNeighbours() might return up to 26 blocks and it might be too much

            //eg. we have a max of 50 blocks and have already found 45, if findNeighbours find more than five blocks we want to ignore to others

            //that way we stay within our limit, and once this check is made once the whole loop will end

            if (blocks.size() >= max) {

                break;

            }


            //Only add this block if not yet found/processed/searched

            if (toSearch.contains(around) || blocks.contains(around)) {

                continue;

            }


            toSearch.add(around);

        }


        //If in our toSearch list we already enough blocks to fill our limit we stop the search and add as much as we need to fill up out limit.

        //This can save some resources when searching for common blocks like dirt and stone, which we might find a lot and not all of them will be added to our list

        if (toSearch.size() + blocks.size() >= max) {

            int remains = max - blocks.size(); //Gets how many more blocks we need to fulfill our goal (the limit)


            for (int i = 0; i < remains; i++) {

                blocks.add(toSearch.remove());

            }


            break;

        }

    }


    return blocks;

}


//Finds all neighbours around a block

private List<Block> findNeighbours(Block block) {

    //to avoid a bunch of ifs we use these 3 fors to loop over each axis (X, Y, Z)


    ArrayList<Block> blocks = new ArrayList<>();


    //SQUARED 'radius' to search around

    final int searchRadius = 1;


    for (int x = -searchRadius; x <= searchRadius; x++) {

        for (int y = -searchRadius; y <= searchRadius; y++) {

            for (int z = -searchRadius; z <= searchRadius; z++) {

                if (x == 0 && y == 0 && z == 0) {continue;}


                //Get the block at this location (x,y,z)

                Block near = block.getLocation().clone().add(x, y, z).getBlock();


                //Check if the found block is a valid match. eg: is the same type, has the same data/variant

                if (match(near, block)) {

                    blocks.add(near);

                }

            }

        }

    }


    return blocks;

}


//Checks if a matches to b

private boolean match(Block a, Block b) {

    //Checks only the block type, ot its variant/data

    //return a.getType() == b.getType();


    //Checks its type and its data/variant (might not work on all bukkit/spigot versions)

    return a.getType() == b.getType() && a.getState().getData().equals(b.getState().getData());

}



查看完整回答
反對 回復 2021-07-07
  • 1 回答
  • 0 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號