| 1 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  * This file is part of Araknemu. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Araknemu is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * it under the terms of the GNU Lesser General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Araknemu is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * GNU Lesser General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * You should have received a copy of the GNU Lesser General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Copyright (c) 2017-2019 Vincent Quatrevieux | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | package fr.quatrevieux.araknemu.game.fight.castable.effect.buff; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | import fr.quatrevieux.araknemu.game.fight.castable.CastScope; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.Damage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | import fr.quatrevieux.araknemu.game.fight.fighter.Fighter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | import fr.quatrevieux.araknemu.network.game.out.fight.AddBuff; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | import java.util.Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | import java.util.Iterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | import java.util.LinkedList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | import java.util.function.Predicate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | import java.util.stream.Stream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | import java.util.stream.StreamSupport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * Handle buff list for a fighter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | public final class BuffList implements Iterable<Buff>, Buffs { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private final Fighter fighter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |     private final Collection<Buff> buffs = new LinkedList<>(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |     public BuffList(Fighter fighter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |         this.fighter = fighter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     @Override | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     public Iterator<Buff> iterator() { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 1 |  |         return buffs.iterator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * Get the Buff stream | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public Stream<Buff> stream() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |         return StreamSupport.stream(spliterator(), false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     @Override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public void add(Buff buff) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |         buffs.add(buff); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         buff.hook().onBuffStarted(buff); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         if (buff.remainingTurns() == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |             buff.incrementRemainingTurns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |         fighter.fight().send(new AddBuff(buff)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         // Add one turn on self-buff | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |         if (fighter.equals(buff.caster())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |             buff.incrementRemainingTurns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     @Override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public boolean onStartTurn() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |         boolean result = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |         for (Buff buff : buffs) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |             result &= buff.hook().onStartTurn(buff); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |         return result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     @Override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     public void onEndTurn() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |         for (Buff buff : buffs) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |             buff.hook().onEndTurn(buff); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     @Override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public void onCastTarget(CastScope cast) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |         for (Buff buff : buffs) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |             buff.hook().onCastTarget(buff, cast); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     @Override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     public void onDamage(Damage value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |         for (Buff buff : buffs) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |             buff.hook().onDamage(buff, value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     @Override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public void refresh() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |         removeIf(buff -> { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |             buff.decrementRemainingTurns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |             return !buff.valid(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     @Override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public void removeAll() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |         removeIf(Buff::canBeDispelled); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     @Override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     public void removeByCaster(PassiveFighter caster) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 1 |  |         removeIf(buff -> buff.caster().equals(caster)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     private void removeIf(Predicate<Buff> predicate) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  |         final Iterator<Buff> iterator = buffs.iterator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 1 |  |         while (iterator.hasNext()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  |             final Buff buff = iterator.next(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |             if (predicate.test(buff)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |                 iterator.remove(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |                 buff.hook().onBuffTerminated(buff); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 1 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 138 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |  |