ItemStack[] ArrayList

Discussion in 'Plugin Development' started by SuperOmegaCow, Dec 10, 2013.

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

    SuperOmegaCow

    With this array:
    Code:java
    1. public ArrayList<ItemStack[]> items = new ArrayList<ItemStack[]>();


    how would I add a itemstack.
     
  2. Offline

    AoH_Ruthless

    Code:
        public ArrayList<ItemStack> items = new ArrayList<ItemStack>();
        items.add(new ItemStack(Material.AIR));
     
  3. Offline

    Not2EXceL

    Umm google java array list.javadocs. the answer is there.

    Seriously its just .add
    Should have learned it while learning about lists
     
  4. Offline

    JRL1004

    SuperOmegaCow Something similar to this:
    Code:java
    1. public void addStack(ArrayList<ItemStack[]> list, ItemStack stack) {
    2. ItemStack[] temp = { stack };
    3. list.add(temp);
    4. }

    Just make a new ItemStack[] and make it equal whatever you want. My method is a fairly simple way of adding one IemStack[]. I'll edit this if I find a way of adding multiple. Alternately you can make it just ItemStack instead of ItemStack[] and use the method from the first post.

    SuperOmegaCow Decided not to edit my last post. Here is a method for supplying as many ItemStacks as you want, I have not testing it but it should work (in theory):
    Code:java
    1. public void addStack(ArrayList<ItemStack[]> list, ItemStack... stack)
    2. {
    3. List<ItemStack> temp1 = new ArrayList<ItemStack>();
    4. for (int x = 0; x < stack.length; x++)
    5. {
    6. temp1.add(stack[x]);
    7. }
    8. ItemStack[] temp2 = (ItemStack[]) temp1.toArray();
    9. list.add(temp2);
    10. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  5. Offline

    SuperOmegaCow

    AoH_Ruthless JRL1004 Maybe I have a improper import because it gives me an error... that is why I came here:
    Code:java
    1. items.add(new ItemStack(Material.BOW), 1);
     
  6. Offline

    JRL1004

    SuperOmegaCow No, you are getting that error because you need to put an ItemStack[] and are supplying just an ItemStack.
     
  7. Offline

    AoH_Ruthless

    SuperOmegaCow
    ItemStacks don't have the integer attached to it; remove it and you should be all set.

    Edit: JRL1004
    I believe he is using what I did in a previous post.
     
  8. Offline

    JRL1004

    AoH_Ruthless If so then what you put should be fine, otherwise they can use my post.
     
  9. Offline

    AoH_Ruthless

    SuperOmegaCow
    I just noticed that your integer was in the wrong location. The proper way:
    Code:java
    1. items.add(new ItemStack(Material.BOW, 1));


    The import you should use is this:
    Code:java
    1. import org.bukkit.inventory.ItemStack;
     
  10. Offline

    SuperOmegaCow

    AoH_Ruthless JRL1004 thanks this worked for me:
    Code:java
    1. public class ArenaClasses {
    2.  
    3. public String player;
    4. public String character;
    5. public ArrayList<ItemStack[]> items = new ArrayList<ItemStack[]>();
    6.  
    7. public ArenaClasses(String p, String character) {
    8. this.player = p;
    9. this.character = character;
    10. switch (character) {
    11. case "Archer":
    12. this.setArcher();
    13. break;
    14. case "":
    15. break;
    16. }
    17.  
    18. for(int i = 1; i < items.size(); i++) {
    19. Bukkit.getPlayer(this.player).getInventory().addItem(items.get(i));
    20. }
    21.  
    22. }
    23.  
    24. public void setArcher() {
    25. ItemStack[] temp = { (new ItemStack(Material.BOW, 1)) };
    26. items.add(temp);
    27. }
    28.  
    29.  
    30.  
    31. }


    And thank you Not2EXceL for nothing.
     
  11. Offline

    Garris0n

    Why are you doing that at all, though? Unless you need to save multiple arrays of itemstacks, which you're not doing, just use a list of ItemStacks. Also you should use .clone() when you get the item.
     
  12. Offline

    xize

    why using ItemStack[] in a ArrayList this can be very nasty... people told me that ItemStack already can be nasty and serializing is alot better though depending what you need because other way you also are saving things which you may not want to use like ItemMeta thats why I say it depends.

    i've wrote here almost the same thing in one of my psuodo codes how to make a ItemStack[] from a ArrayList though I haven't checked myself if it works.
    http://forums.bukkit.org/threads/respawning-chests-with-random-loot.204954/
     
  13. Offline

    SuperOmegaCow

  14. Offline

    Not2EXceL

    SuperOmegaCow So I helped you with nothing ehh?

    Your working code
    Code:java
    1.  
    2. public void setArcher() {
    3. ItemStack[] temp = { (new ItemStack(Material.BOW, 1)) };
    4. items.add(temp);
    5. }
    6.  


    What I told you to do
    Code:
    Umm google java array list.javadocs. the answer is there.
     
    Seriously its just .add
    Should have learned it while learning about lists
    
    What my method comes up with:
    Code:
    [URL]http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html[/URL]
    add(E e)
    Appends the specified element to the end of this list.
    
    I clearly told you the exact way to solve your problem. So now you proceed to go f yourself if you think I didn't help. I gave you the tools to solve it yourself.

    I still don't get why you have a list of ItemStack array's when you're not even using the array portion. You're just putting a single item array into the list from what's shown. It's a waste of resources to create a new array when its uneccessary
     
  15. Offline

    xize

    SuperOmegaCow
    yes all these are 'Object' in some term but since it has its class reference it tells java what to do with it also in the IDE;)
    just a quick question are you using the bukkit.jar api in your references directory? because this is needed so it doesn't say Object to these ItemStacks and so on you can use ItemStacks as ItemStacks instead of Objects.
     
  16. Offline

    Not2EXceL

    An ItemStack when used like this is an object....
     
  17. Offline

    xize

    Not2EXceL thats true when its used as [], but I don't fully understand his question.
     
  18. Offline

    Not2EXceL

    umm..
    ItemStack == Object
    ItemStack[] == array of ItemStacks

    bukkit doesn't handle ItemStacks in any way other than objects.
     
  19. Offline

    xize

    Not2EXceL that is true but without a reference to it, it could be a Object but underfined as.
    thats why I asked the op if he added the bukkit api inside the references.
    though I believe if its not added it shouldn't see it as a Object inside the IDE though but rather more than a error but in case it shows it as a Object then the bukkit api isn't added correct inside the references folder.

    in eclipse whenever I hover over my ItemStack[] it shows: org.bukkit.inventory.ItemStack if it said Object there something is terible wrong that is what I try to explain:p.

    every thing is handled with Objects but have their own references to tell java what it does but Object it self means undefined which is really strange here.
     
Thread Status:
Not open for further replies.

Share This Page