Converting ItemStack[] ArrayList to ItemStack

Discussion in 'Plugin Development' started by xepisolonxx, Mar 7, 2015.

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

    xepisolonxx

    How would i convert the arraylist of ItemStack[] i have to save as ItemStack[] in my hashmap so i can retrieve it later
    Code:

    Code:
           
     public void add(Player player,  ItemStack... stack)
                                {
                                    ArrayList<ItemStack> temp1 = Lists.items;
                                    ArrayList<ItemStack[]> list = new ArrayList<ItemStack[]>();
    
                                    for (int x = 0; x < stack.length; x++)
                                    {
                                        temp1.add(stack[x]);
                                    }
                                    ItemStack[] temp2 = (ItemStack[]) temp1.toArray();
                                    list.add(temp2);
                                   
                                    //How to add ItemStack[] ArrayList ass ItemStack[] in my hashmap
                                    Lists.pi.put(player.getName(), list);
    
                                }
                                
                                
     
  2. Offline

    RROD

    Code:
    ItemStack[] stack = list.toArray(list.size());
    Where list should be a List<ItemStack> object.
     
  3. Offline

    xepisolonxx

    @RROD You mean like this? It still doesnt work
    Code:
        public void add(Player player,  ItemStack[]... stack)
                                {
                                    ArrayList<ItemStack> temp1 = Lists.items;
                                    List<ItemStack[]> list = new ArrayList<ItemStack[]>();
                                     stack = list.toArray(list.size());
    
                                    for (int x = 0; x < stack.length; x++)
                                    {
                                        temp1.add(stack[x]);
                                    }
                                    ItemStack[] temp2 = (ItemStack[]) temp1.toArray();
    
                                    list.add(temp2);
                                   
                                    //How to add ItemStack[] ArrayList ass ItemStack[] in my hashmap
                                    Lists.pi.put(player.getName(), list);
    
                                }
     
  4. Offline

    Totom3

    @xepisolonxx
    Code:
    ItemStack[] temp2 = (ItemStack[]) temp1.toArray();
    ArrayList#toArray() returns Object[], not ItemStack[]. This is the correct way:
    Code:
    List<ItemStack> list = [...];
    
    list.toArray(new ItemStack[list.size()]);
    If you're interested in why you have to do all this, check out this link. Explanation is partially in the comments.
     
    CheesyFreezy likes this.
  5. Offline

    xepisolonxx

    @Totom3 Im just having super big trouble with it sorry for the spood feeding
    Code:
    public void add(Player player,  ItemStack... stack)
        {
            ArrayList<ItemStack> temp1 = Lists.items;
            List<ItemStack[]> list = new ArrayList<ItemStack[]>();
             stack = (ItemStack[]) list.toArray();
    
            for (int x = 0; x < stack.length; x++)
            {
                temp1.add((ItemStack) stack[x]);
            }
            ItemStack[] temp2 = (ItemStack[]) temp1.toArray();
    
            list.add(temp2);
          
            Lists.pi.put(player.getName(), (ItemStack[]) stack);
    
        }
    }
    Making a random kit thing it gets a random item from a ItemStack of a hashmap
    Code:
    public static void randomkit(final Player p){
        final Random random = new Random();
         final Random gumby = new Random();
         ArrayList<ItemStack> mat = Lists.items;
         kit.add(p, mat);
           final int  go = random.nextInt(34) + 1;
    
         slot.remove(p.getName());         
             slot.put(p.getName(), 0);         
    
             final Inventory inv = Bukkit.getServer().createInventory(p, 36, ChatColor.BLUE + "Random Kit"); 
             
             new BukkitRunnable(){                   
               @SuppressWarnings("deprecation")
            @Override
               public void run(){
                   p.openInventory(inv);            
                 
                     int size = Lists.pi.get(p.getName()).length;
                     int index = gumby.nextInt(size);
            
                   if(slot.get(p.getName()) ==  go){
    
                     inv.clear();
                     p.openInventory(inv);
                       p.updateInventory();
                       Lists.copycatList.add(p.getName());
                       p.performCommand("" + ChatColor.stripColor(inv.getItem(go).getItemMeta().getDisplayName()));
                       Lists.copycatList.remove(p.getName());
    
                        p.playSound(p.getLocation(), Sound.ANVIL_LAND, 3, 3);
    
                    
                   this.cancel();
                   return;
                 } else {
    
                     inv.clear();
                     inv.setItem(slot.get(p.getName()),Lists.items.get(p.getName().indexOf(index)));
    
                 }
             
                  slot.put(p.getName(),slot.get(p.getName()) + 1);
                     p.playSound(p.getLocation(), Sound.CLICK, 3, 3);
    
    
               }
             }.runTaskTimer(plugin, 0L, 4L);
    
    }
    
    
    }
     
  6. Offline

    xepisolonxx

  7. Offline

    xepisolonxx

  8. Offline

    teej107

    @xepisolonxx Why are you wanting to convert an ArrayList to an Array?
     
  9. Offline

    xepisolonxx

    @teej107 I want to convert the arraylist or itemstacks to itemstacks so im able to manipulate it in my inventory
     
  10. Offline

    teej107

    Still confused on why. You can still manipulate inventories without Arrays
     
  11. Offline

    xepisolonxx

    @teej107 im adding items to an arraylist<ItemStack[]> so i can save it within a hashmap so im able to get it when selecting a random item from that list if you have another method of doing this please tell i been stuck on this for 2+ weeks
     
  12. Offline

    xepisolonxx

  13. Offline

    FisheyLP

    you can have a HashMap<String, ItemStack[]> where for example the String would be the kit name and the ItemStack[] the items.
    If you want to save your ArrayList<ItemStack[]>, loop throug every ItemStack[] in the ArrayList:
    Code:
    int i = 1;
    for (ItemStack[] items : list) {
    hashmap.put("Kit nr."+i, items);
    i++;
    }
     
  14. Offline

    CheesyFreezy

    I needed this! Thanks!
     
    Totom3 likes this.
Thread Status:
Not open for further replies.

Share This Page