這就是我不斷收到錯誤(這我理解,但不能因爲解決IDK的是什麼問題)界必須積極
[14:25:50 ERROR]: Could not pass event BlockBreakEvent to SurgeGlowstone v1.0
org.bukkit.event.EventException
at org.bukkit.plugin.java.JavaPluginLoader$1.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之間的隨機數,但stil它說的減去基本上我想要的是一個int在2和4之間以獲得隨機輸出
錯誤確實說明了一切。檢查[Java文檔](https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt-int-):*「bound - 上限(獨佔)。必須是積極的。「* - 你傳入的值會導致負值。您應該通過在您的方法中添加安全措施來防止這種情況例如,檢查哪個值更大,並將其用作max,另一個用作min。 – domsson
也許你的意思是'randInt(2,4)',而不是'randInt(4,2)',以便得到'min' 2和'max' 4(含)之間的數字? – Andreas
爲什麼你甚至有交換的界限? 'randInt(4,2)'會給你帶'rand.nextInt(-1)',引發異常。參數應該是'randInt(2,4)'。更大的數字之前的較小數字幾乎普遍用於表示範圍或種類的任何事物。 – QBrute