4

我正在为名为 Minecraft 的游戏制作一个插件Bukkit API

我有一个Reinforcements使用以下字段调用的数据库表:x integer, y integer, z 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);
        }
}
4

2 回答 2

2

如果你的球体以x0, y0 z0半径为中心r,那么你需要:

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

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

于 2012-03-20T22:35:35.130 回答
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和爆炸半径

于 2012-03-20T22:38:49.383 回答