界限必须是积极的
bound must be positive
这是我不断收到的错误,我理解,但无法修复,因为我不知道问题出在哪里。
[14:25:50 ERROR]: Could not pass event BlockBreakEvent to SurgeGlowstone v1.0
org.bukkit.event.EventException
at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.java:297) ~[custom.jar:git-PaperSpigot-a925999]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[custom.jar:git-PaperSpigot-a925999]
at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:513) [custom.jar:git-PaperSpigot-a925999]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:498) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.PlayerInteractManager.breakBlock(PlayerInteractManager.java:264) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.PlayerInteractManager.dig(PlayerInteractManager.java:118) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:569) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.PacketPlayInBlockDig.a(PacketPlayInBlockDig.java:41) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.PacketPlayInBlockDig.handle(PacketPlayInBlockDig.java:65) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:189) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:103) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:801) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:286) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:651) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:557) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [custom.jar:git-PaperSpigot-a925999]
Caused by: java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Random.java:388) ~[?:1.8.0_131]
at com.surgehcf.listeners.PlayerListener.onBreak(PlayerListener.java:49) ~[?:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_131]
这是我的代码:
public static int randInt(int min, int max) {
// NOTE: This will (intentionally) not run as written so that folks
// copy-pasting have to think about how to initialize their
// Random instance. Initialization of the Random instance is outside
// the main scope of the question, but some decent options are to have
// a field that is initialized once and then re-used as needed or to
// use ThreadLocalRandom (if using at least Java 1.7).
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
@EventHandler
public void onBreak(BlockBreakEvent e) {
Player p = e.getPlayer();
Block b = e.getBlock();
JsonBox bx = GlowstoneMountain.getInstance().getRegionAqui(b.getLocation());
if (bx != null && b.getType() == Material.GLOWSTONE && b.getWorld().getName().equalsIgnoreCase("world_nether")) {
b.getWorld().dropItemNaturally(b.getLocation(), new ItemStack(Material.GLOWSTONE_DUST,randInt(4,2)));
}
}
我总是做一个 4 到 2 之间的随机整数。但是,它仍然说否定我想要的:一个 2 到 4 之间的整数,以获得随机输出。
在 onBreak()
方法中,在 if
语句中,更改为:
new ItemStack(Material.GLOWSTONE_DUST,randInt(4,2)));
对此:
new ItemStack(Material.GLOWSTONE_DUST,randInt(2,4)));
现在您正在尝试获取一个最大为 2 最小为 4 的随机整数。这是不可能的。改成最大4个,最小2个。
randInt()
函数的参数顺序为 (min, max)
。目前您将其视为 (max, min)
.
要了解错误(以及如何修复),请查看 Java docs on Random's nextInt()
:
Parameters:
bound - the upper bound (exclusive). Must be positive.
Throws:
IllegalArgumentException - if bound is not positive
但是,randInt(4,2)
,正如您所说的方法,是完全错误的方法。它使用 4
作为 min
,2
作为 max
,因此你最终得到一个负边界(-1
),导致异常。
因此,我建议对您的方法进行以下更改:
public static int randInt(int a, int b) {
int min = Math.min(a, b);
int max = Math.max(a, b);
return rand.nextInt((max - min) + 1) + min;
}
解释:
现在,您相信函数的用户会遵守参数的顺序(min, max
- 而不是 max, min
)。虽然您可以做到这一点,但您刚刚体验到这会给您带来麻烦的速度有多快,即使用户是您。
因此,像上面那样添加安全措施将导致更健壮的代码。在这里,我们只是检查两个值中的哪一个是 smaller and which is the bigger 那个,然后相应地使用它们。
当然,您也可以保持方法不变,只需将调用更改为 randInt(2,4)
。
注:
用户仍然有可能让您的方法中断(如上所示抛出异常),但我将把它作为练习留给您,让您弄清楚如何 - 以及如何防止它。 :-)
这是我不断收到的错误,我理解,但无法修复,因为我不知道问题出在哪里。
[14:25:50 ERROR]: Could not pass event BlockBreakEvent to SurgeGlowstone v1.0
org.bukkit.event.EventException
at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.java:297) ~[custom.jar:git-PaperSpigot-a925999]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[custom.jar:git-PaperSpigot-a925999]
at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:513) [custom.jar:git-PaperSpigot-a925999]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:498) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.PlayerInteractManager.breakBlock(PlayerInteractManager.java:264) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.PlayerInteractManager.dig(PlayerInteractManager.java:118) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:569) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.PacketPlayInBlockDig.a(PacketPlayInBlockDig.java:41) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.PacketPlayInBlockDig.handle(PacketPlayInBlockDig.java:65) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:189) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:103) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:801) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:286) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:651) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:557) [custom.jar:git-PaperSpigot-a925999]
at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [custom.jar:git-PaperSpigot-a925999]
Caused by: java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Random.java:388) ~[?:1.8.0_131]
at com.surgehcf.listeners.PlayerListener.onBreak(PlayerListener.java:49) ~[?:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_131]
这是我的代码:
public static int randInt(int min, int max) {
// NOTE: This will (intentionally) not run as written so that folks
// copy-pasting have to think about how to initialize their
// Random instance. Initialization of the Random instance is outside
// the main scope of the question, but some decent options are to have
// a field that is initialized once and then re-used as needed or to
// use ThreadLocalRandom (if using at least Java 1.7).
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
@EventHandler
public void onBreak(BlockBreakEvent e) {
Player p = e.getPlayer();
Block b = e.getBlock();
JsonBox bx = GlowstoneMountain.getInstance().getRegionAqui(b.getLocation());
if (bx != null && b.getType() == Material.GLOWSTONE && b.getWorld().getName().equalsIgnoreCase("world_nether")) {
b.getWorld().dropItemNaturally(b.getLocation(), new ItemStack(Material.GLOWSTONE_DUST,randInt(4,2)));
}
}
我总是做一个 4 到 2 之间的随机整数。但是,它仍然说否定我想要的:一个 2 到 4 之间的整数,以获得随机输出。
在 onBreak()
方法中,在 if
语句中,更改为:
new ItemStack(Material.GLOWSTONE_DUST,randInt(4,2)));
对此:
new ItemStack(Material.GLOWSTONE_DUST,randInt(2,4)));
现在您正在尝试获取一个最大为 2 最小为 4 的随机整数。这是不可能的。改成最大4个,最小2个。
randInt()
函数的参数顺序为 (min, max)
。目前您将其视为 (max, min)
.
要了解错误(以及如何修复),请查看 Java docs on Random's nextInt()
:
Parameters:
bound - the upper bound (exclusive). Must be positive.Throws:
IllegalArgumentException - if bound is not positive
但是,randInt(4,2)
,正如您所说的方法,是完全错误的方法。它使用 4
作为 min
,2
作为 max
,因此你最终得到一个负边界(-1
),导致异常。
因此,我建议对您的方法进行以下更改:
public static int randInt(int a, int b) {
int min = Math.min(a, b);
int max = Math.max(a, b);
return rand.nextInt((max - min) + 1) + min;
}
解释:
现在,您相信函数的用户会遵守参数的顺序(min, max
- 而不是 max, min
)。虽然您可以做到这一点,但您刚刚体验到这会给您带来麻烦的速度有多快,即使用户是您。
因此,像上面那样添加安全措施将导致更健壮的代码。在这里,我们只是检查两个值中的哪一个是 smaller and which is the bigger 那个,然后相应地使用它们。
当然,您也可以保持方法不变,只需将调用更改为 randInt(2,4)
。
注:
用户仍然有可能让您的方法中断(如上所示抛出异常),但我将把它作为练习留给您,让您弄清楚如何 - 以及如何防止它。 :-)