Solved Items Scrolling trough Inventory

Discussion in 'Plugin Development' started by Omel, May 16, 2016.

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

    Omel

    I wanna make a plugin to scroll items trough a Inventory but i dont have any ideas how to do it.. Can anyone help me?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Omel Runnable that sets the items in the inventory and moves them around. Don't forget to update it.
     
  3. Offline

    Omel

    Yes but i wanna do that they cycle that u have 18 Items and that they cycle trough the 9 Slots i dont know how to do that @timtower
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Omel You save the items in a list.
    You set the inventory to the first 9 items.
    You get the first item of the list and put it at the back.
    Repeat till you think that you scrolled enough.
     
  5. Offline

    Omel

    I will try it thank you @timtower

    It dont work.. :(
    <Code Deleted>
     
    Last edited: May 17, 2016
  6. Offline

    LemonCoder


    Did you use your method?
     
  7. Offline

    I Al Istannen

    @Omel
    Why should that code spin? There is no variable changing during each iteration. This means, nothing changes. Apart from that, calling p.openInventory() is NOT needed, as the already open inventory is changed. You may need to call Player#updateInventory() though.

    I would do what timtower said and move the items in the list (removing the last one and then using List#set(int index, ItemStack item), and set the index to 0. At least with an ArrayList (didn't check the other Javadocs) this will move all entries and make the 1st the 2nd, the 2nd the 3rd and so on. Just to this before you set the contents of the Inventory again (currently starting at the first line in your runnable).

    Alternatively you could leave the items as they are and cycle through with your index. This way is a bit more complex, but doesn't need to move entries in the List, which, depending on the Implentation, could speed things up a bit. The speed is probably not needed, and, because it is more complex, I would stick to the one timtower suggested.

    Edit: Also save the ID of the task somewhere (the return value of the Schedule method), to be able to cancel the task. Or use a BukkitRunnable and call the .cancel() method.
     
  8. Offline

    Omel

    I tried it but it just set the whole 9 slots to bedrock...

    <Code Deleted>
     
    Last edited: May 17, 2016
  9. Offline

    I Al Istannen

    @Omel
    This is not what I meant. Just remove the last one (list.size() - 1) and save it in a variable (let's use testItem). Then insert "testItem" in the list at index o by using list.setItem(0, testItem). Then just use this (or better a for loop!):
    Code:
            inv.setItem(0, list.get(0));
            inv.setItem(1, list.get(1));
            inv.setItem(2, list.get(2));
            inv.setItem(3, list.get(3));
            inv.setItem(4, list.get(4));
            inv.setItem(5, list.get(5));
            inv.setItem(6, list.get(6));
            inv.setItem(7, list.get(7));
            inv.setItem(8, list.get(8));
    Do these steps inside the runnable.
     
    Last edited: May 17, 2016
  10. Offline

    Zombie_Striker

    @Omel
    Please use for loops instead of violating D.R.Y (Don't Repeat Yourself). Change the above to:
    Code:
    for(int i = 0; i < 8;i++)
            inv.setItem(i, list.get(i));
     
    I Al Istannen likes this.
  11. Offline

    I Al Istannen

    @Zombie_Striker
    But I would REALLY recommend adding the brackets. Just way easier to make mistakes, if you do it the way you did.
     
    Zombie_Striker likes this.
  12. Offline

    Omel

    I replaced it with the loop but it just dont want to work.. what do i wrong.. It sets the whole 9 Slots with Bedrock.. it dont Cycle :(

    <Code Deleted>
     
    Last edited: May 17, 2016
  13. Offline

    I Al Istannen

    @Omel
    You didn't do what I said. The post is here (just post 9).

    EDIT: To clarify: You don't need all the list.set things. Just save list.get(list.size() - 1) in a variable, then call list.remove(list.size() -1) and then list.set(0, the saved variable). After that run your for loop, to set the contents. I think it should work this way.
     
    Last edited: May 17, 2016
    Omel likes this.
  14. Offline

    Omel

  15. Offline

    I Al Istannen

    @Omel
    Okay. Just delete all code in the runnable and start from a new, nice emtpy one.
    Now, what do we want to achieve? We want to change the last item to the first, the first to the second, the second to the third and so on. Basically we just want to move every item one to the right (the last one back to the first).

    The quuestion is now how we can do this with Java code. The answer is: there are multiple ways. I will explain one, which uses some functuality of the List#add(int, Object) method.

    The Javadoc for the method can be found here. I will quote it:
    So, it inserts it and shifts all elements to the right. This is exactly what we want. It just moves everything to the right. If we have
    Glass
    Iron
    Wool​

    in the inventory and add Bedrock at index 0 (the first element) using the method it will be

    Bedrock
    Glass
    Iron
    Wool​

    Now, we want to cycle and don't add new elements. This means, the last element must become the first and the rest must be shifted one to the right.
    This can be achieved, by removing the last Element and saving it in a variable. Now the list looks like this
    Bedrock
    Glass
    Iron​

    See how the wool is gone? We have removed it. Now we add it back at index 0. It is the same as above:

    Wool
    Bedrock
    Glass
    Iron​

    Now we have cycled one time. Everything was moved by one index and the last one moved to the front.

    At last we need to set the items in the inventory to the ones in the list, using the for loop you used and Zombie_Striker suggested.

    Just do this in the runnable and it will move by one every iteration of the runnable.

    If you have any more questions, just ask!
     
    Omel likes this.
  16. Offline

    Omel

    So how i save the first index and put it at the back? @I Al Istannen

    Like this? list.add(0, list.get(8));
     
  17. Offline

    I Al Istannen

    @Omel
    ItemStack variable = List#get(List#size() - 1)? List#remove(List#size() - 1) to remove it then.

    You don't need to put the first at the back, but the last at the front. Use List#add(int index, Object object), where index is 0 and object your previously saved variable.
     
    Last edited: May 17, 2016
    Omel likes this.
  18. Offline

    Omel

    Now it Works fine but how i cycle 18 Items trough 9 Slots?

    <Code Deleted>
     
    Last edited: May 17, 2016
  19. Offline

    I Al Istannen

    @Omel
    I made a mistake in my last post. Sorry about that, but you didn't implement it anyways ;)
    You didn't delete the last item. This means, the list will grow in size, which is not what you want. This is why you should save list.get(list.size() - 1) in a variable (because that is the LAST item in the list, which should go to the front) and then delete this item (List#remove(List#size() - 1)). Now the last item is gone and you add the saved item (which was the last item) to index 0 of the list.

    This way (using List#size() - 1 and saving the item temporarily) it should work with a List of any length. Just use the index 0-8 for the items in the inventory, like you did with your for loop. You don't need to change anything there.
     
    Omel likes this.
  20. Offline

    Omel

    I win every Time the same Item but why?

    <Code Deleted>
     
    Last edited: May 17, 2016
  21. Offline

    I Al Istannen

    @Omel
    You still haven't fixed your runnable. Apart from that, your code is really hard to read.
    1. One Random object is enough.
    2. i7 is not a good name for a variable. Really.
      1. Instead make a list and add the itemstacks to it. Much cleaner and easier, and you can probably use a simple for loop.
    3. What should that code do? Explain it in english.
    4. How do you know what you have won? What decides that? Depending on that you need to implement the "randomness".
     
    Omel likes this.
  22. Offline

    Omel

    You win the item from the 4th Slot. That code cycle 9 Items trough the 9 Slots like u helped me how to do that. So now i win every time the Same item but i dont know why.. @I Al Istannen
     
  23. Offline

    I Al Istannen

    @Omel
    I would correct the other things first. Do you start with the same item every time? If yes that means you will end with the same one too. Try to make the time until it stops spinning random or call Collections#shuffle(List) on the item stack list to change the order. It may be something else, this is just what I would think is most likely, without understanding your code.
     
  24. Offline

    Omel

    @I Al Istannen
    I now used ur method never heard about Collections.shuffle.. :) so how i can cycle 18 Items trough my inv ? :) Should i add just more Items to the list?

    <Code Deleted>
     
    Last edited: May 17, 2016
  25. Offline

    I Al Istannen

    @Omel
    Nearly. On a side note, variables start by convention with a lower case letter, classes with an upper case. Then they are both camelCase, meaning everyNewWordIs capital.
    You still need to remove the last item from the list. Currently the list just grows and grows and grows. I have stated what to do in my last posts (this for example), so try to apply it. If you fix this, you can just add more items to the list, to cycle through more.
     
  26. Offline

    Omel

    @I Al Istannen
    So i add the last index to the list and remove the last index?
     
  27. Offline

    I Al Istannen

    @Omel
    You remove the last index, but save it in a variable, so that you can add it later. Then you add it at the index 0 (so at the start). This means, you MOVE the last element to the start.
     
    Omel likes this.
  28. Offline

    Omel

    So like this? @I Al Istannen
    Code:
    list.add(0, list.get(staticLastItem));
                    list.remove(list.size() - 1);
     
  29. Offline

    I Al Istannen

    @Omel
    Yes, that should work too. Try it out :)
     
    Omel likes this.
  30. Offline

    Omel

    It works.. You was really helpful.. Thank you sooo much <3 @I Al Istannen
     
    I Al Istannen likes this.
Thread Status:
Not open for further replies.

Share This Page