RuneHive-Game
Loading...
Searching...
No Matches
PrayerBook.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.mob.prayer;
2
3import java.util.*;
4import java.util.function.Consumer;
5
6/**
7 * A {@code PrayerBook} which stores prayers. Prayers in a {@code PrayerBook}
8 * can be activated and deactivated.
9 *
10 * @author Michael | Chex
11 */
12public class PrayerBook {
13
14 /** The set of prayers. */
15 private final Set<Prayer> active = EnumSet.noneOf(Prayer.class);
16
17 /** The method of sending the activated prayer config id to the client. */
18 private transient Consumer<Prayer> sendActivated;
19
20 /** The method of sending the deactivated prayer config id to the client. */
21 private transient Consumer<Prayer> sendDeactivated;
22
23 /** The method of sending the overhead prayer to the client. */
24 private transient Consumer<Prayer> sendOverhead;
25
26 /** The method of removing the overhead prayer from the client. */
27 private transient Consumer<Prayer> sendNoOverhead;
28
29 /** The drain counter. */
30 public transient int drainCounter;
31
32 /**
33 * Checks if all given prayers are active.
34 *
35 * @param prayers The prayers to check.
36 * @return {@code True} if the prayer is active.
37 */
38 public boolean isActive(Prayer... prayers) {
39 for (Prayer prayer : prayers)
40 if (!active.contains(prayer)) return false;
41 return true;
42 }
43
44 public boolean hasOverhead() {
46 if (isActive(prayer)) {
47 return true;
48 }
49 }
50 return false;
51 }
52
53 public void deactivateOverhead() {
55 if (isActive(prayer)) {
57 }
58 }
59 }
60
61 /**
62 * Checks if any given prayers are active.
63 *
64 * @param prayers The prayers to check.
65 * @return {@code True} if any prayer is active.
66 */
67 public boolean anyActive(Prayer... prayers) {
68 for (Prayer prayer : prayers)
69 if (active.contains(prayer)) return true;
70 return false;
71 }
72
73 /**
74 * Checks if none of the given prayers are active.
75 *
76 * @param prayers the prayers to check
77 * @return {@code true} if none of the given prayers are active
78 */
79 public boolean noneActive(Prayer... prayers) {
80 for (Prayer prayer : prayers)
81 if (active.contains(prayer)) return false;
82 return true;
83 }
84
85 /**
86 * Activates only the prayers provided. Any currently active prayers will be
87 * deactivated. If all the prayers are already active, then they will be
88 * deactivated, along with any other active prayer.
89 *
90 * @param prayers The prayers to toggle.
91 */
92 private void onlyOrNone(Prayer... prayers) {
93 if (isActive(prayers)) reset();
94 else only(prayers);
95 }
96
97 /**
98 * Activates only the prayers provided. Any currently active prayers will be
99 * deactivated.
100 *
101 * @param prayers The prayers to toggle.
102 */
103 public void only(Prayer... prayers) {
104 List<Prayer> list = Arrays.asList(prayers);
105 Set<Prayer> activate = new HashSet<>(list);
106 Set<Prayer> deactivate = new HashSet<>(active);
107 deactivate.removeAll(activate);
109 }
110
111 /**
112 * Sends the activate prayer packet to the client. Head icons will also be
113 * activated.
114 *
115 * @param prayers An array of prayers to activate.
116 */
117 private void activate(Prayer... prayers) {
118 for (Prayer prayer : prayers) {
119 if (!active.add(prayer) || sendActivated == null) continue;
120 sendActivated.accept(prayer);
121 if (sendOverhead != null && prayer.is(Prayer.Type.OVERHEAD)) {
122 sendOverhead.accept(prayer);
123 }
124 }
125 }
126
127 /**
128 * Sends the dectivate prayer packet to the client. Head icons will also be
129 * deactivated.
130 *
131 * @param prayers An array of prayers to deactivate.
132 */
133 public void deactivate(Prayer... prayers) {
134 for (Prayer prayer : prayers) {
135 if (!active.remove(prayer) || sendDeactivated == null) continue;
136 sendDeactivated.accept(prayer);
137 if (sendNoOverhead != null && prayer.is(Prayer.Type.OVERHEAD)) {
138 sendNoOverhead.accept(prayer);
139 }
140 }
141 }
142
143 /**
144 * Inverts a prayer's active state. If the prayer is active, then the prayer
145 * will be deactivated. If the prayer is inactive, then the prayer will be
146 * activated. Activating some prayers will cause others to deactivate based
147 * on their {@linkplain Prayer.Type}.
148 *
149 * @param prayers A list of prayers to toggle.
150 */
151 public void toggle(Prayer... prayers) {
152 Set<Prayer> activate = new HashSet<>();
153 Set<Prayer> deactivate = new HashSet<>();
154 for (Prayer prayer : prayers) {
155 if (deactivate.contains(prayer)) continue;
156 if (!active.contains(prayer)) {
157 deactivate.addAll(prayer.toDeactivate());
158 activate.add(prayer);
159 } else deactivate.add(prayer);
160 }
161 deactivate.removeAll(activate);
163 }
164
165 /**
166 * Sets all prayers active from another prayer book.
167 *
168 * @param book The other book.
169 */
170 public void setAs(PrayerBook book) {
171 onlyOrNone(book.toArray());
172 }
173
174 /**
175 * Toggles prayers supplied in the activate and deactivate sets.
176 *
177 * @param activate A set of prayers to activate.
178 * @param deactivate A set of prayers to deactivate.
179 */
180 private void mutate(Set<Prayer> activate, Set<Prayer> deactivate) {
181 deactivate(deactivate.toArray(new Prayer[deactivate.size()]));
182 activate(activate.toArray(new Prayer[activate.size()]));
183 }
184
185 /**
186 * Calculates the amount of prayer points to drain relative to the given
187 * game ticks.
188 *
189 * @param bonus The current prayer bonus.
190 * @return The amount of prayer points to drain.
191 */
192 public int drainAmount(int bonus) {
193 if (active.isEmpty())
194 return 0;
195
196 int effect = 0, amount = 0;
197 int resistance = 60 + 2 * bonus;
198
199 for (Prayer prayer : active) {
200 effect += prayer.getDrainRate();
201 }
202
203 drainCounter += effect;
204
205 if (drainCounter > resistance) {
206 amount = drainCounter / resistance;
207 drainCounter -= amount * resistance;
208 }
209
210 return amount;
211 }
212
221
222 /** Iterates through the active prayers and disables them all. */
223 public void reset() {
224 deactivate(active.toArray(new Prayer[active.size()]));
225 }
226
227 public boolean isActive() {
228 return !active.isEmpty();
229 }
230
231 public Set<Prayer> getEnabled() {
232 return active;
233 }
234
235 /** @return an array of active prayers. */
236 public Prayer[] toArray() {
237 if (active.isEmpty()) return new Prayer[0];
238 return active.toArray(new Prayer[active.size()]);
239 }
240
241 /**
242 * Sets the method of sending the prayer update to the client.
243 *
244 * @param sendActivated The consumer that sends the activated prayer
245 * update to the client.
246 * @param sendDeactivated The consumer that sends the deactivated prayer
247 * update to the client.
248 * @param sendOverhead The consumer that sends the prayer head icon
249 * update to the client.
250 * @param sendNoOverhead The consumer that removes the prayer head icon
251 * from the client.
252 */
253 public void setOnChange(Consumer<Prayer> sendActivated, Consumer<Prayer> sendDeactivated, Consumer<Prayer> sendOverhead, Consumer<Prayer> sendNoOverhead) {
254 this.sendActivated = sendActivated;
255 this.sendDeactivated = sendDeactivated;
256 this.sendOverhead = sendOverhead;
257 this.sendNoOverhead = sendNoOverhead;
258 }
259
260 @Override
261 public String toString() {
262 return "PrayerBook[active=" + active.toString() + "]";
263 }
264
265}
void onlyOrNone(Prayer... prayers)
Activates only the prayers provided.
void reset()
Iterates through the active prayers and disables them all.
final Set< Prayer > active
The set of prayers.
transient Consumer< Prayer > sendDeactivated
The method of sending the deactivated prayer config id to the client.
transient Consumer< Prayer > sendNoOverhead
The method of removing the overhead prayer from the client.
void deactivate(Prayer... prayers)
Sends the dectivate prayer packet to the client.
void only(Prayer... prayers)
Activates only the prayers provided.
transient Consumer< Prayer > sendActivated
The method of sending the activated prayer config id to the client.
void setAs(PrayerBook book)
Sets all prayers active from another prayer book.
boolean noneActive(Prayer... prayers)
Checks if none of the given prayers are active.
boolean isActive(Prayer... prayers)
Checks if all given prayers are active.
int drainAmount(int bonus)
Calculates the amount of prayer points to drain relative to the given game ticks.
void activate(Prayer... prayers)
Sends the activate prayer packet to the client.
void mutate(Set< Prayer > activate, Set< Prayer > deactivate)
Toggles prayers supplied in the activate and deactivate sets.
transient Consumer< Prayer > sendOverhead
The method of sending the overhead prayer to the client.
void setOnChange(Consumer< Prayer > sendActivated, Consumer< Prayer > sendDeactivated, Consumer< Prayer > sendOverhead, Consumer< Prayer > sendNoOverhead)
Sets the method of sending the prayer update to the client.
void toggle(Prayer... prayers)
Inverts a prayer's active state.
boolean anyActive(Prayer... prayers)
Checks if any given prayers are active.
static final Collection< Prayer > OVERHEAD
Definition Prayer.java:187