2017-06-29 144 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)。目前你对待它,喜欢它的(最大,最小)