ArrayList Problem

Cury

Aktives Mitglied
Guten Tag,
Ich habe eine Methode für ein Minecraft Plugin geschrieben, welche mir eine random Anzahl an random Items zurückgeben soll:

Java:
public List<ItemStack> getWZChestLoot() {
        List<ItemStack> list = new ArrayList<>();
        int randomNumber = random(3, 8);
        int chance;
        List<ItemStack> items = new ArrayList<>(getCases().getClosedCases());
        items.add(getOther().getExpBottle(500, this.rarityCommon));
        items.add(getOther().getExpBottle(1000, this.rarityCommon));
        items.add(getOther().getExpBottle(1500, this.rarityRare));
        items.add(getOther().getExpBottle(2000, this.rarityEpic));
        int xpBottleAmount = random(1, 15);
        items.add(new ItemBuilder(Material.EXP_BOTTLE, xpBottleAmount).setLore((xpBottleAmount > 6 ? this.rarityRare : this.rarityCommon)).toItemStack());
        int goldAppleAmount = random(2, 8);
        items.add(new ItemBuilder(Material.GOLDEN_APPLE, goldAppleAmount).setLore(this.rarityCommon).toItemStack());
        int goldCarrotAmount = random(3, 13);
        items.add(new ItemBuilder(Material.GOLDEN_CARROT, goldCarrotAmount).setLore(this.rarityCommon).toItemStack());
        int backed = random(3, 15);
        items.add(new ItemBuilder(Material.BAKED_POTATO, backed).setLore(this.rarityCommon).toItemStack());
        items.add(new ItemBuilder(Material.POTION, 1).setDurability((short) 8227).addLoreLine(this.rarityCommon).toItemStack());
        items.add(new ItemBuilder(Material.POTION, 1).setDurability((short) 8265).addLoreLine(this.rarityRare).toItemStack());
        items.add(new ItemBuilder(Material.POTION, 1).setDurability((short) 16424).addLoreLine(this.rarityCommon).toItemStack());
        items.add(new ItemBuilder(Material.POTION, 1).setDurability((short) 8194).addLoreLine(this.rarityRare).toItemStack());
        items.add(new ItemBuilder(Material.POTION, 1).setDurability((short) 8262).addLoreLine(this.rarityCommon).toItemStack());
        items.add(new ItemBuilder(Material.POTION, 1).setDurability((short) 8238).addLoreLine(this.rarityCommon).toItemStack());
        int enderpearlAmount = random(3, 8);
        items.add(new ItemBuilder(Material.ENDER_PEARL, enderpearlAmount).setLore(this.rarityRare).toItemStack());
        items.add(new ItemBuilder(Material.MONSTER_EGG, 1).setDurability((short) 90).setLore(this.rarityOp).toItemStack());
        items.add(new ItemBuilder(Material.MONSTER_EGG, 1).setDurability((short) 91).setLore(this.rarityOp).toItemStack());
        items.add(new ItemBuilder(Material.MONSTER_EGG, 1).setDurability((short) 92).setLore(this.rarityOp).toItemStack());
        items.add(new ItemBuilder(Material.MONSTER_EGG, 1).setDurability((short) 93).setLore(this.rarityOp).toItemStack());

        for(EnchantmentProvider.EnchantmentType enchantmentType : EnchantmentProvider.EnchantmentType.values()) {
            if(!enchantmentType.getRarity().equalsIgnoreCase(this.rarityOp)) {
                items.add(getBücher().getBook(enchantmentType, "I"));
            }
        }

        for (int i = 0; i < randomNumber; i++) {
            chance = random(1, 100);
            if (chance <= 5) {
                list.add(getItem(this.rarityOp, items));
            } else if (chance <= 10) {
                list.add(getItem(this.rarityMythical, items));
            } else if (chance <= 30) {
                list.add(getItem(this.rarityEpic, items));
            } else if (chance <= 50) {
                list.add(getItem(this.rarityRare, items));
            } else {
                list.add(getItem(this.rarityCommon, items));
            }
        }
        return list;
    }

    private ItemStack getItem(String rarity, List<ItemStack> allSets) {
        for (ItemStack stack : allSets) {
            if (stack.getItemMeta().getLore().contains(rarity)) {
                return stack;
            }
        }
        return new ItemStack(Material.BARRIER, 1);
    }

Allerdings gibt mir die Methode NUR die Items zurück, die ich im Konstruktor übergeben habe. Also getCases().getClosedCases(). Warum werden nur diese Items zurückgegeben und die anderen nicht?

MfG
 
So wie ich das sehe, ist dein Liste items wie folgt aufgebaut:

- die übergebenen getCases().getClosedCases().
- Dahinter die, die du hinzufügst

in deiner Schleife (Zeile 37 bis 50) holst du das items ein Item raus und fügst es in die Liste durch.

Deine getItem Methode arbeitet wie folgt:
Liefere mir das erste Item, was passend ist. Wenn es in den getCases()getClosedCases() schon passende gibt, werden natürlich die gefunden.

Du hast zwei Listen "list" und "items", Die eine füllst du und die andere gibst du zurück.
Dachte ich auch zuerst, aber in der Schleife (37-50) werden die "transferiert"
 

Zurück
Oben