Thread: While for loop (newbie)

Results 1 to 8 of 8
  1. #1 While for loop (newbie) 
    Registered Member
    Join Date
    Mar 2018
    Posts
    17
    Thanks given
    2
    Thanks received
    1
    Rep Power
    11
    Hi, I was working on the tan leather spell which is supposed to tan 5 leathers at a time however it tans 1 leather then the animation keeps working.
    I was wondering if anyone could help me:

    Code:
    package io.ruin.model.skills.magic.spells.lunar;
    
    import io.ruin.model.item.Item;
    import io.ruin.model.skills.magic.Spell;
    import io.ruin.model.skills.magic.rune.Rune;
    import io.ruin.model.skills.magic.rune.RuneRemoval;
    import io.ruin.model.stat.StatType;
    
    public class TanLeather extends Spell{
    
        private enum Leather {
    
            COWHIDE(1739, 1743),
            GREEN_DRAGONHIDE(1753, 1745),
            BLUE_DRAGONHIDE(1751, 2505),
            RED_DRAGONHIDE(1749, 2507),
            BLACK_DRAGONHIDE(1747, 2509);
    
            private int leather, hide;
    
    
            Leather(int hide, int leather) {
                this.hide = hide;
                this.leather = leather;
            }
        }
    
        public TanLeather() {
            Item[] runes = {
                    Rune.ASTRAL.toItem(2),
                    Rune.NATURE.toItem(1),
                    Rune.FIRE.toItem(5)
            };
            clickAction = p -> {
                if (!p.getStats().check(StatType.Magic, 78, "cast this spell"))
                    return;
                p.startEvent(e -> {
                    int count = 0;
                    for (Item item : p.getInventory().getItems()) {
                        for (Leather leather : Leather.values()) {
                            if (item != null && item.getId() == leather.hide) {
                                RuneRemoval r = null;
                                if (runes != null && (r = RuneRemoval.get(p, runes)) == null) {
                                    p.sendMessage("You don't have enough runes to cast this spell.");
                                    break;
                                }
                                while (true) {
                                    int hideCount = p.getInventory().getAmount(leather.hide);
                                    if (hideCount > 5) {
                                        hideCount = 5;
                                    }
                                    for(int i = 0; i < hideCount; i++) {
                                        item.setId(leather.leather);
                                        r.remove();
                                        count++;
                                    }
                                    p.animate(4413);
                                    p.graphics(746, 96, 0);
                                    p.publicSound(2879);
                                    p.getStats().addXp(StatType.Magic, 81, true);
                                    e.delay(3);
                                }
                            }
                        }
                    }
                    if (count == 0)
                        p.sendMessage("You don't have any leather to tan.");
                });
                return;
            };
        }
    
    }
    Reply With Quote  
     

  2. #2  
    Registered Member
    hc747's Avatar
    Join Date
    Dec 2013
    Age
    23
    Posts
    1,448
    Thanks given
    3,133
    Thanks received
    672
    Discord
    View profile
    Rep Power
    1029
    Quote Originally Posted by Stephanie98 View Post
    Hi, I was working on the tan leather spell which is supposed to tan 5 leathers at a time however it tans 1 leather then the animation keeps working.
    I was wondering if anyone could help me:

    Code:
    package io.ruin.model.skills.magic.spells.lunar;
    
    import io.ruin.model.item.Item;
    import io.ruin.model.skills.magic.Spell;
    import io.ruin.model.skills.magic.rune.Rune;
    import io.ruin.model.skills.magic.rune.RuneRemoval;
    import io.ruin.model.stat.StatType;
    
    public class TanLeather extends Spell{
    
        private enum Leather {
    
            COWHIDE(1739, 1743),
            GREEN_DRAGONHIDE(1753, 1745),
            BLUE_DRAGONHIDE(1751, 2505),
            RED_DRAGONHIDE(1749, 2507),
            BLACK_DRAGONHIDE(1747, 2509);
    
            private int leather, hide;
    
    
            Leather(int hide, int leather) {
                this.hide = hide;
                this.leather = leather;
            }
        }
    
        public TanLeather() {
            Item[] runes = {
                    Rune.ASTRAL.toItem(2),
                    Rune.NATURE.toItem(1),
                    Rune.FIRE.toItem(5)
            };
            clickAction = p -> {
                if (!p.getStats().check(StatType.Magic, 78, "cast this spell"))
                    return;
                p.startEvent(e -> {
                    int count = 0;
                    for (Item item : p.getInventory().getItems()) {
                        for (Leather leather : Leather.values()) {
                            if (item != null && item.getId() == leather.hide) {
                                RuneRemoval r = null;
                                if (runes != null && (r = RuneRemoval.get(p, runes)) == null) {
                                    p.sendMessage("You don't have enough runes to cast this spell.");
                                    break;
                                }
                                while (true) {
                                    int hideCount = p.getInventory().getAmount(leather.hide);
                                    if (hideCount > 5) {
                                        hideCount = 5;
                                    }
                                    for(int i = 0; i < hideCount; i++) {
                                        item.setId(leather.leather);
                                        r.remove();
                                        count++;
                                    }
                                    p.animate(4413);
                                    p.graphics(746, 96, 0);
                                    p.publicSound(2879);
                                    p.getStats().addXp(StatType.Magic, 81, true);
                                    e.delay(3);
                                }
                            }
                        }
                    }
                    if (count == 0)
                        p.sendMessage("You don't have any leather to tan.");
                });
                return;
            };
        }
    
    }
    The while loop is infinite and only ever processing one eligible item; you need to restructure your loops / this code in general.
    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Mar 2018
    Posts
    17
    Thanks given
    2
    Thanks received
    1
    Rep Power
    11
    Quote Originally Posted by hc747 View Post
    The while loop is infinite and only ever processing one eligible item; you need to restructure your loops / this code in general.
    How do I make it process multiple items, kinda stuck on that.
    Reply With Quote  
     

  4. #4  
    Registered Member
    hc747's Avatar
    Join Date
    Dec 2013
    Age
    23
    Posts
    1,448
    Thanks given
    3,133
    Thanks received
    672
    Discord
    View profile
    Rep Power
    1029
    Quote Originally Posted by Stephanie98 View Post
    How do I make it process multiple items, kinda stuck on that.
    Are you wanting the action to repeat 5 times or for 5 items to be processed per cast?
    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  5. #5  
    Registered Member
    Join Date
    Mar 2018
    Posts
    17
    Thanks given
    2
    Thanks received
    1
    Rep Power
    11
    Quote Originally Posted by hc747 View Post
    Are you wanting the action to repeat 5 times or for 5 items to be processed per cast?
    So each action should have 5 items processed per cast, but should still work for 4,3,2,1 items but not 0. How come it's only choosing one item thats what I don't understand.
    Reply With Quote  
     

  6. #6  
    Registered Member
    hc747's Avatar
    Join Date
    Dec 2013
    Age
    23
    Posts
    1,448
    Thanks given
    3,133
    Thanks received
    672
    Discord
    View profile
    Rep Power
    1029
    Because of this outer loop:
    Code:
    for (Item item : p.getInventory().getItems()) {
    Quote Originally Posted by Stephanie98 View Post
    So each action should have 5 items processed per cast, but should still work for 4,3,2,1 items but not 0. How come it's only choosing one item thats what I don't understand.
    Something like this might be what you're after.
    Code:
    package io.ruin.model.skills.magic.spells.lunar;
    
    import io.ruin.model.item.Item;
    import io.ruin.model.skills.magic.Spell;
    import io.ruin.model.skills.magic.rune.Rune;
    import io.ruin.model.skills.magic.rune.RuneRemoval;
    import io.ruin.model.stat.StatType;
    
    public class TanLeather extends Spell{
    
        private enum Leather {
    
            COWHIDE(1739, 1743),
            GREEN_DRAGONHIDE(1753, 1745),
            BLUE_DRAGONHIDE(1751, 2505),
            RED_DRAGONHIDE(1749, 2507),
            BLACK_DRAGONHIDE(1747, 2509);
    
            private int leather, hide;
    
            Leather(int hide, int leather) {
                this.hide = hide;
                this.leather = leather;
            }
    
            private static final Map<Integer, Leather> LOOKUP;
    
            static {
                Map<Integer, Leather> $values = new HashMap<>();
                for (Leather leather : values()) {
                    $values.put(leather.hide, leather);
                }
                LOOKUP = $values;
            }
    
            static Leather forId(int id) {
                return LOOKUP.get(id);
            }
        }
    
        private static final int AMOUNT_TO_TAN = 5;
    
        public TanLeather() {
            Item[] required = {
                    Rune.ASTRAL.toItem(2),
                    Rune.NATURE.toItem(1),
                    Rune.FIRE.toItem(5)
            };
            clickAction = p -> {
                if (!p.getStats().check(StatType.Magic, 78, "cast this spell"))
                    return;
                p.startEvent(e -> {
                    int count = AMOUNT_TO_TAN;
                    RuneRemoval runes = null;
                    for (Item item : p.getInventory().getItems()) {
                        if (item == null) {
                            continue;
                        }
    
                        Leather leather = Leather.forId(item.getId());
    
                        if (leather == null) {
                            continue;
                        }
    
                        if (runes == null && (removal = RuneRemoval.get(p, required)) == null) {
                            p.sendMessage("You don't have enough runes to cast this spell.");
                            break;
                        }
    
                        item.setId(leather.leather);
                        runes.remove();
                        p.animate(4413);
                        p.graphics(746, 96, 0);
                        p.publicSound(2879);
                        p.getStats().addXp(StatType.Magic, 81, true);
                        e.delay(3);
                        
                        if (--count <= 0) {
                            break;
                        }
                    }
                    if (count == AMOUNT_TO_TAN) {
                        p.sendMessage("You don't have any leather to tan.");
                    }
                });
            };
        }
    
    }
    P.S. you'll need to import the map classes.
    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  7. Thankful user:


  8. #7  
    Registered Member
    Join Date
    Mar 2018
    Posts
    17
    Thanks given
    2
    Thanks received
    1
    Rep Power
    11
    Quote Originally Posted by hc747 View Post
    Because of this outer loop:
    Code:
    for (Item item : p.getInventory().getItems()) {

    Something like this might be what you're after.
    Code:
    package io.ruin.model.skills.magic.spells.lunar;
    
    import io.ruin.model.item.Item;
    import io.ruin.model.skills.magic.Spell;
    import io.ruin.model.skills.magic.rune.Rune;
    import io.ruin.model.skills.magic.rune.RuneRemoval;
    import io.ruin.model.stat.StatType;
    
    public class TanLeather extends Spell{
    
        private enum Leather {
    
            COWHIDE(1739, 1743),
            GREEN_DRAGONHIDE(1753, 1745),
            BLUE_DRAGONHIDE(1751, 2505),
            RED_DRAGONHIDE(1749, 2507),
            BLACK_DRAGONHIDE(1747, 2509);
    
            private int leather, hide;
    
            Leather(int hide, int leather) {
                this.hide = hide;
                this.leather = leather;
            }
    
            private static final Map<Integer, Leather> LOOKUP;
    
            static {
                Map<Integer, Leather> $values = new HashMap<>();
                for (Leather leather : values()) {
                    $values.put(leather.hide, leather);
                }
                LOOKUP = $values;
            }
    
            static Leather forId(int id) {
                return LOOKUP.get(id);
            }
        }
    
        private static final int AMOUNT_TO_TAN = 5;
    
        public TanLeather() {
            Item[] required = {
                    Rune.ASTRAL.toItem(2),
                    Rune.NATURE.toItem(1),
                    Rune.FIRE.toItem(5)
            };
            clickAction = p -> {
                if (!p.getStats().check(StatType.Magic, 78, "cast this spell"))
                    return;
                p.startEvent(e -> {
                    int count = AMOUNT_TO_TAN;
                    RuneRemoval runes = null;
                    for (Item item : p.getInventory().getItems()) {
                        if (item == null) {
                            continue;
                        }
    
                        Leather leather = Leather.forId(item.getId());
    
                        if (leather == null) {
                            continue;
                        }
    
                        if (runes == null && (removal = RuneRemoval.get(p, required)) == null) {
                            p.sendMessage("You don't have enough runes to cast this spell.");
                            break;
                        }
    
                        item.setId(leather.leather);
                        runes.remove();
                        p.animate(4413);
                        p.graphics(746, 96, 0);
                        p.publicSound(2879);
                        p.getStats().addXp(StatType.Magic, 81, true);
                        e.delay(3);
                        
                        if (--count <= 0) {
                            break;
                        }
                    }
                    if (count == AMOUNT_TO_TAN) {
                        p.sendMessage("You don't have any leather to tan.");
                    }
                });
            };
        }
    
    }
    P.S. you'll need to import the map classes.
    Thank you very much, a bit of fancy code in there but I'll try and work it out online. Thanks again.
    Reply With Quote  
     

  9. #8  
    Registered Member
    hc747's Avatar
    Join Date
    Dec 2013
    Age
    23
    Posts
    1,448
    Thanks given
    3,133
    Thanks received
    672
    Discord
    View profile
    Rep Power
    1029
    Quote Originally Posted by Stephanie98 View Post
    Thank you very much, a bit of fancy code in there but I'll try and work it out online. Thanks again.
    No worries - noticed a few mistakes I made in it, but I'm sure you'll figure them out too.
    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Easy Commands Commad, using for loop
    By i hazy i in forum Configuration
    Replies: 3
    Last Post: 09-08-2009, 02:07 AM
  2. For loop statements
    By Nathan in forum Tutorials
    Replies: 5
    Last Post: 04-30-2009, 08:22 PM
  3. [Tut] Proper fix for looping in servers.
    By Colby in forum Tutorials
    Replies: 8
    Last Post: 03-19-2009, 05:36 AM
  4. JavaScripting - For loop
    By Toplessgrunt in forum Application Development
    Replies: 37
    Last Post: 01-04-2009, 10:35 PM
  5. For loops
    By PeeHPee in forum Configuration
    Replies: 15
    Last Post: 05-25-2008, 11:09 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •