Discussion in 'Spigot Plugin Development' started by SmaxoS, Jun 2, 2018.

  1. Hi, I have this code, but console sending warn:
    Code (Java):
        public HashMap<Player, Integer> taskID = new HashMap<>();
        public HashMap<Player, Integer> countdown = new HashMap<>();
        public HashMap<ItemStack, String> items = new HashMap<>();

        public void open(Player p) {
            Inventory inv = Bukkit.createInventory(null, 27, "Opening");
            for(int i = 0; i <= 25; i++) {
                inv.setItem(i, Items.newItem(Material.STAINED_GLASS_PANE, (byte) i , ""));
            for(int i = 17; i <= 25; i++) {
                int in = i-9;
                inv.setItem(i, Items.newItem(Material.STAINED_GLASS_PANE, (byte) in , ""));
            taskID.put(p, Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.plugin, new Runnable() {
                public void run() {
                    ItemStack randomItem = (ItemStack) items.keySet().toArray()[new Random().nextInt(items.keySet().toArray().length)];
                    inv.setItem(13, randomItem);
                    if(h.countdown.get(p) != null) {
                        countdown.put(p, countdown.get(p) + 1);
                    }else {
                        countdown.put(p, 0);
                    if(countdown.get(p) == 5) {
                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), items.get(inv.getItem(13)));
                        p.sendMessage("You open " + inv.getItem(13).getItemMeta().getDisplayName());
            }, 0, 10));

    and warn:

    Code (Text):
    [19:20:29 WARN]: [Plugin] Task #18 for Plugin v1.2 generated an exception
    java.lang.IllegalArgumentException: bound must be positive
            at java.util.Random.nextInt(Random.java:388) ~[?:1.8.0_171]
            at me.SmaxoS.Plugin$1.run(Plugin.java:39) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71) ~[server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:723) [server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [server.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Thread.java:748) [?:1.8.0_171]
  2. How many items are in the HashMap?
  3. @CreepyCode, 5, here:
    Code (Java):
            items.put(newItem(Material.WOOL, (byte) 1, "§a§lItem-1"), "command");
            items.put(newItem(Material.WOOL, (byte) 2, "§a§lItem-2"), "command");
            items.put(newItem(Material.WOOL, (byte) 3, "§a§lItem-3"), "command");
            items.put(newItem(Material.WOOL, (byte) 4, "§a§lItem-4"), "command");
            items.put(newItem(Material.WOOL, (byte) 5, "§a§lItem-5"), "command");
  4. You're sure these items are inside the HashMap while the "run" method is executed?
  5. I have method load() where are these items and this method is in onEnable().
  6. This is line 39, isn't it?
  7. Yes, it is.
  8. As per the error im guessing the value returned from a list length or value entered wasn’t positive when it needed to be. You could always multiply it by -1 afternoon checking if it is negative
  9. JanTuck


    Or call Math.abs. I don’t think that is the underlying problem here though.
  10. And how I can do that?
  11. items.keySet().toArray().length returns a negative number, so you get an exception.
    • Useful Useful x 1
  12. Well first we need to determine why your list returns a negative number. Or you can use Math.abs like @JanTuck said
  13. JanTuck


    I never said that as a actual solution that would be stupid. His list clearly has more than 1 elements