gpt4 book ai didi

java - MySQL 查询获取球体中的行(X、Y、Z 坐标)?

转载 作者:可可西里 更新时间:2023-11-01 07:33:24 28 4
gpt4 key购买 nike

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

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

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

我循环遍历 event.blocklist() 并将每个 block 与 Reinforcements 表中的条目进行比较。如果存在,则使用 event.blocklist().remove 防止在爆炸中对 Reinforced block 造成损坏。

我可以通过获取每个坐标 (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);
}
}

最佳答案

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和爆炸半径

关于java - MySQL 查询获取球体中的行(X、Y、Z 坐标)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9795921/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com