Solved Why are these equal?

Discussion in 'Plugin Development' started by Gorbit99, Dec 14, 2015.

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

    Gorbit99

    I have a code, that should generate three lists with different items in them, but they are always the same.
    Code:
    List<ItemStack> itemL = new ArrayList<ItemStack>();
                    List<ItemStack> levelL = new ArrayList<ItemStack>();
                    List<ItemStack> successL = new ArrayList<ItemStack>();
                    Random random = new Random();
                    for (int x = 0; x < 102; x++) {
                        ItemStack item = new ItemStack(Material.STAINED_GLASS_PANE);
                        ItemMeta meta = item.getItemMeta();
                        int rand = random.nextInt(4);
                        int rand1 = random.nextInt(4);
                        int rand2 = random.nextInt(4);
                        switch (rand) {
                        case 0:
                            item.setDurability((byte) 3);
                            meta.setDisplayName(ChatColor.BLUE + "Common");
                            item.setItemMeta(meta);
                            break;
                        case 1:
                            item.setDurability((byte) 4);
                            meta.setDisplayName(ChatColor.YELLOW + "Uncommon");
                            item.setItemMeta(meta);
                            break;
                        case 2:
                            item.setDurability((byte) 1);
                            meta.setDisplayName(ChatColor.GOLD + "Rare");
                            item.setItemMeta(meta);
                            break;
                        case 3:
                            item.setDurability((byte) 14);
                            meta.setDisplayName(ChatColor.RED + "Ultra rare");
                            item.setItemMeta(meta);
                            break;
                        }
                        System.out.println(meta);
                        itemL.add(item);
                        switch (rand1) {
                        case 0:
                            item.setDurability((byte) 3);
                            meta.setDisplayName(ChatColor.BLUE + "Common");
                            item.setItemMeta(meta);
                            break;
                        case 1:
                            item.setDurability((byte) 4);
                            meta.setDisplayName(ChatColor.YELLOW + "Uncommon");
                            item.setItemMeta(meta);
                            break;
                        case 2:
                            item.setDurability((byte) 1);
                            meta.setDisplayName(ChatColor.GOLD + "Rare");
                            item.setItemMeta(meta);
                            break;
                        case 3:
                            item.setDurability((byte) 14);
                            meta.setDisplayName(ChatColor.RED + "Ultra rare");
                            item.setItemMeta(meta);
                            break;
                        }
                        levelL.add(item);
                        System.out.println(meta);
                        switch (rand2) {
                        case 0:
                            item.setDurability((byte) 3);
                            meta.setDisplayName(ChatColor.BLUE + "Common");
                            item.setItemMeta(meta);
                            break;
                        case 1:
                            item.setDurability((byte) 4);
                            meta.setDisplayName(ChatColor.YELLOW + "Uncommon");
                            item.setItemMeta(meta);
                            break;
                        case 2:
                            item.setDurability((byte) 1);
                            meta.setDisplayName(ChatColor.GOLD + "Rare");
                            item.setItemMeta(meta);
                            break;
                        case 3:
                            item.setDurability((byte) 14);
                            meta.setDisplayName(ChatColor.RED + "Ultra rare");
                            item.setItemMeta(meta);
                            break;
                        }
                        successL.add(item);
                        System.out.println(meta);
                    }
                    new AnimateGui(usedEnchanter, p, p.getOpenInventory().getTopInventory(), itemL, levelL, successL)
                            .runTaskTimer(EnchantingMainClass.getPlugin(), 0, 2);
                }
     
  2. Offline

    Zombie_Striker

    @Gorbit99
    Did you debug? I would suggest adding System.outs..... in each case, not just afterwards.
     
  3. Offline

    Mrs. bwfctower

    @Gorbit99 For starters sit your sun out in the sun and let it dry out.
     
    Zombie_Striker likes this.
  4. Offline

    Gorbit99

    @Mrs. bwfctower Yeah, I know, I just couldn't bother shortening it, until it actually works. If you could help me with that though... I'll put it insde a function.

    @Zombie_Striker I added sysouts to every possible spot. It seems like, that the randoms are working, they are different, the ItemMetas too, but the items are the same for some reasons. I started it with the class animategui, and the lists were the same, then I checked the randoms, they were different, then I checked the lists inside the class I copied inside the thread, they were the same, and then I checked the randoms once more, and the metas, they were different.
     
  5. Offline

    Zombie_Striker

    @Gorbit99
    Looked at your code again. You add the same instance to each list. You never "reset" (items = new ItemStack). Even after you put it into the list, you modify it. If you add something to a list, and then modify it, the instance in the arraylist changes.

    How to fix: After each time you add it to the array, use items = items.clone(); to make a new instance each time.
     
    Gorbit99 likes this.
  6. Offline

    Gorbit99

    @Zombie_Striker Thank you for the quick reply, I didn't know that Arraylists work this way.

    EDIT: I'm going to get to sleep right now, if it works, I'll mark this as solved
     
  7. Offline

    Zombie_Striker

    @Gorbit99
    It's not how arraylists work. Its how Java handles updated objects.
     
    Gorbit99 likes this.
Thread Status:
Not open for further replies.

Share This Page