MySQL Query获取球体中的行(X,Y,Z坐标)?

时间:2012-03-20 22:22:56

标签: java mysql minecraft

我正在使用Bukkit API制作名为Minecraft的游戏插件。

我有一个名为Reinforcements的数据库表,其中包含以下字段:x integery integerz integer。加固块是受保护的块,意味着它不能被破坏。

我正在使用EntityExplodeEvent检查TNT爆炸。

我遍历event.blocklist()并将每个块与Reinforcements表中的条目进行比较。如果存在,则使用event.blocklist().remove防止爆炸中的钢筋块损坏。

我可以通过获取每个坐标(x,y,z)的最小和最大,然后检查这两个数字之间的数据库行来实现。这个问题是它是一个立方体。我应该检查球体。我该怎么做?

这是我到目前为止所做的,请注意:我知道这不是一个select语句的问题,因为我可以将返回的行与event.blocklist()进行比较,但我需要知道该怎么做这是我稍后再去发表更新声明的时候。

我之所以需要知道如何检查球体中的行是因为最终我会在Reinforcements表中添加一个名为'durability integer'的额外字段,该表将在每次爆炸后减少。由于爆炸是一个球体,因此更新查询应该只更新该球体中的行,而不是多维数据集。

任何? 谢谢

当前的MySQL查询

"SELECT X, Y, Z FROM REINFORCEMENTS WHERE DURABILITY >= 1 " +
                "AND x<=? AND x>=? AND y<=? AND y>=? AND z<=? AND z>=? AND world=?");

完整代码

@EventHandler
    public void checkForExplosion(EntityExplodeEvent event){

        if(event.isCancelled()){
            return;
        }

        //Store blocks that are inside explosion's blast radius
        List<Block> blastRadiusBlocks = event.blockList();

        //If explosion occurs in mid air it returns 0 so no need to go any
        //further since there are no blocks inside the explosions radius
        if(blastRadiusBlocks.size() < 1){
            return;
        }

        HashMap<Coordinate, Block> affectedBlocks = new HashMap<Coordinate, Block>();

        //Initialize min & max X,Y,Z coordinates
        int smallestX = blastRadiusBlocks.get(0).getX();
        int largestX = smallestX;
        int smallestY = blastRadiusBlocks.get(0).getY();
        int largestY = smallestY;
        int smallestZ = blastRadiusBlocks.get(0).getZ();
        int largestZ = smallestZ;

        //World Name
        String worldName = blastRadiusBlocks.get(0).getWorld().getName();
        World world = this.myPlugin.getServer().getWorld(worldName);

        //Find min & max X,Y,Z coordinates
        for(int i = 0; i < blastRadiusBlocks.size(); i++){
            Block block = blastRadiusBlocks.get(i);
            int blockX = block.getX();
            int blockY = block.getY();
            int blockZ = block.getZ();

            if(blockX < smallestX){
                smallestX = blockX;
            }

            if(blockX > largestX){
                largestX = blockX;
            }

            if(blockY < smallestY){
                smallestY = blockY;
            }

            if(blockY > largestY){
                largestY = blockY;
            }

            if(blockZ < smallestZ){
                smallestZ = blockZ;
            }

            if(blockZ > largestZ){
                largestZ = blockZ;
            }

            //Instantiate Coordinate class passing in parameters
            Coordinate coordinate = new Coordinate(world, blockX, blockY, blockZ);
            //Put a new entry of type Coordinate as key and type Block as value
            affectedBlocks.put(coordinate, block);
        }

        try {
            //Query database for any reinforced blocks that may be in the blast radius
            //Reinforced blocks should have a durability > 0 (aka >= 1)
            PreparedStatement ask = this.conn.prepareStatement(
                "SELECT X, Y, Z FROM REINFORCEMENTS WHERE DURABILITY >= 1 " +
                "AND x<=? AND x>=? AND y<=? AND y>=? AND z<=? AND z>=? AND world=?");
            ask.setInt(1, largestX);
            ask.setInt(2, smallestX);
            ask.setInt(3, largestY);
            ask.setInt(4, smallestY);
            ask.setInt(5, largestZ);
            ask.setInt(6, smallestZ);
            ask.setString(7, worldName);
            ask.execute();
            ResultSet result = ask.getResultSet();

            //If there was some found, loop through each one
            while(result.next()){
                //Get X,Y,Z coords of reinforced block
                int x = result.getInt(1);
                int y = result.getInt(2);
                int z = result.getInt(3);

                //Pass in x, y, z of reinforced block into affectedBlocks HashMap to instantiate a Block
                Block protectedBlock = affectedBlocks.get(new Coordinate(world, x, y, z));
                //Then remove the protectedBlock from explosion list
                event.blockList().remove(protectedBlock);
            }

            result.close();
            ask.close();

        } catch (SQLException e) {
            System.err.println("Citadel - Select Reinforcement can't keep up (possibly too many explosions):\n" + e);
        }
}

2 个答案:

答案 0 :(得分:2)

如果您的球体位于x0, y0 z0并且半径为r,那么您需要:

select ... from ... where ((x-x0)*(x-x0)+(y-y0)*(y-y0)+(z-z0)*(z-z0) < r*r);

(这只是3d中的毕达哥拉斯)。

答案 1 :(得分:2)

SELECT X, Y, Z FROM REINFORCEMENTS 
WHERE DURABILITY >= 1 
AND world=?
AND (POW((X-?),2)+POW((Y-?),2)+POW((Z-?),2))<POW(?,2)

参数为woldID,爆炸的X,Y,Z和爆炸半径