RuneHive-Game
Loading...
Searching...
No Matches
DuelRule.java
Go to the documentation of this file.
1package com.runehive.content.activity.impl.duelarena;
2
3import com.google.common.collect.ImmutableSet;
4import com.runehive.content.activity.Activity;
5import com.runehive.game.world.entity.mob.player.Player;
6import com.runehive.game.world.items.Item;
7import com.runehive.game.world.items.ItemDefinition;
8import com.runehive.game.world.items.containers.equipment.Equipment;
9import com.runehive.net.packet.out.SendConfig;
10import com.runehive.net.packet.out.SendMessage;
11import com.runehive.net.packet.out.SendString;
12import com.runehive.net.packet.out.SendToggle;
13
14import java.util.EnumSet;
15import java.util.Optional;
16
17public enum DuelRule {
18 NO_RANGED(31034, 631, new RuleCondition() {
19 @Override
20 public boolean canSelect(Player player, DuelRules rules) {
21 if (rules.contains(DuelRule.NO_MELEE) && rules.contains(DuelRule.NO_MAGIC)) {
22 player.send(new SendMessage("You must have at least one combat type checked."));
23 return false;
24 }
25 return true;
26 }
27
28 }),
29
30 NO_MELEE(31035, 632, new RuleCondition() {
31 @Override
32 public boolean canSelect(Player player, DuelRules rules) {
33 if (rules.contains(DuelRule.NO_MAGIC) && rules.contains(DuelRule.NO_RANGED)) {
34 player.send(new SendMessage("You must have at least one combat type checked."));
35 return false;
36 }
37
38 if (rules.contains(DuelRule.NO_WEAPON) && rules.contains(DuelRule.NO_MAGIC)) {
39 player.message("You can't have No Melee, No Magic, Nothing in your Right Hand, how would you fight?");
40 return false;
41 }
42
43 if (rules.contains(DuelRule.NO_FORFEIT)) {
44 player.send(new SendMessage("You can't have No Forfeit and No Melee, you could run out of ammo."));
45 return false;
46 }
47
48 if (rules.contains(DuelRule.ONLY_FUN_WEAPONS)) {
49 player.send(new SendMessage("You cannot select no melee when only fun weapons selected."));
50 return false;
51 }
52
53 if (rules.contains(DuelRule.ONLY_WHIP_DDS)) {
54 player.send(new SendMessage("You cannot use whip and dragon dagger if melee is off."));
55 return false;
56 }
57 return true;
58 }
59 }),
60
61 NO_MAGIC(31036, 633, new RuleCondition() {
62 @Override
63 public boolean canSelect(Player player, DuelRules rules) {
64 if (rules.contains(DuelRule.NO_MELEE) && rules.contains(DuelRule.NO_RANGED)) {
65 player.send(new SendMessage("You must have at least one combat type checked."));
66 return false;
67 }
68
69 if (rules.contains(DuelRule.NO_WEAPON) && rules.contains(DuelRule.NO_MELEE)) {
70 player.message("You can't have No Melee, No Magic, Nothing in your Right Hand, how would you fight?");
71 return false;
72 }
73 return true;
74 }
75 }),
76 NO_SPECIAL_ATTACK(31037, 634, (player, rules) -> true),
77 ONLY_FUN_WEAPONS(31038, 635, new RuleCondition() {
78 @Override
79 public boolean canSelect(Player player, DuelRules rules) {
80 if (rules.contains(DuelRule.NO_WEAPON)) {
81 player.send(new SendMessage("You cannot select only fun weapons when no weapons selected."));
82 return false;
83 }
84 return true;
85 }
86 }),
87 NO_FORFEIT(31039, 636, new RuleCondition() {
88 @Override
89 public boolean canSelect(Player player, DuelRules rules) {
90 if (rules.contains(DuelRule.NO_MELEE)) {
91 player.send(new SendMessage("You can't have No Forfeit and No Melee, you could run out of ammo."));
92 return false;
93 }
94
95 if (rules.contains(DuelRule.OBSTACLES)) {
96 player.send(new SendMessage("You cannot have no-movement and obstacles on at the same time."));
97 return false;
98 }
99 return true;
100 }
101 }),
102 NO_PRAYER(31040, 637, (player, rules) -> true),
103 NO_DRINKS(31041, 638, (player, rules) -> true),
104 NO_FOOD(31042, 639, (player, rules) -> true),
105 NO_MOVEMENT(31043, 640, new RuleCondition() {
106 @Override
107 public boolean canSelect(Player player, DuelRules rules) {
108 if (rules.contains(DuelRule.OBSTACLES)) {
109 player.send(new SendMessage("You cannot have no-movement and obstacles on at the same time."));
110 return false;
111 }
112 return true;
113 }
114 }),
115 OBSTACLES(31044, 641, new RuleCondition() {
116 @Override
117 public boolean canSelect(Player player, DuelRules rules) {
118 if (rules.contains(DuelRule.NO_MOVEMENT)) {
119 player.send(new SendMessage("You cannot have no-movement and obstacles on at the same time."));
120 return false;
121 }
122 if (rules.contains(DuelRule.OBSTACLES)) {
123 player.message("Obstacles are permanently disabled.");
124 return false;
125 }
126 player.message("Obstacles are permanently disabled.");
127 return false;
128 }
129 }),
130 ONLY_WHIP_DDS(31056, 642, new RuleCondition() {
131 @Override
132 public boolean canSelect(Player player, DuelRules rules) {
133 if (rules.contains(DuelRule.NO_WEAPON)) {
134 player.send(new SendMessage("You cannot select only whip and dds when no weapons selected."));
135 return false;
136 }
137 if (rules.contains(DuelRule.LOCK_ITEMS)) {
138 player.send(new SendMessage("You cannot select only whip and dds when lock items is selected."));
139 return false;
140 }
141 return true;
142 }
143 }),
144 LOCK_ITEMS(31059, 643, new RuleCondition() {
145 @Override
146 public boolean canSelect(Player player, DuelRules rules) {
147 if (rules.contains(DuelRule.ONLY_WHIP_DDS)) {
148 player.send(new SendMessage("You cannot select lock items when only whip and dds is selected."));
149 return false;
150 }
151 return true;
152 }
153 }),
154 NO_HEAD(13813, 16384, Equipment.HELM_SLOT, (player, rules) -> true),
155 NO_CAPE(13814, 32768, Equipment.CAPE_SLOT, (player, rules) -> true),
156 NO_NECKLACE(13815, 65536, Equipment.AMULET_SLOT, (player, rules) -> true),
157 NO_AMMO(13816, 134217728, Equipment.ARROWS_SLOT, (player, rules) -> true),
159 @Override
160 public boolean canSelect(Player player, DuelRules rules) {
161 if (rules.contains(DuelRule.NO_MELEE) && rules.contains(DuelRule.NO_MAGIC)) {
162 player.message("You can't have No Melee, No Magic, Nothing in your Right Hand, how would you fight?");
163 return false;
164 }
165
166 if (rules.contains(DuelRule.ONLY_FUN_WEAPONS)) {
167 player.send(new SendMessage("You cannot select no weapons when only fun weapons selected."));
168 return false;
169 }
170
171 if (rules.contains(DuelRule.ONLY_WHIP_DDS)) {
172 player.send(new SendMessage("You cannot select no weapons when only whip and dds only is selected."));
173 return false;
174 }
175 return true;
176 }
177 }),
178 NO_BODY(13818, 262144, Equipment.CHEST_SLOT,(player, rules) -> true),
179 NO_SHIELD(13819, 524288, Equipment.SHIELD_SLOT, (player, rules) -> true),
180 NO_LEGS(13820, 2097152, Equipment.LEGS_SLOT, (player, rules) -> true),
181 NO_GLOVES(13823, 8388608, Equipment.HANDS_SLOT, (player, rules) -> true),
182 NO_BOOTS(13822, 16777216, Equipment.FEET_SLOT, (player, rules) -> true),
183 NO_RINGS(13821, 67108864, Equipment.RING_SLOT, (player, rules) -> true);
184
185 /**
186 * The button for this rule.
187 */
188 private final int button;
189
190 /**
191 * The value for the config.
192 */
193 private final int value;
194
195 /**
196 * The equipment slot for this rule.
197 */
198 private final int slot;
199
200 /**
201 * The condition for this rule.
202 */
204
205 /**
206 * Creates a new {@link DuelRule}.
207 *
208 * @param button The button for this rule.
209 * @param value The config id for this rule.
210 * @param condition The condition for this rule.
211 */
213 this(button, value, -1, condition);
214 }
215
216 /**
217 * Creates a new {@link DuelRule}.
218 *
219 * @param button The button for this rule.
220 * @param value The config id for this rule.
221 * @param slot The equipment slot for this rule.
222 * @param condition The condition for this rule.
223 */
225 this.button = button;
226 this.value = value;
227 this.slot = slot;
228 this.condition = condition;
229 }
230
231 public static final ImmutableSet<DuelRule> EQUIPMENT_RULES = ImmutableSet.of(NO_HEAD, NO_CAPE, NO_NECKLACE, NO_AMMO, NO_WEAPON, NO_BODY, NO_SHIELD, NO_LEGS, NO_GLOVES, NO_BOOTS, NO_RINGS);
232
233 public static Optional<DuelRule> forButton(int button) {
234 for (DuelRule rules : values()) {
235 if (rules.getButtonId() == button) {
236 return Optional.of(rules);
237 }
238 }
239 return Optional.empty();
240 }
241
242 public boolean canSet(Player player) {
243 if (!Activity.search(player, DuelArenaActivity.class).isPresent()) {
244 return false;
245 }
246
248
249 final DuelRules rules = activity.rules;
250
251 if (!condition.canSelect(player, rules)) {
252 return false;
253 }
254
255 Player other = activity.opponent;
256
257 if (inventorySlotsRequired(player, rules, this) > player.inventory.getFreeSlots()) {
258 return false;
259 }
260
261 if (inventorySlotsRequired(other, rules, this) > other.inventory.getFreeSlots()) {
262 return false;
263 }
264
265 return true;
266 }
267
268 public static void validateEquipmentRules(Player player) {
269 if (Activity.search(player, DuelArenaActivity.class).isPresent()) {
271
272 for (DuelRule rule : DuelRule.EQUIPMENT_RULES) {
273 if (activity.rules.contains(rule) && !rule.canSet(player)) {
274 activity.rules.unflag(rule);
275 }
276 }
277
278 player.send(new SendToggle(286, activity.rules.getValue()));
279 activity.opponent.send(new SendToggle(286, activity.rules.getValue()));
280 }
281 }
282
283 public static void showRules(DuelArenaActivity activity, Player player, Player other) {
284 for (DuelRule rule : DuelRule.values()) {
285 if (!EQUIPMENT_RULES.contains(rule)) {
286 player.send(new SendConfig(rule.getValue(), activity.rules.contains(rule) ? 1 : 0));
287 other.send(new SendConfig(rule.getValue(), activity.rules.contains(rule) ? 1 : 0));
288 }
289 }
290
291 player.send(new SendToggle(286, activity.rules.getValue()));
292 other.send(new SendToggle(286, activity.rules.getValue()));
293 }
294
295 public boolean set(Player player, boolean showMessage) {
296 if (!Activity.search(player, DuelArenaActivity.class).isPresent()) {
297 return false;
298 }
299
301
302 final DuelRules rules = activity.rules;
303
304 boolean meets = condition.canSelect(player, rules);
305
306 Player other = player.equals(activity.opponent) ? activity.player : activity.opponent;
307
308 if (meets) {
309 if (inventorySlotsRequired(player, rules, this) > player.inventory.getFreeSlots()) {
310 if (showMessage) {
311 player.send(new SendMessage("You do not have enough inventory space."));
312 }
313 return false;
314 }
315
316 if (inventorySlotsRequired(other, rules, this) > other.inventory.getFreeSlots()) {
317 if (showMessage) {
318 player.send(new SendMessage("The other player does not have enough space."));
319 }
320 return false;
321 }
322
323 rules.alternate(this);
324
325 if ((this.getButtonId() >= 31034 && this.getButtonId() <= 31056) || this.getButtonId() == 31059) {
326 player.send(new SendConfig(this.getValue(), rules.contains(this) ? 1 : 0));
327 other.send(new SendConfig(this.getValue(), rules.contains(this) ? 1 : 0));
328 } else {
329 player.send(new SendToggle(286, rules.getValue()));
330 other.send(new SendToggle(286, rules.getValue()));
331 }
332
333 player.send(new SendString("", 31009));
334 other.send(new SendString("", 31009));
335 }
336 return meets;
337 }
338
339 static int inventorySlotsRequired(Player player, DuelRules rules, DuelRule rule) {
340 int required = 0;
341
342 EnumSet<DuelRule> tempRules = EnumSet.copyOf(rules.getFlags());
343 tempRules.add(rule);
344
345 if (player.equipment.hasHead()) {
346 if (tempRules.contains(DuelRule.NO_HEAD)) {
347 required++;
348 }
349 }
350
351 if (player.equipment.hasAmulet()) {
352 if (tempRules.contains(DuelRule.NO_NECKLACE)) {
353 required++;
354 }
355 }
356
357 if (player.equipment.hasWeapon()) {
358 Item item = player.equipment.get(Equipment.WEAPON_SLOT);
359 if (item != null) {
360 String name = item.getName().toLowerCase();
361
362 if (tempRules.contains(DuelRule.NO_WEAPON)) {
363 required++;
364 }
365
366 if (tempRules.contains(DuelRule.ONLY_WHIP_DDS)) {
367 if (!(name.contains("whip") || name.contains("abyssal tentacle") || name.contains("dragon dagger"))) {
368 required++;
369 }
370 }
371 }
372 }
373
374 if (player.equipment.hasCape()) {
375 if (tempRules.contains(DuelRule.NO_CAPE)) {
376 required++;
377 }
378 }
379
380 if (player.equipment.hasAmmo()) {
381 if (tempRules.contains(DuelRule.NO_AMMO)) {
382 required++;
383 }
384 }
385
386 if (player.equipment.hasChest()) {
387 if (tempRules.contains(DuelRule.NO_BODY)) {
388 required++;
389 }
390 }
391
392 if (tempRules.contains(DuelRule.NO_SHIELD)) {
393
394 if (player.equipment.hasShield()) {
395 required++;
396 }
397
398 Item weapon = player.equipment.get(Equipment.WEAPON_SLOT);
399
400 if (weapon != null) {
401
402 ItemDefinition def = ItemDefinition.get(weapon.getId());
403
404 if (def != null && def.isTwoHanded()) {
405 required++;
406 }
407
408 }
409 }
410
411 if (player.equipment.hasLegs()) {
412 if (tempRules.contains(DuelRule.NO_LEGS)) {
413 required++;
414 }
415 }
416
417 if (player.equipment.hasHands()) {
418 if (tempRules.contains(DuelRule.NO_GLOVES)) {
419 required++;
420 }
421 }
422
423 if (player.equipment.hasFeet()) {
424 if (tempRules.contains(DuelRule.NO_BOOTS)) {
425 required++;
426 }
427 }
428
429 if (player.equipment.hasRing()) {
430 if (tempRules.contains(DuelRule.NO_RINGS)) {
431 required++;
432 }
433 }
434
435 tempRules.clear();
436 return required;
437 }
438
439 /**
440 * @return the buttonId
441 */
442 public int getButtonId() {
443 return button;
444 }
445
446 /**
447 * @return the value
448 */
449 public int getValue() {
450 return value;
451 }
452
453 /**
454 * @return the slot
455 */
456 public int getSlot() {
457 return slot;
458 }
459
460 /**
461 * @return the condition
462 */
464 return condition;
465 }
466
467 public interface RuleCondition {
468
469 boolean canSelect(final Player player, final DuelRules rules);
470
471 }
472
473
474}
A Activity object constructs an in-game activity and sequences it through the start() and finish() me...
Definition Activity.java:31
static< T extends Activity > Optional< T > search(Player player, Class< T > clazz)
Definition Activity.java:60
This class represents a character controlled by a player.
Definition Player.java:125
Represents all of an in-game Item's attributes.
static ItemDefinition get(int id)
Gets an item definition.
The container class that represents an item that can be interacted with.
Definition Item.java:21
final int getId()
Gets the identification of this item.
Definition Item.java:324
final Item get(int index)
Gets the Item located on index.
The container that manages the equipment for a player.
The OutgoingPacket responsible for changing settings on a client.
The OutgoingPacket that sends a message to a Players chatbox in the client.
The OutgoingPacket that sends a string to a Players itemcontainer in the client.
DuelRule(int button, int value, RuleCondition condition)
Creates a new DuelRule.
final int value
The value for the config.
static final ImmutableSet< DuelRule > EQUIPMENT_RULES
static Optional< DuelRule > forButton(int button)
DuelRule(int button, int value, int slot, RuleCondition condition)
Creates a new DuelRule.
static void showRules(DuelArenaActivity activity, Player player, Player other)
static int inventorySlotsRequired(Player player, DuelRules rules, DuelRule rule)
final RuleCondition condition
The condition for this rule.
final int button
The button for this rule.
boolean canSelect(final Player player, final DuelRules rules)