Minecraft with というゲームのプラグインを作っていますBukkit API
。
Reinforcements
次のフィールドで呼び出されるデータベース テーブルがあります: x integer
、y integer
、z integer
。強化ブロックは保護されたブロックであり、破壊することはできません。
を使用してEntityExplodeEvent
、TNT の爆発を確認しています。
各ブロックをループしてevent.blocklist()
、増援テーブルのエントリと比較します。存在する場合は、 を使用して爆発時の強化ブロックへのダメージを防ぎevent.blocklist().remove
ます。
これを行うには、各座標 (x、y、z) の最小値と最大値を取得し、これら 2 つの数値の間のデータベース行をチェックします。これの問題は、それが立方体であることです。球をチェックする必要があります。どうすればいいですか?
これが私がこれまでに得たものです、注:返された行を比較できるので、これはselectステートメントの問題ではないことを認識していますevent.blocklist()
が、更新を行うときにこれを行う方法を知る必要があります声明は後日。
球体の行をチェックする方法を知る必要がある理由は、最終的には、'durability integer'
爆発のたびに減少する、呼び出された 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);
}
}