1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sf.magicproject.event;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 import net.sf.magicproject.clickable.ability.Ability;
28 import net.sf.magicproject.clickable.targetable.card.MCard;
29 import net.sf.magicproject.test.Test;
30 import net.sf.magicproject.token.IdZones;
31
32 /***
33 * When a zone is arranged.
34 *
35 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36 * @since 0.54
37 */
38 public class ArrangedZone extends TriggeredEvent {
39
40 /***
41 * Create an instance of MEventListener by reading a file Offset's file must
42 * pointing on the first byte of this event <br>
43 * <ul>
44 * Structure of InputStream : Data[size]
45 * <li>idZone [1]</li>
46 * <li>test [...]</li>
47 * <li>matched zone [1]</li>
48 * </ul>
49 *
50 * @param inputFile
51 * is the file containing this event
52 * @param card
53 * is the card owning this event
54 * @throws IOException
55 * if error occurred during the reading process from the specified
56 * input stream
57 */
58 ArrangedZone(InputStream inputFile, MCard card) throws IOException {
59 super(inputFile, card);
60 arrangedZone = inputFile.read();
61 }
62
63 /***
64 * Creates a new instance of CanICast specifying all attributes of this class.
65 * All parameters are copied, not cloned. So this new object shares the card
66 * and the specified codes
67 *
68 * @param idZone
69 * the place constraint to activate this event
70 * @param test
71 * the additional test of this event
72 * @param card
73 * is the card owning this card
74 * @param arrangedZone
75 * the arranged zone
76 */
77 public ArrangedZone(int idZone, Test test, MCard card, int arrangedZone) {
78 super(idZone, test, card);
79 this.arrangedZone = arrangedZone;
80 }
81
82 @Override
83 public MEventListener clone(MCard card) {
84 return new ArrangedZone(idZone, test, card, arrangedZone);
85 }
86
87 /***
88 * Tell if the current event matches with this event. If there is an
89 * additional code to check, it'would be checked if the main event matches
90 * with the main event
91 *
92 * @param ability
93 * is the ability owning this test. The card component of this
94 * ability should correspond to the card owning this test too.
95 * @param source
96 * the source of the spell/ability
97 * @param arrangedZone
98 * the arranged zone
99 * @return true if the current event match with this event
100 */
101 public boolean isMatching(Ability ability, MCard source, int arrangedZone) {
102 return test.test(ability, source)
103 && (this.arrangedZone == IdZones.ANYWHERE || this.arrangedZone == arrangedZone);
104 }
105
106 /***
107 * Dispatch this event to all active event listeners able to understand this
108 * event. The listening events able to understand this event are <code>this
109 * </code>
110 * and other multiple event listeners. For each event listeners having
111 * responded they have been activated, the corresponding ability is added to
112 * the triggered buffer zone of player owning this ability
113 *
114 * @param arrangedZone
115 * the arranged zone
116 * @see #isMatching(Ability, MCard, int)
117 */
118 public static void dispatchEvent(int arrangedZone) {
119 for (Ability ability : TRIGGRED_ABILITIES.get(EVENT)) {
120 if (((ArrangedZone) ability.eventComing()).isMatching(ability, ability
121 .getCard(), arrangedZone)) {
122 ability.triggerIt(null);
123 }
124 }
125 }
126
127 @Override
128 public final Event getIdEvent() {
129 return EVENT;
130 }
131
132 /***
133 * The event type.
134 */
135 public static final Event EVENT = Event.ARRANGED_ZONE;
136
137 /***
138 * The arrangedZone
139 */
140 private int arrangedZone;
141
142 }