Problems with Random

Discussion in 'Plugin Development' started by Grauju, Oct 17, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    Grauju

    Hey,

    i want to drop 3 random items at 10 random locations. So I created two randoms. But there is a Problem: As soon as I create the Item in the world there is a Error: Cannot drop a null Item.

    How can I set the Item?

    Here is my code and Error:

    Code:
    [KnockIT] Task #887 for KnockIT v1.0 generated an exception
    java.lang.IllegalArgumentException: Cannot drop a Null item.
        at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot.jar:git-Spigot-f94fe8f-d27e6d0]
        at org.bukkit.craftbukkit.v1_8_R3.CraftWorld.dropItem(CraftWorld.java:309) ~[spigot.jar:git-Spigot-f94fe8f-d27e6d0]
        at org.bukkit.craftbukkit.v1_8_R3.CraftWorld.dropItemNaturally(CraftWorld.java:352) ~[spigot.jar:git-Spigot-f94fe8f-d27e6d0]
        at de.JulsGra.KnockIT.Main.main$1.run(main.java:169) ~[?:?]
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot.jar:git-Spigot-f94fe8f-d27e6d0]
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:722) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot.jar:git-Spigot-f94fe8f-d27e6d0]
        at java.base/java.lang.Thread.run(Thread.java:830) [?:?]

    Code:
    World w = Bukkit.getWorld((String) this.getConfig().get("spawn.world"));
                    
                    
                        Location loc = null;
                    
                        int high = 11;
                    
                        ItemStack random = null;
                    
                        public void Run() {
                            
                                     Random r = new Random();
                                    int zufall = r.nextInt(3);
                                    switch(zufall) {
                                    case 0:
                                        random = new ItemStack(Material.GOLD_AXE, 1);
                                        break;
                                    case 1:
                                        random = new ItemStack(Material.BLAZE_ROD, 1);
                                        break;
                                    case 3:
                                        random = new ItemStack(Material.SNOW_BALL, 8);
                                        break;
                                    }
                                    Random r2 = new Random();
                                    int zufall2 = r2.nextInt(10);
                                    switch(zufall2) {
                                    case 0:
                                        loc = Itemspawns.onItemSpawn1();
                                        break;
                                    case 1:
                                        loc = Itemspawns.onItemSpawn2();
                                        break;
                                    case 2:
                                        loc = Itemspawns.onItemSpawn3();
                                        break;
                                    case 3:
                                        loc = Itemspawns.onItemSpawn4();
                                        break;
                                    case 4:
                                        loc = Itemspawns.onItemSpawn5();
                                        break;
                                    case 5:
                                        loc = Itemspawns.onItemSpawn6();
                                        break;
                                    case 6:
                                        loc = Itemspawns.onItemSpawn7();
                                        break;
                                    case 7:
                                        loc = Itemspawns.onItemSpawn8();
                                        break;
                                    case 8:
                                        loc = Itemspawns.onItemSpawn9();
                                        break;
                                    case 9:
                                        loc = Itemspawns.onItemSpawn10();
                                        break;
                                
                                }
                                
                                    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    
                                        @Override
                                        public void run() {
                                            if(high != 0) {
                                                w.dropItem(loc, random);
                                                Bukkit.getServer().broadcastMessage("Spawn");
                                        }
                                    }
                                    
                                    
                                    
                                    
                                    
                                    
                                    }, 0, 20*5);
     
    Last edited: Oct 17, 2019
  2. Offline

    robertlit

    Never set anything that you wish to change afterwards to null, you should just declare the variable
    Code:
    ItemStack random;
     
  3. Offline

    Strahan

    Also your code is very inefficient. Use some sort of collection/array rather than making a bunch of methods with incrementing numbers stuck on the end. That's painful to look at.
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Grauju Where is case 2 for zufall?
     
Thread Status:
Not open for further replies.

Share This Page