Solved setItem not working in loop

Discussion in 'Plugin Development' started by Cat 700, Mar 12, 2020.

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

    Cat 700

    so when I put setItem in a loop it didn't work I had it have a changing variable for the slot. this is my code

    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    public class mythicCrafter implements Listener {
        @SuppressWarnings("deprecation")
        @EventHandler
      
        public void playerclick(PlayerInteractEvent event) {
          
            Player player = event.getPlayer();
            if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
            if (player.getItemInHand().getType() == Material.BREWING_STAND) {
              
                int inv = 45;
                int loop = 0;
                Inventory Crafter = Bukkit.createInventory(null, inv, "Mythical Crafter");
                    while (loop <= inv) {
                        Crafter.setItem(loop, new ItemStack(Material.BLACK_STAINED_GLASS_PANE));
                        loop =+ 1;
                }
              
                player.openInventory(Crafter);
              
                if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    event.setCancelled(true);
                }
            }
        }
        }
    }
    I tried having it start at 0 and I also tried having it start at 1 neither worked the server just lags then crashes.
     
    Last edited: Mar 12, 2020
  2. Offline

    yPedx

    You are setting the loop variable to 1 after every setItem. The JVM reads the code above as loop = +1; which is just 1 every time.

    The correct syntax is;
    Code:text
    1. loop += 1;

    Or, for readability, you can use either ++loop; or loop++;
     
    Last edited: Mar 12, 2020
  3. Offline

    Cat 700

    oh thanks should have realized that myself
     
  4. Offline

    Machine Maker

    If your issue is resolved, please mark this thread as Solved using the Thread Tools link at the top of the page.
     
  5. Offline

    Cat 700

    it still doesn't work because when I try it now it gives this error

    org.bukkit.event.EventException: null
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:319) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:541) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:528) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:442) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:409) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:405) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1294) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at net.minecraft.server.v1_15_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:28) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at net.minecraft.server.v1_15_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) [spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at net.minecraft.server.v1_15_R1.MinecraftServer.ba(MinecraftServer.java:918) [spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:911) [spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:895) [spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:828) [spigot-1.15.2.jar:git-Spigot-8faa8b4-13ed05d]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.ArrayIndexOutOfBoundsException
     
  6. Offline

    CraftCreeper6

  7. Offline

    Cat 700

    ok it works now but I didn't change inv to 44 I change the if statement to be just if (loop < inv) instead of if (loop <= inv) because inv also sets the number of slots
     
Thread Status:
Not open for further replies.

Share This Page