我正在制作一个名为Minecraft with Bukkit API的游戏插件.
我有一个名为Reinforcements的数据库表,其中包含以下字段:x integer,y integer,z integer.加固块是受保护的块,意味着它不能被破坏.
我正在使用EntityExplodeEvent来检查TNT爆炸.
我遍历event.blocklist()并将每个块与Reinforcements表中的条目进行比较.如果存在,则使用event.blocklist().remove来防止在爆炸中对Reinforced块的损坏.
我可以通过获取每个坐标(x,y,z)的最小和最大,然后检查这两个数字之间的数据库行来实现.这个问题是它是一个立方体.我应该检查球体.我该怎么做呢?
这是我到目前为止所得到的,请注意:我知道这不是一个select语句的问题,因为我可以将返回的行与event.blocklist()进行比较,但我需要知道如何在我执行此操作时稍后再去做一个更新声明.
我之所以需要知道如何检查球体中的行是因为最终我会在Reinforcements表中添加一个额外的字段,称为“耐久性整数”,它会在每次爆炸后减少.由于爆炸是一个球体,因此更新查询应该只更新该球体中的行,而不是多维数据集.
任何人?
谢谢
当前的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);
}
}
解决方法:
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和爆炸半径