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