-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
EntityPacketTracker.java
88 lines (69 loc) · 2.42 KB
/
EntityPacketTracker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package net.citizensnpcs.util;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
import org.bukkit.entity.Player;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
public interface EntityPacketTracker extends Runnable {
public void link(Player player);
public void unlink(Player player);
public void unlinkAll(Consumer<Player> callback);
public static class PacketAggregator {
private final Set<PlayerConnection> connections = Sets.newHashSet();
private List<Object> packets;
public void add(UUID uuid, Consumer<Object> conn) {
connections.add(new PlayerConnection(uuid, conn));
}
public void removeConnection(UUID uuid) {
connections.remove(new PlayerConnection(uuid, null));
}
public void send(Object packet) {
if (packets != null) {
packets.add(packet);
return;
}
for (PlayerConnection conn : connections) {
conn.conn.accept(packet);
}
}
public void startBundling() {
packets = Lists.newArrayList();
}
public void stopBundlingAndSend() {
Iterable<Object> packets = NMS.createBundlePacket(this.packets);
this.packets = null;
for (Object packet : packets) {
for (PlayerConnection conn : connections) {
conn.conn.accept(packet);
}
}
}
private static class PlayerConnection {
Consumer<Object> conn;
UUID uuid;
public PlayerConnection(UUID uuid, Consumer<Object> conn) {
this.uuid = uuid;
this.conn = conn;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
PlayerConnection other = (PlayerConnection) obj;
if (!Objects.equals(uuid, other.uuid)) {
return false;
}
return true;
}
@Override
public int hashCode() {
return 31 + (uuid == null ? 0 : uuid.hashCode());
}
}
}
}