Solved Problem with Inventory

Discussion in 'Plugin Development' started by PreFiXAUT, Jun 7, 2014.

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

    PreFiXAUT

    Hey Guys, I got a problem with my Inventory.
    What I want to do:
    Adding all Items into the Inventory, and then completing the row with empty Slots. The Commands are starting in the next row then.
    This is my Code currently:
    Show Spoiler

    Code:java
    1.  
    2. if (!(cs instanceof Player)) {
    3. cs.sendMessage(noPlayer);
    4. return true;
    5. }
    6. Player p = (Player) cs;
    7. List<MysteryItem> items = MysteryCrate.getCrateItems(args[1]);
    8. List<MysteryCommand> commands = MysteryCrate.getCrateCommands(args[1]);
    9. if ((items == null || items.isEmpty()) && (commands == null || commands.isEmpty())) {
    10. p.sendMessage("§4Sorry, but there are no Items saved for this kind of Crate.");
    11. return true;
    12. }
    13. int lines = 0;
    14. int amt = items.size() + commands.size();
    15. while (lines * 9 < amt) {
    16. lines++;
    17. }
    18. if (lines >= 6) lines = 6;
    19. System.out.println("DEBUG: Lines > " + lines);
    20. Inventory inv = Bukkit.createInventory(p, lines*9, "Aviable Loot");
    21. int counter =0;
    22. int count = 0;
    23. for (MysteryItem item : items) {
    24. if (counter > lines*9) break;
    25. ItemStack itm = item.getItem();
    26. itm.setAmount(1);
    27. inv.addItem(itm);
    28. count++;
    29. counter++;
    30. }
    31. System.out.println("DEBUG: couter > " + counter);
    32. while ((count%9) != 0) {
    33. inv.addItem(new ItemStack(Material.AIR));
    34. count++;
    35. System.out.println("DEBUG: AIR added");
    36. }
    37. if (counter != count) {
    38. System.out.println("DEBUG: counter [" + counter + "], count [" + count + "]");
    39. counter = count;
    40. //counter ++;
    41. System.out.println("DEBUG: counter > " + counter);
    42. System.out.println("DEBUG: Lines: " + lines*9);
    43. int newlines = (lines+1)*9;
    44. System.out.println("DEBUG: new Lines: " + newlines);
    45. if (newlines < 6*9) {
    46. inv.setMaxStackSize(newlines);
    47. System.out.println("DEBUG: Max-size: " + inv.getMaxStackSize());
    48. System.out.println("DEBUG: Size: " + inv.getSize());
    49. }
    50. }
    51. for (MysteryCommand command : commands) {
    52. if (counter > lines*9) break;
    53. System.out.println("DEBUG: Counter: " + counter);
    54. ItemStack itm = command.getItem();
    55. inv.setItem(counter,itm);
    56. //inv.addItem(itm);
    57. counter++;
    58. }
    59. System.out.println("DEBUG: Size2: " + inv.getSize());
    60. p.openInventory(inv);
    61. log.put(p, inv);
    62. return true;
    63.  


    Currently I get a Error, that it's out of Index, but it shouldn't be, because I test if it should go into a new Line and then add a new Line.
    My Debug-Messages:
    Show Spoiler

    Code:
    [00:07:55] [Server thread/INFO]: prefixaut issued server command: /cr list super
    [00:07:55] [Server thread/INFO]: DEBUG: Lines > 1
    [00:07:55] [Server thread/INFO]: DEBUG: couter > 1
    [00:07:55] [Server thread/INFO]: DEBUG: AIR added
    [00:07:55] [Server thread/INFO]: DEBUG: AIR added
    [00:07:55] [Server thread/INFO]: DEBUG: AIR added
    [00:07:55] [Server thread/INFO]: DEBUG: AIR added
    [00:07:55] [Server thread/INFO]: DEBUG: AIR added
    [00:07:55] [Server thread/INFO]: DEBUG: AIR added
    [00:07:55] [Server thread/INFO]: DEBUG: AIR added
    [00:07:55] [Server thread/INFO]: DEBUG: AIR added
    [00:07:55] [Server thread/INFO]: DEBUG: counter [1], count [9]
    [00:07:55] [Server thread/INFO]: DEBUG: counter > 9
    [00:07:55] [Server thread/INFO]: DEBUG: Lines: 9
    [00:07:55] [Server thread/INFO]: DEBUG: new Lines: 18
    [00:07:55] [Server thread/INFO]: DEBUG: Max-size: 18
    [00:07:55] [Server thread/INFO]: DEBUG: Size: 9
    [00:07:55] [Server thread/INFO]: DEBUG: Counter: 9
    [00:07:55] [Server thread/INFO]: DEBUG: Size2: 9

    Stack-trace:
    Show Spoiler

    Code:
    [00:12:12] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'cr' in plugin MysteryCrate v0.3
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:701) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 9
        at org.bukkit.craftbukkit.v1_7_R3.inventory.CraftInventoryCustom$MinecraftInventory.setItem(CraftInventoryCustom.java:103) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at org.bukkit.craftbukkit.v1_7_R3.inventory.CraftInventory.setItem(CraftInventory.java:82) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.prefixaut.mysterycrate.commands.CrateCommand.onCommand(CrateCommand.java:145) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        ... 13 more
    

    Line 145: inv.setItem(counter,itm);

    Hope someone can help me, Thanks in advance.
     
  2. Offline

    Bavestry

    See the part with your stack trace that says "ArrayIndexOutOfBoundsException: 9"? That's probably because, yes, each line of an inventory has nine slots, but you're forgetting that the first of almost anything in java is considered 0 not 1.
     
  3. Offline

    PreFiXAUT

    Uhm...did you even read the Debug-Messages?
     
  4. Offline

    Bavestry

    PreFiXAUT Why don't you just try creating a new inventory with all those items and then opening it? I don't see the point in adding air, just skip adding items to the slots you don't want items in.

    Code:
    public static Inventory main; {
    main = Bukkit.createInventory(null, 27, "Inventory title");
    main.setItem(0, createItem(Material.GOLD_INGOT, 1, "Item title"));
    main.setItem(26, createItem(Material.STONE, 1, "Item title"));
    }
    That should add a gold ingot to the first slot in a small chest inventory, and a block of stone in the last with air inbetween. Then you can use player.openInventory(main) to open the inventory. Just a thought..
     
  5. Offline

    PreFiXAUT

    That's what I'm trying.
    To add the AIR is just optional, but the important thing is that the count increases.
    Then I'm checking if it's not already in the new Line. When it isn't, I set it to it. So Slot 9 should start in the secound row, but I get the Error. That's where we are currently.
     
  6. Offline

    Bavestry

    PreFiXAUT My bad. I'm not quite sure what the problem is here.. but on line 52 here, should it be (Lines*9 - 1) since there's one less slot than Lines*9? Or am I just being stupid again? xD
     
  7. Offline

    PreFiXAUT

    Bavestry Since I'm only counting how many lines I need, and not how many Slots, it needs to be like this.
     
  8. Offline

    Bavestry

    PreFiXAUT Lines*9 is going to return how many slots you have in the inventory.. and 'counter' started on zero, so it should end with one less than Lines*9 because Lines*9 doesn't include zero. Say you have one line for a crate or whatever, and you times that by 9 so you have 9 slots. Your counter variable started on 0, therefore it should only reach slot number 8 for the first line.. maybe that's why it's "ArrayIndexOutOfBoundsException: 9"?

    EDIT: Just try running it with Lines*9 - 1 to see if it works.
     
  9. Offline

    PreFiXAUT

    Bavestry Mystery Solved!
    Problem was: the "inv.setMaxStackSize(amount);" Doesn't change the Maximum SLOTS, it changes the Maximal StackSize (obviously) of the ItemStacks which are in the Inventory. Now I just recreate the Inventory and add the Items again.
    Code I use:
    Show Spoiler

    Code:java
    1. if (!(cs instanceof Player)) {
    2. cs.sendMessage(noPlayer);
    3. return true;
    4. }
    5. Player p = (Player) cs;
    6. List<MysteryItem> items = MysteryCrate.getCrateItems(args[1]);
    7. List<MysteryCommand> commands = MysteryCrate.getCrateCommands(args[1]);
    8. if ((items == null || items.isEmpty()) && (commands == null || commands.isEmpty())) {
    9. p.sendMessage("§4Sorry, but there are no Items saved for this kind of Crate.");
    10. return true;
    11. }
    12. int lines = 0;
    13. int amt = items.size() + commands.size();
    14. while (lines * 9 < amt) {
    15. lines++;
    16. }
    17. if (lines >= 6) lines = 6;
    18. Inventory inv = Bukkit.createInventory(p, lines*9, "Aviable Loot");
    19. int counter = 0;
    20. int count = 0;
    21. for (MysteryItem item : items) {
    22. if (counter > lines*9) break;
    23. ItemStack itm = item.getItem();
    24. itm.setAmount(1);
    25. inv.addItem(itm);
    26. count++;
    27. counter++;
    28. }
    29. while ((count%9) != 0) {
    30. inv.addItem(new ItemStack(Material.AIR));
    31. count++;
    32. }
    33. if (counter != count) {
    34. counter = count;
    35. int newlines = (lines+1)*9;
    36. if (newlines < 6*9) {
    37. lines++;
    38. inv = Bukkit.createInventory(p, newlines, "Aviable Loot");
    39. for (MysteryItem item : items) {
    40. if (counter > lines*9) break;
    41. ItemStack itm = item.getItem();
    42. itm.setAmount(1);
    43. inv.addItem(itm);
    44. }
    45. }
    46. }
    47. for (MysteryCommand command : commands) {
    48. if (counter > lines*(9-1)) break;
    49. ItemStack itm = command.getItem();
    50. itm.setAmount(1);
    51. inv.setItem(counter,itm);
    52. counter++;
    53. }
    54. p.openInventory(inv);
    55. log.put(p, inv);
    56. return true;

     
Thread Status:
Not open for further replies.

Share This Page