在 minecraft bukkit 插件中构建金字塔
Build a pyramid in a minecraft bukkit plugin
我想在 Minecraft 中使用 bukkit 插件中的方法构建一个简单的金字塔。最终结果应如下所示:
我写了这段代码:
public static void buildPyramid(Location l) {
Location pos;
for(int i = -2; i <= 2; i++) {
for(int j = -2; j <= 2; j++) {
pos = l.clone().add(i, 0, j);
Bukkit.broadcastMessage(Math.abs(i) + Math.abs(j) + ""); // for test
int diff = Math.abs(i) + Math.abs(j);
switch(diff) {
case 2:
l.getBlock().setType(Material.BEDROCK);
break;
case 1:
l.getBlock().setType(Material.BEDROCK);
pos.add(0, 1, 0);
l.getBlock().setType(Material.BEDROCK);
pos.add(0, -1, 0);
break;
case 0:
l.getBlock().setType(Material.BEDROCK);
pos.add(0, 1, 0);
l.getBlock().setType(Material.BEDROCK);
pos.add(0, 1, 0);
l.getBlock().setType(Material.BEDROCK);
pos.add(0, -2, 0);
break;
default:
break;
}
}
}
}
不幸的是,一个基岩被放置在位置 l 上,没有其他任何事情发生。
好郁闷啊。。。有什么帮助吗?
您的问题在这里:
for(int i = -2; i <= 2; i++) {
for(int j = -2; j <= 2; j++) {
pos = l.clone().add(i, 0, j);
Bukkit.broadcastMessage(Math.abs(i) + Math.abs(j) + ""); // for test
int diff = Math.abs(i) + Math.abs(j);
body 变量情况的第一种方法是:i = -2 和 j = -2。执行此行后:
int diff = Math.abs(i) + Math.abs(j);
它们将是 i = -2 和 j = -2 但 diff = 4,因为 Math.abs()
方法将两个变量的 -2 转换为 2,然后将它们求和以获得 diff。因此,您的 switch-case
语句将无法正常工作。顺便说一下,我建议你从头开始重新计算所有的东西。
对不起各位,解决方法就这么简单。我使用了错误的变量:
public static void buildPyramid(Location l) {
Location pos;
for(int i = -2; i <= 2; i++) {
for(int j = -2; j <= 2; j++) {
pos = l.clone().add(i, 0, j);
Bukkit.broadcastMessage(Math.abs(i) + Math.abs(j) + ""); // for test
int diff = Math.abs(i) + Math.abs(j);
switch(diff) {
case 2:
pos.getBlock().setType(Material.BEDROCK);
break;
case 1:
pos.getBlock().setType(Material.BEDROCK);
pos.add(0, 1, 0);
pos.getBlock().setType(Material.BEDROCK);
pos.add(0, -1, 0);
break;
case 0:
pos.getBlock().setType(Material.BEDROCK);
pos.add(0, 1, 0);
pos.getBlock().setType(Material.BEDROCK);
pos.add(0, 1, 0);
pos.getBlock().setType(Material.BEDROCK);
pos.add(0, -2, 0);
break;
default:
break;
}
}
}
}
我想在 Minecraft 中使用 bukkit 插件中的方法构建一个简单的金字塔。最终结果应如下所示:
我写了这段代码:
public static void buildPyramid(Location l) {
Location pos;
for(int i = -2; i <= 2; i++) {
for(int j = -2; j <= 2; j++) {
pos = l.clone().add(i, 0, j);
Bukkit.broadcastMessage(Math.abs(i) + Math.abs(j) + ""); // for test
int diff = Math.abs(i) + Math.abs(j);
switch(diff) {
case 2:
l.getBlock().setType(Material.BEDROCK);
break;
case 1:
l.getBlock().setType(Material.BEDROCK);
pos.add(0, 1, 0);
l.getBlock().setType(Material.BEDROCK);
pos.add(0, -1, 0);
break;
case 0:
l.getBlock().setType(Material.BEDROCK);
pos.add(0, 1, 0);
l.getBlock().setType(Material.BEDROCK);
pos.add(0, 1, 0);
l.getBlock().setType(Material.BEDROCK);
pos.add(0, -2, 0);
break;
default:
break;
}
}
}
}
不幸的是,一个基岩被放置在位置 l 上,没有其他任何事情发生。 好郁闷啊。。。有什么帮助吗?
您的问题在这里:
for(int i = -2; i <= 2; i++) {
for(int j = -2; j <= 2; j++) {
pos = l.clone().add(i, 0, j);
Bukkit.broadcastMessage(Math.abs(i) + Math.abs(j) + ""); // for test
int diff = Math.abs(i) + Math.abs(j);
body 变量情况的第一种方法是:i = -2 和 j = -2。执行此行后:
int diff = Math.abs(i) + Math.abs(j);
它们将是 i = -2 和 j = -2 但 diff = 4,因为 Math.abs()
方法将两个变量的 -2 转换为 2,然后将它们求和以获得 diff。因此,您的 switch-case
语句将无法正常工作。顺便说一下,我建议你从头开始重新计算所有的东西。
对不起各位,解决方法就这么简单。我使用了错误的变量:
public static void buildPyramid(Location l) {
Location pos;
for(int i = -2; i <= 2; i++) {
for(int j = -2; j <= 2; j++) {
pos = l.clone().add(i, 0, j);
Bukkit.broadcastMessage(Math.abs(i) + Math.abs(j) + ""); // for test
int diff = Math.abs(i) + Math.abs(j);
switch(diff) {
case 2:
pos.getBlock().setType(Material.BEDROCK);
break;
case 1:
pos.getBlock().setType(Material.BEDROCK);
pos.add(0, 1, 0);
pos.getBlock().setType(Material.BEDROCK);
pos.add(0, -1, 0);
break;
case 0:
pos.getBlock().setType(Material.BEDROCK);
pos.add(0, 1, 0);
pos.getBlock().setType(Material.BEDROCK);
pos.add(0, 1, 0);
pos.getBlock().setType(Material.BEDROCK);
pos.add(0, -2, 0);
break;
default:
break;
}
}
}
}