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.clickable.ability;
23
24 import net.sf.magicproject.action.MAction;
25 import net.sf.magicproject.action.RemoveMe;
26 import net.sf.magicproject.clickable.targetable.card.MCard;
27 import net.sf.magicproject.event.MEventListener;
28 import net.sf.magicproject.event.TriggeredEvent;
29 import net.sf.magicproject.event.context.ContextEventListener;
30 import net.sf.magicproject.modifier.Unregisterable;
31 import net.sf.magicproject.token.TrueFalseAuto;
32
33 /***
34 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
35 */
36 public class RemoveModifier extends AbstractAbility {
37
38 /***
39 * Creates a new instance of RemoveModifier <br>
40 *
41 * @param event
42 * the event attached to this ability.
43 * @param modifier
44 */
45 public RemoveModifier(TriggeredEvent event, Unregisterable modifier) {
46 super("removemyself modifier", event, null, null, TrueFalseAuto.FALSE);
47 actionList = new MAction[] {};
48 effectList = new MAction[] { new RemoveMe(this) };
49 this.modifier = modifier;
50
51
52 priority = Priority.hidden_high;
53
54 optimizer = Optimization.first;
55 unregisteringmodifier = false;
56 }
57
58 @Override
59 public void removeFromManager() {
60 if (unregisteringmodifier) {
61
62 return;
63 }
64 unregisteringmodifier = true;
65 super.removeFromManager();
66 modifier.removeFromManager();
67 unregisteringmodifier = false;
68 }
69
70 @Override
71 public void optimizeRegisterToManager() {
72 for (Ability ability : MEventListener.TRIGGRED_ABILITIES.get(eventComing
73 .getIdEvent())) {
74 if (equals(ability)) {
75
76
77
78
79
80 MEventListener newEvent = ability.eventComing.appendOr(eventComing);
81 if (newEvent != null) {
82 ability.eventComing = newEvent;
83 return;
84 }
85 }
86 }
87
88 MEventListener.TRIGGRED_ABILITIES.get(eventComing.getIdEvent()).add(this);
89 }
90
91 @Override
92 public boolean equals(ContextEventListener thisContext, Ability ability,
93 ContextEventListener context) {
94 return ability instanceof RemoveModifier
95 && ((RemoveModifier) ability).modifier == modifier;
96 }
97
98 @Override
99 public int hashCode() {
100 if (eventComing != null && eventComing.card != null)
101 return eventComing.card.hashCode();
102 return super.hashCode();
103 }
104
105 @Override
106 public MCard getCard() {
107 return eventComing.card;
108 }
109
110 @Override
111 public String toString() {
112 return getName() + ", from card " + eventComing.card;
113 }
114
115 @Override
116 public boolean triggerIt(ContextEventListener context) {
117 super.triggerIt(context);
118 eventComing.card.getController().zoneManager.triggeredBuffer.addHidden(
119 this, context);
120 return true;
121 }
122
123 @Override
124 public String getLog(ContextEventListener context) {
125 return "removed modifier of '" + getCard() + "'";
126 }
127
128 @Override
129 public String getAbilityTitle() {
130 return "RemoveModifierAbility"
131 + (getName() != null ? ": " + getName() : "") + super.getAbilityTitle();
132 }
133
134 /***
135 * The modifier to remove with this action
136 */
137 public Unregisterable modifier;
138
139 /***
140 * Tag indicating we are currently unregistering this modifier
141 */
142 protected boolean unregisteringmodifier;
143
144 }