RuneHive-Game
Loading...
Searching...
No Matches
Locking.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.mob;
2
3import com.runehive.game.world.entity.mob.data.LockType;
4import com.runehive.game.world.entity.mob.data.PacketType;
5
6import java.util.Arrays;
7import java.util.concurrent.TimeUnit;
8
9public class Locking {
10 private final Mob mob;
11 private LockType lock = null;
12 private long lockTime = -1;
13
15 this.mob = mob;
16 }
17
18 /** Locks the mob indefinitely. */
19 public void lock() {
20 lock(Integer.MAX_VALUE, LockType.MASTER);
21 }
22
23 public void lock(int time) {
24 lock(time, LockType.MASTER);
25 }
26
27 public void lock(LockType type) {
28 lock(Integer.MAX_VALUE, type);
29 }
30
31 /** Locks the mob for a certain amount of time. */
32 public void lock(int time, LockType type) {
33 lock(time, TimeUnit.SECONDS, type);
34 }
35
36 /** Locks the mob for a certain amount of time. */
37 public void lock(int time, TimeUnit gUnit, LockType type) {
38 long start = System.currentTimeMillis();
39 long timer = TimeUnit.MILLISECONDS.convert(time, gUnit);
40
41 if (Long.MAX_VALUE - start <= timer)
42 timer = (Long.MAX_VALUE - start);
43
44 if (type.execute(mob, time, gUnit)) {
45 mob.movement.reset();
46 lock = type;
47 lockTime = start + timer;
48 }
49 }
50
51 /** Checks if the mob is locked. */
52 public boolean locked() {
53 if (mob.isDead())
54 return true;
55 boolean state = lock != null && lockTime - System.currentTimeMillis() >= 0;
56 if (!state) unlock();
57 return state;
58 }
59
60 public void status() {
61 System.out.println();
62 System.out.println("Lock Status");
63 System.out.println("Locked: " + locked(LockType.WALKING));
64 System.out.println("Lock Time: " + lockTime);
65 System.out.println("Lock: " + lock);
66 if (lock == null) {
67 System.out.println("Lock Packet: null");
68 } else {
69 System.out.println("Lock Packet: ");
70 Arrays.stream(lock.packets).forEach(System.out::println);
71 }
72
73 System.out.println();
74
75 }
76
77 /** Checks if the mob is locked by a certain type. */
78 public boolean locked(LockType type) {
79 return locked() && lock == type;
80 }
81
82 public boolean locked(PacketType packet) {
83 return locked() && lock.isLocked(packet);
84 }
85
86 public boolean locked(PacketType packet, Object object) {
87 return locked() && lock.isLocked(packet, mob, object);
88 }
89
90 /** Unlocks the mob. */
91 public void unlock() {
92 lock = null;
93 lockTime = 0;
94 mob.freezeImmunity.reset();
95 }
96
97 public LockType getLock() {
98 return lock;
99 }
100}
boolean locked(LockType type)
Checks if the mob is locked by a certain type.
Definition Locking.java:78
boolean locked(PacketType packet, Object object)
Definition Locking.java:86
void lock(int time, TimeUnit gUnit, LockType type)
Locks the mob for a certain amount of time.
Definition Locking.java:37
void lock(int time, LockType type)
Locks the mob for a certain amount of time.
Definition Locking.java:32
boolean locked(PacketType packet)
Definition Locking.java:82
boolean locked()
Checks if the mob is locked.
Definition Locking.java:52
void lock()
Locks the mob indefinitely.
Definition Locking.java:19
Handles the mob class.
Definition Mob.java:66
abstract boolean execute(Mob mob, int time, TimeUnit gUnit)
Handles the execution of the lock.