2017-06-29 142 views
-6

這就是我不斷收到錯誤(這我理解,但不能因爲解決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之間以獲得隨機輸出

+0

錯誤確實說明了一切。檢查[Java文檔](https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt-int-):*「bound - 上限(獨佔)。必須是積極的。「* - 你傳入的值會導致負值。您應該通過在您的方法中添加安全措施來防止這種情況例如,檢查哪個值更大,並將其用作max,另一個用作min。 – domsson

+0

也許你的意思是'randInt(2,4)',而不是'randInt(4,2)',以便得到'min' 2和'max' 4(含)之間的數字? – Andreas

+0

爲什麼你甚至有交換的界限? 'randInt(4,2)'會給你帶'rand.nextInt(-1)',引發異常。參數應該是'randInt(2,4)'。更大的數字之前的較小數字幾乎普遍用於表示範圍或種類的任何事物。 – QBrute

回答

3

理解的錯誤(和如何解決它),檢查Java docs on Random's nextInt()

參數:

bound - the upper bound (exclusive). Must be positive.

拋出:

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,哪一個是bigger,然後相應地使用它們。

當然,您也可以將您的方法保持不變,只需將呼叫更改爲randInt(2,4)即可。

注:

它仍然有可能爲用戶獲取方法打破(拋出一個異常,如上),但我會離開它作爲一個練習,你要弄清楚如何 - 和你如何防範它。 :-)

+0

感謝那些完美工作:) –

+1

@jakeyancey如何只固定電話,而不是:'randInt(2,4)' – Andreas

+0

@Andreas這是明顯的修復,如已被[賈馬爾的回答(https://stackoverflow.com覆蓋/一個/3316645分之44825319)。但是,爲了完整起見,我在其上添加了一個句子。 – domsson

2

在onBreak方法中,在if語句中,更改此:

new ItemStack(Material.GLOWSTONE_DUST,randInt(4,2))); 

這樣:

new ItemStack(Material.GLOWSTONE_DUST,randInt(2,4))); 

現在你正在試圖獲得爲2的最高分和4分鐘一那是不可能的隨機int值。將其更改爲最大值4和最小值2。

randInt函數的參數順序爲(min,max)。目前你對待它,喜歡它的(最大,最小)