必须是积极的

时间:2017-06-29 12:32:05

标签: java bukkit

这是我继续犯的错误(我理解但无法解决,因为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它说减去基本上我想要的是2 en 4之间的int以获得随机输出

2 个答案:

答案 0 :(得分:3)

要了解错误(以及如何修复错误),请查看Java docs on Random's nextInt()

  

参数:

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

投掷:

IllegalArgumentException - if bound is not positive

但是,//Get Blob (Document is table column name) TechnicalSpreadsheet.Document.CREATEINSTREAM(Lstr_BlobInstream); //init Memory Stream MemStream := MemStream.MemoryStream(); COPYSTREAM(MemStream,Lstr_BlobInstream); //Init DotNet Variable ExcelDocument := ExcelDocument.ExcelDocument; //Set byte[] ExcelDocument.Excel := MemStream.GetBuffer; //Call function in AddIn CurrPage.spreadsheet.SetExcelDocument(ExcelDocument); ,就像你调用你的方法一样,是完全错误的方法。它使用randInt(4,2)作为4min作为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)。虽然你可以做到这一点,但你只是经历了多快会让你陷入麻烦,即使用户就是你。

因此,添加上述安全措施将会产生更强大的代码。在这里,我们只检查两个值中的哪一个是smaller,哪个是bigger,然后相应地使用它们。

当然,您也可以按原样保留方法,只需将通话更改为max, min

注意:

用户仍然可以让您的方法中断(如上所述抛出异常),但我会将其作为练习留给您弄清楚如何 - 以及如何防范它。 : - )

答案 1 :(得分: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)。目前,您可以像对待它一样(最大,最小)