我正在开发一个Minecraft插件来保护一个区域。我有一个区域类,它是在玩家选择3个区块后创建的,这个区域类有一个名为"tooBig
"的方法,用于检测区域是否大于"block^3
"。问题是这个方法总是返回false
。
public boolean tooBig(int i) {
boolean bo1, bo2, bo3;
bo1 = Math.abs(b1.getX() - b2.getX()) > i;
bo2 = Math.abs(b1.getZ() - b2.getZ()) > i;
bo3 = Math.abs(b1.getY() - b3.getY()) > i;
return bo1 && bo2 && bo3;
}
b1
、b2
和b3
是Block
对象。
您对变量的使用不一致。
public boolean tooBig(int i) {
boolean bo1, bo2, bo3;
bo1 = Math.abs(b1.getX() - b2.getX()) > i;
bo2 = Math.abs(b1.getZ() - b2.getZ()) > i;
bo3 = Math.abs(b1.getY() - b3.getY()) > i;
return bo1 && bo2 && bo3;
}
你的算法完全不正确。计算长方体区域体积的公式是
Base (Length * Width) * Height
其中长方体的长度、宽度和高度为从通过轴的最大点到最小点的距离,而不是随机减去两点。因此,获得该区域的正确代码是:
public boolean tooBig(int i) {
int minX = Math.min(Math.min(b1.getBlockX(), b2.getBlockX()), b3.getBlockX());
int maxX = Math.max(Math.max(b1.getBlockX(), b2.getBlockX()), b3.getBlockX());
int minY = Math.min(Math.min(b1.getBlockY(), b2.getBlockY()), b3.getBlockY());
int maxY = Math.max(Math.max(b1.getBlockY(), b2.getBlockY()), b3.getBlockY());
int minZ = Math.min(Math.min(b1.getBlockZ(), b2.getBlockZ()), b3.getBlockZ());
int maxZ = Math.max(Math.max(b1.getBlockZ(), b2.getBlockZ()), b3.getBlockZ());
int area = (maxX - minX) * (maxY - minY) * (maxY - minY);
}
让它发挥作用:
return area > Math.pow(i, 3);
另请参阅:
Block.getBlockX()
只有当区域的宽度、长度和高度同时大于i
时,此函数才会返回true
。
也许您应该使用return (bo1 || bo2 || bo3);
,这样如果至少有一个维度超过了限制,函数就会返回true
。